diff --git a/app/post_metadata.go b/app/post_metadata.go index 9cc18d618b..57e2246348 100644 --- a/app/post_metadata.go +++ b/app/post_metadata.go @@ -59,11 +59,11 @@ func (a *App) PreparePostForClient(originalPost *model.Post) (*model.Post, *mode post.Metadata = &model.PostMetadata{} // Emojis and reaction counts - if emojis, reactionCounts, err := a.getEmojisAndReactionCountsForPost(post); err != nil { + if emojis, reactions, err := a.getEmojisAndReactionsForPost(post); err != nil { mlog.Warn("Failed to get emojis and reactions for a post", mlog.String("post_id", post.Id), mlog.Any("err", err)) } else { post.Metadata.Emojis = emojis - post.Metadata.ReactionCounts = reactionCounts + post.Metadata.Reactions = reactions } // Files @@ -90,7 +90,7 @@ func (a *App) PreparePostForClient(originalPost *model.Post) (*model.Post, *mode return post, nil } -func (a *App) getEmojisAndReactionCountsForPost(post *model.Post) ([]*model.Emoji, model.ReactionCounts, *model.AppError) { +func (a *App) getEmojisAndReactionsForPost(post *model.Post) ([]*model.Emoji, []*model.Reaction, *model.AppError) { reactions, err := a.GetReactionsForPost(post.Id) if err != nil { return nil, nil, err @@ -101,7 +101,7 @@ func (a *App) getEmojisAndReactionCountsForPost(post *model.Post) ([]*model.Emoj return nil, nil, err } - return emojis, model.CountReactions(reactions), nil + return emojis, reactions, nil } func (a *App) getEmbedForPost(post *model.Post, firstLink string) (*model.PostEmbed, error) { diff --git a/app/post_metadata_test.go b/app/post_metadata_test.go index deb08930c8..4ebb8773fd 100644 --- a/app/post_metadata_test.go +++ b/app/post_metadata_test.go @@ -52,7 +52,7 @@ func TestPreparePostForClient(t *testing.T) { assert.Equal(t, message, clientPost.Message, "shouldn't have changed Message") assert.NotEqual(t, nil, clientPost.Metadata, "should've populated Metadata") assert.Len(t, clientPost.Metadata.Embeds, 0, "should've populated Embeds") - assert.Len(t, clientPost.Metadata.ReactionCounts, 0, "should've populated ReactionCounts") + assert.Len(t, clientPost.Metadata.Reactions, 0, "should've populated Reactions") assert.Len(t, clientPost.Metadata.Files, 0, "should've populated Files") assert.Len(t, clientPost.Metadata.Emojis, 0, "should've populated Emojis") assert.Len(t, clientPost.Metadata.Images, 0, "should've populated Images") @@ -73,19 +73,22 @@ func TestPreparePostForClient(t *testing.T) { assert.Equal(t, clientPost, post, "shouldn't have changed any metadata") }) - t.Run("reaction counts", func(t *testing.T) { + t.Run("reactions", func(t *testing.T) { th := setup() defer th.TearDown() post := th.CreatePost(th.BasicChannel) - th.AddReactionToPost(post, th.BasicUser, "smile") + reaction1 := th.AddReactionToPost(post, th.BasicUser, "smile") + reaction2 := th.AddReactionToPost(post, th.BasicUser2, "smile") + reaction3 := th.AddReactionToPost(post, th.BasicUser2, "ice_cream") clientPost, err := th.App.PreparePostForClient(post) require.Nil(t, err) - assert.Equal(t, model.ReactionCounts{ - "smile": 1, - }, clientPost.Metadata.ReactionCounts, "should've populated ReactionCounts") + assert.Len(t, clientPost.Metadata.Reactions, 3, "should've populated Reactions") + assert.Equal(t, reaction1, clientPost.Metadata.Reactions[0], "first reaction is incorrect") + assert.Equal(t, reaction2, clientPost.Metadata.Reactions[1], "second reaction is incorrect") + assert.Equal(t, reaction3, clientPost.Metadata.Reactions[2], "third reaction is incorrect") }) t.Run("files", func(t *testing.T) { @@ -139,10 +142,8 @@ func TestPreparePostForClient(t *testing.T) { }) t.Run("populates reaction counts", func(t *testing.T) { - reactionCounts := clientPost.Metadata.ReactionCounts - assert.Len(t, reactionCounts, 2, "should've populated ReactionCounts") - assert.Equal(t, 1, reactionCounts["smile"], "should've included 'smile' in ReactionCounts") - assert.Equal(t, 2, reactionCounts["angry"], "should've included 'angry' in ReactionCounts") + reactions := clientPost.Metadata.Reactions + assert.Len(t, reactions, 3, "should've populated Reactions") }) }) @@ -178,11 +179,8 @@ func TestPreparePostForClient(t *testing.T) { }) t.Run("populates reaction counts", func(t *testing.T) { - reactionCounts := clientPost.Metadata.ReactionCounts - assert.Len(t, reactionCounts, 3, "should've populated ReactionCounts") - assert.Equal(t, 1, reactionCounts[emoji1.Name], "should've included emoji1 in ReactionCounts") - assert.Equal(t, 2, reactionCounts[emoji2.Name], "should've included emoji2 in ReactionCounts") - assert.Equal(t, 1, reactionCounts["angry"], "should've included angry in ReactionCounts") + reactions := clientPost.Metadata.Reactions + assert.Len(t, reactions, 4, "should've populated Reactions") }) }) diff --git a/model/post_metadata.go b/model/post_metadata.go index 1ec0695fae..bc4da5843b 100644 --- a/model/post_metadata.go +++ b/model/post_metadata.go @@ -18,8 +18,8 @@ type PostMetadata struct { // of file attachments which are contained in PostMetadata.FileInfos. Images map[string]*PostImage `json:"images,omitempty"` - // A map of emoji names to a count of users that reacted with the given emoji. - ReactionCounts ReactionCounts `json:"reaction_counts,omitempty"` + // A list of reactions made to the post + Reactions []*Reaction `json:"reactions,omitempty"` } type PostImage struct { diff --git a/model/reaction.go b/model/reaction.go index 8eb0674d4d..c1b9c499a8 100644 --- a/model/reaction.go +++ b/model/reaction.go @@ -17,8 +17,6 @@ type Reaction struct { CreateAt int64 `json:"create_at"` } -type ReactionCounts map[string]int - func (o *Reaction) ToJson() string { b, _ := json.Marshal(o) return string(b) @@ -76,13 +74,3 @@ func (o *Reaction) PreSave() { o.CreateAt = GetMillis() } } - -func CountReactions(reactions []*Reaction) ReactionCounts { - reactionCounts := ReactionCounts{} - - for _, reaction := range reactions { - reactionCounts[reaction.EmojiName] += 1 - } - - return reactionCounts -} diff --git a/model/reaction_test.go b/model/reaction_test.go index 26d3a6bd22..a357504775 100644 --- a/model/reaction_test.go +++ b/model/reaction_test.go @@ -82,38 +82,3 @@ func TestReactionIsValid(t *testing.T) { t.Fatal("create at should be invalid") } } - -func TestCountReactions(t *testing.T) { - userId := NewId() - userId2 := NewId() - - reactions := []*Reaction{ - { - UserId: userId, - EmojiName: "smile", - }, - { - UserId: userId, - EmojiName: "frowning", - }, - { - UserId: userId2, - EmojiName: "smile", - }, - { - UserId: userId2, - EmojiName: "neutral_face", - }, - } - - reactionCounts := CountReactions(reactions) - if len(reactionCounts) != 3 { - t.Fatal("should've received counts for 3 reactions") - } else if reactionCounts["smile"] != 2 { - t.Fatal("should've received 2 smile reactions") - } else if reactionCounts["frowning"] != 1 { - t.Fatal("should've received 1 frowning reaction") - } else if reactionCounts["neutral_face"] != 1 { - t.Fatal("should've received 2 neutral_face reaction") - } -}