diff --git a/app/post_metadata.go b/app/post_metadata.go index f5d751bbf5..233bd4a841 100644 --- a/app/post_metadata.go +++ b/app/post_metadata.go @@ -87,6 +87,11 @@ func (a *App) PreparePostForClient(originalPost *model.Post, isNewPost bool, isE post.Metadata = &model.PostMetadata{} + if post.DeleteAt > 0 { + // Don't fill out metadata for deleted posts + return post + } + // Emojis and reaction counts 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.Err(err)) diff --git a/app/post_metadata_test.go b/app/post_metadata_test.go index 240f18df1d..367ec99372 100644 --- a/app/post_metadata_test.go +++ b/app/post_metadata_test.go @@ -477,6 +477,36 @@ func TestPreparePostForClient(t *testing.T) { }, imageDimensions[server.URL+"/test-image1.png"]) }) }) + + t.Run("no metadata for deleted posts", func(t *testing.T) { + th := setup() + defer th.TearDown() + + fileInfo, err := th.App.DoUploadFile(time.Now(), th.BasicTeam.Id, th.BasicChannel.Id, th.BasicUser.Id, "test.txt", []byte("test")) + require.Nil(t, err) + + post, err := th.App.CreatePost(&model.Post{ + Message: "test", + FileIds: []string{fileInfo.Id}, + UserId: th.BasicUser.Id, + ChannelId: th.BasicChannel.Id, + }, th.BasicChannel, false) + require.Nil(t, err) + + th.AddReactionToPost(post, th.BasicUser, "taco") + + post, err = th.App.DeletePost(post.Id, th.BasicUser.Id) + require.Nil(t, err) + + // DeleteAt isn't set on the post returned by App.DeletePost + post.DeleteAt = model.GetMillis() + + clientPost := th.App.PreparePostForClient(post, false, false) + + assert.NotEqual(t, nil, clientPost.Metadata, "should've populated Metadata“") + assert.Nil(t, clientPost.Metadata.Reactions, "should not have populated Reactions") + assert.Nil(t, clientPost.Metadata.Files, "should not have populated Files") + }) } func TestPreparePostForClientWithImageProxy(t *testing.T) {