🐛 Trying to fix post link auto preview

This commit is contained in:
LittleSheep 2025-06-21 14:17:55 +08:00
parent a431fbbd51
commit d385abbf57

View File

@ -282,7 +282,7 @@ public partial class PostService(
// Initialize meta dictionary if null // Initialize meta dictionary if null
item.Meta ??= new Dictionary<string, object>(); item.Meta ??= new Dictionary<string, object>();
// Initialize embeds array if it doesn't exist // Initialize the embeds' array if it doesn't exist
if (!item.Meta.TryGetValue("embeds", out var existingEmbeds) || existingEmbeds is not List<IEmbeddable>) if (!item.Meta.TryGetValue("embeds", out var existingEmbeds) || existingEmbeds is not List<IEmbeddable>)
{ {
item.Meta["embeds"] = new List<IEmbeddable>(); item.Meta["embeds"] = new List<IEmbeddable>();
@ -292,7 +292,7 @@ public partial class PostService(
// Process up to 3 links to avoid excessive processing // Process up to 3 links to avoid excessive processing
var processedLinks = 0; var processedLinks = 0;
foreach (System.Text.RegularExpressions.Match match in matches) foreach (Match match in matches)
{ {
if (processedLinks >= 3) if (processedLinks >= 3)
break; break;
@ -301,7 +301,7 @@ public partial class PostService(
try try
{ {
// Check if this URL is already in the embeds list // Check if this URL is already in the embed list
var urlAlreadyEmbedded = embeds.OfType<LinkEmbed>().Any(e => e.Url == url); var urlAlreadyEmbedded = embeds.OfType<LinkEmbed>().Any(e => e.Url == url);
if (urlAlreadyEmbedded) if (urlAlreadyEmbedded)
continue; continue;
@ -317,6 +317,8 @@ public partial class PostService(
} }
} }
item.Meta["embeds"] = embeds;
return item; return item;
} }
@ -339,8 +341,7 @@ public partial class PostService(
// If embeds were added, update the post in the database // If embeds were added, update the post in the database
if (updatedPost.Meta != null && if (updatedPost.Meta != null &&
updatedPost.Meta.TryGetValue("embeds", out var embeds) && updatedPost.Meta.TryGetValue("embeds", out var embeds) &&
embeds is List<IEmbeddable> embedsList && embeds is List<IEmbeddable> { Count: > 0 } embedsList)
embedsList.Count > 0)
{ {
// Get a fresh copy of the post from the database // Get a fresh copy of the post from the database
var dbPost = await dbContext.Posts.FindAsync(post.Id); var dbPost = await dbContext.Posts.FindAsync(post.Id);