diff options
author | Saturnino Abril <saturnino.abril@gmail.com> | 2017-04-11 22:14:33 +0900 |
---|---|---|
committer | George Goldberg <george@gberg.me> | 2017-04-11 14:14:33 +0100 |
commit | e841d0c5023640efc3dcf9d5284f55458e06a0a6 (patch) | |
tree | b78e475ffd4760e1c8aed16dd0ab3fbb7fa6e571 /api4 | |
parent | d2be3de2d3b54c80f3499359dbd32306ccabd722 (diff) | |
download | chat-e841d0c5023640efc3dcf9d5284f55458e06a0a6.tar.gz chat-e841d0c5023640efc3dcf9d5284f55458e06a0a6.tar.bz2 chat-e841d0c5023640efc3dcf9d5284f55458e06a0a6.zip |
APIv4 GET /posts/{post_id}/reactions (#6047)
Diffstat (limited to 'api4')
-rw-r--r-- | api4/api.go | 1 | ||||
-rw-r--r-- | api4/reaction.go | 39 | ||||
-rw-r--r-- | api4/reaction_test.go | 89 |
3 files changed, 129 insertions, 0 deletions
diff --git a/api4/api.go b/api4/api.go index 6451e4102..b1b8e0c69 100644 --- a/api4/api.go +++ b/api4/api.go @@ -177,6 +177,7 @@ func InitApi(full bool) { InitStatus() InitWebSocket() InitEmoji() + InitReaction() app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404)) diff --git a/api4/reaction.go b/api4/reaction.go new file mode 100644 index 000000000..b60b98f47 --- /dev/null +++ b/api4/reaction.go @@ -0,0 +1,39 @@ +// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package api4 + +import ( + "net/http" + + l4g "github.com/alecthomas/log4go" + "github.com/mattermost/platform/app" + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" +) + +func InitReaction() { + l4g.Debug(utils.T("api.reaction.init.debug")) + + BaseRoutes.Post.Handle("/reactions", ApiSessionRequired(getReactions)).Methods("GET") +} + +func getReactions(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequirePostId() + if c.Err != nil { + return + } + + if !app.SessionHasPermissionToChannelByPost(c.Session, c.Params.PostId, model.PERMISSION_READ_CHANNEL) { + c.SetPermissionError(model.PERMISSION_READ_CHANNEL) + return + } + + if reactions, err := app.GetReactionsForPost(c.Params.PostId); err != nil { + c.Err = err + return + } else { + w.Write([]byte(model.ReactionsToJson(reactions))) + return + } +} diff --git a/api4/reaction_test.go b/api4/reaction_test.go new file mode 100644 index 000000000..457b905da --- /dev/null +++ b/api4/reaction_test.go @@ -0,0 +1,89 @@ +// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package api4 + +import ( + "testing" + + "reflect" + + "github.com/mattermost/platform/app" + "github.com/mattermost/platform/model" +) + +func TestGetReactions(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + userId := th.BasicUser.Id + user2Id := th.BasicUser2.Id + postId := th.BasicPost.Id + + userReactions := []*model.Reaction{ + { + UserId: userId, + PostId: postId, + EmojiName: "smile", + }, + { + UserId: userId, + PostId: postId, + EmojiName: "happy", + }, + { + UserId: userId, + PostId: postId, + EmojiName: "sad", + }, + { + UserId: user2Id, + PostId: postId, + EmojiName: "smile", + }, + { + UserId: user2Id, + PostId: postId, + EmojiName: "sad", + }, + } + + var reactions []*model.Reaction + + for _, userReaction := range userReactions { + if result := <-app.Srv.Store.Reaction().Save(userReaction); result.Err != nil { + t.Fatal(result.Err) + } else { + reactions = append(reactions, result.Data.(*model.Reaction)) + } + } + + rr, resp := Client.GetReactions(postId) + CheckNoError(t, resp) + + if len(rr) != 5 { + t.Fatal("reactions should returned correct length") + } + + if !reflect.DeepEqual(rr, reactions) { + t.Fatal("reactions should have matched") + } + + rr, resp = Client.GetReactions("junk") + CheckBadRequestStatus(t, resp) + + if len(rr) != 0 { + t.Fatal("reactions should return empty") + } + + _, resp = Client.GetReactions(GenerateTestId()) + CheckForbiddenStatus(t, resp) + + Client.Logout() + + _, resp = Client.GetReactions(postId) + CheckUnauthorizedStatus(t, resp) + + _, resp = th.SystemAdminClient.GetReactions(postId) + CheckNoError(t, resp) +} |