From d1bbf751d32651c995b99f3cb23bc6aa6e33d29b Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 13 Oct 2024 20:12:23 +0800 Subject: [PATCH] :sparkles: Truncate too content when listing --- pkg/internal/server/api/posts_api.go | 24 +++++++++++++++++++ pkg/internal/server/api/recommendation_api.go | 24 +++++++++++++++++++ pkg/internal/services/posts.go | 13 ++++++++++ 3 files changed, 61 insertions(+) diff --git a/pkg/internal/server/api/posts_api.go b/pkg/internal/server/api/posts_api.go index 8261b2b..c23646d 100644 --- a/pkg/internal/server/api/posts_api.go +++ b/pkg/internal/server/api/posts_api.go @@ -108,6 +108,14 @@ func listPost(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } + if c.QueryBool("truncate", true) { + for _, item := range items { + if item != nil { + item = lo.ToPtr(services.TruncatePostContent(*item)) + } + } + } + return c.JSON(fiber.Map{ "count": count, "data": items, @@ -161,6 +169,14 @@ func listPostMinimal(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } + if c.QueryBool("truncate", false) { + for _, item := range items { + if item != nil { + item = lo.ToPtr(services.TruncatePostContent(*item)) + } + } + } + return c.JSON(fiber.Map{ "count": count, "data": items, @@ -188,6 +204,14 @@ func listDraftPost(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } + if c.QueryBool("truncate", true) { + for _, item := range items { + if item != nil { + item = lo.ToPtr(services.TruncatePostContent(*item)) + } + } + } + return c.JSON(fiber.Map{ "count": count, "data": items, diff --git a/pkg/internal/server/api/recommendation_api.go b/pkg/internal/server/api/recommendation_api.go index 54a6c52..8103605 100644 --- a/pkg/internal/server/api/recommendation_api.go +++ b/pkg/internal/server/api/recommendation_api.go @@ -52,6 +52,14 @@ func listRecommendationNews(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } + if c.QueryBool("truncate", true) { + for _, item := range items { + if item != nil { + item = lo.ToPtr(services.TruncatePostContent(*item)) + } + } + } + return c.JSON(fiber.Map{ "count": count, "data": items, @@ -106,6 +114,14 @@ func listRecommendationFriends(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } + if c.QueryBool("truncate", true) { + for _, item := range items { + if item != nil { + item = lo.ToPtr(services.TruncatePostContent(*item)) + } + } + } + return c.JSON(fiber.Map{ "count": count, "data": items, @@ -148,6 +164,14 @@ func listRecommendationShuffle(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } + if c.QueryBool("truncate", true) { + for _, item := range items { + if item != nil { + item = lo.ToPtr(services.TruncatePostContent(*item)) + } + } + } + return c.JSON(fiber.Map{ "count": count, "data": items, diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 8c9f1c4..af3b010 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -470,3 +470,16 @@ func PinPost(post models.Post) (bool, error) { } return post.PinnedAt != nil, nil } + +const TruncatePostContentThreshold = 160 + +func TruncatePostContent(post models.Post) models.Post { + if post.Body["content"] != nil { + if val, ok := post.Body["content"].(string); ok && len(val) >= TruncatePostContentThreshold { + post.Body["content"] = val[:TruncatePostContentThreshold] + "..." + post.Body["content_truncated"] = true + } + } + + return post +}