MM-12826 Include list of reactions in post metadata instead of just counts (#9749)

Этот коммит содержится в:
Harrison Healey
2018-10-26 11:26:32 -04:00
родитель 2190c37359
Коммит ba173414d9
5 изменённых файлов: 19 добавлений и 68 удалений

Просмотреть файл

@@ -59,11 +59,11 @@ func (a *App) PreparePostForClient(originalPost *model.Post) (*model.Post, *mode
post.Metadata = &model.PostMetadata{} post.Metadata = &model.PostMetadata{}
// Emojis and reaction counts // 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)) mlog.Warn("Failed to get emojis and reactions for a post", mlog.String("post_id", post.Id), mlog.Any("err", err))
} else { } else {
post.Metadata.Emojis = emojis post.Metadata.Emojis = emojis
post.Metadata.ReactionCounts = reactionCounts post.Metadata.Reactions = reactions
} }
// Files // Files
@@ -90,7 +90,7 @@ func (a *App) PreparePostForClient(originalPost *model.Post) (*model.Post, *mode
return post, nil 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) reactions, err := a.GetReactionsForPost(post.Id)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@@ -101,7 +101,7 @@ func (a *App) getEmojisAndReactionCountsForPost(post *model.Post) ([]*model.Emoj
return nil, nil, err 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) { func (a *App) getEmbedForPost(post *model.Post, firstLink string) (*model.PostEmbed, error) {

Просмотреть файл

@@ -52,7 +52,7 @@ func TestPreparePostForClient(t *testing.T) {
assert.Equal(t, message, clientPost.Message, "shouldn't have changed Message") assert.Equal(t, message, clientPost.Message, "shouldn't have changed Message")
assert.NotEqual(t, nil, clientPost.Metadata, "should've populated Metadata") 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.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.Files, 0, "should've populated Files")
assert.Len(t, clientPost.Metadata.Emojis, 0, "should've populated Emojis") assert.Len(t, clientPost.Metadata.Emojis, 0, "should've populated Emojis")
assert.Len(t, clientPost.Metadata.Images, 0, "should've populated Images") 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") 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() th := setup()
defer th.TearDown() defer th.TearDown()
post := th.CreatePost(th.BasicChannel) 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) clientPost, err := th.App.PreparePostForClient(post)
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, model.ReactionCounts{ assert.Len(t, clientPost.Metadata.Reactions, 3, "should've populated Reactions")
"smile": 1, assert.Equal(t, reaction1, clientPost.Metadata.Reactions[0], "first reaction is incorrect")
}, clientPost.Metadata.ReactionCounts, "should've populated ReactionCounts") 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) { 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) { t.Run("populates reaction counts", func(t *testing.T) {
reactionCounts := clientPost.Metadata.ReactionCounts reactions := clientPost.Metadata.Reactions
assert.Len(t, reactionCounts, 2, "should've populated ReactionCounts") assert.Len(t, reactions, 3, "should've populated Reactions")
assert.Equal(t, 1, reactionCounts["smile"], "should've included 'smile' in ReactionCounts")
assert.Equal(t, 2, reactionCounts["angry"], "should've included 'angry' in ReactionCounts")
}) })
}) })
@@ -178,11 +179,8 @@ func TestPreparePostForClient(t *testing.T) {
}) })
t.Run("populates reaction counts", func(t *testing.T) { t.Run("populates reaction counts", func(t *testing.T) {
reactionCounts := clientPost.Metadata.ReactionCounts reactions := clientPost.Metadata.Reactions
assert.Len(t, reactionCounts, 3, "should've populated ReactionCounts") assert.Len(t, reactions, 4, "should've populated Reactions")
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")
}) })
}) })

Просмотреть файл

@@ -18,8 +18,8 @@ type PostMetadata struct {
// of file attachments which are contained in PostMetadata.FileInfos. // of file attachments which are contained in PostMetadata.FileInfos.
Images map[string]*PostImage `json:"images,omitempty"` Images map[string]*PostImage `json:"images,omitempty"`
// A map of emoji names to a count of users that reacted with the given emoji. // A list of reactions made to the post
ReactionCounts ReactionCounts `json:"reaction_counts,omitempty"` Reactions []*Reaction `json:"reactions,omitempty"`
} }
type PostImage struct { type PostImage struct {

Просмотреть файл

@@ -17,8 +17,6 @@ type Reaction struct {
CreateAt int64 `json:"create_at"` CreateAt int64 `json:"create_at"`
} }
type ReactionCounts map[string]int
func (o *Reaction) ToJson() string { func (o *Reaction) ToJson() string {
b, _ := json.Marshal(o) b, _ := json.Marshal(o)
return string(b) return string(b)
@@ -76,13 +74,3 @@ func (o *Reaction) PreSave() {
o.CreateAt = GetMillis() o.CreateAt = GetMillis()
} }
} }
func CountReactions(reactions []*Reaction) ReactionCounts {
reactionCounts := ReactionCounts{}
for _, reaction := range reactions {
reactionCounts[reaction.EmojiName] += 1
}
return reactionCounts
}

Просмотреть файл

@@ -82,38 +82,3 @@ func TestReactionIsValid(t *testing.T) {
t.Fatal("create at should be invalid") 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")
}
}