Interactive/pkg/internal/http/api/stories_api.go

224 lines
6.5 KiB
Go
Raw Permalink Normal View History

package api
import (
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/authkit"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"strconv"
"time"
2024-11-02 05:41:51 +00:00
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
"git.solsynth.dev/hypernet/interactive/pkg/internal/http/exts"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
"github.com/gofiber/fiber/v2"
jsoniter "github.com/json-iterator/go"
"github.com/samber/lo"
)
func createStory(c *fiber.Ctx) error {
if err := sec.EnsureGrantedPerm(c, "CreatePosts", true); err != nil {
return err
}
user := c.Locals("user").(authm.Account)
var data struct {
Publisher uint `json:"publisher"`
2024-08-17 07:19:59 +00:00
Alias *string `json:"alias"`
2024-07-23 10:05:54 +00:00
Title *string `json:"title"`
2024-07-21 17:44:04 +00:00
Content string `json:"content" validate:"required,max=4096"`
2024-07-23 10:05:54 +00:00
Location *string `json:"location"`
2024-08-10 17:22:55 +00:00
Thumbnail *uint `json:"thumbnail"`
Attachments []string `json:"attachments"`
2024-07-21 17:44:04 +00:00
Tags []models.Tag `json:"tags"`
Categories []models.Category `json:"categories"`
PublishedAt *time.Time `json:"published_at"`
PublishedUntil *time.Time `json:"published_until"`
2024-07-27 17:49:16 +00:00
VisibleUsers []uint `json:"visible_users_list"`
InvisibleUsers []uint `json:"invisible_users_list"`
Visibility *int8 `json:"visibility"`
2024-07-21 17:44:04 +00:00
IsDraft bool `json:"is_draft"`
ReplyTo *uint `json:"reply_to"`
RepostTo *uint `json:"repost_to"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
publisher, err := services.GetPublisher(data.Publisher, user.ID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
body := models.PostStoryBody{
2024-08-10 17:22:55 +00:00
Thumbnail: data.Thumbnail,
Title: data.Title,
Content: data.Content,
Location: data.Location,
Attachments: data.Attachments,
}
var bodyMapping map[string]any
rawBody, _ := jsoniter.Marshal(body)
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
item := models.Post{
2024-08-17 07:19:59 +00:00
Alias: data.Alias,
2024-07-21 17:44:04 +00:00
Type: models.PostTypeStory,
Body: bodyMapping,
Language: services.DetectLanguage(data.Content),
2024-07-21 17:44:04 +00:00
Tags: data.Tags,
Categories: data.Categories,
PublishedAt: data.PublishedAt,
PublishedUntil: data.PublishedUntil,
IsDraft: data.IsDraft,
2024-07-27 17:49:16 +00:00
VisibleUsers: data.VisibleUsers,
InvisibleUsers: data.InvisibleUsers,
PublisherID: publisher.ID,
}
if item.PublishedAt == nil {
item.PublishedAt = lo.ToPtr(time.Now())
}
2024-07-27 17:49:16 +00:00
if data.Visibility != nil {
item.Visibility = *data.Visibility
} else {
item.Visibility = models.PostVisibilityAll
}
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))
} else {
item.RepostID = &repostTo.ID
}
}
item, err = services.NewPost(publisher, item)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
_ = authkit.AddEventExt(
gap.Nx,
"posts.new",
strconv.Itoa(int(item.ID)),
c,
)
}
return c.JSON(item)
}
func editStory(c *fiber.Ctx) error {
id, _ := c.ParamsInt("postId", 0)
if err := sec.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(authm.Account)
var data struct {
Publisher uint `json:"publisher"`
2024-08-17 07:19:59 +00:00
Alias *string `json:"alias"`
2024-07-23 10:05:54 +00:00
Title *string `json:"title"`
2024-07-21 17:44:04 +00:00
Content string `json:"content" validate:"required,max=4096"`
2024-08-10 17:22:55 +00:00
Thumbnail *uint `json:"thumbnail"`
2024-07-23 10:05:54 +00:00
Location *string `json:"location"`
Attachments []string `json:"attachments"`
2024-07-21 17:44:04 +00:00
Tags []models.Tag `json:"tags"`
Categories []models.Category `json:"categories"`
PublishedAt *time.Time `json:"published_at"`
PublishedUntil *time.Time `json:"published_until"`
2024-07-27 17:49:16 +00:00
VisibleUsers []uint `json:"visible_users_list"`
InvisibleUsers []uint `json:"invisible_users_list"`
Visibility *int8 `json:"visibility"`
2024-07-21 17:44:04 +00:00
IsDraft bool `json:"is_draft"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
publisher, err := services.GetPublisher(data.Publisher, user.ID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
var item models.Post
if err := database.C.Where(models.Post{
BaseModel: cruda.BaseModel{ID: uint(id)},
PublisherID: publisher.ID,
}).First(&item).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
2024-07-28 04:50:27 +00:00
if item.LockedAt != nil {
return fiber.NewError(fiber.StatusForbidden, "post was locked")
}
if !item.IsDraft && !data.IsDraft {
item.EditedAt = lo.ToPtr(time.Now())
}
if item.IsDraft && !data.IsDraft && data.PublishedAt == nil {
item.PublishedAt = lo.ToPtr(time.Now())
} else {
item.PublishedAt = data.PublishedAt
}
body := models.PostStoryBody{
2024-08-10 17:22:55 +00:00
Thumbnail: data.Thumbnail,
Title: data.Title,
Content: data.Content,
Location: data.Location,
Attachments: data.Attachments,
}
var bodyMapping map[string]any
rawBody, _ := jsoniter.Marshal(body)
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
2024-08-17 07:19:59 +00:00
item.Alias = data.Alias
item.Body = bodyMapping
item.Language = services.DetectLanguage(data.Content)
item.Tags = data.Tags
item.Categories = data.Categories
2024-07-21 17:44:04 +00:00
item.PublishedUntil = data.PublishedUntil
item.IsDraft = data.IsDraft
2024-07-27 17:49:16 +00:00
item.VisibleUsers = data.VisibleUsers
item.InvisibleUsers = data.InvisibleUsers
// Preload publisher data
item.Publisher = publisher
2024-07-27 17:49:16 +00:00
if data.Visibility != nil {
item.Visibility = *data.Visibility
}
if item, err = services.EditPost(item); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
_ = authkit.AddEventExt(
gap.Nx,
"posts.edit",
strconv.Itoa(int(item.ID)),
c,
)
}
return c.JSON(item)
}