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"
|
2024-02-02 15:42:42 +00:00
|
|
|
)
|
|
|
|
|
2024-03-05 15:40:54 +00:00
|
|
|
func contextMoment() *services.PostTypeContext {
|
|
|
|
return &services.PostTypeContext{
|
|
|
|
Tx: database.C,
|
|
|
|
TableName: "moments",
|
|
|
|
ColumnName: "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 createMoment(c *fiber.Ctx) error {
|
2024-02-02 15:42:42 +00:00
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
2024-03-07 14:41:00 +00:00
|
|
|
Alias string `json:"alias" form:"alias"`
|
|
|
|
Content string `json:"content" form:"content" validate:"required"`
|
|
|
|
Hashtags []models.Tag `json:"hashtags" form:"hashtags"`
|
|
|
|
Categories []models.Category `json:"categories" form:"categories"`
|
|
|
|
Attachments []models.Attachment `json:"attachments" form:"attachments"`
|
|
|
|
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
|
|
|
RealmID *uint `json:"realm_id" form:"realm_id"`
|
|
|
|
RepostTo uint `json:"repost_to" form:"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 14:57:17 +00:00
|
|
|
item := &models.Moment{
|
2024-03-02 17:23:11 +00:00
|
|
|
PostBase: models.PostBase{
|
|
|
|
Alias: data.Alias,
|
|
|
|
PublishedAt: data.PublishedAt,
|
|
|
|
AuthorID: user.ID,
|
|
|
|
},
|
2024-03-10 10:38:42 +00:00
|
|
|
Hashtags: data.Hashtags,
|
|
|
|
Categories: data.Categories,
|
|
|
|
Attachments: data.Attachments,
|
|
|
|
Content: data.Content,
|
|
|
|
RealmID: data.RealmID,
|
2024-03-02 17:23:11 +00:00
|
|
|
}
|
|
|
|
|
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-05 15:40:54 +00:00
|
|
|
item, err := services.NewPost(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 {
|
2024-03-07 14:41:00 +00:00
|
|
|
Alias string `json:"alias" form:"alias" validate:"required"`
|
|
|
|
Content string `json:"content" form:"content" validate:"required"`
|
|
|
|
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
|
|
|
Hashtags []models.Tag `json:"hashtags" form:"hashtags"`
|
|
|
|
Categories []models.Category `json:"categories" form:"categories"`
|
|
|
|
Attachments []models.Attachment `json:"attachments" form:"attachments"`
|
2024-02-03 17:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-05 15:40:54 +00:00
|
|
|
var item *models.Moment
|
|
|
|
if err := database.C.Where(models.Comment{
|
|
|
|
PostBase: models.PostBase{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
AuthorID: user.ID,
|
|
|
|
},
|
|
|
|
}).First(&item).Error; 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
|
|
|
|
|
2024-03-05 15:40:54 +00:00
|
|
|
if item, err := services.EditPost(item); err != nil {
|
2024-03-03 04:17:18 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-03-05 15:40:54 +00:00
|
|
|
return c.JSON(item)
|
2024-03-03 04:17:18 +00:00
|
|
|
}
|
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-05 15:40:54 +00:00
|
|
|
var item *models.Moment
|
|
|
|
if err := database.C.Where(models.Comment{
|
|
|
|
PostBase: models.PostBase{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
AuthorID: user.ID,
|
|
|
|
},
|
|
|
|
}).First(&item).Error; err != nil {
|
2024-02-03 17:08:31 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-03-05 15:40:54 +00:00
|
|
|
if err := services.DeletePost(item); err != nil {
|
2024-02-03 17:08:31 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|