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-02-06 03:21:23 +00:00
|
|
|
func getPost(c *fiber.Ctx) error {
|
2024-02-10 04:05:34 +00:00
|
|
|
id := c.Params("postId")
|
2024-02-02 15:42:42 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
2024-02-03 11:22:50 +00:00
|
|
|
|
2024-02-06 03:21:23 +00:00
|
|
|
tx := database.C.Where(&models.Post{
|
2024-02-10 04:05:34 +00:00
|
|
|
Alias: id,
|
2024-02-11 05:12:37 +00:00
|
|
|
}).Where("published_at <= ? OR published_at IS NULL", time.Now())
|
2024-02-03 11:22:50 +00:00
|
|
|
|
2024-02-06 03:21:23 +00:00
|
|
|
post, err := services.GetPost(tx)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
2024-02-03 11:22:50 +00:00
|
|
|
}
|
2024-02-02 15:42:42 +00:00
|
|
|
|
2024-02-06 03:21:23 +00:00
|
|
|
tx = database.C.
|
|
|
|
Where(&models.Post{ReplyID: &post.ID}).
|
|
|
|
Where("published_at <= ? OR published_at IS NULL", time.Now()).
|
|
|
|
Order("created_at desc")
|
|
|
|
|
2024-02-02 15:42:42 +00:00
|
|
|
var count int64
|
2024-02-03 11:22:50 +00:00
|
|
|
if err := tx.
|
2024-02-02 15:42:42 +00:00
|
|
|
Model(&models.Post{}).
|
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-02-03 11:22:50 +00:00
|
|
|
posts, err := services.ListPost(tx, take, offset)
|
2024-02-03 07:20:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-02-02 16:50:23 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 15:42:42 +00:00
|
|
|
return c.JSON(fiber.Map{
|
2024-02-06 03:21:23 +00:00
|
|
|
"data": post,
|
|
|
|
"count": count,
|
|
|
|
"related": posts,
|
2024-02-02 15:42:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-06 03:21:23 +00:00
|
|
|
func listPost(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-02-06 03:21:23 +00:00
|
|
|
tx := database.C.
|
2024-02-05 17:10:22 +00:00
|
|
|
Where("published_at <= ? OR published_at IS NULL", time.Now()).
|
|
|
|
Order("created_at desc")
|
|
|
|
|
2024-02-09 03:43:21 +00:00
|
|
|
if realmId > 0 {
|
|
|
|
tx = tx.Where(&models.Post{RealmID: lo.ToPtr(uint(realmId))})
|
|
|
|
} else {
|
|
|
|
tx = tx.Where("realm_id IS NULL")
|
|
|
|
}
|
|
|
|
|
2024-02-06 03:21:23 +00:00
|
|
|
var author models.Account
|
2024-02-07 08:41:35 +00:00
|
|
|
if len(c.Query("authorId")) > 0 {
|
|
|
|
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {
|
2024-02-06 03:21:23 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
tx = tx.Where(&models.Post{AuthorID: author.ID})
|
|
|
|
}
|
|
|
|
|
2024-02-07 08:41:35 +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"))
|
|
|
|
}
|
|
|
|
|
2024-02-14 10:48:02 +00:00
|
|
|
if !c.QueryBool("reply", true) {
|
|
|
|
tx = tx.Where("reply_id IS NULL")
|
|
|
|
}
|
|
|
|
|
2024-02-05 17:10:22 +00:00
|
|
|
var count int64
|
|
|
|
if err := tx.
|
|
|
|
Model(&models.Post{}).
|
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
posts, err := services.ListPost(tx, take, offset)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
2024-02-06 03:21:23 +00:00
|
|
|
"count": count,
|
|
|
|
"data": posts,
|
2024-02-05 17:10:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-02 15:42:42 +00:00
|
|
|
func createPost(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
2024-02-04 10:40:20 +00:00
|
|
|
Alias string `json:"alias"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Content string `json:"content" validate:"required"`
|
|
|
|
Tags []models.Tag `json:"tags"`
|
|
|
|
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"`
|
|
|
|
ReplyTo uint `json:"reply_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-02-03 16:24:04 +00:00
|
|
|
var repostTo *uint = nil
|
|
|
|
var replyTo *uint = nil
|
|
|
|
var relatedCount int64
|
|
|
|
if data.RepostTo > 0 {
|
|
|
|
if err := database.C.Where(&models.Post{
|
|
|
|
BaseModel: models.BaseModel{ID: data.RepostTo},
|
|
|
|
}).Model(&models.Post{}).Count(&relatedCount).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else if relatedCount <= 0 {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, "related post was not found")
|
|
|
|
} else {
|
|
|
|
repostTo = &data.RepostTo
|
|
|
|
}
|
|
|
|
} else if data.ReplyTo > 0 {
|
|
|
|
if err := database.C.Where(&models.Post{
|
|
|
|
BaseModel: models.BaseModel{ID: data.ReplyTo},
|
|
|
|
}).Model(&models.Post{}).Count(&relatedCount).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else if relatedCount <= 0 {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, "related post was not found")
|
|
|
|
} else {
|
|
|
|
replyTo = &data.ReplyTo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-03 17:08:31 +00:00
|
|
|
post, err := services.NewPost(
|
|
|
|
user,
|
2024-02-05 11:25:56 +00:00
|
|
|
realm,
|
2024-02-03 17:08:31 +00:00
|
|
|
data.Alias,
|
|
|
|
data.Title,
|
|
|
|
data.Content,
|
2024-02-04 10:40:20 +00:00
|
|
|
data.Attachments,
|
2024-02-03 17:08:31 +00:00
|
|
|
data.Categories,
|
|
|
|
data.Tags,
|
|
|
|
data.PublishedAt,
|
|
|
|
replyTo,
|
|
|
|
repostTo,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(post)
|
|
|
|
}
|
|
|
|
|
|
|
|
func editPost(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
id, _ := c.ParamsInt("postId", 0)
|
|
|
|
|
|
|
|
var data struct {
|
2024-02-09 17:39:58 +00:00
|
|
|
Alias string `json:"alias" validate:"required"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Content string `json:"content" validate:"required"`
|
|
|
|
PublishedAt *time.Time `json:"published_at"`
|
|
|
|
Tags []models.Tag `json:"tags"`
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var post models.Post
|
|
|
|
if err := database.C.Where(&models.Post{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
AuthorID: user.ID,
|
|
|
|
}).First(&post).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
post, err := services.EditPost(
|
|
|
|
post,
|
|
|
|
data.Alias,
|
|
|
|
data.Title,
|
|
|
|
data.Content,
|
|
|
|
data.PublishedAt,
|
|
|
|
data.Categories,
|
|
|
|
data.Tags,
|
2024-02-09 17:39:58 +00:00
|
|
|
data.Attachments,
|
2024-02-03 17:08:31 +00:00
|
|
|
)
|
2024-02-02 15:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(post)
|
|
|
|
}
|
2024-02-02 16:50:23 +00:00
|
|
|
|
|
|
|
func reactPost(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
id, _ := c.ParamsInt("postId", 0)
|
|
|
|
|
|
|
|
var post models.Post
|
|
|
|
if err := database.C.Where(&models.Post{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
}).First(&post).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(c.Params("reactType")) {
|
|
|
|
case "like":
|
|
|
|
if positive, err := services.LikePost(user, post); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.SendStatus(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent))
|
|
|
|
}
|
|
|
|
case "dislike":
|
|
|
|
if positive, err := services.DislikePost(user, post); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.SendStatus(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "unsupported reaction")
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 17:08:31 +00:00
|
|
|
|
|
|
|
func deletePost(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
id, _ := c.ParamsInt("postId", 0)
|
|
|
|
|
|
|
|
var post models.Post
|
|
|
|
if err := database.C.Where(&models.Post{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
AuthorID: user.ID,
|
|
|
|
}).First(&post).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.DeletePost(post); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|