Flag post

This commit is contained in:
2025-02-17 15:30:55 +08:00
parent db59826253
commit 14c17eded8
6 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,50 @@
package api
import (
"strconv"
"strings"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"github.com/gofiber/fiber/v2"
)
func createFlag(c *fiber.Ctx) error {
if err := sec.EnsureGrantedPerm(c, "FlagPost", true); err != nil {
return err
}
user := c.Locals("user").(authm.Account)
id := c.Params("postId")
var item models.Post
var err error
tx := services.FilterPostDraft(database.C)
if numericId, paramErr := strconv.Atoi(id); paramErr == nil {
item, err = services.GetPost(tx, uint(numericId))
} else {
segments := strings.Split(id, ":")
if len(segments) != 2 {
return fiber.NewError(fiber.StatusBadRequest, "invalid post id, must be a number or a string with two segment divided by a colon")
}
area := segments[0]
alias := segments[1]
item, err = services.GetPostByAlias(tx, alias, area)
}
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
flag, err := services.NewFlag(item, user.ID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(flag)
}

View File

@ -62,6 +62,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
posts.Get("/drafts", listDraftPost)
posts.Get("/:postId", getPost)
posts.Get("/:postId/insight", getPostInsight)
posts.Get("/:postId/flag", createFlag)
posts.Post("/:postId/react", reactPost)
posts.Post("/:postId/pin", pinPost)
posts.Delete("/:postId", deletePost)