diff options
author | Saturnino Abril <saturnino.abril@gmail.com> | 2017-04-19 05:15:15 +0900 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2017-04-18 16:15:15 -0400 |
commit | d2b86f1b8de4784baf578b611cf80779ccfa722a (patch) | |
tree | 076d56f2522c0d9580e04061db3cce99facce67b /api4/reaction.go | |
parent | 8aab290d10cc7cdd864cebbd463044abfa2d2aea (diff) | |
download | chat-d2b86f1b8de4784baf578b611cf80779ccfa722a.tar.gz chat-d2b86f1b8de4784baf578b611cf80779ccfa722a.tar.bz2 chat-d2b86f1b8de4784baf578b611cf80779ccfa722a.zip |
APIv4 POST /reactions (#6092)
* APIv4 POST /reactions
* update corresponding V3 endpoint
Diffstat (limited to 'api4/reaction.go')
-rw-r--r-- | api4/reaction.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/api4/reaction.go b/api4/reaction.go index 4deae4370..7d5952eea 100644 --- a/api4/reaction.go +++ b/api4/reaction.go @@ -15,9 +15,43 @@ import ( func InitReaction() { l4g.Debug(utils.T("api.reaction.init.debug")) + BaseRoutes.Reactions.Handle("", ApiSessionRequired(saveReaction)).Methods("POST") BaseRoutes.Post.Handle("/reactions", ApiSessionRequired(getReactions)).Methods("GET") } +func saveReaction(c *Context, w http.ResponseWriter, r *http.Request) { + reaction := model.ReactionFromJson(r.Body) + if reaction == nil { + c.SetInvalidParam("reaction") + return + } + + if len(reaction.UserId) != 26 || len(reaction.PostId) != 26 || len(reaction.EmojiName) == 0 || len(reaction.EmojiName) > 64 { + c.Err = model.NewLocAppError("saveReaction", "api.reaction.save_reaction.invalid.app_error", nil, "") + c.Err.StatusCode = http.StatusBadRequest + return + } + + if reaction.UserId != c.Session.UserId { + c.Err = model.NewLocAppError("saveReaction", "api.reaction.save_reaction.user_id.app_error", nil, "") + c.Err.StatusCode = http.StatusForbidden + return + } + + if !app.SessionHasPermissionToChannelByPost(c.Session, reaction.PostId, model.PERMISSION_READ_CHANNEL) { + c.SetPermissionError(model.PERMISSION_READ_CHANNEL) + return + } + + if reaction, err := app.SaveReactionForPost(reaction); err != nil { + c.Err = err + return + } else { + w.Write([]byte(reaction.ToJson())) + return + } +} + func getReactions(c *Context, w http.ResponseWriter, r *http.Request) { c.RequirePostId() if c.Err != nil { |