2024-07-21 16:49:36 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-25 14:39:19 +00:00
|
|
|
"time"
|
|
|
|
|
2024-07-21 16:49:36 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server/exts"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-07-25 14:39:19 +00:00
|
|
|
"github.com/samber/lo"
|
2024-07-21 16:49:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func createArticle(c *fiber.Ctx) error {
|
|
|
|
if err := gap.H.EnsureGrantedPerm(c, "CreatePosts", true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
2024-07-21 17:44:04 +00:00
|
|
|
Title string `json:"title" validate:"required,max=1024"`
|
2024-07-23 10:05:54 +00:00
|
|
|
Description *string `json:"description"`
|
2024-07-21 17:44:04 +00:00
|
|
|
Content string `json:"content" validate:"required"`
|
|
|
|
Attachments []uint `json:"attachments"`
|
|
|
|
Tags []models.Tag `json:"tags"`
|
|
|
|
Categories []models.Category `json:"categories"`
|
|
|
|
PublishedAt *time.Time `json:"published_at"`
|
|
|
|
PublishedUntil *time.Time `json:"published_until"`
|
|
|
|
IsDraft bool `json:"is_draft"`
|
|
|
|
RealmAlias *string `json:"realm"`
|
2024-07-21 16:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
body := models.PostArticleBody{
|
|
|
|
Title: data.Title,
|
|
|
|
Description: data.Description,
|
|
|
|
Content: data.Content,
|
|
|
|
Attachments: data.Attachments,
|
|
|
|
}
|
|
|
|
|
|
|
|
var bodyMapping map[string]any
|
|
|
|
rawBody, _ := jsoniter.Marshal(body)
|
|
|
|
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
|
|
|
|
|
|
|
item := models.Post{
|
2024-07-21 17:44:04 +00:00
|
|
|
Type: models.PostTypeArticle,
|
|
|
|
Body: bodyMapping,
|
2024-07-21 17:46:38 +00:00
|
|
|
Language: services.DetectLanguage(data.Content),
|
2024-07-21 17:44:04 +00:00
|
|
|
Tags: data.Tags,
|
|
|
|
Categories: data.Categories,
|
|
|
|
IsDraft: data.IsDraft,
|
|
|
|
PublishedAt: data.PublishedAt,
|
|
|
|
PublishedUntil: data.PublishedUntil,
|
|
|
|
AuthorID: user.ID,
|
2024-07-21 16:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.RealmAlias != nil {
|
|
|
|
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("unable to post in the realm, access denied: %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 editArticle(c *fiber.Ctx) error {
|
|
|
|
id, _ := c.ParamsInt("postId", 0)
|
|
|
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
2024-07-21 17:44:04 +00:00
|
|
|
Title string `json:"title" validate:"required,max=1024"`
|
2024-07-23 10:05:54 +00:00
|
|
|
Description *string `json:"description"`
|
2024-07-21 17:44:04 +00:00
|
|
|
Content string `json:"content" validate:"required"`
|
|
|
|
Attachments []uint `json:"attachments"`
|
|
|
|
Tags []models.Tag `json:"tags"`
|
|
|
|
Categories []models.Category `json:"categories"`
|
|
|
|
IsDraft bool `json:"is_draft"`
|
|
|
|
PublishedAt *time.Time `json:"published_at"`
|
|
|
|
PublishedUntil *time.Time `json:"published_until"`
|
2024-07-21 16:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := exts.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-07-25 14:41:18 +00:00
|
|
|
if item.IsDraft && !data.IsDraft && data.PublishedAt == nil {
|
2024-07-25 14:39:19 +00:00
|
|
|
item.PublishedAt = lo.ToPtr(time.Now())
|
2024-07-25 14:41:18 +00:00
|
|
|
} else {
|
|
|
|
item.PublishedAt = data.PublishedAt
|
2024-07-25 14:39:19 +00:00
|
|
|
}
|
|
|
|
|
2024-07-21 16:49:36 +00:00
|
|
|
body := models.PostArticleBody{
|
|
|
|
Title: data.Title,
|
|
|
|
Content: data.Content,
|
|
|
|
Attachments: data.Attachments,
|
|
|
|
}
|
|
|
|
|
|
|
|
var bodyMapping map[string]any
|
|
|
|
rawBody, _ := jsoniter.Marshal(body)
|
|
|
|
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
|
|
|
|
|
|
|
item.Body = bodyMapping
|
2024-07-21 17:46:38 +00:00
|
|
|
item.Language = services.DetectLanguage(data.Content)
|
2024-07-21 16:49:36 +00:00
|
|
|
item.Tags = data.Tags
|
|
|
|
item.Categories = data.Categories
|
2024-07-21 17:44:04 +00:00
|
|
|
item.IsDraft = data.IsDraft
|
|
|
|
item.PublishedUntil = data.PublishedUntil
|
2024-07-21 16:49:36 +00:00
|
|
|
|
|
|
|
if item, err := services.EditPost(item); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(item)
|
|
|
|
}
|
|
|
|
}
|