[GH-6798] added a new api endpoint to get the bulk reactions for posts (#10049)
* 6798 added a new api to get the bulk reactions for posts * 6798 added the permsission check before getting the reactions * GH-6798 added a new app function for the new endpoint * 6798 added a store method to get reactions for multiple posts * 6798 connected the app function with the new store function * 6798 fixed the review comments
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
7b7b89d5ae
Коммит
99160ff0bc
@@ -13,6 +13,7 @@ func (api *API) InitReaction() {
|
||||
api.BaseRoutes.Reactions.Handle("", api.ApiSessionRequired(saveReaction)).Methods("POST")
|
||||
api.BaseRoutes.Post.Handle("/reactions", api.ApiSessionRequired(getReactions)).Methods("GET")
|
||||
api.BaseRoutes.ReactionByNameForPostForUser.Handle("", api.ApiSessionRequired(deleteReaction)).Methods("DELETE")
|
||||
api.BaseRoutes.Posts.Handle("/ids/reactions", api.ApiSessionRequired(getBulkReactions)).Methods("POST")
|
||||
}
|
||||
|
||||
func saveReaction(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -106,3 +107,20 @@ func deleteReaction(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
func getBulkReactions(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
postIds := model.ArrayFromJson(r.Body)
|
||||
for _, postId := range postIds {
|
||||
if !c.App.SessionHasPermissionToChannelByPost(c.App.Session, postId, model.PERMISSION_READ_CHANNEL) {
|
||||
c.SetPermissionError(model.PERMISSION_READ_CHANNEL)
|
||||
return
|
||||
}
|
||||
}
|
||||
reactions, err := c.App.GetBulkReactionsForPosts(postIds)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte(model.MapPostIdToReactionsToJson(reactions)))
|
||||
}
|
||||
|
||||
@@ -567,3 +567,85 @@ func TestDeleteReaction(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetBulkReactions(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
Client := th.Client
|
||||
userId := th.BasicUser.Id
|
||||
user2Id := th.BasicUser2.Id
|
||||
post1 := &model.Post{UserId: userId, ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a"}
|
||||
post2 := &model.Post{UserId: userId, ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a"}
|
||||
post3 := &model.Post{UserId: userId, ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a"}
|
||||
|
||||
post4 := &model.Post{UserId: user2Id, ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a"}
|
||||
post5 := &model.Post{UserId: user2Id, ChannelId: th.BasicChannel.Id, Message: "zz" + model.NewId() + "a"}
|
||||
|
||||
post1, _ = Client.CreatePost(post1)
|
||||
post2, _ = Client.CreatePost(post2)
|
||||
post3, _ = Client.CreatePost(post3)
|
||||
post4, _ = Client.CreatePost(post4)
|
||||
post5, _ = Client.CreatePost(post5)
|
||||
|
||||
expectedPostIdsReactionsMap := make(map[string][]*model.Reaction)
|
||||
expectedPostIdsReactionsMap[post1.Id] = []*model.Reaction{}
|
||||
expectedPostIdsReactionsMap[post2.Id] = []*model.Reaction{}
|
||||
expectedPostIdsReactionsMap[post3.Id] = []*model.Reaction{}
|
||||
expectedPostIdsReactionsMap[post5.Id] = []*model.Reaction{}
|
||||
|
||||
userReactions := []*model.Reaction{
|
||||
{
|
||||
UserId: userId,
|
||||
PostId: post1.Id,
|
||||
EmojiName: "happy",
|
||||
},
|
||||
{
|
||||
UserId: userId,
|
||||
PostId: post1.Id,
|
||||
EmojiName: "sad",
|
||||
},
|
||||
{
|
||||
UserId: userId,
|
||||
PostId: post2.Id,
|
||||
EmojiName: "smile",
|
||||
},
|
||||
{
|
||||
UserId: user2Id,
|
||||
PostId: post4.Id,
|
||||
EmojiName: "smile",
|
||||
},
|
||||
}
|
||||
|
||||
for _, userReaction := range userReactions {
|
||||
reactions := expectedPostIdsReactionsMap[userReaction.PostId]
|
||||
if result := <-th.App.Srv.Store.Reaction().Save(userReaction); result.Err != nil {
|
||||
t.Fatal(result.Err)
|
||||
} else {
|
||||
reactions = append(reactions, result.Data.(*model.Reaction))
|
||||
|
||||
}
|
||||
expectedPostIdsReactionsMap[userReaction.PostId] = reactions
|
||||
}
|
||||
|
||||
postIds := []string{post1.Id, post2.Id, post3.Id, post4.Id, post5.Id}
|
||||
|
||||
t.Run("get-reactions", func(t *testing.T) {
|
||||
postIdsReactionsMap, resp := Client.GetBulkReactions(postIds)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
assert.ElementsMatch(t, expectedPostIdsReactionsMap[post1.Id], postIdsReactionsMap[post1.Id])
|
||||
assert.ElementsMatch(t, expectedPostIdsReactionsMap[post2.Id], postIdsReactionsMap[post2.Id])
|
||||
assert.ElementsMatch(t, expectedPostIdsReactionsMap[post3.Id], postIdsReactionsMap[post3.Id])
|
||||
assert.ElementsMatch(t, expectedPostIdsReactionsMap[post4.Id], postIdsReactionsMap[post4.Id])
|
||||
assert.ElementsMatch(t, expectedPostIdsReactionsMap[post5.Id], postIdsReactionsMap[post5.Id])
|
||||
assert.Equal(t, expectedPostIdsReactionsMap, postIdsReactionsMap)
|
||||
|
||||
})
|
||||
|
||||
t.Run("get-reactions-as-anonymous-user", func(t *testing.T) {
|
||||
Client.Logout()
|
||||
|
||||
_, resp := Client.GetBulkReactions(postIds)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user