2024-06-22 09:29:53 +00:00
|
|
|
package api
|
2024-03-05 15:40:54 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-06 15:31:22 +00:00
|
|
|
"fmt"
|
2024-10-31 14:41:32 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
|
|
|
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-10-13 13:25:15 +00:00
|
|
|
"gorm.io/gorm"
|
2024-08-17 07:40:07 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-07-23 16:10:04 +00:00
|
|
|
|
2024-11-02 05:41:51 +00:00
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/http/exts"
|
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
|
2024-06-01 14:15:10 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-03-05 15:40:54 +00:00
|
|
|
"github.com/samber/lo"
|
|
|
|
)
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
func universalPostFilter(c *fiber.Ctx, tx *gorm.DB) (*gorm.DB, error) {
|
|
|
|
tx = services.FilterPostDraft(tx)
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
|
2024-10-13 13:25:15 +00:00
|
|
|
tx = services.FilterPostWithUserContext(tx, &user)
|
|
|
|
} else {
|
|
|
|
tx = services.FilterPostWithUserContext(tx, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.QueryBool("noReply", true) {
|
|
|
|
tx = services.FilterPostReply(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Query("author")) > 0 {
|
2024-10-31 14:41:32 +00:00
|
|
|
var author models.Publisher
|
2024-10-31 14:48:51 +00:00
|
|
|
if err := database.C.Where("name = ?", c.Query("author")).First(&author).Error; err != nil {
|
2024-10-13 13:25:15 +00:00
|
|
|
return tx, fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
2024-11-02 15:47:44 +00:00
|
|
|
tx = tx.Where("publisher_id = ?", author.ID)
|
2024-10-13 13:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
2024-03-05 15:40:54 +00:00
|
|
|
func getPost(c *fiber.Ctx) error {
|
2024-08-17 07:40:07 +00:00
|
|
|
id := c.Params("postId")
|
|
|
|
|
|
|
|
var item models.Post
|
|
|
|
var err error
|
|
|
|
|
|
|
|
tx := services.FilterPostDraft(database.C)
|
2024-09-16 13:28:48 +00:00
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
|
2024-09-16 13:28:48 +00:00
|
|
|
tx = services.FilterPostWithUserContext(tx, &user)
|
|
|
|
} else {
|
|
|
|
tx = services.FilterPostWithUserContext(tx, nil)
|
|
|
|
}
|
|
|
|
|
2024-08-17 07:40:07 +00:00
|
|
|
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]
|
2024-08-17 09:12:04 +00:00
|
|
|
item, err = services.GetPostByAlias(tx, alias, area)
|
2024-08-17 07:40:07 +00:00
|
|
|
}
|
2024-03-05 15:40:54 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-07-13 15:16:40 +00:00
|
|
|
item.Metric = models.PostMetric{
|
|
|
|
ReplyCount: services.CountPostReply(item.ID),
|
|
|
|
ReactionCount: services.CountPostReactions(item.ID),
|
|
|
|
}
|
2024-10-13 05:37:11 +00:00
|
|
|
item.Metric.ReactionList, err = services.ListPostReactions(database.C.Where("post_id = ?", item.ID))
|
2024-03-05 15:40:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(item)
|
|
|
|
}
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
func searchPost(c *fiber.Ctx) error {
|
2024-03-05 15:40:54 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
tx := database.C
|
2024-07-29 16:09:00 +00:00
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
probe := c.Query("probe")
|
|
|
|
if len(probe) == 0 {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "probe is required")
|
2024-07-29 16:09:00 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
tx = services.FilterPostWithFuzzySearch(tx, probe)
|
2024-09-16 13:24:48 +00:00
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
var err error
|
|
|
|
if tx, err = universalPostFilter(c, tx); err != nil {
|
|
|
|
return err
|
2024-03-05 15:40:54 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 13:06:18 +00:00
|
|
|
countTx := tx
|
|
|
|
count, err := services.CountPost(countTx)
|
2024-03-05 15:40:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-07-23 08:12:19 +00:00
|
|
|
items, err := services.ListPost(tx, take, offset, "published_at DESC")
|
2024-03-05 15:40:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-13 12:12:23 +00:00
|
|
|
if c.QueryBool("truncate", true) {
|
|
|
|
for _, item := range items {
|
|
|
|
if item != nil {
|
|
|
|
item = lo.ToPtr(services.TruncatePostContent(*item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 15:40:54 +00:00
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": items,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
func listPost(c *fiber.Ctx) error {
|
2024-08-10 13:11:55 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
tx := database.C
|
2024-08-10 13:11:55 +00:00
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
var err error
|
|
|
|
if tx, err = universalPostFilter(c, tx); err != nil {
|
|
|
|
return err
|
2024-08-10 13:11:55 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
countTx := tx
|
|
|
|
count, err := services.CountPost(countTx)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2024-08-10 13:11:55 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
items, err := services.ListPost(tx, take, offset, "published_at DESC")
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-08-10 13:11:55 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
if c.QueryBool("truncate", true) {
|
|
|
|
for _, item := range items {
|
|
|
|
if item != nil {
|
|
|
|
item = lo.ToPtr(services.TruncatePostContent(*item))
|
|
|
|
}
|
|
|
|
}
|
2024-08-10 13:11:55 +00:00
|
|
|
}
|
2024-10-13 13:25:15 +00:00
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": items,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func listPostMinimal(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
|
|
|
tx := database.C
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if tx, err = universalPostFilter(c, tx); err != nil {
|
|
|
|
return err
|
2024-08-10 13:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
countTx := tx
|
|
|
|
count, err := services.CountPost(countTx)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := services.ListPostMinimal(tx, take, offset, "published_at DESC")
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-13 12:12:23 +00:00
|
|
|
if c.QueryBool("truncate", false) {
|
|
|
|
for _, item := range items {
|
|
|
|
if item != nil {
|
|
|
|
item = lo.ToPtr(services.TruncatePostContent(*item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-10 13:11:55 +00:00
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": items,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-03 14:16:23 +00:00
|
|
|
func listDraftPost(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-07-03 14:16:23 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-07-03 14:16:23 +00:00
|
|
|
|
|
|
|
tx := services.FilterPostWithAuthorDraft(database.C, user.ID)
|
|
|
|
|
|
|
|
count, err := services.CountPost(tx)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-07-23 08:12:19 +00:00
|
|
|
items, err := services.ListPost(tx, take, offset, "created_at DESC", true)
|
2024-07-03 14:16:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-13 12:12:23 +00:00
|
|
|
if c.QueryBool("truncate", true) {
|
|
|
|
for _, item := range items {
|
|
|
|
if item != nil {
|
|
|
|
item = lo.ToPtr(services.TruncatePostContent(*item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 14:16:23 +00:00
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": items,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
func deletePost(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-06-22 09:29:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-05-15 11:45:49 +00:00
|
|
|
id, _ := c.ParamsInt("postId", 0)
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
publisherId := c.QueryInt("publisherId", 0)
|
|
|
|
if publisherId <= 0 {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "missing publisher id in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
publisher, err := services.GetPublisher(uint(publisherId), user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
var item models.Post
|
|
|
|
if err := database.C.Where(models.Post{
|
2024-10-31 14:41:32 +00:00
|
|
|
BaseModel: cruda.BaseModel{ID: uint(id)},
|
|
|
|
PublisherID: publisher.ID,
|
2024-05-15 11:45:49 +00:00
|
|
|
}).First(&item).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.DeletePost(item); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-10-14 13:30:02 +00:00
|
|
|
} else {
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.delete",
|
|
|
|
strconv.Itoa(int(item.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:30:02 +00:00
|
|
|
)
|
2024-05-15 11:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-03-05 15:40:54 +00:00
|
|
|
func reactPost(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureGrantedPerm(c, "CreateReactions", true); err != nil {
|
2024-06-22 09:29:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-03-05 15:40:54 +00:00
|
|
|
|
|
|
|
var data struct {
|
2024-07-03 14:16:23 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Attitude models.ReactionAttitude `json:"attitude"`
|
2024-03-05 15:40:54 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 09:29:53 +00:00
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
2024-03-05 15:40:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reaction := models.Reaction{
|
|
|
|
Symbol: data.Symbol,
|
|
|
|
Attitude: data.Attitude,
|
|
|
|
AccountID: user.ID,
|
2024-03-06 15:31:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
var res models.Post
|
2024-07-23 16:10:04 +00:00
|
|
|
if err := database.C.Where("id = ?", c.Params("postId")).Select("id").First(&res).Error; err != nil {
|
2024-05-15 11:45:49 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find post to react: %v", err))
|
2024-03-06 15:31:22 +00:00
|
|
|
} else {
|
2024-10-31 14:41:32 +00:00
|
|
|
reaction.PostID = res.ID
|
2024-03-05 15:40:54 +00:00
|
|
|
}
|
|
|
|
|
2024-06-08 04:33:10 +00:00
|
|
|
if positive, reaction, err := services.ReactPost(user, reaction); err != nil {
|
2024-03-05 15:40:54 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.react",
|
|
|
|
strconv.Itoa(int(res.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:30:02 +00:00
|
|
|
)
|
|
|
|
|
2024-03-05 15:40:54 +00:00
|
|
|
return c.Status(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent)).JSON(reaction)
|
|
|
|
}
|
|
|
|
}
|
2024-07-25 14:45:31 +00:00
|
|
|
|
|
|
|
func pinPost(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-07-25 14:45:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-07-25 14:45:31 +00:00
|
|
|
|
|
|
|
var res models.Post
|
2024-11-02 15:47:44 +00:00
|
|
|
if err := database.C.Where("id = ? AND publisher_id = ?", c.Params("postId"), user.ID).First(&res).Error; err != nil {
|
2024-07-25 14:45:31 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find post in your posts to pin: %v", err))
|
|
|
|
}
|
|
|
|
|
2024-07-25 14:58:47 +00:00
|
|
|
if status, err := services.PinPost(res); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else if status {
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.pin",
|
|
|
|
strconv.Itoa(int(res.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:30:02 +00:00
|
|
|
)
|
2024-07-25 14:58:47 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
} else {
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.unpin",
|
|
|
|
strconv.Itoa(int(res.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:30:02 +00:00
|
|
|
)
|
2024-07-25 14:58:47 +00:00
|
|
|
return c.SendStatus(fiber.StatusNoContent)
|
2024-07-25 14:45:31 +00:00
|
|
|
}
|
|
|
|
}
|