implement GET /emoji for apiV4 (#6007)
Этот коммит содержится в:
коммит произвёл
Corey Hulen
родитель
08a469a006
Коммит
1bd19f006d
@@ -37,12 +37,12 @@ func getEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if result := <-app.Srv.Store.Emoji().GetAll(); result.Err != nil {
|
listEmoji, err := app.GetEmojiList()
|
||||||
c.Err = result.Err
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
emoji := result.Data.([]*model.Emoji)
|
w.Write([]byte(model.EmojiListToJson(listEmoji)))
|
||||||
w.Write([]byte(model.EmojiListToJson(emoji)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,37 +19,33 @@ func InitEmoji() {
|
|||||||
l4g.Debug(utils.T("api.emoji.init.debug"))
|
l4g.Debug(utils.T("api.emoji.init.debug"))
|
||||||
|
|
||||||
BaseRoutes.Emojis.Handle("", ApiSessionRequired(createEmoji)).Methods("POST")
|
BaseRoutes.Emojis.Handle("", ApiSessionRequired(createEmoji)).Methods("POST")
|
||||||
|
BaseRoutes.Emojis.Handle("", ApiSessionRequired(getEmojiList)).Methods("GET")
|
||||||
}
|
}
|
||||||
|
|
||||||
func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
|
func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
if !*utils.Cfg.ServiceSettings.EnableCustomEmoji {
|
if !*utils.Cfg.ServiceSettings.EnableCustomEmoji {
|
||||||
c.Err = model.NewLocAppError("createEmoji", "api.emoji.disabled.app_error", nil, "")
|
c.Err = model.NewAppError("createEmoji", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||||
c.Err.StatusCode = http.StatusNotImplemented
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if emojiInterface := einterfaces.GetEmojiInterface(); emojiInterface != nil &&
|
if emojiInterface := einterfaces.GetEmojiInterface(); emojiInterface != nil &&
|
||||||
!emojiInterface.CanUserCreateEmoji(c.Session.Roles, c.Session.TeamMembers) {
|
!emojiInterface.CanUserCreateEmoji(c.Session.Roles, c.Session.TeamMembers) {
|
||||||
c.Err = model.NewLocAppError("createEmoji", "api.emoji.create.permissions.app_error", nil, "user_id="+c.Session.UserId)
|
c.Err = model.NewAppError("getEmoji", "api.emoji.disabled.app_error", nil, "user_id="+c.Session.UserId, http.StatusUnauthorized)
|
||||||
c.Err.StatusCode = http.StatusUnauthorized
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(utils.Cfg.FileSettings.DriverName) == 0 {
|
if len(utils.Cfg.FileSettings.DriverName) == 0 {
|
||||||
c.Err = model.NewLocAppError("createEmoji", "api.emoji.storage.app_error", nil, "")
|
c.Err = model.NewAppError("createEmoji", "api.emoji.storage.app_error", nil, "", http.StatusNotImplemented)
|
||||||
c.Err.StatusCode = http.StatusNotImplemented
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.ContentLength > app.MaxEmojiFileSize {
|
if r.ContentLength > app.MaxEmojiFileSize {
|
||||||
c.Err = model.NewLocAppError("createEmoji", "api.emoji.create.too_large.app_error", nil, "")
|
c.Err = model.NewAppError("createEmoji", "api.emoji.create.too_large.app_error", nil, "", http.StatusRequestEntityTooLarge)
|
||||||
c.Err.StatusCode = http.StatusRequestEntityTooLarge
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.ParseMultipartForm(app.MaxEmojiFileSize); err != nil {
|
if err := r.ParseMultipartForm(app.MaxEmojiFileSize); err != nil {
|
||||||
c.Err = model.NewLocAppError("createEmoji", "api.emoji.create.parse.app_error", nil, err.Error())
|
c.Err = model.NewAppError("createEmoji", "api.emoji.create.parse.app_error", nil, err.Error(), http.StatusBadRequest)
|
||||||
c.Err.StatusCode = http.StatusBadRequest
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,3 +66,18 @@ func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte(newEmoji.ToJson()))
|
w.Write([]byte(newEmoji.ToJson()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getEmojiList(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !*utils.Cfg.ServiceSettings.EnableCustomEmoji {
|
||||||
|
c.Err = model.NewAppError("getEmoji", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
listEmoji, err := app.GetEmojiList()
|
||||||
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
w.Write([]byte(model.EmojiListToJson(listEmoji)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -137,3 +137,54 @@ func TestCreateEmoji(t *testing.T) {
|
|||||||
_, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
_, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
||||||
CheckForbiddenStatus(t, resp)
|
CheckForbiddenStatus(t, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetEmojiList(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
defer TearDown()
|
||||||
|
Client := th.Client
|
||||||
|
|
||||||
|
EnableCustomEmoji := *utils.Cfg.ServiceSettings.EnableCustomEmoji
|
||||||
|
defer func() {
|
||||||
|
*utils.Cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji
|
||||||
|
}()
|
||||||
|
*utils.Cfg.ServiceSettings.EnableCustomEmoji = true
|
||||||
|
|
||||||
|
emojis := []*model.Emoji{
|
||||||
|
{
|
||||||
|
CreatorId: th.BasicUser.Id,
|
||||||
|
Name: model.NewId(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
CreatorId: th.BasicUser.Id,
|
||||||
|
Name: model.NewId(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
CreatorId: th.BasicUser.Id,
|
||||||
|
Name: model.NewId(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx, emoji := range emojis {
|
||||||
|
emoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
emojis[idx] = emoji
|
||||||
|
}
|
||||||
|
|
||||||
|
listEmoji, resp := Client.GetEmojiList()
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
for _, emoji := range emojis {
|
||||||
|
found := false
|
||||||
|
for _, savedEmoji := range listEmoji {
|
||||||
|
if emoji.Id == savedEmoji.Id {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Fatalf("failed to get emoji with id %v", emoji.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ADD delete test when create the delete endpoint
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -59,6 +59,14 @@ func CreateEmoji(sessionUserId string, emoji *model.Emoji, multiPartImageData *m
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetEmojiList() ([]*model.Emoji, *model.AppError) {
|
||||||
|
if result := <-Srv.Store.Emoji().GetAll(); result.Err != nil {
|
||||||
|
return nil, result.Err
|
||||||
|
} else {
|
||||||
|
return result.Data.([]*model.Emoji), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError {
|
func UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError {
|
||||||
file, err := imageData.Open()
|
file, err := imageData.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2267,3 +2267,13 @@ func (c *Client4) CreateEmoji(emoji *Emoji, image []byte, filename string) (*Emo
|
|||||||
|
|
||||||
return c.DoEmojiUploadFile(c.GetEmojisRoute(), body.Bytes(), writer.FormDataContentType())
|
return c.DoEmojiUploadFile(c.GetEmojisRoute(), body.Bytes(), writer.FormDataContentType())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetEmojiList returns a list of custom emoji in the system.
|
||||||
|
func (c *Client4) GetEmojiList() ([]*Emoji, *Response) {
|
||||||
|
if r, err := c.DoApiGet(c.GetEmojisRoute(), ""); err != nil {
|
||||||
|
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
||||||
|
} else {
|
||||||
|
defer closeBody(r)
|
||||||
|
return EmojiListFromJson(r.Body), BuildResponse(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user