From 05cd245e9799e5a7113a1fe7d932ed6e4c2a5e69 Mon Sep 17 00:00:00 2001 From: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com> Date: Fri, 30 Jun 2023 10:29:27 +0530 Subject: [PATCH] Fixed a bug that prevent post deletion (#23908) * Fixed a bug that prevent post deletion * lint fix: --- server/channels/app/post_metadata.go | 10 +++- server/channels/app/post_metadata_test.go | 57 +++++++++++++++++++ .../src/reducers/entities/posts.ts | 9 +-- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/server/channels/app/post_metadata.go b/server/channels/app/post_metadata.go index 06d1123db6..abb930eff0 100644 --- a/server/channels/app/post_metadata.go +++ b/server/channels/app/post_metadata.go @@ -218,9 +218,17 @@ func (a *App) SanitizePostMetadataForUser(c request.CTX, post *model.Post, userI } if previewedChannel != nil && !a.HasPermissionToReadChannel(c, userID, previewedChannel) { + // Remove all permalink embeds and only keep non-permalink embeds. + // We always have only one permalink embed even if the post + // contains multiple permalinks. + var newEmbeds []*model.PostEmbed for _, embed := range post.Metadata.Embeds { - embed.Data = nil + if embed.Type != model.PostEmbedPermalink { + newEmbeds = append(newEmbeds, embed) + } } + + post.Metadata.Embeds = newEmbeds } return post, nil diff --git a/server/channels/app/post_metadata_test.go b/server/channels/app/post_metadata_test.go index e0b87daa49..e664e04b41 100644 --- a/server/channels/app/post_metadata_test.go +++ b/server/channels/app/post_metadata_test.go @@ -2868,3 +2868,60 @@ func TestSanitizePostMetaDataForAudit(t *testing.T) { } } } + +func TestSanitizePostMetadataForUser(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + privateChannel, err := th.App.CreateChannel(th.Context, &model.Channel{ + Name: "private_chanenl", + Type: model.ChannelTypePrivate, + TeamId: th.BasicTeam.Id, + CreatorId: th.SystemAdminUser.Id, + }, true) + + require.Nil(t, err) + require.NotEmpty(t, privateChannel.Id) + + post := &model.Post{ + Id: "post_id_1", + UserId: th.BasicUser.Id, + Metadata: &model.PostMetadata{ + Embeds: []*model.PostEmbed{ + { + Type: model.PostEmbedPermalink, + Data: &model.PreviewPost{ + PostID: "permalink_post_id", + Post: &model.Post{ + Id: "permalink_post_id", + Message: "permalink post message", + ChannelId: privateChannel.Id, + }, + }, + }, + { + Type: model.PostEmbedPermalink, + Data: &model.PreviewPost{ + PostID: "permalink_post_id_2", + Post: &model.Post{ + Id: "permalink_post_id_2", + Message: "permalink post message 2", + ChannelId: privateChannel.Id, + }, + }, + }, + { + Type: model.PostEmbedLink, + URL: "https://mattermost.com", + }, + }, + }, + } + + sanitizedPost, err := th.App.SanitizePostMetadataForUser(th.Context, post, th.BasicUser.Id) + require.Nil(t, err) + require.NotNil(t, sanitizedPost) + + require.Equal(t, 1, len(sanitizedPost.Metadata.Embeds)) + require.Equal(t, model.PostEmbedLink, sanitizedPost.Metadata.Embeds[0].Type) +} diff --git a/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/posts.ts b/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/posts.ts index b1e4134695..e42c212c45 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/posts.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/posts.ts @@ -218,14 +218,7 @@ export function handlePosts(state: RelationOneToOne = {}, action: Ge const newEmbeds: PostEmbed[] = []; for (const embed of otherPost.metadata.embeds) { - if (embed.type === 'permalink' && embed.data && !('post_id' in embed.data)) { - // eslint-disable-next-line no-console - console.error('post_id missing in post embed data for permalink.'); - // eslint-disable-next-line no-console - console.error(embed.data); - } - - if (embed.type === 'permalink' && (embed.data as PostPreviewMetadata).post_id === post.id) { + if (embed.type === 'permalink' && embed.data && (embed.data as PostPreviewMetadata).post_id === post.id) { // skip if the embed is the deleted post continue; }