🐛 Fix embeddable parsing

This commit is contained in:
2025-08-06 17:55:30 +08:00
parent 1105d6f11e
commit 4f2e18ca27
4 changed files with 11 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ using System.Text.RegularExpressions;
using DysonNetwork.Shared.Data; using DysonNetwork.Shared.Data;
using DysonNetwork.Shared.Proto; using DysonNetwork.Shared.Proto;
using DysonNetwork.Sphere.Chat.Realtime; using DysonNetwork.Sphere.Chat.Realtime;
using DysonNetwork.Sphere.WebReader;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NodaTime; using NodaTime;
@@ -131,7 +132,7 @@ public partial class ChatService(
// Preview the link // Preview the link
var linkEmbed = await webReader.GetLinkPreviewAsync(url); var linkEmbed = await webReader.GetLinkPreviewAsync(url);
embeds.Add(linkEmbed.ToDictionary()); embeds.Add(EmbeddableBase.ToDictionary(linkEmbed));
processedLinks++; processedLinks++;
} }
catch catch

View File

@@ -351,7 +351,7 @@ public class PostController(
existingEmbeds is not List<EmbeddableBase>) existingEmbeds is not List<EmbeddableBase>)
post.Meta["embeds"] = new List<Dictionary<string, object>>(); post.Meta["embeds"] = new List<Dictionary<string, object>>();
var embeds = (List<Dictionary<string, object>>)post.Meta["embeds"]; var embeds = (List<Dictionary<string, object>>)post.Meta["embeds"];
embeds.Add(pollEmbed.ToDictionary()); embeds.Add(EmbeddableBase.ToDictionary(pollEmbed));
post.Meta["embeds"] = embeds; post.Meta["embeds"] = embeds;
} }
catch (Exception ex) catch (Exception ex)
@@ -505,7 +505,7 @@ public class PostController(
var embeds = (List<Dictionary<string, object>>)post.Meta["embeds"]; var embeds = (List<Dictionary<string, object>>)post.Meta["embeds"];
// Remove all old poll embeds // Remove all old poll embeds
embeds.RemoveAll(e => e.TryGetValue("type", out var type) && type.ToString() == "poll"); 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; post.Meta["embeds"] = embeds;
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -68,10 +68,8 @@ public partial class PostService(
? string.Concat(post.Content.AsSpan(0, 97), "...") ? string.Concat(post.Content.AsSpan(0, 97), "...")
: post.Content; : post.Content;
var title = post.Title ?? (post.Content?.Length >= 10 ? post.Content[..10] + "..." : post.Content); var title = post.Title ?? (post.Content?.Length >= 10 ? post.Content[..10] + "..." : post.Content);
if (content is null) content ??= localizer["PostOnlyMedia"];
content = localizer["PostOnlyMedia"]; title ??= localizer["PostOnlyMedia"];
if (title is null)
title = localizer["PostOnlyMedia"];
return (title, content); return (title, content);
} }
@@ -329,7 +327,7 @@ public partial class PostService(
// Preview the link // Preview the link
var linkEmbed = await reader.GetLinkPreviewAsync(url); var linkEmbed = await reader.GetLinkPreviewAsync(url);
embeds.Add(linkEmbed.ToDictionary()); embeds.Add(EmbeddableBase.ToDictionary(linkEmbed));
processedLinks++; processedLinks++;
} }
catch catch
@@ -694,7 +692,7 @@ public partial class PostService(
Guid? currentUserId = currentUser is not null ? Guid.Parse(currentUser.Id) : null; Guid? currentUserId = currentUser is not null ? Guid.Parse(currentUser.Id) : null;
var updatedPoll = await polls.LoadPollEmbed(pollId, currentUserId); var updatedPoll = await polls.LoadPollEmbed(pollId, currentUserId);
embeds[pollIndex] = updatedPoll.ToDictionary(); embeds[pollIndex] = EmbeddableBase.ToDictionary(updatedPoll);
post.Meta["embeds"] = embeds; post.Meta["embeds"] = embeds;
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -29,9 +29,9 @@ public abstract class EmbeddableBase
{ {
public abstract string Type { get; } public abstract string Type { get; }
public Dictionary<string, object> ToDictionary() public static Dictionary<string, object> ToDictionary(dynamic input)
{ {
var jsonRaw = JsonSerializer.Serialize(this, GrpcTypeHelper.SerializerOptions); var jsonRaw = JsonSerializer.Serialize(input, GrpcTypeHelper.SerializerOptions);
return JsonSerializer.Deserialize<Dictionary<string, object>>(jsonRaw, GrpcTypeHelper.SerializerOptions) ?? []; return JsonSerializer.Deserialize<Dictionary<string, object>>(jsonRaw, GrpcTypeHelper.SerializerOptions);
} }
} }