From 4f2e18ca278cdd963034c334f4be5764e051088d Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 6 Aug 2025 17:55:30 +0800 Subject: [PATCH] :bug: Fix embeddable parsing --- DysonNetwork.Sphere/Chat/ChatService.cs | 3 ++- DysonNetwork.Sphere/Post/PostController.cs | 4 ++-- DysonNetwork.Sphere/Post/PostService.cs | 10 ++++------ DysonNetwork.Sphere/WebReader/EmbeddableBase.cs | 6 +++--- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/DysonNetwork.Sphere/Chat/ChatService.cs b/DysonNetwork.Sphere/Chat/ChatService.cs index 8abbb2d..593c47b 100644 --- a/DysonNetwork.Sphere/Chat/ChatService.cs +++ b/DysonNetwork.Sphere/Chat/ChatService.cs @@ -2,6 +2,7 @@ using System.Text.RegularExpressions; using DysonNetwork.Shared.Data; using DysonNetwork.Shared.Proto; using DysonNetwork.Sphere.Chat.Realtime; +using DysonNetwork.Sphere.WebReader; using Microsoft.EntityFrameworkCore; using NodaTime; @@ -131,7 +132,7 @@ public partial class ChatService( // Preview the link var linkEmbed = await webReader.GetLinkPreviewAsync(url); - embeds.Add(linkEmbed.ToDictionary()); + embeds.Add(EmbeddableBase.ToDictionary(linkEmbed)); processedLinks++; } catch diff --git a/DysonNetwork.Sphere/Post/PostController.cs b/DysonNetwork.Sphere/Post/PostController.cs index 9f183ee..be9a4c8 100644 --- a/DysonNetwork.Sphere/Post/PostController.cs +++ b/DysonNetwork.Sphere/Post/PostController.cs @@ -351,7 +351,7 @@ public class PostController( existingEmbeds is not List) post.Meta["embeds"] = new List>(); var embeds = (List>)post.Meta["embeds"]; - embeds.Add(pollEmbed.ToDictionary()); + embeds.Add(EmbeddableBase.ToDictionary(pollEmbed)); post.Meta["embeds"] = embeds; } catch (Exception ex) @@ -505,7 +505,7 @@ public class PostController( var embeds = (List>)post.Meta["embeds"]; // Remove all old poll embeds embeds.RemoveAll(e => e.TryGetValue("type", out var type) && type.ToString() == "poll"); - embeds.Add(pollEmbed.ToDictionary()); + embeds.Add(EmbeddableBase.ToDictionary(pollEmbed)); post.Meta["embeds"] = embeds; } catch (Exception ex) diff --git a/DysonNetwork.Sphere/Post/PostService.cs b/DysonNetwork.Sphere/Post/PostService.cs index eac0d57..d0b25d9 100644 --- a/DysonNetwork.Sphere/Post/PostService.cs +++ b/DysonNetwork.Sphere/Post/PostService.cs @@ -68,10 +68,8 @@ public partial class PostService( ? string.Concat(post.Content.AsSpan(0, 97), "...") : post.Content; var title = post.Title ?? (post.Content?.Length >= 10 ? post.Content[..10] + "..." : post.Content); - if (content is null) - content = localizer["PostOnlyMedia"]; - if (title is null) - title = localizer["PostOnlyMedia"]; + content ??= localizer["PostOnlyMedia"]; + title ??= localizer["PostOnlyMedia"]; return (title, content); } @@ -329,7 +327,7 @@ public partial class PostService( // Preview the link var linkEmbed = await reader.GetLinkPreviewAsync(url); - embeds.Add(linkEmbed.ToDictionary()); + embeds.Add(EmbeddableBase.ToDictionary(linkEmbed)); processedLinks++; } catch @@ -694,7 +692,7 @@ public partial class PostService( Guid? currentUserId = currentUser is not null ? Guid.Parse(currentUser.Id) : null; var updatedPoll = await polls.LoadPollEmbed(pollId, currentUserId); - embeds[pollIndex] = updatedPoll.ToDictionary(); + embeds[pollIndex] = EmbeddableBase.ToDictionary(updatedPoll); post.Meta["embeds"] = embeds; } catch (Exception ex) diff --git a/DysonNetwork.Sphere/WebReader/EmbeddableBase.cs b/DysonNetwork.Sphere/WebReader/EmbeddableBase.cs index 6854a2f..28d9ee3 100644 --- a/DysonNetwork.Sphere/WebReader/EmbeddableBase.cs +++ b/DysonNetwork.Sphere/WebReader/EmbeddableBase.cs @@ -29,9 +29,9 @@ public abstract class EmbeddableBase { public abstract string Type { get; } - public Dictionary ToDictionary() + public static Dictionary ToDictionary(dynamic input) { - var jsonRaw = JsonSerializer.Serialize(this, GrpcTypeHelper.SerializerOptions); - return JsonSerializer.Deserialize>(jsonRaw, GrpcTypeHelper.SerializerOptions) ?? []; + var jsonRaw = JsonSerializer.Serialize(input, GrpcTypeHelper.SerializerOptions); + return JsonSerializer.Deserialize>(jsonRaw, GrpcTypeHelper.SerializerOptions); } } \ No newline at end of file