Skip to content

Commit

Permalink
APIv4 GET /posts/{post_id}/reactions (#6047)
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril authored and grundleborg committed Apr 11, 2017
1 parent d2be3de commit e841d0c
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions api4/api.go
Expand Up @@ -177,6 +177,7 @@ func InitApi(full bool) {
InitStatus()
InitWebSocket()
InitEmoji()
InitReaction()

app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404))

Expand Down
39 changes: 39 additions & 0 deletions 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
}
}
89 changes: 89 additions & 0 deletions 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)
}
16 changes: 16 additions & 0 deletions app/reaction.go
@@ -0,0 +1,16 @@
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package app

import (
"github.com/mattermost/platform/model"
)

func GetReactionsForPost(postId string) ([]*model.Reaction, *model.AppError) {
if result := <-Srv.Store.Reaction().GetForPost(postId, true); result.Err != nil {
return nil, result.Err
} else {
return result.Data.([]*model.Reaction), nil
}
}
12 changes: 12 additions & 0 deletions model/client4.go
Expand Up @@ -2311,3 +2311,15 @@ func (c *Client4) GetEmojiList() ([]*Emoji, *Response) {
return EmojiListFromJson(r.Body), BuildResponse(r)
}
}

// Reaction Section

// GetReactions returns a list of reactions to a post.
func (c *Client4) GetReactions(postId string) ([]*Reaction, *Response) {
if r, err := c.DoApiGet(c.GetPostRoute(postId)+"/reactions", ""); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return ReactionsFromJson(r.Body), BuildResponse(r)
}
}

0 comments on commit e841d0c

Please sign in to comment.