From 525a103a768724156bfca344266ac36d4337d8cb Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 15 Sep 2024 18:39:09 +0800 Subject: [PATCH] :sparkles: Be able to lookup sticker via thier pack prefix + alias --- pkg/internal/server/api/index.go | 1 + pkg/internal/server/api/stickers_api.go | 9 +++++++++ pkg/internal/services/stickers.go | 14 ++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index 5f6a307..5792acd 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -23,6 +23,7 @@ func MapAPIs(app *fiber.App, baseURL string) { api.Post("/attachments/multipart", createAttachmentMultipartPlaceholder) api.Post("/attachments/multipart/:file/:chunk", uploadAttachmentMultipart) + api.Get("/stickers/lookup/:alias", lookupSticker) api.Get("/stickers/manifest", listStickerManifest) api.Get("/stickers/packs", listStickerPacks) api.Post("/stickers/packs", createStickerPack) diff --git a/pkg/internal/server/api/stickers_api.go b/pkg/internal/server/api/stickers_api.go index a5baeb4..a911209 100644 --- a/pkg/internal/server/api/stickers_api.go +++ b/pkg/internal/server/api/stickers_api.go @@ -12,6 +12,15 @@ import ( "github.com/gofiber/fiber/v2" ) +func lookupSticker(c *fiber.Ctx) error { + alias := c.Params("alias") + if sticker, err := services.GetStickerWithAlias(alias); err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } else { + return c.JSON(sticker) + } +} + func listStickers(c *fiber.Ctx) error { take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) diff --git a/pkg/internal/services/stickers.go b/pkg/internal/services/stickers.go index cfc2d64..a88dddf 100644 --- a/pkg/internal/services/stickers.go +++ b/pkg/internal/services/stickers.go @@ -1,10 +1,24 @@ package services import ( + "fmt" + "git.solsynth.dev/hydrogen/paperclip/pkg/internal/database" "git.solsynth.dev/hydrogen/paperclip/pkg/internal/models" + "github.com/spf13/viper" ) +func GetStickerWithAlias(alias string) (models.Sticker, error) { + var sticker models.Sticker + prefix := viper.GetString("database.prefix") + if err := database.C. + Joins(fmt.Sprintf("LEFT JOIN %ssticker_packs pk ON pack_id = pk.id", prefix)). + Where("CONCAT(pk.prefix, alias) = ?", alias).First(&sticker).Error; err != nil { + return sticker, err + } + return sticker, nil +} + func GetSticker(id uint) (models.Sticker, error) { var sticker models.Sticker if err := database.C.Where("id = ?", id).Preload("Attachment").First(&sticker).Error; err != nil {