Able to search sticker with alias

This commit is contained in:
LittleSheep 2024-09-16 20:44:40 +08:00
parent 778d09d74a
commit bf5c718780
3 changed files with 24 additions and 0 deletions

View File

@ -23,6 +23,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
api.Post("/attachments/multipart", createAttachmentMultipartPlaceholder) api.Post("/attachments/multipart", createAttachmentMultipartPlaceholder)
api.Post("/attachments/multipart/:file/:chunk", uploadAttachmentMultipart) api.Post("/attachments/multipart/:file/:chunk", uploadAttachmentMultipart)
api.Get("/stickers/lookup", lookupStickerBatch)
api.Get("/stickers/lookup/:alias", lookupSticker) api.Get("/stickers/lookup/:alias", lookupSticker)
api.Get("/stickers/manifest", listStickerManifest) api.Get("/stickers/manifest", listStickerManifest)
api.Get("/stickers/packs", listStickerPacks) api.Get("/stickers/packs", listStickerPacks)

View File

@ -12,6 +12,15 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func lookupStickerBatch(c *fiber.Ctx) error {
probe := c.Query("probe")
if stickers, err := services.GetStickerLikeAlias(probe); err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else {
return c.JSON(stickers)
}
}
func lookupSticker(c *fiber.Ctx) error { func lookupSticker(c *fiber.Ctx) error {
alias := c.Params("alias") alias := c.Params("alias")
if sticker, err := services.GetStickerWithAlias(alias); err != nil { if sticker, err := services.GetStickerWithAlias(alias); err != nil {

View File

@ -8,6 +8,20 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func GetStickerLikeAlias(alias string) ([]models.Sticker, error) {
var stickers []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("UPPER(CONCAT(pk.prefix, alias)) LIKE UPPER(?)", "%"+alias+"%").
Preload("Attachment").Preload("Pack").
Limit(10).
Find(&stickers).Error; err != nil {
return stickers, err
}
return stickers, nil
}
func GetStickerWithAlias(alias string) (models.Sticker, error) { func GetStickerWithAlias(alias string) (models.Sticker, error) {
var sticker models.Sticker var sticker models.Sticker
prefix := viper.GetString("database.prefix") prefix := viper.GetString("database.prefix")