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
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
5e62ba8ccc
Коммит
ef66f7beab
96
server/channels/app/web_broadcast_hooks.go
Обычный файл
96
server/channels/app/web_broadcast_hooks.go
Обычный файл
@@ -0,0 +1,96 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/app/platform"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
broadcastAddMentions = "add_mentions"
|
||||
broadcastAddFollowers = "add_followers"
|
||||
)
|
||||
|
||||
func (s *Server) makeBroadcastHooks() map[string]platform.BroadcastHook {
|
||||
return map[string]platform.BroadcastHook{
|
||||
broadcastAddMentions: &addMentionsBroadcastHook{},
|
||||
broadcastAddFollowers: &addFollowersBroadcastHook{},
|
||||
}
|
||||
}
|
||||
|
||||
type addMentionsBroadcastHook struct{}
|
||||
|
||||
func (h *addMentionsBroadcastHook) Process(msg *platform.HookedWebSocketEvent, webConn *platform.WebConn, args map[string]any) error {
|
||||
mentions, err := getTypedArg[model.StringArray](args, "mentions")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Invalid mentions value passed to addMentionsBroadcastHook")
|
||||
}
|
||||
|
||||
if len(mentions) > 0 && pUtils.Contains[string](mentions, webConn.UserId) {
|
||||
// Note that the client expects this field to be stringified
|
||||
msg.Add("mentions", model.ArrayToJSON([]string{webConn.UserId}))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func useAddMentionsHook(message *model.WebSocketEvent, mentionedUsers model.StringArray) {
|
||||
message.GetBroadcast().AddHook(broadcastAddMentions, map[string]any{
|
||||
"mentions": mentionedUsers,
|
||||
})
|
||||
}
|
||||
|
||||
type addFollowersBroadcastHook struct{}
|
||||
|
||||
func (h *addFollowersBroadcastHook) Process(msg *platform.HookedWebSocketEvent, webConn *platform.WebConn, args map[string]any) error {
|
||||
followers, err := getTypedArg[model.StringArray](args, "followers")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Invalid followers value passed to addFollowersBroadcastHook")
|
||||
}
|
||||
|
||||
if len(followers) > 0 && pUtils.Contains[string](followers, webConn.UserId) {
|
||||
// Note that the client expects this field to be stringified
|
||||
msg.Add("followers", model.ArrayToJSON([]string{webConn.UserId}))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func useAddFollowersHook(message *model.WebSocketEvent, followers model.StringArray) {
|
||||
message.GetBroadcast().AddHook(broadcastAddFollowers, map[string]any{
|
||||
"followers": followers,
|
||||
})
|
||||
}
|
||||
|
||||
// getTypedArg returns a correctly typed hook argument with the given key, reinterpreting the type using JSON encoding
|
||||
// if necessary. This is needed because broadcast hook args are JSON encoded in a multi-server environment, and any
|
||||
// type information is lost because those types aren't known at decode time.
|
||||
func getTypedArg[T any](args map[string]any, key string) (T, error) {
|
||||
var value T
|
||||
|
||||
untyped, ok := args[key]
|
||||
if !ok {
|
||||
return value, fmt.Errorf("No argument found with key: %s", key)
|
||||
}
|
||||
|
||||
// If the value is already correct, just return it
|
||||
if typed, ok := untyped.(T); ok {
|
||||
return typed, nil
|
||||
}
|
||||
|
||||
// Marshal and unmarshal the data with the correct typing information
|
||||
buf, err := json.Marshal(untyped)
|
||||
if err != nil {
|
||||
return value, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(buf, &value)
|
||||
return value, err
|
||||
}
|
||||
Ссылка в новой задаче
Block a user