diff --git a/api4/post_test.go b/api4/post_test.go index f937d8affe..614d7d4f08 100644 --- a/api4/post_test.go +++ b/api4/post_test.go @@ -1993,6 +1993,56 @@ func TestDeletePost(t *testing.T) { CheckNoError(t, resp) } +func TestDeletePostMessage(t *testing.T) { + th := Setup(t).InitBasic() + th.LinkUserToTeam(th.SystemAdminUser, th.BasicTeam) + th.App.AddUserToChannel(th.SystemAdminUser, th.BasicChannel) + + defer th.TearDown() + + testCases := []struct { + description string + client *model.Client4 + delete_by interface{} + }{ + {"Do not send delete_by to regular user", th.Client, nil}, + {"Send delete_by to system admin user", th.SystemAdminClient, th.SystemAdminUser.Id}, + } + + for _, tc := range testCases { + t.Run(tc.description, func(t *testing.T) { + wsClient, err := th.CreateWebSocketClientWithClient(tc.client) + require.Nil(t, err) + defer wsClient.Close() + + wsClient.Listen() + + post := th.CreatePost() + + status, resp := th.SystemAdminClient.DeletePost(post.Id) + require.True(t, status, "post should return status OK") + CheckNoError(t, resp) + + timeout := time.After(5 * time.Second) + + for { + select { + case ev := <-wsClient.EventChannel: + if ev.EventType() == model.WEBSOCKET_EVENT_POST_DELETED { + assert.Equal(t, tc.delete_by, ev.GetData()["delete_by"]) + return + } + case <-timeout: + // We just skip the test instead of failing because waiting for more than 5 seconds + // to get a response does not make sense, and it will unncessarily slow down + // the tests further in an already congested CI environment. + t.Skip("timed out waiting for event") + } + } + }) + } +} + func TestGetPostThread(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() diff --git a/app/post.go b/app/post.go index 5175db6a98..5b19ec16af 100644 --- a/app/post.go +++ b/app/post.go @@ -1041,9 +1041,18 @@ func (a *App) DeletePost(postId, deleteByID string) (*model.Post, *model.AppErro } } - message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_DELETED, "", post.ChannelId, "", nil) - message.Add("post", a.PreparePostForClient(post, false, false).ToJson()) - a.Publish(message) + postData := a.PreparePostForClient(post, false, false).ToJson() + + userMessage := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_DELETED, "", post.ChannelId, "", nil) + userMessage.Add("post", postData) + userMessage.GetBroadcast().ContainsSanitizedData = true + a.Publish(userMessage) + + adminMessage := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_DELETED, "", post.ChannelId, "", nil) + adminMessage.Add("post", postData) + adminMessage.Add("delete_by", deleteByID) + adminMessage.GetBroadcast().ContainsSensitiveData = true + a.Publish(adminMessage) a.Srv().Go(func() { a.DeletePostFiles(post)