Feature replies

👔 Hide replies from listing by default
This commit is contained in:
LittleSheep 2024-09-16 21:24:48 +08:00
parent 20e399bb39
commit 13a65ad518
4 changed files with 53 additions and 0 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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 {

View File

@ -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)
}