Skip to content

Commit

Permalink
Removed PostPreview from MessageHasBeenPosted payload. (#18613)
Browse files Browse the repository at this point in the history
Automatic Merge
  • Loading branch information
Martin Kraft committed Oct 8, 2021
1 parent 4a7cf86 commit 55ea076
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/post.go
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions model/post.go
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions model/post_metadata.go
Expand Up @@ -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,
}
}

0 comments on commit 55ea076

Please sign in to comment.