From 13a65ad5187c0dd7f1f91bc969ee76dde5b6616f Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 16 Sep 2024 21:24:48 +0800 Subject: [PATCH] :sparkles: Feature replies :necktie: Hide replies from listing by default --- pkg/internal/server/api/index.go | 1 + pkg/internal/server/api/posts_api.go | 4 +++ pkg/internal/server/api/recommendation_api.go | 13 +++++++ pkg/internal/server/api/replies_api.go | 35 +++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index 2d3d46a..390a596 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -42,6 +42,7 @@ func MapAPIs(app *fiber.App, baseURL string) { posts.Delete("/:postId", deletePost) posts.Get("/:postId/replies", listPostReplies) + posts.Get("/:postId/featured", listPostFeaturedReply) } api.Get("/categories", listCategories) diff --git a/pkg/internal/server/api/posts_api.go b/pkg/internal/server/api/posts_api.go index 1e44186..d1beae0 100644 --- a/pkg/internal/server/api/posts_api.go +++ b/pkg/internal/server/api/posts_api.go @@ -71,6 +71,10 @@ func listPost(c *fiber.Ctx) error { } } + if c.QueryBool("noReply", true) { + tx = services.FilterPostReply(tx) + } + if len(c.Query("author")) > 0 { var author models.Account if err := database.C.Where(&hyper.BaseUser{Name: c.Query("author")}).First(&author).Error; err != nil { diff --git a/pkg/internal/server/api/recommendation_api.go b/pkg/internal/server/api/recommendation_api.go index 8c2143a..54a6c52 100644 --- a/pkg/internal/server/api/recommendation_api.go +++ b/pkg/internal/server/api/recommendation_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "git.solsynth.dev/hydrogen/interactive/pkg/internal/database" "git.solsynth.dev/hydrogen/interactive/pkg/internal/gap" "git.solsynth.dev/hydrogen/interactive/pkg/internal/models" @@ -31,6 +32,10 @@ func listRecommendationNews(c *fiber.Ctx) error { } } + if c.QueryBool("noReply", true) { + tx = services.FilterPostReply(tx) + } + countTx := tx count, err := services.CountPost(countTx) if err != nil { @@ -81,6 +86,10 @@ func listRecommendationFriends(c *fiber.Ctx) error { } } + if c.QueryBool("noReply", true) { + tx = services.FilterPostReply(tx) + } + countTx := tx count, err := services.CountPost(countTx) if err != nil { @@ -124,6 +133,10 @@ func listRecommendationShuffle(c *fiber.Ctx) error { } } + if c.QueryBool("noReply", true) { + tx = services.FilterPostReply(tx) + } + countTx := tx count, err := services.CountPost(countTx) if err != nil { diff --git a/pkg/internal/server/api/replies_api.go b/pkg/internal/server/api/replies_api.go index 1aabe62..70c8f51 100644 --- a/pkg/internal/server/api/replies_api.go +++ b/pkg/internal/server/api/replies_api.go @@ -52,3 +52,38 @@ func listPostReplies(c *fiber.Ctx) error { "data": items, }) } + +func listPostFeaturedReply(c *fiber.Ctx) error { + take := c.QueryInt("take", 0) + take = max(1, min(take, 3)) + + tx := database.C + var post models.Post + if err := database.C.Where("id = ?", c.Params("postId")).First(&post).Error; err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find post: %v", err)) + } else { + tx = services.FilterPostReply(tx, post.ID) + } + + if len(c.Query("author")) > 0 { + var author models.Account + if err := database.C.Where(&hyper.BaseUser{Name: c.Query("author")}).First(&author).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + tx = tx.Where("author_id = ?", author.ID) + } + + if len(c.Query("category")) > 0 { + tx = services.FilterPostWithCategory(tx, c.Query("category")) + } + if len(c.Query("tag")) > 0 { + tx = services.FilterPostWithTag(tx, c.Query("tag")) + } + + items, err := services.ListPost(tx, take, 0, "(COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC, published_at DESC") + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + + return c.JSON(items) +}