From d231b5f27eaf04313de64df1f307fd6cabd50e43 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 5 Aug 2025 21:26:31 +0800 Subject: [PATCH] :bug: Fix loading poll --- DysonNetwork.Sphere/Post/PostController.cs | 4 ++-- DysonNetwork.Sphere/Post/PostService.cs | 27 +++++++++------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/DysonNetwork.Sphere/Post/PostController.cs b/DysonNetwork.Sphere/Post/PostController.cs index 9ff8442..9f183ee 100644 --- a/DysonNetwork.Sphere/Post/PostController.cs +++ b/DysonNetwork.Sphere/Post/PostController.cs @@ -341,7 +341,7 @@ public class PostController( post.ForwardedPostId = forwardedPost.Id; } - if (request.PollId is not null) + if (request.PollId.HasValue) { try { @@ -493,7 +493,7 @@ public class PostController( if (request.Type is not null) post.Type = request.Type.Value; if (request.Meta is not null) post.Meta = request.Meta; - if (request.PollId is not null) + if (request.PollId.HasValue) { try { diff --git a/DysonNetwork.Sphere/Post/PostService.cs b/DysonNetwork.Sphere/Post/PostService.cs index aecb89b..7cd430e 100644 --- a/DysonNetwork.Sphere/Post/PostService.cs +++ b/DysonNetwork.Sphere/Post/PostService.cs @@ -661,24 +661,20 @@ public partial class PostService( g => g.Count() ); } - + private async Task LoadPollEmbed(Post post, Account? currentUser) { - if (!post.Meta!.TryGetValue("embeds", out var existingEmbeds) || - existingEmbeds is not List) - { - post.Meta["embeds"] = new List>(); + if (!post.Meta!.TryGetValue("embeds", out var value)) return; - } - var embeds = (List>)post.Meta["embeds"]; - + var embeds = (List>)value; + // Find the index of the poll embed first - var pollIndex = embeds.FindIndex(e => + var pollIndex = embeds.FindIndex(e => e.ContainsKey("type") && (string)e["type"] == "poll"); if (pollIndex < 0) return; - + var pollEmbed = embeds[pollIndex]; try { @@ -698,7 +694,7 @@ public partial class PostService( } catch (Exception ex) { - logger?.LogError(ex, "Failed to load poll embed for post {PostId}", post.Id); + logger.LogError(ex, "Failed to load poll embed for post {PostId}", post.Id); } } @@ -713,11 +709,10 @@ public partial class PostService( posts = await LoadPublishers(posts); posts = await LoadInteractive(posts, currentUser); - var postsWithEmbed = posts - .Where(e => e.Meta is not null && e.Meta.ContainsKey("embeds")) - .ToList(); - - foreach (var post in postsWithEmbed) + foreach ( + var post in posts + .Where(e => e.Meta is not null && e.Meta.ContainsKey("embeds")) + ) await LoadPollEmbed(post, currentUser); if (truncate)