implement DELETE /emoji/{emoji_id} fro apiV4 (#6021)
implement GET /emoji/{emoji_id} for apiv4
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
62974f19cd
Коммит
80684ad69f
41
api/emoji.go
41
api/emoji.go
@@ -148,41 +148,24 @@ func deleteEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var emoji *model.Emoji
|
||||
if result := <-app.Srv.Store.Emoji().Get(id, false); result.Err != nil {
|
||||
c.Err = result.Err
|
||||
return
|
||||
} else {
|
||||
emoji = result.Data.(*model.Emoji)
|
||||
|
||||
if c.Session.UserId != emoji.CreatorId && !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
||||
c.Err = model.NewLocAppError("deleteEmoji", "api.emoji.delete.permissions.app_error", nil, "user_id="+c.Session.UserId)
|
||||
c.Err.StatusCode = http.StatusUnauthorized
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := (<-app.Srv.Store.Emoji().Delete(id, model.GetMillis())).Err; err != nil {
|
||||
emoji, err := app.GetEmoji(id)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
go deleteEmojiImage(id)
|
||||
go deleteReactionsForEmoji(emoji.Name)
|
||||
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
func deleteEmojiImage(id string) {
|
||||
if err := app.MoveFile(getEmojiImagePath(id), "emoji/"+id+"/image_deleted"); err != nil {
|
||||
l4g.Error("Failed to rename image when deleting emoji %v", id)
|
||||
if c.Session.UserId != emoji.CreatorId && !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
||||
c.Err = model.NewLocAppError("deleteEmoji", "api.emoji.delete.permissions.app_error", nil, "user_id="+c.Session.UserId)
|
||||
c.Err.StatusCode = http.StatusUnauthorized
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func deleteReactionsForEmoji(emojiName string) {
|
||||
if result := <-app.Srv.Store.Reaction().DeleteAllWithEmojiName(emojiName); result.Err != nil {
|
||||
l4g.Warn(utils.T("api.emoji.delete.delete_reactions.app_error"), emojiName)
|
||||
l4g.Warn(result.Err)
|
||||
err = app.DeleteEmoji(emoji)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
} else {
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -405,6 +405,17 @@ func (c *Context) RequireReportId() *Context {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Context) RequireEmojiId() *Context {
|
||||
if c.Err != nil {
|
||||
return c
|
||||
}
|
||||
|
||||
if len(c.Params.EmojiId) != 26 {
|
||||
c.SetInvalidUrlParam("emoji_id")
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Context) RequireTeamName() *Context {
|
||||
if c.Err != nil {
|
||||
return c
|
||||
|
||||
@@ -20,6 +20,8 @@ func InitEmoji() {
|
||||
|
||||
BaseRoutes.Emojis.Handle("", ApiSessionRequired(createEmoji)).Methods("POST")
|
||||
BaseRoutes.Emojis.Handle("", ApiSessionRequired(getEmojiList)).Methods("GET")
|
||||
BaseRoutes.Emoji.Handle("", ApiSessionRequired(deleteEmoji)).Methods("DELETE")
|
||||
BaseRoutes.Emoji.Handle("", ApiSessionRequired(getEmoji)).Methods("GET")
|
||||
}
|
||||
|
||||
func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -81,3 +83,49 @@ func getEmojiList(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(model.EmojiListToJson(listEmoji)))
|
||||
}
|
||||
}
|
||||
|
||||
func deleteEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireEmojiId()
|
||||
if c.Err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
emoji, err := app.GetEmoji(c.Params.EmojiId)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
if c.Session.UserId != emoji.CreatorId && !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
||||
c.Err = model.NewAppError("deleteImage", "api.emoji.delete.permissions.app_error", nil, "user_id="+c.Session.UserId, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
err = app.DeleteEmoji(emoji)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
} else {
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
}
|
||||
|
||||
func getEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireEmojiId()
|
||||
if c.Err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if !*utils.Cfg.ServiceSettings.EnableCustomEmoji {
|
||||
c.Err = model.NewAppError("getEmoji", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
|
||||
emoji, err := app.GetEmoji(c.Params.EmojiId)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
} else {
|
||||
w.Write([]byte(emoji.ToJson()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,6 +185,116 @@ func TestGetEmojiList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ADD delete test when create the delete endpoint
|
||||
_, resp = Client.DeleteEmoji(emojis[0].Id)
|
||||
CheckNoError(t, resp)
|
||||
listEmoji, resp = Client.GetEmojiList()
|
||||
CheckNoError(t, resp)
|
||||
found := false
|
||||
for _, savedEmoji := range listEmoji {
|
||||
if savedEmoji.Id == emojis[0].Id {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
if found {
|
||||
t.Fatalf("should not get a deleted emoji %v", emojis[0].Id)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDeleteEmoji(t *testing.T) {
|
||||
th := Setup().InitBasic().InitSystemAdmin()
|
||||
defer TearDown()
|
||||
Client := th.Client
|
||||
|
||||
EnableCustomEmoji := *utils.Cfg.ServiceSettings.EnableCustomEmoji
|
||||
defer func() {
|
||||
*utils.Cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji
|
||||
}()
|
||||
*utils.Cfg.ServiceSettings.EnableCustomEmoji = true
|
||||
|
||||
emoji := &model.Emoji{
|
||||
CreatorId: th.BasicUser.Id,
|
||||
Name: model.NewId(),
|
||||
}
|
||||
|
||||
newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
||||
CheckNoError(t, resp)
|
||||
|
||||
ok, resp := Client.DeleteEmoji(newEmoji.Id)
|
||||
CheckNoError(t, resp)
|
||||
if ok != true {
|
||||
t.Fatal("should return true")
|
||||
} else {
|
||||
_, err := Client.GetEmoji(newEmoji.Id)
|
||||
if err == nil {
|
||||
t.Fatal("should not return the emoji it was deleted")
|
||||
}
|
||||
}
|
||||
|
||||
//Admin can delete other users emoji
|
||||
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
||||
CheckNoError(t, resp)
|
||||
|
||||
ok, resp = th.SystemAdminClient.DeleteEmoji(newEmoji.Id)
|
||||
CheckNoError(t, resp)
|
||||
if ok != true {
|
||||
t.Fatal("should return true")
|
||||
} else {
|
||||
_, err := th.SystemAdminClient.GetEmoji(newEmoji.Id)
|
||||
if err == nil {
|
||||
t.Fatal("should not return the emoji it was deleted")
|
||||
}
|
||||
}
|
||||
|
||||
// Try to delete just deleted emoji
|
||||
_, resp = Client.DeleteEmoji(newEmoji.Id)
|
||||
CheckInternalErrorStatus(t, resp)
|
||||
|
||||
//Try to delete non-existing emoji
|
||||
_, resp = Client.DeleteEmoji(model.NewId())
|
||||
CheckInternalErrorStatus(t, resp)
|
||||
|
||||
//Try to delete without Id
|
||||
_, resp = Client.DeleteEmoji("")
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
//Try to delete other user's custom emoji
|
||||
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
||||
CheckNoError(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
th.LoginBasic2()
|
||||
ok, resp = Client.DeleteEmoji(newEmoji.Id)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestGetEmoji(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
|
||||
|
||||
emoji := &model.Emoji{
|
||||
CreatorId: th.BasicUser.Id,
|
||||
Name: model.NewId(),
|
||||
}
|
||||
|
||||
newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
||||
CheckNoError(t, resp)
|
||||
|
||||
emoji, resp = Client.GetEmoji(newEmoji.Id)
|
||||
CheckNoError(t, resp)
|
||||
if emoji.Id != newEmoji.Id {
|
||||
t.Fatal("wrong emoji was returned")
|
||||
}
|
||||
|
||||
_, resp = Client.GetEmoji(model.NewId())
|
||||
CheckInternalErrorStatus(t, resp)
|
||||
|
||||
}
|
||||
|
||||
42
app/emoji.go
42
app/emoji.go
@@ -15,8 +15,11 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -119,6 +122,32 @@ func UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppErro
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteEmoji(emoji *model.Emoji) *model.AppError {
|
||||
if err := (<-Srv.Store.Emoji().Delete(emoji.Id, model.GetMillis())).Err; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go deleteEmojiImage(emoji.Id)
|
||||
go deleteReactionsForEmoji(emoji.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetEmoji(emojiId string) (*model.Emoji, *model.AppError) {
|
||||
if !*utils.Cfg.ServiceSettings.EnableCustomEmoji {
|
||||
return nil, model.NewAppError("deleteEmoji", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
if len(utils.Cfg.FileSettings.DriverName) == 0 {
|
||||
return nil, model.NewAppError("deleteImage", "api.emoji.storage.app_error", nil, "", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
if result := <-Srv.Store.Emoji().Get(emojiId, false); result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.(*model.Emoji), nil
|
||||
}
|
||||
}
|
||||
|
||||
func resizeEmojiGif(gifImg *gif.GIF) *gif.GIF {
|
||||
// Create a new RGBA image to hold the incremental frames.
|
||||
firstFrame := gifImg.Image[0].Bounds()
|
||||
@@ -162,3 +191,16 @@ func imageToPaletted(img image.Image) *image.Paletted {
|
||||
draw.FloydSteinberg.Draw(pm, b, img, image.ZP)
|
||||
return pm
|
||||
}
|
||||
|
||||
func deleteEmojiImage(id string) {
|
||||
if err := MoveFile(getEmojiImagePath(id), "emoji/"+id+"/image_deleted"); err != nil {
|
||||
l4g.Error("Failed to rename image when deleting emoji %v", id)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteReactionsForEmoji(emojiName string) {
|
||||
if result := <-Srv.Store.Reaction().DeleteAllWithEmojiName(emojiName); result.Err != nil {
|
||||
l4g.Warn(utils.T("api.emoji.delete.delete_reactions.app_error"), emojiName)
|
||||
l4g.Warn(result.Err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,6 +234,10 @@ func (c *Client4) GetEmojisRoute() string {
|
||||
return fmt.Sprintf("/emoji")
|
||||
}
|
||||
|
||||
func (c *Client4) GetEmojiRoute(emojiId string) string {
|
||||
return fmt.Sprintf(c.GetEmojisRoute()+"/%v", emojiId)
|
||||
}
|
||||
|
||||
func (c *Client4) DoApiGet(url string, etag string) (*http.Response, *AppError) {
|
||||
return c.DoApiRequest(http.MethodGet, url, "", etag)
|
||||
}
|
||||
@@ -2335,6 +2339,26 @@ func (c *Client4) GetEmojiList() ([]*Emoji, *Response) {
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteEmoji delete an custom emoji on the provided emoji id string.
|
||||
func (c *Client4) DeleteEmoji(emojiId string) (bool, *Response) {
|
||||
if r, err := c.DoApiDelete(c.GetEmojiRoute(emojiId)); err != nil {
|
||||
return false, &Response{StatusCode: r.StatusCode, Error: err}
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
return CheckStatusOK(r), BuildResponse(r)
|
||||
}
|
||||
}
|
||||
|
||||
// GetEmoji returns a custom emoji in the system on the provided emoji id string.
|
||||
func (c *Client4) GetEmoji(emojiId string) (*Emoji, *Response) {
|
||||
if r, err := c.DoApiGet(c.GetEmojiRoute(emojiId), ""); err != nil {
|
||||
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
return EmojiFromJson(r.Body), BuildResponse(r)
|
||||
}
|
||||
}
|
||||
|
||||
// Reaction Section
|
||||
|
||||
// GetReactions returns a list of reactions to a post.
|
||||
|
||||
Ссылка в новой задаче
Block a user