Adds OmitConnection parameter to broadcast (#20723)

* Adds OmitConnection parameter to broadcast

Currently we have no means to omit sending a websocket event to a
specific connection id.
This is needed mainly so that the initiator won't receive an event for
the action it just initiated.
Will be used for the global drafts feature, so that we won't update
drafts through ws when a user is typing.

This commit adds OmitConnection to the Broadcast struct and to the
NewWebSocketEvent function signature.
shouldSendEvent should return false for that specific connection.

* Return early only if connection id matches the omitted

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Kyriakos Z
2022-09-02 13:17:22 +03:00
коммит произвёл GitHub
родитель 501e1bf876
Коммит c11ad8995f
32 изменённых файлов: 125 добавлений и 116 удалений

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

@@ -86,11 +86,12 @@ type WebSocketMessage interface {
}
type WebsocketBroadcast struct {
OmitUsers map[string]bool `json:"omit_users"` // broadcast is omitted for users listed here
UserId string `json:"user_id"` // broadcast only occurs for this user
ChannelId string `json:"channel_id"` // broadcast only occurs for users in this channel
TeamId string `json:"team_id"` // broadcast only occurs for users in this team
ConnectionId string `json:"connection_id"` // broadcast only occurs for this connection
OmitUsers map[string]bool `json:"omit_users"` // broadcast is omitted for users listed here
UserId string `json:"user_id"` // broadcast only occurs for this user
ChannelId string `json:"channel_id"` // broadcast only occurs for users in this channel
TeamId string `json:"team_id"` // broadcast only occurs for users in this team
ConnectionId string `json:"connection_id"` // broadcast only occurs for this connection
OmitConnectionId string `json:"omit_connection_id"` // broadcast is omitted for this connection
ContainsSanitizedData bool `json:"-"`
ContainsSensitiveData bool `json:"-"`
// ReliableClusterSend indicates whether or not the message should
@@ -113,6 +114,7 @@ func (wb *WebsocketBroadcast) copy() *WebsocketBroadcast {
c.UserId = wb.UserId
c.ChannelId = wb.ChannelId
c.TeamId = wb.TeamId
c.OmitConnectionId = wb.OmitConnectionId
c.ContainsSanitizedData = wb.ContainsSanitizedData
c.ContainsSensitiveData = wb.ContainsSensitiveData
@@ -185,15 +187,16 @@ func (ev *WebSocketEvent) Add(key string, value any) {
ev.data[key] = value
}
func NewWebSocketEvent(event, teamId, channelId, userId string, omitUsers map[string]bool) *WebSocketEvent {
func NewWebSocketEvent(event, teamId, channelId, userId string, omitUsers map[string]bool, omitConnectionId string) *WebSocketEvent {
return &WebSocketEvent{
event: event,
data: make(map[string]any),
broadcast: &WebsocketBroadcast{
TeamId: teamId,
ChannelId: channelId,
UserId: userId,
OmitUsers: omitUsers},
TeamId: teamId,
ChannelId: channelId,
UserId: userId,
OmitUsers: omitUsers,
OmitConnectionId: omitConnectionId},
}
}

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

@@ -13,7 +13,7 @@ import (
func TestWebSocketEvent(t *testing.T) {
userId := NewId()
m := NewWebSocketEvent("some_event", NewId(), NewId(), userId, nil)
m := NewWebSocketEvent("some_event", NewId(), NewId(), userId, nil, "")
m.Add("RootId", NewId())
user := &User{
Id: userId,
@@ -32,7 +32,7 @@ func TestWebSocketEvent(t *testing.T) {
}
func TestWebSocketEventImmutable(t *testing.T) {
m := NewWebSocketEvent("some_event", NewId(), NewId(), NewId(), nil)
m := NewWebSocketEvent("some_event", NewId(), NewId(), NewId(), nil, "")
new := m.SetEvent("new_event")
if new == m {
@@ -111,7 +111,7 @@ func TestWebSocketResponse(t *testing.T) {
}
func TestWebSocketEvent_PrecomputeJSON(t *testing.T) {
event := NewWebSocketEvent(WebsocketEventPosted, "foo", "bar", "baz", nil)
event := NewWebSocketEvent(WebsocketEventPosted, "foo", "bar", "baz", nil, "")
event = event.SetSequence(7)
before, err := event.ToJSON()
@@ -126,7 +126,7 @@ func TestWebSocketEvent_PrecomputeJSON(t *testing.T) {
var stringSink []byte
func BenchmarkWebSocketEvent_ToJSON(b *testing.B) {
event := NewWebSocketEvent(WebsocketEventPosted, "foo", "bar", "baz", nil)
event := NewWebSocketEvent(WebsocketEventPosted, "foo", "bar", "baz", nil, "")
for i := 0; i < 100; i++ {
event.GetData()[NewId()] = NewId()
}
@@ -217,7 +217,7 @@ func TestWebSocketEventDeepCopy(t *testing.T) {
ContainsSensitiveData: true,
}
ev := NewWebSocketEvent("test", "team", "channel", "user", omitUsers)
ev := NewWebSocketEvent("test", "team", "channel", "user", omitUsers, "")
ev.Add("post", &Post{})
ev.SetBroadcast(broadcast)