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()
|
||||
|
||||
@@ -61,6 +61,7 @@ func NewMainHelperWithOptions(options *HelperOptions) *MainHelper {
|
||||
// Unset environment variables commonly set for development that interfere with tests.
|
||||
os.Unsetenv("MM_SERVICESETTINGS_SITEURL")
|
||||
os.Unsetenv("MM_SERVICESETTINGS_LISTENADDRESS")
|
||||
os.Unsetenv("MM_SERVICESETTINGS_CONNECTIONSECURITY")
|
||||
os.Unsetenv("MM_SERVICESETTINGS_ENABLEDEVELOPER")
|
||||
|
||||
var mainHelper MainHelper
|
||||
|
||||
@@ -1796,6 +1796,10 @@
|
||||
"id": "api.emoji.get_image.read.app_error",
|
||||
"translation": "Unable to read image file for emoji."
|
||||
},
|
||||
{
|
||||
"id": "api.emoji.get_multiple_by_name_too_many.request_error",
|
||||
"translation": "Unable to get that many emojis by name. Only {{.MaxNames}} emojis can be requested at once."
|
||||
},
|
||||
{
|
||||
"id": "api.emoji.storage.app_error",
|
||||
"translation": "File storage not configured properly. Please configure for either S3 or local server file storage."
|
||||
|
||||
@@ -6618,6 +6618,26 @@ func (c *Client4) GetSortedEmojiList(ctx context.Context, page, perPage int, sor
|
||||
return list, BuildResponse(r), nil
|
||||
}
|
||||
|
||||
// GetEmojisByNames takes an array of custom emoji names and returns an array of those emojis.
|
||||
func (c *Client4) GetEmojisByNames(ctx context.Context, names []string) ([]*Emoji, *Response, error) {
|
||||
buf, err := json.Marshal(names)
|
||||
if err != nil {
|
||||
return nil, nil, NewAppError("GetEmojisByNames", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
|
||||
r, err := c.DoAPIPostBytes(ctx, c.emojisRoute()+"/names", buf)
|
||||
if err != nil {
|
||||
return nil, BuildResponse(r), err
|
||||
}
|
||||
defer closeBody(r)
|
||||
|
||||
var list []*Emoji
|
||||
if err := json.NewDecoder(r.Body).Decode(&list); err != nil {
|
||||
return nil, nil, NewAppError("GetEmojisByNames", "api.unmarshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
return list, BuildResponse(r), nil
|
||||
}
|
||||
|
||||
// DeleteEmoji delete an custom emoji on the provided emoji id string.
|
||||
func (c *Client4) DeleteEmoji(ctx context.Context, emojiId string) (*Response, error) {
|
||||
r, err := c.DoAPIDelete(ctx, c.emojiRoute(emojiId))
|
||||
|
||||
Ссылка в новой задаче
Block a user