✨ Stickers and sticker packs
This commit is contained in:
@ -80,11 +80,10 @@ func getAttachmentMeta(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func createAttachment(c *fiber.Ctx) error {
|
||||
var user models.Account
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user = c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
usage := c.FormValue("usage")
|
||||
if !lo.Contains(viper.GetStringSlice("accepts_usage"), usage) {
|
||||
|
@ -13,5 +13,15 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
api.Post("/attachments", createAttachment)
|
||||
api.Put("/attachments/:id", updateAttachmentMeta)
|
||||
api.Delete("/attachments/:id", deleteAttachment)
|
||||
|
||||
api.Get("/stickers/packs", listStickerPacks)
|
||||
api.Post("/stickers/packs", createStickerPack)
|
||||
api.Put("/stickers/packs/:packId", updateStickerPack)
|
||||
api.Delete("/stickers/packs/:packId", deleteStickerPack)
|
||||
|
||||
api.Get("/stickers/:stickerId", getSticker)
|
||||
api.Post("/stickers", createSticker)
|
||||
api.Put("/stickers/:stickerId", updateSticker)
|
||||
api.Delete("/stickers/:stickerId", deleteSticker)
|
||||
}
|
||||
}
|
||||
|
100
pkg/internal/server/api/sticker_packs_api.go
Normal file
100
pkg/internal/server/api/sticker_packs_api.go
Normal file
@ -0,0 +1,100 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func listStickerPacks(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
if take > 100 {
|
||||
take = 100
|
||||
}
|
||||
|
||||
stickers, err := services.ListStickerPackWithStickers(take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return c.JSON(stickers)
|
||||
}
|
||||
|
||||
func createStickerPack(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Prefix string `json:"prefix" validate:"required,alphanum,min=2,max=12"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pack, err := services.NewStickerPack(user, data.Prefix, data.Name, data.Description)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(pack)
|
||||
}
|
||||
|
||||
func updateStickerPack(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Prefix string `json:"prefix" validate:"required,alphanum,min=2,max=12"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id, _ := c.ParamsInt("packId", 0)
|
||||
pack, err := services.GetStickerPackWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
pack.Prefix = data.Prefix
|
||||
pack.Name = data.Name
|
||||
pack.Description = data.Description
|
||||
|
||||
if pack, err = services.UpdateStickerPack(pack); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(pack)
|
||||
}
|
||||
|
||||
func deleteStickerPack(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
id, _ := c.ParamsInt("packId", 0)
|
||||
pack, err := services.GetStickerPackWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if pack, err = services.DeleteStickerPack(pack); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(pack)
|
||||
}
|
152
pkg/internal/server/api/stickers_api.go
Normal file
152
pkg/internal/server/api/stickers_api.go
Normal file
@ -0,0 +1,152 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func getSticker(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("stickerId", 0)
|
||||
sticker, err := services.GetSticker(uint(id))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
return c.JSON(sticker)
|
||||
}
|
||||
|
||||
func createSticker(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required,alphanum,min=2,max=12"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
AttachmentID uint `json:"attachment_id"`
|
||||
PackID uint `json:"pack_id"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var attachment models.Attachment
|
||||
if err := database.C.Where("id = ?", data.AttachmentID).First(&attachment).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find attachment: %v", err))
|
||||
} else if !attachment.IsAnalyzed {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must be analyzed")
|
||||
}
|
||||
|
||||
if strings.SplitN(attachment.MimeType, "/", 2)[0] != "image" {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must be an image")
|
||||
} else if width, ok := attachment.Metadata["width"].(float64); !ok {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must has width metadata")
|
||||
} else if height, ok := attachment.Metadata["height"].(float64); !ok {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must has height metadata")
|
||||
} else if width != 28 || height != 28 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must be a 28x28 image")
|
||||
}
|
||||
|
||||
var pack models.StickerPack
|
||||
if err := database.C.Where("id = ? AND account_id = ?", data.PackID, user.ID).First(&pack).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find pack: %v", err))
|
||||
}
|
||||
|
||||
sticker, err := services.NewSticker(models.Sticker{
|
||||
Alias: data.Alias,
|
||||
Name: data.Name,
|
||||
Attachment: attachment,
|
||||
AccountID: user.ID,
|
||||
PackID: pack.ID,
|
||||
AttachmentID: data.AttachmentID,
|
||||
})
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(sticker)
|
||||
}
|
||||
|
||||
func updateSticker(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required,alphanum,min=2,max=12"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
AttachmentID uint `json:"attachment_id"`
|
||||
PackID uint `json:"pack_id"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id, _ := c.ParamsInt("stickerId", 0)
|
||||
sticker, err := services.GetStickerWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
var attachment models.Attachment
|
||||
if err := database.C.Where("id = ?", data.AttachmentID).First(&attachment).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find attachment: %v", err))
|
||||
} else if !attachment.IsAnalyzed {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must be analyzed")
|
||||
}
|
||||
|
||||
if strings.SplitN(attachment.MimeType, "/", 2)[0] != "image" {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must be an image")
|
||||
} else if width, ok := attachment.Metadata["width"].(float64); !ok {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must has width metadata")
|
||||
} else if height, ok := attachment.Metadata["height"].(float64); !ok {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must has height metadata")
|
||||
} else if width != 28 || height != 28 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "sticker attachment must be a 28x28 image")
|
||||
}
|
||||
|
||||
var pack models.StickerPack
|
||||
if err := database.C.Where("id = ? AND account_id = ?", data.PackID, user.ID).First(&pack).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find pack: %v", err))
|
||||
}
|
||||
|
||||
sticker.Alias = data.Alias
|
||||
sticker.Name = data.Name
|
||||
sticker.PackID = data.PackID
|
||||
sticker.AttachmentID = data.AttachmentID
|
||||
|
||||
if sticker, err = services.UpdateSticker(sticker); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(sticker)
|
||||
}
|
||||
|
||||
func deleteSticker(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
id, _ := c.ParamsInt("stickerId", 0)
|
||||
sticker, err := services.GetStickerWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if sticker, err = services.DeleteSticker(sticker); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(sticker)
|
||||
}
|
Reference in New Issue
Block a user