diff --git a/DysonNetwork.Sphere/Connection/WebReader/EmbeddableBase.cs b/DysonNetwork.Sphere/Connection/WebReader/EmbeddableBase.cs new file mode 100644 index 0000000..afca49a --- /dev/null +++ b/DysonNetwork.Sphere/Connection/WebReader/EmbeddableBase.cs @@ -0,0 +1,44 @@ +using System.Reflection; +using System.Text.Json.Serialization; + +namespace DysonNetwork.Sphere.Connection.WebReader; + +/// +/// The embeddable can be used in the post or messages' meta's embeds fields +/// To render a richer type of content. +/// +/// A simple example of using link preview embed: +/// +/// { +/// // ... post content +/// "meta": { +/// "embeds": [ +/// { +/// "type": "link", +/// "title: "...", +/// /// ... +/// } +/// ] +/// } +/// } +/// +/// +public abstract class EmbeddableBase +{ + public abstract string Type { get; } + + public Dictionary ToDictionary() + { + var dict = new Dictionary(); + foreach (var prop in GetType().GetProperties()) + { + if (prop.GetCustomAttribute() is not null) + continue; + var value = prop.GetValue(this); + if (value is null) continue; + dict[prop.Name] = value; + } + + return dict; + } +} \ No newline at end of file diff --git a/DysonNetwork.Sphere/Connection/WebReader/IEmbeddable.cs b/DysonNetwork.Sphere/Connection/WebReader/IEmbeddable.cs deleted file mode 100644 index 958f05c..0000000 --- a/DysonNetwork.Sphere/Connection/WebReader/IEmbeddable.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace DysonNetwork.Sphere.Connection.WebReader; - -/// -/// The embeddable can be used in the post or messages' meta's embeds fields -/// To render richer type of content. -/// -/// A simple example of using link preview embed: -/// -/// { -/// // ... post content -/// "meta": { -/// "embeds": [ -/// { -/// "type": "link", -/// "title: "...", -/// /// ... -/// } -/// ] -/// } -/// } -/// -/// -public interface IEmbeddable -{ - public string Type { get; } -} \ No newline at end of file diff --git a/DysonNetwork.Sphere/Connection/WebReader/LinkEmbed.cs b/DysonNetwork.Sphere/Connection/WebReader/LinkEmbed.cs index 72e4849..ab2131f 100644 --- a/DysonNetwork.Sphere/Connection/WebReader/LinkEmbed.cs +++ b/DysonNetwork.Sphere/Connection/WebReader/LinkEmbed.cs @@ -4,9 +4,9 @@ namespace DysonNetwork.Sphere.Connection.WebReader; /// The link embed is a part of the embeddable implementations /// It can be used in the post or messages' meta's embeds fields /// -public class LinkEmbed : IEmbeddable +public class LinkEmbed : EmbeddableBase { - public string Type => "link"; + public override string Type => "link"; /// /// The original URL that was processed diff --git a/DysonNetwork.Sphere/Post/PostService.cs b/DysonNetwork.Sphere/Post/PostService.cs index bee4964..40c9ab2 100644 --- a/DysonNetwork.Sphere/Post/PostService.cs +++ b/DysonNetwork.Sphere/Post/PostService.cs @@ -283,12 +283,12 @@ public partial class PostService( item.Meta ??= new Dictionary(); // Initialize the embeds' array if it doesn't exist - if (!item.Meta.TryGetValue("embeds", out var existingEmbeds) || existingEmbeds is not List) + if (!item.Meta.TryGetValue("embeds", out var existingEmbeds) || existingEmbeds is not List) { - item.Meta["embeds"] = new List(); + item.Meta["embeds"] = new List>(); } - var embeds = (List)item.Meta["embeds"]; + var embeds = (List>)item.Meta["embeds"]; // Process up to 3 links to avoid excessive processing var processedLinks = 0; @@ -302,13 +302,14 @@ public partial class PostService( try { // Check if this URL is already in the embed list - var urlAlreadyEmbedded = embeds.OfType().Any(e => e.Url == url); + var urlAlreadyEmbedded = embeds.Any(e => + e.TryGetValue("Url", out var originalUrl) && (string)originalUrl == url); if (urlAlreadyEmbedded) continue; // Preview the link var linkEmbed = await reader.GetLinkPreviewAsync(url); - embeds.Add(linkEmbed); + embeds.Add(linkEmbed.ToDictionary()); processedLinks++; } catch @@ -341,7 +342,7 @@ public partial class PostService( // If embeds were added, update the post in the database if (updatedPost.Meta != null && updatedPost.Meta.TryGetValue("embeds", out var embeds) && - embeds is List { Count: > 0 } embedsList) + embeds is List { Count: > 0 } embedsList) { // Get a fresh copy of the post from the database var dbPost = await dbContext.Posts.FindAsync(post.Id);