Exclude sending file metadata to plugin hooks (#18454)

* Exclude sending file metadata to plugin hooks

A FileInfo object contained a MiniPreview which is
a slice of bytes. This can be particularly costly
while marshalling to plugin hooks.

We avoid this by refactoring the Embeds and Images
population to a separate method and calling that
to prevent posts from getting updated.

https://community-daily.mattermost.com/boards/workspace/zyoahc9uapdn3xdptac6jb69ic/285b80a3-257d-41f6-8cf4-ed80ca9d92e5/495cdb4d-c13a-4992-8eb9-80cfee2819a4?c=9c0b5413-5401-4ef2-83d5-b9f756585bbc

```release-note
NONE
```

* refactor to separate method

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2021-09-27 08:35:39 +05:30
коммит произвёл GitHub
родитель ef639973b7
Коммит 225461fd7e
6 изменённых файлов: 45 добавлений и 14 удалений

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

@@ -60,7 +60,7 @@ func (a *App) PreparePostListForClient(originalList *model.PostList) *model.Post
}
for id, originalPost := range originalList.Posts {
post := a.PreparePostForClient(originalPost, false, false)
post := a.PreparePostForClientWithEmbedsAndImages(originalPost, false, false)
list.Posts[id] = post
}
@@ -94,15 +94,16 @@ func (a *App) OverrideIconURLIfEmoji(post *model.Post) {
}
}
func (a *App) PreparePostForClient(originalPost *model.Post, isNewPost bool, isEditPost bool) *model.Post {
func (a *App) PreparePostForClient(originalPost *model.Post, isNewPost, isEditPost bool) *model.Post {
post := originalPost.Clone()
// Proxy image links before constructing metadata so that requests go through the proxy
post = a.PostWithProxyAddedToImageURLs(post)
a.OverrideIconURLIfEmoji(post)
post.Metadata = &model.PostMetadata{}
if post.Metadata == nil {
post.Metadata = &model.PostMetadata{}
}
if post.DeleteAt > 0 {
// For deleted posts we don't fill out metadata nor do we return the post content
@@ -125,9 +126,22 @@ func (a *App) PreparePostForClient(originalPost *model.Post, isNewPost bool, isE
post.Metadata.Files = fileInfos
}
return post
}
func (a *App) PreparePostForClientWithEmbedsAndImages(originalPost *model.Post, isNewPost, isEditPost bool) *model.Post {
post := a.PreparePostForClient(originalPost, isNewPost, isEditPost)
post = a.getEmbedsAndImages(post, true)
return post
}
func (a *App) getEmbedsAndImages(post *model.Post, isNewPost bool) *model.Post {
if post.Metadata == nil {
post.Metadata = &model.PostMetadata{}
}
// Embeds and image dimensions
firstLink, images := a.getFirstLinkAndImages(post.Message)
if embed, err := a.getEmbedForPost(post, firstLink, isNewPost); err != nil {
appErr, ok := err.(*model.AppError)
isNotFound := ok && appErr.StatusCode == http.StatusNotFound
@@ -142,7 +156,6 @@ func (a *App) PreparePostForClient(originalPost *model.Post, isNewPost bool, isE
}
post.Metadata.Images = a.getImagesForPost(post, images, isNewPost)
return post
}