2024-03-05 15:40:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-03-06 15:31:22 +00:00
|
|
|
"fmt"
|
2024-05-17 12:52:13 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-05-15 11:45:49 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"strings"
|
2024-03-05 15:40:54 +00:00
|
|
|
"time"
|
|
|
|
|
2024-03-20 12:57:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/database"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/models"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/services"
|
2024-03-05 15:40:54 +00:00
|
|
|
"github.com/samber/lo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getPost(c *fiber.Ctx) error {
|
2024-05-19 12:05:44 +00:00
|
|
|
alias := c.Params("post")
|
2024-03-05 15:40:54 +00:00
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
item, err := services.GetPostWithAlias(alias)
|
2024-03-05 15:40:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
item.ReplyCount = services.CountPostReply(item.ID)
|
|
|
|
item.ReactionCount = services.CountPostReactions(item.ID)
|
|
|
|
item.ReactionList, err = services.ListPostReactions(item.ID)
|
2024-03-05 15:40:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listPost(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
realmId := c.QueryInt("realmId", 0)
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
tx := database.C
|
|
|
|
if realmId > 0 {
|
|
|
|
tx = services.FilterWithRealm(tx, uint(realmId))
|
|
|
|
}
|
2024-03-05 15:40:54 +00:00
|
|
|
|
|
|
|
if len(c.Query("authorId")) > 0 {
|
2024-05-15 11:45:49 +00:00
|
|
|
var author models.Account
|
2024-03-05 15:40:54 +00:00
|
|
|
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
2024-05-15 11:45:49 +00:00
|
|
|
tx = tx.Where("author_id = ?", author.ID)
|
2024-03-05 15:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Query("category")) > 0 {
|
2024-05-15 11:45:49 +00:00
|
|
|
tx = services.FilterPostWithCategory(tx, c.Query("category"))
|
2024-03-05 15:40:54 +00:00
|
|
|
}
|
|
|
|
if len(c.Query("tag")) > 0 {
|
2024-05-15 11:45:49 +00:00
|
|
|
tx = services.FilterPostWithTag(tx, c.Query("tag"))
|
2024-03-05 15:40:54 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
count, err := services.CountPost(tx)
|
2024-03-05 15:40:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
items, err := services.ListPost(tx, take, offset)
|
2024-03-05 15:40:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": items,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
func createPost(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
2024-05-17 12:52:13 +00:00
|
|
|
Alias string `json:"alias" form:"alias"`
|
|
|
|
Content string `json:"content" form:"content" validate:"required,max=4096"`
|
|
|
|
Tags []models.Tag `json:"tags" form:"tags"`
|
|
|
|
Categories []models.Category `json:"categories" form:"categories"`
|
|
|
|
Attachments []string `json:"attachments" form:"attachments"`
|
|
|
|
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
|
|
|
RealmAlias string `json:"realm" form:"realm"`
|
|
|
|
ReplyTo *uint `json:"reply_to" form:"reply_to"`
|
|
|
|
RepostTo *uint `json:"repost_to" form:"repost_to"`
|
2024-05-15 11:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(data.Alias) == 0 {
|
|
|
|
data.Alias = strings.ReplaceAll(uuid.NewString(), "-", "")
|
|
|
|
}
|
|
|
|
|
2024-05-17 12:52:13 +00:00
|
|
|
for _, attachment := range data.Attachments {
|
2024-05-18 12:28:40 +00:00
|
|
|
if !services.CheckAttachmentByUUIDExists(attachment, "i.attachment") {
|
2024-05-18 08:59:58 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %s not found", attachment))
|
2024-05-17 12:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
item := models.Post{
|
|
|
|
Alias: data.Alias,
|
|
|
|
PublishedAt: data.PublishedAt,
|
|
|
|
AuthorID: user.ID,
|
|
|
|
Tags: data.Tags,
|
|
|
|
Categories: data.Categories,
|
|
|
|
Attachments: data.Attachments,
|
|
|
|
Content: data.Content,
|
|
|
|
}
|
|
|
|
|
2024-05-15 12:31:25 +00:00
|
|
|
if data.ReplyTo != nil {
|
|
|
|
var replyTo models.Post
|
|
|
|
if err := database.C.Where("id = ?", data.ReplyTo).First(&replyTo).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("related post was not found: %v", err))
|
|
|
|
} else {
|
|
|
|
item.ReplyID = &replyTo.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if data.RepostTo != nil {
|
|
|
|
var repostTo models.Post
|
|
|
|
if err := database.C.Where("id = ?", data.RepostTo).First(&repostTo).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("related post was not found: %v", err))
|
2024-05-15 11:45:49 +00:00
|
|
|
} else {
|
2024-05-15 12:31:25 +00:00
|
|
|
item.RepostID = &repostTo.ID
|
2024-05-15 11:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data.RealmAlias) > 0 {
|
|
|
|
if realm, err := services.GetRealmWithAlias(data.RealmAlias); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else if _, err := services.GetRealmMember(realm.ExternalID, user.ExternalID); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you aren't a part of related realm: %v", err))
|
|
|
|
} else {
|
|
|
|
item.RealmID = &realm.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item, err := services.NewPost(user, item)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func editPost(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
id, _ := c.ParamsInt("postId", 0)
|
|
|
|
|
|
|
|
var data struct {
|
2024-05-17 12:52:13 +00:00
|
|
|
Alias string `json:"alias" form:"alias" validate:"required"`
|
|
|
|
Content string `json:"content" form:"content" validate:"required,max=1024"`
|
|
|
|
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
|
|
|
Tags []models.Tag `json:"tags" form:"tags"`
|
|
|
|
Categories []models.Category `json:"categories" form:"categories"`
|
|
|
|
Attachments []string `json:"attachments" form:"attachments"`
|
2024-05-15 11:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var item models.Post
|
|
|
|
if err := database.C.Where(models.Post{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
AuthorID: user.ID,
|
|
|
|
}).First(&item).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-05-17 12:52:13 +00:00
|
|
|
for _, attachment := range data.Attachments {
|
2024-05-18 12:28:40 +00:00
|
|
|
if !services.CheckAttachmentByUUIDExists(attachment, "i.attachment") {
|
2024-05-18 08:59:58 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %s not found", attachment))
|
2024-05-17 12:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
item.Alias = data.Alias
|
|
|
|
item.Content = data.Content
|
|
|
|
item.PublishedAt = data.PublishedAt
|
|
|
|
item.Tags = data.Tags
|
|
|
|
item.Categories = data.Categories
|
|
|
|
item.Attachments = data.Attachments
|
|
|
|
|
|
|
|
if item, err := services.EditPost(item); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deletePost(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
id, _ := c.ParamsInt("postId", 0)
|
|
|
|
|
|
|
|
var item models.Post
|
|
|
|
if err := database.C.Where(models.Post{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
AuthorID: user.ID,
|
|
|
|
}).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())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-03-05 15:40:54 +00:00
|
|
|
func reactPost(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
2024-03-07 14:41:00 +00:00
|
|
|
Symbol string `json:"symbol" form:"symbol" validate:"required"`
|
2024-04-15 15:22:54 +00:00
|
|
|
Attitude models.ReactionAttitude `json:"attitude" form:"attitude"`
|
2024-03-05 15:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reaction := models.Reaction{
|
|
|
|
Symbol: data.Symbol,
|
|
|
|
Attitude: data.Attitude,
|
|
|
|
AccountID: user.ID,
|
2024-03-06 15:31:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-19 12:05:44 +00:00
|
|
|
alias := c.Params("post")
|
2024-03-06 15:31:22 +00:00
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
var res models.Post
|
2024-05-19 12:05:44 +00:00
|
|
|
if err := database.C.Where("alias = ?", alias).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-05-15 11:45:49 +00:00
|
|
|
reaction.PostID = &res.ID
|
2024-03-05 15:40:54 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
if positive, reaction, err := services.ReactPost(reaction); err != nil {
|
2024-03-05 15:40:54 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.Status(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent)).JSON(reaction)
|
|
|
|
}
|
|
|
|
}
|