From 87ff64ea6d26c9412f88840d31c980e1dab24c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Thu, 23 May 2019 14:29:41 +0200 Subject: [PATCH] MM-15035: Read from file infos from master on post creation (#10899) * MM-15035: Read from file infos from master on post creation * Handling edit post cases for get files metadata --- api4/post.go | 4 ++-- app/post.go | 16 ++++++++-------- app/post_metadata.go | 10 +++++----- app/post_metadata_test.go | 26 +++++++++++++------------- app/post_test.go | 4 ++-- app/reaction.go | 2 +- 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/api4/post.go b/api4/post.go index a6b0529902..ef9158931a 100644 --- a/api4/post.go +++ b/api4/post.go @@ -98,7 +98,7 @@ func createEphemeralPost(c *Context, w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) rp = model.AddPostActionCookies(rp, c.App.PostActionCookieSecret()) - rp = c.App.PreparePostForClient(rp, true) + rp = c.App.PreparePostForClient(rp, true, false) w.Write([]byte(rp.ToJson())) } @@ -261,7 +261,7 @@ func getPost(c *Context, w http.ResponseWriter, r *http.Request) { } } - post = c.App.PreparePostForClient(post, false) + post = c.App.PreparePostForClient(post, false, false) if c.HandleEtag(post.Etag(), "Get Post", w, r) { return diff --git a/app/post.go b/app/post.go index 745f68b583..0783c7f10c 100644 --- a/app/post.go +++ b/app/post.go @@ -295,7 +295,7 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo // Normally, we would let the API layer call PreparePostForClient, but we do it here since it also needs // to be done when we send the post over the websocket in handlePostEvents - rpost = a.PreparePostForClient(rpost, true) + rpost = a.PreparePostForClient(rpost, true, false) if err := a.handlePostEvents(rpost, user, channel, triggerWebhooks, parentPostList); err != nil { mlog.Error("Failed to handle post events", mlog.Err(err)) @@ -415,7 +415,7 @@ func (a *App) SendEphemeralPost(userId string, post *model.Post) *model.Post { post.GenerateActionIds() message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE, "", post.ChannelId, userId, nil) - post = a.PreparePostForClient(post, true) + post = a.PreparePostForClient(post, true, false) post = model.AddPostActionCookies(post, a.PostActionCookieSecret()) message.Add("post", post.ToJson()) a.Publish(message) @@ -433,7 +433,7 @@ func (a *App) UpdateEphemeralPost(userId string, post *model.Post) *model.Post { post.GenerateActionIds() message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", post.ChannelId, userId, nil) - post = a.PreparePostForClient(post, true) + post = a.PreparePostForClient(post, true, false) post = model.AddPostActionCookies(post, a.PostActionCookieSecret()) message.Add("post", post.ToJson()) a.Publish(message) @@ -563,7 +563,7 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model }) } - rpost = a.PreparePostForClient(rpost, false) + rpost = a.PreparePostForClient(rpost, false, true) message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", rpost.ChannelId, "", nil) message.Add("post", rpost.ToJson()) @@ -741,7 +741,7 @@ func (a *App) DeletePost(postId, deleteByID string) (*model.Post, *model.AppErro } message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_DELETED, "", post.ChannelId, "", nil) - message.Add("post", a.PreparePostForClient(post, false).ToJson()) + message.Add("post", a.PreparePostForClient(post, false, false).ToJson()) a.Publish(message) a.Srv.Go(func() { @@ -957,7 +957,7 @@ func (a *App) SearchPostsInTeamForUser(terms string, userId string, teamId strin func (a *App) GetFileInfosForPostWithMigration(postId string) ([]*model.FileInfo, *model.AppError) { pchan := a.Srv.Store.Post().GetSingle(postId) - infos, err := a.GetFileInfosForPost(postId) + infos, err := a.GetFileInfosForPost(postId, false) if err != nil { return nil, err } @@ -980,8 +980,8 @@ func (a *App) GetFileInfosForPostWithMigration(postId string) ([]*model.FileInfo return infos, nil } -func (a *App) GetFileInfosForPost(postId string) ([]*model.FileInfo, *model.AppError) { - return a.Srv.Store.FileInfo().GetForPost(postId, false, true) +func (a *App) GetFileInfosForPost(postId string, fromMaster bool) ([]*model.FileInfo, *model.AppError) { + return a.Srv.Store.FileInfo().GetForPost(postId, fromMaster, true) } func (a *App) PostWithProxyAddedToImageURLs(post *model.Post) *model.Post { diff --git a/app/post_metadata.go b/app/post_metadata.go index bddd310bc9..8a5acdd6ca 100644 --- a/app/post_metadata.go +++ b/app/post_metadata.go @@ -45,7 +45,7 @@ func (a *App) PreparePostListForClient(originalList *model.PostList) *model.Post } for id, originalPost := range originalList.Posts { - post := a.PreparePostForClient(originalPost, false) + post := a.PreparePostForClient(originalPost, false, false) list.Posts[id] = post } @@ -53,7 +53,7 @@ func (a *App) PreparePostListForClient(originalList *model.PostList) *model.Post return list } -func (a *App) PreparePostForClient(originalPost *model.Post, isNewPost bool) *model.Post { +func (a *App) PreparePostForClient(originalPost *model.Post, isNewPost bool, isEditPost bool) *model.Post { post := originalPost.Clone() // Proxy image links before constructing metadata so that requests go through the proxy @@ -74,7 +74,7 @@ func (a *App) PreparePostForClient(originalPost *model.Post, isNewPost bool) *mo } // Files - if fileInfos, err := a.getFileMetadataForPost(post); err != nil { + if fileInfos, err := a.getFileMetadataForPost(post, isNewPost || isEditPost); err != nil { mlog.Warn("Failed to get files for a post", mlog.String("post_id", post.Id), mlog.Err(err)) } else { post.Metadata.Files = fileInfos @@ -96,12 +96,12 @@ func (a *App) PreparePostForClient(originalPost *model.Post, isNewPost bool) *mo return post } -func (a *App) getFileMetadataForPost(post *model.Post) ([]*model.FileInfo, *model.AppError) { +func (a *App) getFileMetadataForPost(post *model.Post, fromMaster bool) ([]*model.FileInfo, *model.AppError) { if len(post.FileIds) == 0 { return nil, nil } - return a.GetFileInfosForPost(post.Id) + return a.GetFileInfosForPost(post.Id, fromMaster) } func (a *App) getEmojisAndReactionsForPost(post *model.Post) ([]*model.Emoji, []*model.Reaction, *model.AppError) { diff --git a/app/post_metadata_test.go b/app/post_metadata_test.go index 6d64afc9d4..0f3561c143 100644 --- a/app/post_metadata_test.go +++ b/app/post_metadata_test.go @@ -83,7 +83,7 @@ func TestPreparePostForClient(t *testing.T) { Message: message, } - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) t.Run("doesn't mutate provided post", func(t *testing.T) { assert.NotEqual(t, clientPost, post, "should've returned a new post") @@ -109,7 +109,7 @@ func TestPreparePostForClient(t *testing.T) { post := th.CreatePost(th.BasicChannel) - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) assert.False(t, clientPost == post, "should've returned a new post") assert.Equal(t, clientPost, post, "shouldn't have changed any metadata") @@ -125,7 +125,7 @@ func TestPreparePostForClient(t *testing.T) { reaction3 := th.AddReactionToPost(post, th.BasicUser2, "ice_cream") post.HasReactions = true - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) assert.Len(t, clientPost.Metadata.Reactions, 3, "should've populated Reactions") assert.Equal(t, reaction1, clientPost.Metadata.Reactions[0], "first reaction is incorrect") @@ -149,7 +149,7 @@ func TestPreparePostForClient(t *testing.T) { fileInfo.PostId = post.Id - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) assert.Equal(t, []*model.FileInfo{fileInfo}, clientPost.Metadata.Files, "should've populated Files") }) @@ -183,7 +183,7 @@ func TestPreparePostForClient(t *testing.T) { th.AddReactionToPost(post, th.BasicUser2, "angry") post.HasReactions = true - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) t.Run("populates emojis", func(t *testing.T) { assert.ElementsMatch(t, []*model.Emoji{}, clientPost.Metadata.Emojis, "should've populated empty Emojis") @@ -228,7 +228,7 @@ func TestPreparePostForClient(t *testing.T) { th.AddReactionToPost(post, th.BasicUser2, "angry") post.HasReactions = true - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) t.Run("pupulates emojis", func(t *testing.T) { assert.ElementsMatch(t, []*model.Emoji{emoji1, emoji2, emoji3, emoji4}, clientPost.Metadata.Emojis, "should've populated post.Emojis") @@ -251,7 +251,7 @@ func TestPreparePostForClient(t *testing.T) { }, th.BasicChannel, false) require.Nil(t, err) - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) t.Run("populates image dimensions", func(t *testing.T) { imageDimensions := clientPost.Metadata.Images @@ -295,7 +295,7 @@ func TestPreparePostForClient(t *testing.T) { }, th.BasicChannel, false) require.Nil(t, err) - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) // Reminder that only the first link gets an embed and dimensions @@ -330,7 +330,7 @@ func TestPreparePostForClient(t *testing.T) { }, th.BasicChannel, false) require.Nil(t, err) - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) t.Run("populates embeds", func(t *testing.T) { assert.ElementsMatch(t, []*model.PostEmbed{ @@ -381,7 +381,7 @@ func TestPreparePostForClient(t *testing.T) { }, th.BasicChannel, false) require.Nil(t, err) - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) t.Run("populates embeds", func(t *testing.T) { assert.ElementsMatch(t, []*model.PostEmbed{ @@ -411,7 +411,7 @@ func TestPreparePostForClient(t *testing.T) { }) post := th.CreatePost(th.BasicChannel) - post = th.App.PreparePostForClient(post, false) + post = th.App.PreparePostForClient(post, false, false) assert.Nil(t, post.Metadata) @@ -464,7 +464,7 @@ func testProxyLinkedImage(t *testing.T, th *TestHelper, shouldProxy bool) { Message: fmt.Sprintf(postTemplate, imageURL), } - clientPost := th.App.PreparePostForClient(post, false) + clientPost := th.App.PreparePostForClient(post, false, false) if shouldProxy { assert.Equal(t, fmt.Sprintf(postTemplate, imageURL), post.Message, "should not have mutated original post") @@ -482,7 +482,7 @@ func testProxyOpenGraphImage(t *testing.T, th *TestHelper, shouldProxy bool) { }, th.BasicChannel, false) require.Nil(t, err) - embeds := th.App.PreparePostForClient(post, false).Metadata.Embeds + embeds := th.App.PreparePostForClient(post, false, false).Metadata.Embeds require.Len(t, embeds, 1, "should have one embed") embed := embeds[0] diff --git a/app/post_test.go b/app/post_test.go index ebe5822ab5..6609ea7b46 100644 --- a/app/post_test.go +++ b/app/post_test.go @@ -208,7 +208,7 @@ func TestAttachFilesToPost(t *testing.T) { err = th.App.attachFilesToPost(post) assert.Nil(t, err) - infos, err := th.App.GetFileInfosForPost(post.Id) + infos, err := th.App.GetFileInfosForPost(post.Id, false) assert.Nil(t, err) assert.Len(t, infos, 2) }) @@ -236,7 +236,7 @@ func TestAttachFilesToPost(t *testing.T) { err = th.App.attachFilesToPost(post) assert.Nil(t, err) - infos, err := th.App.GetFileInfosForPost(post.Id) + infos, err := th.App.GetFileInfosForPost(post.Id, false) assert.Nil(t, err) assert.Len(t, infos, 1) assert.Equal(t, info2.Id, infos[0].Id) diff --git a/app/reaction.go b/app/reaction.go index fb5f7bf57f..0df27ca702 100644 --- a/app/reaction.go +++ b/app/reaction.go @@ -137,7 +137,7 @@ func (a *App) sendReactionEvent(event string, reaction *model.Reaction, post *mo post.HasReactions = hasReactions post.UpdateAt = model.GetMillis() - clientPost := a.PreparePostForClient(post, false) + clientPost := a.PreparePostForClient(post, false, false) umessage := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", post.ChannelId, "", nil) umessage.Add("post", clientPost.ToJson())