Removed PostPreview from MessageHasBeenPosted payload. (#18613)

Automatic Merge
Этот коммит содержится в:
Martin Kraft
2021-10-08 12:50:03 -04:00
коммит произвёл GitHub
родитель 4a7cf864be
Коммит 55ea07677d
3 изменённых файлов: 50 добавлений и 0 удалений

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

@@ -292,6 +292,14 @@ func (a *App) CreatePost(c *request.Context, post *model.Post, channel *model.Ch
// We make a copy of the post for the plugin hook to avoid a race condition.
rPostCopy := rpost.Clone()
// FIXME: Removes PreviewPost from the post payload sent to the MessageHasBeenPosted hook so that plugins compiled with older versions of
// Mattermost—without the gob registeration of the PreviewPost struct—won't crash.
if rPostCopy.Metadata != nil {
rPostCopy.Metadata = rPostCopy.Metadata.Copy()
}
rPostCopy.RemovePreviewPost()
if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil {
a.Srv().Go(func() {
pluginContext := pluginContext(c)

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

@@ -686,6 +686,20 @@ func (o *Post) ToNilIfInvalid() *Post {
return o
}
func (o *Post) RemovePreviewPost() {
if o.Metadata == nil || o.Metadata.Embeds == nil {
return
}
n := 0
for _, embed := range o.Metadata.Embeds {
if embed.Type != PostEmbedPermalink {
o.Metadata.Embeds[n] = embed
n++
}
}
o.Metadata.Embeds = o.Metadata.Embeds[:n]
}
func (o *Post) GetPreviewPost() *PreviewPost {
for _, embed := range o.Metadata.Embeds {
if embed.Type == PostEmbedPermalink {

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

@@ -34,3 +34,31 @@ type PostImage struct {
// FrameCount stores the number of frames in this image, if it is an animated gif. It will be 0 for other formats.
FrameCount int `json:"frame_count"`
}
// Copy does a deep copy
func (p *PostMetadata) Copy() *PostMetadata {
embedsCopy := make([]*PostEmbed, len(p.Embeds))
copy(embedsCopy, p.Embeds)
emojisCopy := make([]*Emoji, len(p.Emojis))
copy(emojisCopy, p.Emojis)
filesCopy := make([]*FileInfo, len(p.Files))
copy(filesCopy, p.Files)
imagesCopy := map[string]*PostImage{}
for k, v := range p.Images {
imagesCopy[k] = v
}
reactionsCopy := make([]*Reaction, len(p.Reactions))
copy(reactionsCopy, p.Reactions)
return &PostMetadata{
Embeds: embedsCopy,
Emojis: emojisCopy,
Files: filesCopy,
Images: imagesCopy,
Reactions: reactionsCopy,
}
}