diff --git a/server/channels/app/post.go b/server/channels/app/post.go index 002276e3bd..71b83a6ec8 100644 --- a/server/channels/app/post.go +++ b/server/channels/app/post.go @@ -574,6 +574,7 @@ func (a *App) SendEphemeralPost(c request.CTX, userID string, post *model.Post) // If we failed to sanitize the post, we still want to remove the metadata. sanitizedPost = post.Clone() sanitizedPost.Metadata = nil + sanitizedPost.DelProp(model.PostPropsPreviewedPost) } post = sanitizedPost @@ -599,6 +600,18 @@ func (a *App) UpdateEphemeralPost(c request.CTX, userID string, post *model.Post message := model.NewWebSocketEvent(model.WebsocketEventPostEdited, "", post.ChannelId, userID, nil, "") post = a.PreparePostForClientWithEmbedsAndImages(c, post, true, false, true) post = model.AddPostActionCookies(post, a.PostActionCookieSecret()) + + sanitizedPost, appErr := a.SanitizePostMetadataForUser(c, post, userID) + if appErr != nil { + mlog.Error("Failed to sanitize post metadata for user", mlog.String("user_id", userID), mlog.Err(appErr)) + + // If we failed to sanitize the post, we still want to remove the metadata. + sanitizedPost = post.Clone() + sanitizedPost.Metadata = nil + sanitizedPost.DelProp(model.PostPropsPreviewedPost) + } + post = sanitizedPost + postJSON, jsonErr := post.ToJSON() if jsonErr != nil { c.Logger().Warn("Failed to encode post to JSON", mlog.Err(jsonErr)) @@ -768,6 +781,7 @@ func (a *App) UpdatePost(c request.CTX, receivedUpdatedPost *model.Post, safeUpd // If we failed to sanitize the post, we still want to remove the metadata. sanitizedPost = rpost.Clone() sanitizedPost.Metadata = nil + sanitizedPost.DelProp(model.PostPropsPreviewedPost) } rpost = sanitizedPost diff --git a/server/channels/app/post_test.go b/server/channels/app/post_test.go index 69aabe605e..66ea158313 100644 --- a/server/channels/app/post_test.go +++ b/server/channels/app/post_test.go @@ -1379,6 +1379,76 @@ func TestPatchPostInArchivedChannel(t *testing.T) { require.Equal(t, "api.post.patch_post.can_not_update_post_in_deleted.error", err.Id) } +func TestUpdateEphemeralPost(t *testing.T) { + t.Run("Post contains preview if the user has permissions", func(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + th.AddUserToChannel(th.BasicUser, th.BasicChannel) + + referencedPost := &model.Post{ + ChannelId: th.BasicChannel.Id, + Message: "hello world", + UserId: th.BasicUser.Id, + } + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.SiteURL = "http://mymattermost.com" + }) + + th.Context.Session().UserId = th.BasicUser.Id + + referencedPost, err := th.App.CreatePost(th.Context, referencedPost, th.BasicChannel, false, false) + require.Nil(t, err) + + permalink := fmt.Sprintf("%s/%s/pl/%s", *th.App.Config().ServiceSettings.SiteURL, th.BasicTeam.Name, referencedPost.Id) + + testPost := &model.Post{ + ChannelId: th.BasicChannel.Id, + Message: permalink, + UserId: th.BasicUser.Id, + } + + testPost = th.App.UpdateEphemeralPost(th.Context, th.BasicUser.Id, testPost) + require.NotNil(t, testPost.Metadata) + require.Len(t, testPost.Metadata.Embeds, 1) + require.Equal(t, model.PostEmbedPermalink, testPost.Metadata.Embeds[0].Type) + }) + + t.Run("Post does not contain preview if the user has no permissions", func(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + privateChannel := th.CreatePrivateChannel(th.Context, th.BasicTeam) + th.AddUserToChannel(th.BasicUser, privateChannel) + th.AddUserToChannel(th.BasicUser2, th.BasicChannel) + + referencedPost := &model.Post{ + ChannelId: privateChannel.Id, + Message: "hello world", + UserId: th.BasicUser.Id, + } + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.SiteURL = "http://mymattermost.com" + }) + + th.Context.Session().UserId = th.BasicUser.Id + + referencedPost, err := th.App.CreatePost(th.Context, referencedPost, th.BasicChannel, false, false) + require.Nil(t, err) + + permalink := fmt.Sprintf("%s/%s/pl/%s", *th.App.Config().ServiceSettings.SiteURL, th.BasicTeam.Name, referencedPost.Id) + + testPost := &model.Post{ + ChannelId: th.BasicChannel.Id, + Message: permalink, + UserId: th.BasicUser2.Id, + } + + testPost = th.App.UpdateEphemeralPost(th.Context, th.BasicUser2.Id, testPost) + require.Nil(t, testPost.Metadata.Embeds) + }) +} + func TestUpdatePost(t *testing.T) { t.Run("call PreparePostForClient before returning", func(t *testing.T) { th := Setup(t).InitBasic()