websocket_message.go 4.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"encoding/json"
	"io"
)

const (
	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"
17
	WEBSOCKET_EVENT_CHANNEL_CREATED    = "channel_created"
18
	WEBSOCKET_EVENT_DIRECT_ADDED       = "direct_added"
19
	WEBSOCKET_EVENT_GROUP_ADDED        = "group_added"
20 21
	WEBSOCKET_EVENT_NEW_USER           = "new_user"
	WEBSOCKET_EVENT_LEAVE_TEAM         = "leave_team"
enahum's avatar
enahum committed
22
	WEBSOCKET_EVENT_UPDATE_TEAM        = "update_team"
23
	WEBSOCKET_EVENT_USER_ADDED         = "user_added"
24
	WEBSOCKET_EVENT_USER_UPDATED       = "user_updated"
25 26 27 28
	WEBSOCKET_EVENT_USER_REMOVED       = "user_removed"
	WEBSOCKET_EVENT_PREFERENCE_CHANGED = "preference_changed"
	WEBSOCKET_EVENT_EPHEMERAL_MESSAGE  = "ephemeral_message"
	WEBSOCKET_EVENT_STATUS_CHANGE      = "status_change"
29
	WEBSOCKET_EVENT_HELLO              = "hello"
30
	WEBSOCKET_EVENT_WEBRTC             = "webrtc"
31
	WEBSOCKET_AUTHENTICATION_CHALLENGE = "authentication_challenge"
32 33
	WEBSOCKET_EVENT_REACTION_ADDED     = "reaction_added"
	WEBSOCKET_EVENT_REACTION_REMOVED   = "reaction_removed"
34 35 36 37 38
)

type WebSocketMessage interface {
	ToJson() string
	IsValid() bool
39 40
	DoPreComputeJson()
	GetPreComputeJson() []byte
41
	EventType() string
42 43
}

44
type WebsocketBroadcast struct {
enahum's avatar
enahum committed
45
	OmitUsers map[string]bool `json:"omit_users"` // broadcast is omitted for users listed here
46 47 48 49 50
	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
}

51
type WebSocketEvent struct {
52 53 54 55
	Event          string                 `json:"event"`
	Data           map[string]interface{} `json:"data"`
	Broadcast      *WebsocketBroadcast    `json:"broadcast"`
	PreComputeJson []byte                 `json:"-"`
56 57 58 59 60 61
}

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

62 63 64
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}}
65 66 67 68 69 70
}

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

71 72 73 74
func (o *WebSocketEvent) EventType() string {
	return o.Event
}

75 76 77 78 79 80 81 82 83 84 85 86 87
func (o *WebSocketEvent) DoPreComputeJson() {
	b, err := json.Marshal(o)
	if err != nil {
		o.PreComputeJson = []byte("")
	} else {
		o.PreComputeJson = b
	}
}

func (o *WebSocketEvent) GetPreComputeJson() []byte {
	return o.PreComputeJson
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
func (o *WebSocketEvent) ToJson() string {
	b, err := json.Marshal(o)
	if err != nil {
		return ""
	} else {
		return string(b)
	}
}

func WebSocketEventFromJson(data io.Reader) *WebSocketEvent {
	decoder := json.NewDecoder(data)
	var o WebSocketEvent
	err := decoder.Decode(&o)
	if err == nil {
		return &o
	} else {
		return nil
	}
}

type WebSocketResponse struct {
109 110 111 112 113
	Status         string                 `json:"status"`
	SeqReply       int64                  `json:"seq_reply,omitempty"`
	Data           map[string]interface{} `json:"data,omitempty"`
	Error          *AppError              `json:"error,omitempty"`
	PreComputeJson []byte                 `json:"-"`
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}

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 != ""
}

132 133 134 135
func (o *WebSocketResponse) EventType() string {
	return ""
}

136 137 138 139 140 141 142 143 144
func (o *WebSocketResponse) ToJson() string {
	b, err := json.Marshal(o)
	if err != nil {
		return ""
	} else {
		return string(b)
	}
}

145 146 147 148 149 150 151 152 153 154 155 156 157
func (o *WebSocketResponse) DoPreComputeJson() {
	b, err := json.Marshal(o)
	if err != nil {
		o.PreComputeJson = []byte("")
	} else {
		o.PreComputeJson = b
	}
}

func (o *WebSocketResponse) GetPreComputeJson() []byte {
	return o.PreComputeJson
}

158 159 160 161 162 163 164 165 166 167
func WebSocketResponseFromJson(data io.Reader) *WebSocketResponse {
	decoder := json.NewDecoder(data)
	var o WebSocketResponse
	err := decoder.Decode(&o)
	if err == nil {
		return &o
	} else {
		return nil
	}
}