👔 Update post truncate logic

This commit is contained in:
2025-12-27 23:03:26 +08:00
parent 983f57c4c2
commit f06d93a348

View File

@@ -9,6 +9,7 @@ using DysonNetwork.Sphere.Publisher;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using NodaTime; using NodaTime;
using Markdig;
using DysonNetwork.Shared.Models; using DysonNetwork.Shared.Models;
namespace DysonNetwork.Sphere.Post; namespace DysonNetwork.Sphere.Post;
@@ -37,23 +38,27 @@ public partial class PostService(
{ {
if (item.Content?.Length > maxLength) if (item.Content?.Length > maxLength)
{ {
item.Content = item.Content[..maxLength]; var plainText = Markdown.ToPlainText(item.Content);
item.Content = plainText.Length > maxLength ? plainText[..maxLength] : plainText;
item.IsTruncated = true; item.IsTruncated = true;
} }
// Truncate replied post content with shorter embed length // Truncate replied post content with shorter embed length
if (item.RepliedPost?.Content?.Length > embedMaxLength) if (item.RepliedPost?.Content != null)
{ {
item.RepliedPost.Content = item.RepliedPost.Content[..embedMaxLength]; var plainText = Markdown.ToPlainText(item.RepliedPost.Content);
item.RepliedPost.IsTruncated = true; if (plainText.Length > embedMaxLength)
{
item.RepliedPost.Content = plainText[..embedMaxLength];
item.RepliedPost.IsTruncated = true;
}
} }
// Truncate forwarded post content with shorter embed length // Truncate forwarded post content with shorter embed length
if (item.ForwardedPost?.Content?.Length > embedMaxLength) if (item.ForwardedPost?.Content == null || Markdown.ToPlainText(item.ForwardedPost.Content).Length <= embedMaxLength) continue;
{ var forwardedPlainText = Markdown.ToPlainText(item.ForwardedPost.Content);
item.ForwardedPost.Content = item.ForwardedPost.Content[..embedMaxLength]; item.ForwardedPost.Content = forwardedPlainText[..embedMaxLength];
item.ForwardedPost.IsTruncated = true; item.ForwardedPost.IsTruncated = true;
}
} }
return input; return input;
@@ -1033,4 +1038,4 @@ public static class PostQueryExtensions
(e.Publisher.AccountId != null && userFriends.Contains(e.Publisher.AccountId.Value)) || (e.Publisher.AccountId != null && userFriends.Contains(e.Publisher.AccountId.Value)) ||
publishersId.Contains(e.PublisherId)); publishersId.Contains(e.PublisherId));
} }
} }