✨ Truncated the post's body to prevent them from being too long
This commit is contained in:
		| @@ -30,6 +30,7 @@ public class ActivityService(AppDatabase db) | ||||
|                 .Include(e => e.Tags) | ||||
|                 .FilterWithVisibility(currentUser, userFriends) | ||||
|                 .ToListAsync(); | ||||
|             posts = PostService.TruncatePostContent(posts); | ||||
|  | ||||
|             var postsDict = posts.ToDictionary(p => p.Id); | ||||
|  | ||||
|   | ||||
| @@ -51,7 +51,7 @@ public class Post : ModelBase | ||||
|     public Post? ForwardedPost { get; set; } | ||||
|     public ICollection<CloudFile> Attachments { get; set; } = new List<CloudFile>(); | ||||
|      | ||||
|     public NpgsqlTsVector SearchVector { get; set; } | ||||
|     [JsonIgnore] public NpgsqlTsVector SearchVector { get; set; } | ||||
|  | ||||
|     public Publisher Publisher { get; set; } = null!; | ||||
|     public ICollection<PostReaction> Reactions { get; set; } = new List<PostReaction>(); | ||||
| @@ -59,7 +59,8 @@ public class Post : ModelBase | ||||
|     public ICollection<PostCategory> Categories { get; set; } = new List<PostCategory>(); | ||||
|     public ICollection<PostCollection> Collections { get; set; } = new List<PostCollection>(); | ||||
|  | ||||
|     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; | ||||
| } | ||||
|  | ||||
| public class PostTag : ModelBase | ||||
|   | ||||
| @@ -38,6 +38,7 @@ public class PostController(AppDatabase db, PostService ps, RelationshipService | ||||
|             .Skip(offset) | ||||
|             .Take(take) | ||||
|             .ToListAsync(); | ||||
|         posts = PostService.TruncatePostContent(posts); | ||||
|  | ||||
|         Response.Headers["X-Total"] = totalCount.ToString(); | ||||
|  | ||||
| @@ -101,6 +102,7 @@ public class PostController(AppDatabase db, PostService ps, RelationshipService | ||||
|             .Skip(offset) | ||||
|             .Take(take) | ||||
|             .ToListAsync(); | ||||
|         posts = PostService.TruncatePostContent(posts); | ||||
|  | ||||
|         Response.Headers["X-Total"] = totalCount.ToString(); | ||||
|  | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| using System.Text.Json; | ||||
| using DysonNetwork.Sphere.Activity; | ||||
| using DysonNetwork.Sphere.Storage; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| @@ -7,6 +8,64 @@ namespace DysonNetwork.Sphere.Post; | ||||
|  | ||||
| public class PostService(AppDatabase db, FileService fs, ActivityService act) | ||||
| { | ||||
|     public static List<Post> TruncatePostContent(List<Post> input) | ||||
|     { | ||||
|         // This truncate post content is designed for quill delta | ||||
|         const int maxLength = 256; | ||||
|         foreach (var item in input) | ||||
|         { | ||||
|             if (item.Content is not { RootElement: var rootElement }) continue; | ||||
|  | ||||
|             if (rootElement.ValueKind != JsonValueKind.Array) continue; | ||||
|             var totalLength = 0; | ||||
|             var truncatedArrayElements = new List<JsonElement>(); | ||||
|  | ||||
|             foreach (var element in rootElement.EnumerateArray()) | ||||
|             { | ||||
|                 if (element is { ValueKind: JsonValueKind.Object } && | ||||
|                     element.TryGetProperty("insert", out var insertProperty)) | ||||
|                 { | ||||
|                     if (insertProperty is { ValueKind: JsonValueKind.String }) | ||||
|                     { | ||||
|                         var textContent = insertProperty.GetString()!; | ||||
|                         if (totalLength + textContent.Length <= maxLength) | ||||
|                         { | ||||
|                             truncatedArrayElements.Add(element); | ||||
|                             totalLength += textContent.Length; | ||||
|                         } | ||||
|                         else | ||||
|                         { | ||||
|                             var remainingLength = maxLength - totalLength; | ||||
|                             if (remainingLength > 0) | ||||
|                             { | ||||
|                                 using var truncatedElementDocument = | ||||
|                                     JsonDocument.Parse( | ||||
|                                         $@"{{ ""insert"": ""{textContent.Substring(0, remainingLength)}"" }}" | ||||
|                                     ); | ||||
|                                 truncatedArrayElements.Add(truncatedElementDocument.RootElement.Clone()); | ||||
|                                 totalLength = maxLength; | ||||
|                             } | ||||
|  | ||||
|                             break; | ||||
|                         } | ||||
|                     } | ||||
|                     else | ||||
|                         truncatedArrayElements.Add(element); | ||||
|                 } | ||||
|                 else | ||||
|                     truncatedArrayElements.Add(element); | ||||
|  | ||||
|                 if (totalLength >= maxLength) | ||||
|                     break; | ||||
|             } | ||||
|  | ||||
|             using var newDocument = JsonDocument.Parse(JsonSerializer.Serialize(truncatedArrayElements)); | ||||
|             item.Content = newDocument; | ||||
|         } | ||||
|  | ||||
|         return input; | ||||
|     } | ||||
|  | ||||
|     public async Task<Post> PostAsync( | ||||
|         Account.Account user, | ||||
|         Post post, | ||||
|   | ||||
							
								
								
									
										18
									
								
								DysonNetwork.Sphere/Realm/Realm.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								DysonNetwork.Sphere/Realm/Realm.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
| using System.Text.Json.Serialization; | ||||
| using DysonNetwork.Sphere.Storage; | ||||
|  | ||||
| namespace DysonNetwork.Sphere.Realm; | ||||
|  | ||||
| public class Realm : ModelBase | ||||
| { | ||||
|     public long Id { get; set; } | ||||
|     [MaxLength(1024)] public string Name { get; set; } = string.Empty; | ||||
|     [MaxLength(4096)] public string Description { get; set; } = string.Empty; | ||||
|  | ||||
|     public CloudFile? Picture { get; set; } | ||||
|     public CloudFile? Background { get; set; } | ||||
|  | ||||
|     public long? AccountId { get; set; } | ||||
|     [JsonIgnore] public Account.Account? Account { get; set; } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user