Truncate too content when listing

This commit is contained in:
LittleSheep 2024-10-13 20:12:23 +08:00
parent d6b6f2e399
commit d1bbf751d3
3 changed files with 61 additions and 0 deletions

View File

@ -108,6 +108,14 @@ func listPost(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.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{ return c.JSON(fiber.Map{
"count": count, "count": count,
"data": items, "data": items,
@ -161,6 +169,14 @@ func listPostMinimal(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.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{ return c.JSON(fiber.Map{
"count": count, "count": count,
"data": items, "data": items,
@ -188,6 +204,14 @@ func listDraftPost(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.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{ return c.JSON(fiber.Map{
"count": count, "count": count,
"data": items, "data": items,

View File

@ -52,6 +52,14 @@ func listRecommendationNews(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.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{ return c.JSON(fiber.Map{
"count": count, "count": count,
"data": items, "data": items,
@ -106,6 +114,14 @@ func listRecommendationFriends(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.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{ return c.JSON(fiber.Map{
"count": count, "count": count,
"data": items, "data": items,
@ -148,6 +164,14 @@ func listRecommendationShuffle(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.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{ return c.JSON(fiber.Map{
"count": count, "count": count,
"data": items, "data": items,

View File

@ -470,3 +470,16 @@ func PinPost(post models.Post) (bool, error) {
} }
return post.PinnedAt != nil, nil 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
}