🐛 Fixing the post truncate

This commit is contained in:
LittleSheep 2025-06-21 11:46:08 +08:00
parent bcd107ae2c
commit aefc38c5a3
2 changed files with 9 additions and 8 deletions

View File

@ -66,7 +66,7 @@ public class Post : ModelBase, IIdentifiedResource, IActivity
public ICollection<PostCollection> Collections { get; set; } = new List<PostCollection>(); public ICollection<PostCollection> Collections { get; set; } = new List<PostCollection>();
[JsonIgnore] public bool Empty => Content == null && Attachments.Count == 0 && ForwardedPostId == null; [JsonIgnore] public bool Empty => Content == null && Attachments.Count == 0 && ForwardedPostId == null;
[NotMapped] public bool IsTruncated = false; [NotMapped] public bool IsTruncated { get; set; } = false;
public string ResourceIdentifier => $"post/{Id}"; public string ResourceIdentifier => $"post/{Id}";

View File

@ -20,9 +20,10 @@ public class PostService(
{ {
private const string PostFileUsageIdentifier = "post"; private const string PostFileUsageIdentifier = "post";
public static List<Post> TruncatePostContent(List<Post> input) private static List<Post> TruncatePostContent(List<Post> input)
{ {
const int maxLength = 256; const int maxLength = 256;
const int embedMaxLength = 80;
foreach (var item in input) foreach (var item in input)
{ {
if (item.Content?.Length > maxLength) if (item.Content?.Length > maxLength)
@ -31,17 +32,17 @@ public class PostService(
item.IsTruncated = true; item.IsTruncated = true;
} }
// Truncate replied post content // Truncate replied post content with shorter embed length
if (item.RepliedPost?.Content?.Length > maxLength) if (item.RepliedPost?.Content?.Length > embedMaxLength)
{ {
item.RepliedPost.Content = item.RepliedPost.Content[..maxLength]; item.RepliedPost.Content = item.RepliedPost.Content[..embedMaxLength];
item.RepliedPost.IsTruncated = true; item.RepliedPost.IsTruncated = true;
} }
// Truncate forwarded post content // Truncate forwarded post content with shorter embed length
if (item.ForwardedPost?.Content?.Length > maxLength) if (item.ForwardedPost?.Content?.Length > embedMaxLength)
{ {
item.ForwardedPost.Content = item.ForwardedPost.Content[..maxLength]; item.ForwardedPost.Content = item.ForwardedPost.Content[..embedMaxLength];
item.ForwardedPost.IsTruncated = true; item.ForwardedPost.IsTruncated = true;
} }
} }