Skip to content

Commit

Permalink
Fixed Slack
Browse files Browse the repository at this point in the history
  • Loading branch information
adamthewilliam committed Oct 1, 2024
1 parent 5ff0942 commit 19c7fc3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
10 changes: 8 additions & 2 deletions src/Hooki/Slack/JsonConverters/BlockBaseConverter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Hooki.Slack.Enums;
Expand All @@ -7,7 +8,7 @@ namespace Hooki.Slack.JsonConverters;

public class BlockBaseConverter : JsonConverter<BlockBase>
{
public override BlockBase? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override BlockBase Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("Deserialization is not implemented for this converter.");
}
Expand All @@ -28,7 +29,12 @@ public override void Write(Utf8JsonWriter writer, BlockBase value, JsonSerialize

if (propertyValue == null) continue;

writer.WritePropertyName(options.PropertyNamingPolicy?.ConvertName(property.Name) ?? property.Name);
var propertyName = property.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name
?? options.PropertyNamingPolicy?.ConvertName(property.Name)
?? property.Name;


writer.WritePropertyName(propertyName);
JsonSerializer.Serialize(writer, propertyValue, property.PropertyType, options);
}

Expand Down
68 changes: 68 additions & 0 deletions src/Hooki/Slack/JsonConverters/BlockElementBaseConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Hooki.Slack.Enums;
using Hooki.Slack.Models.BlockElements;
using Hooki.Slack.Models.Blocks;

namespace Hooki.Slack.JsonConverters;

public class BlockElementBaseConverter : JsonConverter<BlockElementBase>
{
public override BlockElementBase Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("Deserialization is not implemented for this converter.");
}

public override void Write(Utf8JsonWriter writer, BlockElementBase value, JsonSerializerOptions options)
{
writer.WriteStartObject();

// Write the "type" property as a string
writer.WriteString("type", GetBlockElementTypeJsonValue(value.Type));

// Serialize other properties
foreach (var property in value.GetType().GetProperties())
{
if (property.Name == nameof(BlockBase.Type)) continue;

var propertyValue = property.GetValue(value);

if (propertyValue == null) continue;

var propertyName = property.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name
?? options.PropertyNamingPolicy?.ConvertName(property.Name)
?? property.Name;

writer.WritePropertyName(propertyName);
JsonSerializer.Serialize(writer, propertyValue, property.PropertyType, options);
}

writer.WriteEndObject();
}

private static string GetBlockElementTypeJsonValue(BlockElementType blockElementType)
{
return blockElementType switch
{
BlockElementType.Button => nameof(BlockElementType.Button).ToLower(),
BlockElementType.Checkboxes => nameof(BlockElementType.Checkboxes).ToLower(),
BlockElementType.DatePicker => nameof(BlockElementType.DatePicker).ToLower(),
BlockElementType.DatetimePicker => nameof(BlockElementType.DatetimePicker).ToLower(),
BlockElementType.EmailInput => "email_text_input",
BlockElementType.FileInput => "file_input",
BlockElementType.Image => nameof(BlockElementType.Image).ToLower(),
BlockElementType.MultiSelectMenu => "multi_static_select",
BlockElementType.NumberInput => "number_input",
BlockElementType.PlainTextInput => "plain_text_input",
BlockElementType.RadioButtonGroup => "radio_buttons",
BlockElementType.RichTextInput => "rich_text_input",
BlockElementType.SelectMenu => "static_select",
BlockElementType.TimePicker => "timepicker",
BlockElementType.UrlInput => "url_text_input",
BlockElementType.WorkflowButton => "workflow_button",
BlockElementType.OverflowMenu => "overflow",
_ => throw new ArgumentOutOfRangeException(nameof(blockElementType), blockElementType, null)
};
}
}
19 changes: 2 additions & 17 deletions src/Hooki/Slack/Models/BlockElements/BlockElementBase.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
using System.Text.Json.Serialization;
using Hooki.Slack.Enums;
using Hooki.Slack.JsonConverters;

namespace Hooki.Slack.Models.BlockElements;

[JsonDerivedType(typeof(ButtonElement), typeDiscriminator: "button")]
[JsonDerivedType(typeof(CheckboxElement), typeDiscriminator: "checkboxes")]
[JsonDerivedType(typeof(DatePickerElement), typeDiscriminator: "datepicker")]
[JsonDerivedType(typeof(DateTimePickerElement), typeDiscriminator: "datetimepicker")]
[JsonDerivedType(typeof(EmailInputElement), typeDiscriminator: "email_text_input")]
[JsonDerivedType(typeof(FileInputElement), typeDiscriminator: "file_input")]
[JsonDerivedType(typeof(ImageElement), typeDiscriminator: "image")]
[JsonDerivedType(typeof(MultiSelectMenuElement), typeDiscriminator: "multi_static_select")]
[JsonDerivedType(typeof(NumberInputElement), typeDiscriminator: "number_input")]
[JsonDerivedType(typeof(OverflowMenuElement), typeDiscriminator: "overflow")]
[JsonDerivedType(typeof(PlainTextInputElement), typeDiscriminator: "plain_text_input")]
[JsonDerivedType(typeof(RadioButtonGroupElement), typeDiscriminator: "radio_buttons")]
[JsonDerivedType(typeof(RichTextInputElement), typeDiscriminator: "rich_text_input")]
[JsonDerivedType(typeof(SelectMenuElement), typeDiscriminator: "static_select")]
[JsonDerivedType(typeof(TimePickerElement), typeDiscriminator: "timepicker")]
[JsonDerivedType(typeof(UrlInputElement), typeDiscriminator: "url_text_input")]
[JsonDerivedType(typeof(WorkflowButtonElement), typeDiscriminator: "workflow_button")]
[JsonConverter(typeof(BlockElementBaseConverter))]
public abstract class BlockElementBase
{
[JsonPropertyName("type")]
Expand Down
2 changes: 2 additions & 0 deletions src/Hooki/Slack/Models/SlackWebhookPayload.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Text.Json.Serialization;
using Hooki.Slack.Models.Blocks;

namespace Hooki.Slack.Models;

public class SlackWebhookPayload
{
[JsonPropertyName("blocks")]
public required List<BlockBase> Blocks { get; set; }
}

0 comments on commit 19c7fc3

Please # to comment.