websocket_message.go 4.55 KB
Newer Older
1
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11
// See License.txt for license information.

package model

import (
	"encoding/json"
	"io"
)

const (
12 13 14 15 16 17
	WEBSOCKET_EVENT_TYPING              = "typing"
	WEBSOCKET_EVENT_POSTED              = "posted"
	WEBSOCKET_EVENT_POST_EDITED         = "post_edited"
	WEBSOCKET_EVENT_POST_DELETED        = "post_deleted"
	WEBSOCKET_EVENT_CHANNEL_DELETED     = "channel_deleted"
	WEBSOCKET_EVENT_CHANNEL_CREATED     = "channel_created"
18
	WEBSOCKET_EVENT_CHANNEL_UPDATED     = "channel_updated"
19 20 21 22 23 24
	WEBSOCKET_EVENT_DIRECT_ADDED        = "direct_added"
	WEBSOCKET_EVENT_GROUP_ADDED         = "group_added"
	WEBSOCKET_EVENT_NEW_USER            = "new_user"
	WEBSOCKET_EVENT_ADDED_TO_TEAM       = "added_to_team"
	WEBSOCKET_EVENT_LEAVE_TEAM          = "leave_team"
	WEBSOCKET_EVENT_UPDATE_TEAM         = "update_team"
25
	WEBSOCKET_EVENT_DELETE_TEAM         = "delete_team"
26 27
	WEBSOCKET_EVENT_USER_ADDED          = "user_added"
	WEBSOCKET_EVENT_USER_UPDATED        = "user_updated"
28
	WEBSOCKET_EVENT_USER_ROLE_UPDATED   = "user_role_updated"
29
	WEBSOCKET_EVENT_MEMBERROLE_UPDATED  = "memberrole_updated"
30 31 32 33 34 35 36 37 38 39 40
	WEBSOCKET_EVENT_USER_REMOVED        = "user_removed"
	WEBSOCKET_EVENT_PREFERENCE_CHANGED  = "preference_changed"
	WEBSOCKET_EVENT_PREFERENCES_CHANGED = "preferences_changed"
	WEBSOCKET_EVENT_PREFERENCES_DELETED = "preferences_deleted"
	WEBSOCKET_EVENT_EPHEMERAL_MESSAGE   = "ephemeral_message"
	WEBSOCKET_EVENT_STATUS_CHANGE       = "status_change"
	WEBSOCKET_EVENT_HELLO               = "hello"
	WEBSOCKET_EVENT_WEBRTC              = "webrtc"
	WEBSOCKET_AUTHENTICATION_CHALLENGE  = "authentication_challenge"
	WEBSOCKET_EVENT_REACTION_ADDED      = "reaction_added"
	WEBSOCKET_EVENT_REACTION_REMOVED    = "reaction_removed"
41
	WEBSOCKET_EVENT_RESPONSE            = "response"
42
	WEBSOCKET_EVENT_EMOJI_ADDED         = "emoji_added"
43
	WEBSOCKET_EVENT_CHANNEL_VIEWED      = "channel_viewed"
44 45
	WEBSOCKET_EVENT_PLUGIN_ACTIVATED    = "plugin_activated"   // EXPERIMENTAL - SUBJECT TO CHANGE
	WEBSOCKET_EVENT_PLUGIN_DEACTIVATED  = "plugin_deactivated" // EXPERIMENTAL - SUBJECT TO CHANGE
46 47 48 49 50
)

type WebSocketMessage interface {
	ToJson() string
	IsValid() bool
51
	EventType() string
52 53
}

54
type WebsocketBroadcast struct {
enahum's avatar
enahum committed
55
	OmitUsers map[string]bool `json:"omit_users"` // broadcast is omitted for users listed here
56 57 58 59 60
	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
}

61
type WebSocketEvent struct {
62 63 64 65
	Event     string                 `json:"event"`
	Data      map[string]interface{} `json:"data"`
	Broadcast *WebsocketBroadcast    `json:"broadcast"`
	Sequence  int64                  `json:"seq"`
66 67 68 69 70 71
}

func (m *WebSocketEvent) Add(key string, value interface{}) {
	m.Data[key] = value
}

72 73 74
func NewWebSocketEvent(event, teamId, channelId, userId string, omitUsers map[string]bool) *WebSocketEvent {
	return &WebSocketEvent{Event: event, Data: make(map[string]interface{}),
		Broadcast: &WebsocketBroadcast{TeamId: teamId, ChannelId: channelId, UserId: userId, OmitUsers: omitUsers}}
75 76 77 78 79 80
}

func (o *WebSocketEvent) IsValid() bool {
	return o.Event != ""
}

81 82 83 84
func (o *WebSocketEvent) EventType() string {
	return o.Event
}

85
func (o *WebSocketEvent) ToJson() string {
Chris's avatar
Chris committed
86 87
	b, _ := json.Marshal(o)
	return string(b)
88 89 90
}

func WebSocketEventFromJson(data io.Reader) *WebSocketEvent {
Chris's avatar
Chris committed
91 92 93
	var o *WebSocketEvent
	json.NewDecoder(data).Decode(&o)
	return o
94 95 96
}

type WebSocketResponse struct {
97 98 99 100
	Status   string                 `json:"status"`
	SeqReply int64                  `json:"seq_reply,omitempty"`
	Data     map[string]interface{} `json:"data,omitempty"`
	Error    *AppError              `json:"error,omitempty"`
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

func (m *WebSocketResponse) Add(key string, value interface{}) {
	m.Data[key] = value
}

func NewWebSocketResponse(status string, seqReply int64, data map[string]interface{}) *WebSocketResponse {
	return &WebSocketResponse{Status: status, SeqReply: seqReply, Data: data}
}

func NewWebSocketError(seqReply int64, err *AppError) *WebSocketResponse {
	return &WebSocketResponse{Status: STATUS_FAIL, SeqReply: seqReply, Error: err}
}

func (o *WebSocketResponse) IsValid() bool {
	return o.Status != ""
}

119
func (o *WebSocketResponse) EventType() string {
120
	return WEBSOCKET_EVENT_RESPONSE
121 122
}

123
func (o *WebSocketResponse) ToJson() string {
Chris's avatar
Chris committed
124 125
	b, _ := json.Marshal(o)
	return string(b)
126 127 128
}

func WebSocketResponseFromJson(data io.Reader) *WebSocketResponse {
Chris's avatar
Chris committed
129 130 131
	var o *WebSocketResponse
	json.NewDecoder(data).Decode(&o)
	return o
132
}