Indicate deleteBy in post_deleted websocket event (#15332)

This lets the client know who deleted the post. This information is
stored in the database, but cannot be retrieved from an API endpoint
because the API does not return information about deleted post.

Currently, the information is only sent to system administrators.
Этот коммит содержится в:
Dexter Chua
2020-11-16 18:56:29 +08:00
коммит произвёл GitHub
родитель 277f886b3f
Коммит e162c6c787
2 изменённых файлов: 62 добавлений и 3 удалений

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

@@ -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()

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

@@ -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)