коммит произвёл
Claudio Costa
родитель
f49b5dc440
Коммит
f35b025bea
@@ -2501,8 +2501,8 @@ func TestUpdateChannelMemberSchemeRoles(t *testing.T) {
|
|||||||
for waiting {
|
for waiting {
|
||||||
select {
|
select {
|
||||||
case event := <-WebSocketClient.EventChannel:
|
case event := <-WebSocketClient.EventChannel:
|
||||||
if event.Event == model.WebsocketEventChannelMemberUpdated {
|
if event.EventType() == model.WebsocketEventChannelMemberUpdated {
|
||||||
require.Equal(t, model.WebsocketEventChannelMemberUpdated, event.Event)
|
require.Equal(t, model.WebsocketEventChannelMemberUpdated, event.EventType())
|
||||||
waiting = false
|
waiting = false
|
||||||
}
|
}
|
||||||
case <-timeout:
|
case <-timeout:
|
||||||
|
|||||||
@@ -167,8 +167,8 @@ func TestCreatePost(t *testing.T) {
|
|||||||
for eventsToGo > 0 {
|
for eventsToGo > 0 {
|
||||||
select {
|
select {
|
||||||
case event := <-WebSocketClient.EventChannel:
|
case event := <-WebSocketClient.EventChannel:
|
||||||
if event.Event == model.WebsocketEventEphemeralMessage {
|
if event.EventType() == model.WebsocketEventEphemeralMessage {
|
||||||
require.Equal(t, model.WebsocketEventEphemeralMessage, event.Event)
|
require.Equal(t, model.WebsocketEventEphemeralMessage, event.EventType())
|
||||||
eventsToGo = eventsToGo - 1
|
eventsToGo = eventsToGo - 1
|
||||||
}
|
}
|
||||||
case <-timeout:
|
case <-timeout:
|
||||||
|
|||||||
@@ -256,7 +256,7 @@ func TestWebConnDrainDeadQueue(t *testing.T) {
|
|||||||
_, buf, err = conn.ReadMessage()
|
_, buf, err = conn.ReadMessage()
|
||||||
ev := model.WebSocketEventFromJson(bytes.NewReader(buf))
|
ev := model.WebSocketEventFromJson(bytes.NewReader(buf))
|
||||||
require.LessOrEqual(t, int(i), limit)
|
require.LessOrEqual(t, int(i), limit)
|
||||||
assert.Equal(t, i, ev.Sequence)
|
assert.Equal(t, i, ev.GetSequence())
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
if _, ok := err.(*websocket.CloseError); !ok {
|
if _, ok := err.(*websocket.CloseError); !ok {
|
||||||
|
|||||||
@@ -105,13 +105,11 @@ type webSocketEventJSON struct {
|
|||||||
Sequence int64 `json:"seq"`
|
Sequence int64 `json:"seq"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// **NOTE**: Direct access to WebSocketEvent fields is deprecated. They will be
|
|
||||||
// made unexported in next major version release. Provided getter functions should be used instead.
|
|
||||||
type WebSocketEvent struct {
|
type WebSocketEvent struct {
|
||||||
Event string // Deprecated: use EventType()
|
event string
|
||||||
Data map[string]interface{} // Deprecated: use GetData()
|
data map[string]interface{}
|
||||||
Broadcast *WebsocketBroadcast // Deprecated: use GetBroadcast()
|
broadcast *WebsocketBroadcast
|
||||||
Sequence int64 // Deprecated: use GetSequence()
|
sequence int64
|
||||||
precomputedJSON *precomputedWebSocketEventJSON
|
precomputedJSON *precomputedWebSocketEventJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,9 +117,9 @@ type WebSocketEvent struct {
|
|||||||
// This makes ToJson much more efficient when sending the same event to multiple connections.
|
// This makes ToJson much more efficient when sending the same event to multiple connections.
|
||||||
func (ev *WebSocketEvent) PrecomputeJSON() *WebSocketEvent {
|
func (ev *WebSocketEvent) PrecomputeJSON() *WebSocketEvent {
|
||||||
copy := ev.Copy()
|
copy := ev.Copy()
|
||||||
event, _ := json.Marshal(copy.Event)
|
event, _ := json.Marshal(copy.event)
|
||||||
data, _ := json.Marshal(copy.Data)
|
data, _ := json.Marshal(copy.data)
|
||||||
broadcast, _ := json.Marshal(copy.Broadcast)
|
broadcast, _ := json.Marshal(copy.broadcast)
|
||||||
copy.precomputedJSON = &precomputedWebSocketEventJSON{
|
copy.precomputedJSON = &precomputedWebSocketEventJSON{
|
||||||
Event: json.RawMessage(event),
|
Event: json.RawMessage(event),
|
||||||
Data: json.RawMessage(data),
|
Data: json.RawMessage(data),
|
||||||
@@ -131,78 +129,85 @@ func (ev *WebSocketEvent) PrecomputeJSON() *WebSocketEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) Add(key string, value interface{}) {
|
func (ev *WebSocketEvent) Add(key string, value interface{}) {
|
||||||
ev.Data[key] = value
|
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) *WebSocketEvent {
|
||||||
return &WebSocketEvent{Event: event, Data: make(map[string]interface{}),
|
return &WebSocketEvent{
|
||||||
Broadcast: &WebsocketBroadcast{TeamId: teamId, ChannelId: channelId, UserId: userId, OmitUsers: omitUsers}}
|
event: event,
|
||||||
|
data: make(map[string]interface{}),
|
||||||
|
broadcast: &WebsocketBroadcast{
|
||||||
|
TeamId: teamId,
|
||||||
|
ChannelId: channelId,
|
||||||
|
UserId: userId,
|
||||||
|
OmitUsers: omitUsers},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) Copy() *WebSocketEvent {
|
func (ev *WebSocketEvent) Copy() *WebSocketEvent {
|
||||||
copy := &WebSocketEvent{
|
copy := &WebSocketEvent{
|
||||||
Event: ev.Event,
|
event: ev.event,
|
||||||
Data: ev.Data,
|
data: ev.data,
|
||||||
Broadcast: ev.Broadcast,
|
broadcast: ev.broadcast,
|
||||||
Sequence: ev.Sequence,
|
sequence: ev.sequence,
|
||||||
precomputedJSON: ev.precomputedJSON,
|
precomputedJSON: ev.precomputedJSON,
|
||||||
}
|
}
|
||||||
return copy
|
return copy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) GetData() map[string]interface{} {
|
func (ev *WebSocketEvent) GetData() map[string]interface{} {
|
||||||
return ev.Data
|
return ev.data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) GetBroadcast() *WebsocketBroadcast {
|
func (ev *WebSocketEvent) GetBroadcast() *WebsocketBroadcast {
|
||||||
return ev.Broadcast
|
return ev.broadcast
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) GetSequence() int64 {
|
func (ev *WebSocketEvent) GetSequence() int64 {
|
||||||
return ev.Sequence
|
return ev.sequence
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) SetEvent(event string) *WebSocketEvent {
|
func (ev *WebSocketEvent) SetEvent(event string) *WebSocketEvent {
|
||||||
copy := ev.Copy()
|
copy := ev.Copy()
|
||||||
copy.Event = event
|
copy.event = event
|
||||||
return copy
|
return copy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) SetData(data map[string]interface{}) *WebSocketEvent {
|
func (ev *WebSocketEvent) SetData(data map[string]interface{}) *WebSocketEvent {
|
||||||
copy := ev.Copy()
|
copy := ev.Copy()
|
||||||
copy.Data = data
|
copy.data = data
|
||||||
return copy
|
return copy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) SetBroadcast(broadcast *WebsocketBroadcast) *WebSocketEvent {
|
func (ev *WebSocketEvent) SetBroadcast(broadcast *WebsocketBroadcast) *WebSocketEvent {
|
||||||
copy := ev.Copy()
|
copy := ev.Copy()
|
||||||
copy.Broadcast = broadcast
|
copy.broadcast = broadcast
|
||||||
return copy
|
return copy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) SetSequence(seq int64) *WebSocketEvent {
|
func (ev *WebSocketEvent) SetSequence(seq int64) *WebSocketEvent {
|
||||||
copy := ev.Copy()
|
copy := ev.Copy()
|
||||||
copy.Sequence = seq
|
copy.sequence = seq
|
||||||
return copy
|
return copy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) IsValid() bool {
|
func (ev *WebSocketEvent) IsValid() bool {
|
||||||
return ev.Event != ""
|
return ev.event != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) EventType() string {
|
func (ev *WebSocketEvent) EventType() string {
|
||||||
return ev.Event
|
return ev.event
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ev *WebSocketEvent) ToJson() string {
|
func (ev *WebSocketEvent) ToJson() string {
|
||||||
if ev.precomputedJSON != nil {
|
if ev.precomputedJSON != nil {
|
||||||
return fmt.Sprintf(`{"event": %s, "data": %s, "broadcast": %s, "seq": %d}`, ev.precomputedJSON.Event, ev.precomputedJSON.Data, ev.precomputedJSON.Broadcast, ev.Sequence)
|
return fmt.Sprintf(`{"event": %s, "data": %s, "broadcast": %s, "seq": %d}`, ev.precomputedJSON.Event, ev.precomputedJSON.Data, ev.precomputedJSON.Broadcast, ev.GetSequence())
|
||||||
}
|
}
|
||||||
b, _ := json.Marshal(webSocketEventJSON{
|
b, _ := json.Marshal(webSocketEventJSON{
|
||||||
ev.Event,
|
ev.event,
|
||||||
ev.Data,
|
ev.data,
|
||||||
ev.Broadcast,
|
ev.broadcast,
|
||||||
ev.Sequence,
|
ev.sequence,
|
||||||
})
|
})
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
@@ -211,15 +216,15 @@ func (ev *WebSocketEvent) ToJson() string {
|
|||||||
func (ev *WebSocketEvent) Encode(enc *json.Encoder) error {
|
func (ev *WebSocketEvent) Encode(enc *json.Encoder) error {
|
||||||
if ev.precomputedJSON != nil {
|
if ev.precomputedJSON != nil {
|
||||||
return enc.Encode(json.RawMessage(
|
return enc.Encode(json.RawMessage(
|
||||||
fmt.Sprintf(`{"event": %s, "data": %s, "broadcast": %s, "seq": %d}`, ev.precomputedJSON.Event, ev.precomputedJSON.Data, ev.precomputedJSON.Broadcast, ev.Sequence),
|
fmt.Sprintf(`{"event": %s, "data": %s, "broadcast": %s, "seq": %d}`, ev.precomputedJSON.Event, ev.precomputedJSON.Data, ev.precomputedJSON.Broadcast, ev.sequence),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
return enc.Encode(webSocketEventJSON{
|
return enc.Encode(webSocketEventJSON{
|
||||||
ev.Event,
|
ev.event,
|
||||||
ev.Data,
|
ev.data,
|
||||||
ev.Broadcast,
|
ev.broadcast,
|
||||||
ev.Sequence,
|
ev.sequence,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +234,7 @@ func WebSocketEventFromJson(data io.Reader) *WebSocketEvent {
|
|||||||
if err := json.NewDecoder(data).Decode(&o); err != nil {
|
if err := json.NewDecoder(data).Decode(&o); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ev.Event = o.Event
|
ev.event = o.Event
|
||||||
if u, ok := o.Data["user"]; ok {
|
if u, ok := o.Data["user"]; ok {
|
||||||
// We need to convert to and from JSON again
|
// We need to convert to and from JSON again
|
||||||
// because the user is in the form of a map[string]interface{}.
|
// because the user is in the form of a map[string]interface{}.
|
||||||
@@ -239,9 +244,9 @@ func WebSocketEventFromJson(data io.Reader) *WebSocketEvent {
|
|||||||
}
|
}
|
||||||
o.Data["user"] = UserFromJson(bytes.NewReader(buf))
|
o.Data["user"] = UserFromJson(bytes.NewReader(buf))
|
||||||
}
|
}
|
||||||
ev.Data = o.Data
|
ev.data = o.Data
|
||||||
ev.Broadcast = o.Broadcast
|
ev.broadcast = o.Broadcast
|
||||||
ev.Sequence = o.Sequence
|
ev.sequence = o.Sequence
|
||||||
return &ev
|
return &ev
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,26 +35,23 @@ func TestWebSocketEventImmutable(t *testing.T) {
|
|||||||
if new == m {
|
if new == m {
|
||||||
require.Fail(t, "pointers should not be the same")
|
require.Fail(t, "pointers should not be the same")
|
||||||
}
|
}
|
||||||
require.NotEqual(t, m.Event, new.Event)
|
require.NotEqual(t, m.EventType(), new.EventType())
|
||||||
require.Equal(t, new.Event, "new_event")
|
require.Equal(t, new.EventType(), "new_event")
|
||||||
require.Equal(t, new.Event, new.EventType())
|
|
||||||
|
|
||||||
new = m.SetSequence(45)
|
new = m.SetSequence(45)
|
||||||
if new == m {
|
if new == m {
|
||||||
require.Fail(t, "pointers should not be the same")
|
require.Fail(t, "pointers should not be the same")
|
||||||
}
|
}
|
||||||
require.NotEqual(t, m.Sequence, new.Sequence)
|
require.NotEqual(t, m.GetSequence(), new.GetSequence())
|
||||||
require.Equal(t, new.Sequence, int64(45))
|
require.Equal(t, new.GetSequence(), int64(45))
|
||||||
require.Equal(t, new.Sequence, new.GetSequence())
|
|
||||||
|
|
||||||
broadcast := &WebsocketBroadcast{}
|
broadcast := &WebsocketBroadcast{}
|
||||||
new = m.SetBroadcast(broadcast)
|
new = m.SetBroadcast(broadcast)
|
||||||
if new == m {
|
if new == m {
|
||||||
require.Fail(t, "pointers should not be the same")
|
require.Fail(t, "pointers should not be the same")
|
||||||
}
|
}
|
||||||
require.NotEqual(t, m.Broadcast, new.Broadcast)
|
require.NotEqual(t, m.GetBroadcast(), new.GetBroadcast())
|
||||||
require.Equal(t, new.Broadcast, broadcast)
|
require.Equal(t, new.GetBroadcast(), broadcast)
|
||||||
require.Equal(t, new.Broadcast, new.GetBroadcast())
|
|
||||||
|
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"key": "val",
|
"key": "val",
|
||||||
@@ -65,8 +62,8 @@ func TestWebSocketEventImmutable(t *testing.T) {
|
|||||||
require.Fail(t, "pointers should not be the same")
|
require.Fail(t, "pointers should not be the same")
|
||||||
}
|
}
|
||||||
require.NotEqual(t, m, new)
|
require.NotEqual(t, m, new)
|
||||||
require.Equal(t, new.Data, data)
|
require.Equal(t, new.data, data)
|
||||||
require.Equal(t, new.Data, new.GetData())
|
require.Equal(t, new.data, new.GetData())
|
||||||
|
|
||||||
copy := m.Copy()
|
copy := m.Copy()
|
||||||
if copy == m {
|
if copy == m {
|
||||||
@@ -81,10 +78,10 @@ func TestWebSocketEventFromJson(t *testing.T) {
|
|||||||
data := `{"event": "test", "data": {"key": "val"}, "seq": 45, "broadcast": {"user_id": "userid"}}`
|
data := `{"event": "test", "data": {"key": "val"}, "seq": 45, "broadcast": {"user_id": "userid"}}`
|
||||||
ev = WebSocketEventFromJson(strings.NewReader(data))
|
ev = WebSocketEventFromJson(strings.NewReader(data))
|
||||||
require.NotNil(t, ev, "should have parsed")
|
require.NotNil(t, ev, "should have parsed")
|
||||||
require.Equal(t, ev.Event, "test")
|
require.Equal(t, ev.EventType(), "test")
|
||||||
require.Equal(t, ev.Sequence, int64(45))
|
require.Equal(t, ev.GetSequence(), int64(45))
|
||||||
require.Equal(t, ev.Data, map[string]interface{}{"key": "val"})
|
require.Equal(t, ev.data, map[string]interface{}{"key": "val"})
|
||||||
require.Equal(t, ev.Broadcast, &WebsocketBroadcast{UserId: "userid"})
|
require.Equal(t, ev.GetBroadcast(), &WebsocketBroadcast{UserId: "userid"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWebSocketResponse(t *testing.T) {
|
func TestWebSocketResponse(t *testing.T) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user