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,87 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package platform
import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
)
type BroadcastHook interface {
// Process takes a WebSocket event and modifies it in some way. It is passed a HookedWebSocketEvent which allows
// safe modification of the event.
Process(msg *HookedWebSocketEvent, webConn *WebConn, args map[string]any) error
}
func (h *Hub) runBroadcastHooks(msg *model.WebSocketEvent, webConn *WebConn, hookIDs []string, hookArgs []map[string]any) *model.WebSocketEvent {
if len(hookIDs) == 0 {
return msg
}
hookedEvent := MakeHookedWebSocketEvent(msg)
for i, hookID := range hookIDs {
hook := h.broadcastHooks[hookID]
args := hookArgs[i]
if hook == nil {
mlog.Warn("runBroadcastHooks: Unable to find broadcast hook", mlog.String("hook_id", hookID))
continue
}
hook.Process(hookedEvent, webConn, args)
}
return hookedEvent.Event()
}
// HookedWebSocketEvent is a wrapper for model.WebSocketEvent that is intended to provide a similar interface, except
// it ensures the original WebSocket event is not modified.
type HookedWebSocketEvent struct {
original *model.WebSocketEvent
copy *model.WebSocketEvent
}
func MakeHookedWebSocketEvent(event *model.WebSocketEvent) *HookedWebSocketEvent {
return &HookedWebSocketEvent{
original: event,
}
}
func (he *HookedWebSocketEvent) Add(key string, value any) {
he.copyIfNecessary()
he.copy.Add(key, value)
}
func (he *HookedWebSocketEvent) EventType() string {
if he.copy == nil {
return he.original.EventType()
}
return he.copy.EventType()
}
// Get returns a value from the WebSocket event data. You should never mutate a value returned by this method.
func (he *HookedWebSocketEvent) Get(key string) any {
if he.copy == nil {
return he.original.GetData()[key]
}
return he.copy.GetData()[key]
}
// copyIfNecessary should be called by any mutative method to ensure that the copy is instantiated.
func (he *HookedWebSocketEvent) copyIfNecessary() {
if he.copy == nil {
he.copy = he.original.RemovePrecomputedJSON()
}
}
func (he *HookedWebSocketEvent) Event() *model.WebSocketEvent {
if he.copy == nil {
return he.original
}
return he.copy
}