MM-54640 Add API to get multiple emojis by name at once (#24651)
* MM-54640 Add API to get multiple emojis by name at once * Fix status code when too many names are requested * Address feedback * Update unit tests * Fix styling * Fix more styling * Fix mismatched i18n id
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
77cc356d46
Коммит
3d0fd16666
@@ -17,11 +17,13 @@ import (
|
||||
|
||||
const (
|
||||
EmojiMaxAutocompleteItems = 100
|
||||
GetEmojisByNamesMax = 200
|
||||
)
|
||||
|
||||
func (api *API) InitEmoji() {
|
||||
api.BaseRoutes.Emojis.Handle("", api.APISessionRequired(createEmoji)).Methods("POST")
|
||||
api.BaseRoutes.Emojis.Handle("", api.APISessionRequired(getEmojiList)).Methods("GET")
|
||||
api.BaseRoutes.Emojis.Handle("/names", api.APISessionRequired(getEmojisByNames)).Methods("POST")
|
||||
api.BaseRoutes.Emojis.Handle("/search", api.APISessionRequired(searchEmojis)).Methods("POST")
|
||||
api.BaseRoutes.Emojis.Handle("/autocomplete", api.APISessionRequired(autocompleteEmojis)).Methods("GET")
|
||||
api.BaseRoutes.Emoji.Handle("", api.APISessionRequired(deleteEmoji)).Methods("DELETE")
|
||||
@@ -221,6 +223,11 @@ func getEmojiByName(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if !*c.App.Config().ServiceSettings.EnableCustomEmoji {
|
||||
c.Err = model.NewAppError("getEmojiByName", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
|
||||
emoji, err := c.App.GetEmojiByName(c.AppContext, c.Params.EmojiName)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
@@ -232,6 +239,36 @@ func getEmojiByName(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func getEmojisByNames(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
names := model.ArrayFromJSON(r.Body)
|
||||
if len(names) == 0 {
|
||||
c.SetInvalidParam("names")
|
||||
return
|
||||
}
|
||||
|
||||
if !*c.App.Config().ServiceSettings.EnableCustomEmoji {
|
||||
c.Err = model.NewAppError("getEmojisByNames", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
|
||||
if len(names) > GetEmojisByNamesMax {
|
||||
c.Err = model.NewAppError("getEmojisByNames", "api.emoji.get_multiple_by_name_too_many.request_error", map[string]any{
|
||||
"MaxNames": GetEmojisByNamesMax,
|
||||
}, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
emojis, err := c.App.GetMultipleEmojiByName(c.AppContext, names)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(emojis); err != nil {
|
||||
c.Logger.Warn("Error while writing response", mlog.Err(err))
|
||||
}
|
||||
}
|
||||
|
||||
func getEmojiImage(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireEmojiId()
|
||||
if c.Err != nil {
|
||||
|
||||
@@ -280,6 +280,69 @@ func TestGetEmojiList(t *testing.T) {
|
||||
require.Greater(t, len(listEmoji), 0, "should return more than 0")
|
||||
}
|
||||
|
||||
func TestGetEmojisByNames(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
// Set up some custom emojis
|
||||
adminClient := th.SystemAdminClient
|
||||
|
||||
imageBytes := utils.CreateTestJpeg(t, 10, 10)
|
||||
|
||||
emoji1 := &model.Emoji{
|
||||
CreatorId: th.SystemAdminUser.Id,
|
||||
Name: model.NewId(),
|
||||
}
|
||||
emoji1, _, err := adminClient.CreateEmoji(context.Background(), emoji1, imageBytes, "emoji.jpg")
|
||||
require.NoError(t, err)
|
||||
|
||||
emoji2 := &model.Emoji{
|
||||
CreatorId: th.SystemAdminUser.Id,
|
||||
Name: model.NewId(),
|
||||
}
|
||||
emoji2, _, err = adminClient.CreateEmoji(context.Background(), emoji2, imageBytes, "emoji.jpg")
|
||||
require.NoError(t, err)
|
||||
|
||||
client := th.Client
|
||||
|
||||
t.Run("should return a single emoji", func(t *testing.T) {
|
||||
emojis, _, err := client.GetEmojisByNames(context.Background(), []string{emoji1.Name})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, emojis, 1)
|
||||
assert.Equal(t, emoji1.Id, emojis[0].Id)
|
||||
})
|
||||
|
||||
t.Run("should return multiple emojis", func(t *testing.T) {
|
||||
emojis, _, err := client.GetEmojisByNames(context.Background(), []string{emoji1.Name, emoji2.Name})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, emojis, 2)
|
||||
assert.Equal(t, emoji1.Id, emojis[0].Id)
|
||||
assert.Equal(t, emoji2.Id, emojis[1].Id)
|
||||
})
|
||||
|
||||
t.Run("should ignore non-existent emojis", func(t *testing.T) {
|
||||
emojis, _, err := client.GetEmojisByNames(context.Background(), []string{emoji1.Name, emoji2.Name, model.NewId()})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, emojis, 2)
|
||||
assert.Equal(t, emoji1.Id, emojis[0].Id)
|
||||
assert.Equal(t, emoji2.Id, emojis[1].Id)
|
||||
})
|
||||
|
||||
t.Run("should return an error when too many emojis are requested", func(t *testing.T) {
|
||||
names := make([]string, GetEmojisByNamesMax+1)
|
||||
for i := 0; i < len(names); i++ {
|
||||
names[i] = emoji1.Name
|
||||
}
|
||||
|
||||
_, _, err := client.GetEmojisByNames(context.Background(), names)
|
||||
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteEmoji(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
Ссылка в новой задаче
Block a user