Interactive/pkg/server/moments_api.go

237 lines
5.9 KiB
Go
Raw Normal View History

2024-02-02 15:42:42 +00:00
package server
import (
2024-02-03 16:24:04 +00:00
"strings"
2024-02-03 17:08:31 +00:00
"time"
2024-02-03 16:24:04 +00:00
2024-02-02 15:42:42 +00:00
"code.smartsheep.studio/hydrogen/interactive/pkg/database"
"code.smartsheep.studio/hydrogen/interactive/pkg/models"
"code.smartsheep.studio/hydrogen/interactive/pkg/services"
"github.com/gofiber/fiber/v2"
2024-02-02 16:50:23 +00:00
"github.com/google/uuid"
"github.com/samber/lo"
2024-02-02 15:42:42 +00:00
)
2024-03-03 08:10:25 +00:00
func contextMoment() *services.PostTypeContext[models.Moment] {
2024-03-02 17:23:11 +00:00
return &services.PostTypeContext[models.Moment]{
Tx: database.C,
TypeName: "Moment",
CanReply: false,
CanRepost: true,
2024-02-03 11:22:50 +00:00
}
2024-03-02 17:23:11 +00:00
}
2024-02-02 15:42:42 +00:00
2024-03-02 17:23:11 +00:00
func getMoment(c *fiber.Ctx) error {
2024-03-03 13:24:08 +00:00
alias := c.Params("momentId")
2024-03-03 08:10:25 +00:00
mx := contextMoment().FilterPublishedAt(time.Now())
2024-02-02 15:42:42 +00:00
2024-03-03 13:24:08 +00:00
item, err := mx.GetViaAlias(alias)
2024-02-03 07:20:32 +00:00
if err != nil {
2024-03-02 17:23:11 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
2024-02-02 16:50:23 +00:00
}
2024-03-02 17:23:11 +00:00
return c.JSON(item)
2024-02-02 15:42:42 +00:00
}
2024-03-02 17:23:11 +00:00
func listMoment(c *fiber.Ctx) error {
2024-02-05 17:10:22 +00:00
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
2024-02-09 03:43:21 +00:00
realmId := c.QueryInt("realmId", 0)
2024-03-03 08:10:25 +00:00
mx := contextMoment().
2024-03-02 17:23:11 +00:00
FilterPublishedAt(time.Now()).
FilterRealm(uint(realmId)).
SortCreatedAt("desc")
2024-02-09 03:43:21 +00:00
var author models.Account
if len(c.Query("authorId")) > 0 {
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
2024-03-02 17:23:11 +00:00
mx = mx.FilterAuthor(author.ID)
}
if len(c.Query("category")) > 0 {
2024-03-02 17:23:11 +00:00
mx = mx.FilterWithCategory(c.Query("category"))
}
if len(c.Query("tag")) > 0 {
2024-03-02 17:23:11 +00:00
mx = mx.FilterWithTag(c.Query("tag"))
}
2024-02-14 10:48:02 +00:00
if !c.QueryBool("reply", true) {
2024-03-02 17:23:11 +00:00
mx = mx.FilterReply(true)
2024-02-14 10:48:02 +00:00
}
2024-03-02 17:23:11 +00:00
count, err := mx.Count()
if err != nil {
2024-02-05 17:10:22 +00:00
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
2024-03-02 17:23:11 +00:00
items, err := mx.List(take, offset)
2024-02-05 17:10:22 +00:00
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
2024-03-02 17:23:11 +00:00
"data": items,
2024-02-05 17:10:22 +00:00
})
}
2024-03-02 17:23:11 +00:00
func createMoment(c *fiber.Ctx) error {
2024-02-02 15:42:42 +00:00
user := c.Locals("principal").(models.Account)
var data struct {
2024-02-04 10:40:20 +00:00
Alias string `json:"alias"`
Content string `json:"content" validate:"required"`
2024-03-02 17:23:11 +00:00
Hashtags []models.Tag `json:"hashtags"`
2024-02-04 10:40:20 +00:00
Categories []models.Category `json:"categories"`
Attachments []models.Attachment `json:"attachments"`
PublishedAt *time.Time `json:"published_at"`
2024-02-05 11:25:56 +00:00
RealmID *uint `json:"realm_id"`
2024-02-04 10:40:20 +00:00
RepostTo uint `json:"repost_to"`
2024-02-02 15:42:42 +00:00
}
if err := BindAndValidate(c, &data); err != nil {
return err
2024-02-02 16:50:23 +00:00
} else if len(data.Alias) == 0 {
data.Alias = strings.ReplaceAll(uuid.NewString(), "-", "")
2024-02-02 15:42:42 +00:00
}
2024-03-03 08:10:25 +00:00
mx := contextMoment()
2024-03-02 17:23:11 +00:00
item := models.Moment{
PostBase: models.PostBase{
Alias: data.Alias,
Attachments: data.Attachments,
PublishedAt: data.PublishedAt,
AuthorID: user.ID,
},
Hashtags: data.Hashtags,
Categories: data.Categories,
Content: data.Content,
RealmID: data.RealmID,
}
2024-02-03 16:24:04 +00:00
var relatedCount int64
if data.RepostTo > 0 {
2024-03-02 17:23:11 +00:00
if err := database.C.Where("id = ?", data.RepostTo).
Model(&models.Moment{}).Count(&relatedCount).Error; err != nil {
2024-02-03 16:24:04 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else if relatedCount <= 0 {
return fiber.NewError(fiber.StatusNotFound, "related post was not found")
} else {
2024-03-02 17:23:11 +00:00
item.RepostID = &data.RepostTo
2024-02-03 16:24:04 +00:00
}
}
2024-02-05 11:25:56 +00:00
var realm *models.Realm
if data.RealmID != nil {
if err := database.C.Where(&models.Realm{
BaseModel: models.BaseModel{ID: *data.RealmID},
}).First(&realm).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
}
2024-03-02 17:23:11 +00:00
item, err := mx.New(item)
2024-02-03 17:08:31 +00:00
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
2024-03-02 17:23:11 +00:00
return c.JSON(item)
2024-02-03 17:08:31 +00:00
}
2024-03-02 17:23:11 +00:00
func editMoment(c *fiber.Ctx) error {
2024-02-03 17:08:31 +00:00
user := c.Locals("principal").(models.Account)
2024-03-02 17:23:11 +00:00
id, _ := c.ParamsInt("momentId", 0)
2024-02-03 17:08:31 +00:00
var data struct {
Alias string `json:"alias" validate:"required"`
Content string `json:"content" validate:"required"`
PublishedAt *time.Time `json:"published_at"`
2024-03-02 17:23:11 +00:00
Hashtags []models.Tag `json:"hashtags"`
Categories []models.Category `json:"categories"`
Attachments []models.Attachment `json:"attachments"`
2024-02-03 17:08:31 +00:00
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
2024-03-03 08:10:25 +00:00
mx := contextMoment().FilterAuthor(user.ID)
2024-03-02 17:23:11 +00:00
item, err := mx.Get(uint(id))
2024-03-02 17:23:11 +00:00
if err != nil {
2024-02-03 17:08:31 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
2024-03-02 17:23:11 +00:00
item.Alias = data.Alias
item.Content = data.Content
item.PublishedAt = data.PublishedAt
item.Hashtags = data.Hashtags
item.Categories = data.Categories
item.Attachments = data.Attachments
item, err = mx.Edit(item)
2024-02-02 15:42:42 +00:00
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
2024-03-02 17:23:11 +00:00
return c.JSON(item)
2024-02-02 15:42:42 +00:00
}
2024-02-02 16:50:23 +00:00
2024-03-02 17:23:11 +00:00
func reactMoment(c *fiber.Ctx) error {
2024-02-02 16:50:23 +00:00
user := c.Locals("principal").(models.Account)
2024-03-02 17:23:11 +00:00
id, _ := c.ParamsInt("momentId", 0)
2024-02-02 16:50:23 +00:00
var data struct {
Symbol string `json:"symbol" validate:"required"`
Attitude models.ReactionAttitude `json:"attitude" validate:"required"`
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
2024-03-03 08:10:25 +00:00
mx := contextMoment()
2024-03-02 17:23:11 +00:00
item, err := mx.Get(uint(id))
2024-03-02 17:23:11 +00:00
if err != nil {
2024-02-02 16:50:23 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
reaction := models.Reaction{
Symbol: data.Symbol,
Attitude: data.Attitude,
AccountID: user.ID,
MomentID: &item.ID,
2024-02-02 16:50:23 +00:00
}
if positive, reaction, err := mx.React(reaction); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.Status(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent)).JSON(reaction)
}
2024-02-02 16:50:23 +00:00
}
2024-02-03 17:08:31 +00:00
2024-03-02 17:23:11 +00:00
func deleteMoment(c *fiber.Ctx) error {
2024-02-03 17:08:31 +00:00
user := c.Locals("principal").(models.Account)
2024-03-02 17:23:11 +00:00
id, _ := c.ParamsInt("momentId", 0)
2024-02-03 17:08:31 +00:00
2024-03-03 08:10:25 +00:00
mx := contextMoment().FilterAuthor(user.ID)
2024-03-02 17:23:11 +00:00
item, err := mx.Get(uint(id))
2024-03-02 17:23:11 +00:00
if err != nil {
2024-02-03 17:08:31 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
2024-03-02 17:23:11 +00:00
if err := mx.Delete(item); err != nil {
2024-02-03 17:08:31 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}