MM-54238 Add WebSocket broadcast hook and don't broadcast when other users were mentioned (#24641)

* MM-54238 Initial implementation

* MM-54238 Move websocket hook into app package

* MM-54238 Add tests for mentions in posted websocket messages

* Fix styling

* Fix other styling

* Idiomatic ID naming for new code

* Fix more styles

* Separate hooks to add mentions and followers

* Improved error handling for invalid types in hooks

* Rename HasChanges to ShouldProcess

* Pass broadcast hooks through hubStart

* Add test helper for asserting json unmarshaling

* Fix missing arguments in tests

* Ensure broadcast hooks are sent across the cluster and not to users

* Ensure tests actually cover following a post

* Fix code broken by merge

* Go vet again...

* Deep copy event before processing it with hooks

* Replace RemoveBroadcastHooks with WithoutBroadcastHooks

* Address feedback

* Add helper to fix type information for hook args

* Wrap WebSocketEvent and simplify BroadcastHook

* Address feedback

* Address feedback
Этот коммит содержится в:
Harrison Healey
2023-11-08 16:17:07 -05:00
коммит произвёл GitHub
родитель 5e62ba8ccc
Коммит ef66f7beab
12 изменённых файлов: 942 добавлений и 23 удалений

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

@@ -0,0 +1,114 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"testing"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/app/platform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAddMentionsHook_Process(t *testing.T) {
hook := &addMentionsBroadcastHook{}
userID := model.NewId()
otherUserID := model.NewId()
webConn := &platform.WebConn{
UserId: userID,
}
t.Run("should add a mentions entry for the current user", func(t *testing.T) {
msg := platform.MakeHookedWebSocketEvent(model.NewWebSocketEvent(model.WebsocketEventPosted, "", "", "", nil, ""))
require.Nil(t, msg.Event().GetData()["mentions"])
hook.Process(msg, webConn, map[string]any{
"mentions": model.StringArray{userID},
})
assert.Equal(t, `["`+userID+`"]`, msg.Event().GetData()["mentions"])
assert.Nil(t, msg.Event().GetData()["followers"])
})
t.Run("should not add a mentions entry for another user", func(t *testing.T) {
msg := platform.MakeHookedWebSocketEvent(model.NewWebSocketEvent(model.WebsocketEventPosted, "", "", "", nil, ""))
require.Nil(t, msg.Event().GetData()["mentions"])
hook.Process(msg, webConn, map[string]any{
"mentions": model.StringArray{otherUserID},
})
assert.Nil(t, msg.Event().GetData()["mentions"])
})
}
func TestAddFollowersHook_Process(t *testing.T) {
hook := &addFollowersBroadcastHook{}
userID := model.NewId()
otherUserID := model.NewId()
webConn := &platform.WebConn{
UserId: userID,
}
t.Run("should add a followers entry for the current user", func(t *testing.T) {
msg := platform.MakeHookedWebSocketEvent(model.NewWebSocketEvent(model.WebsocketEventPosted, "", "", "", nil, ""))
require.Nil(t, msg.Event().GetData()["followers"])
hook.Process(msg, webConn, map[string]any{
"followers": model.StringArray{userID},
})
assert.Equal(t, `["`+userID+`"]`, msg.Event().GetData()["followers"])
})
t.Run("should not add a followers entry for another user", func(t *testing.T) {
msg := platform.MakeHookedWebSocketEvent(model.NewWebSocketEvent(model.WebsocketEventPosted, "", "", "", nil, ""))
require.Nil(t, msg.Event().GetData()["followers"])
hook.Process(msg, webConn, map[string]any{
"followers": model.StringArray{otherUserID},
})
assert.Nil(t, msg.Event().GetData()["followers"])
})
}
func TestAddMentionsAndAddFollowersHooks(t *testing.T) {
addMentionsHook := &addMentionsBroadcastHook{}
addFollowersHook := &addFollowersBroadcastHook{}
userID := model.NewId()
webConn := &platform.WebConn{
UserId: userID,
}
msg := platform.MakeHookedWebSocketEvent(model.NewWebSocketEvent(model.WebsocketEventPosted, "", "", "", nil, ""))
originalData := msg.Event().GetData()
require.Nil(t, originalData["mentions"])
require.Nil(t, originalData["followers"])
addMentionsHook.Process(msg, webConn, map[string]any{
"mentions": model.StringArray{userID},
})
addFollowersHook.Process(msg, webConn, map[string]any{
"followers": model.StringArray{userID},
})
t.Run("should be able to add both mentions and followers to a single event", func(t *testing.T) {
assert.Equal(t, `["`+userID+`"]`, msg.Event().GetData()["followers"])
assert.Equal(t, `["`+userID+`"]`, msg.Event().GetData()["mentions"])
})
}