github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/websocket_message.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"io"
    10  )
    11  
    12  const (
    13  	WEBSOCKET_EVENT_TYPING                  = "typing"
    14  	WEBSOCKET_EVENT_POSTED                  = "posted"
    15  	WEBSOCKET_EVENT_POST_EDITED             = "post_edited"
    16  	WEBSOCKET_EVENT_POST_DELETED            = "post_deleted"
    17  	WEBSOCKET_EVENT_CHANNEL_CONVERTED       = "channel_converted"
    18  	WEBSOCKET_EVENT_CHANNEL_CREATED         = "channel_created"
    19  	WEBSOCKET_EVENT_CHANNEL_DELETED         = "channel_deleted"
    20  	WEBSOCKET_EVENT_CHANNEL_UPDATED         = "channel_updated"
    21  	WEBSOCKET_EVENT_CHANNEL_MEMBER_UPDATED  = "channel_member_updated"
    22  	WEBSOCKET_EVENT_DIRECT_ADDED            = "direct_added"
    23  	WEBSOCKET_EVENT_GROUP_ADDED             = "group_added"
    24  	WEBSOCKET_EVENT_NEW_USER                = "new_user"
    25  	WEBSOCKET_EVENT_ADDED_TO_TEAM           = "added_to_team"
    26  	WEBSOCKET_EVENT_LEAVE_TEAM              = "leave_team"
    27  	WEBSOCKET_EVENT_UPDATE_TEAM             = "update_team"
    28  	WEBSOCKET_EVENT_DELETE_TEAM             = "delete_team"
    29  	WEBSOCKET_EVENT_RESTORE_TEAM            = "restore_team"
    30  	WEBSOCKET_EVENT_USER_ADDED              = "user_added"
    31  	WEBSOCKET_EVENT_USER_UPDATED            = "user_updated"
    32  	WEBSOCKET_EVENT_USER_ROLE_UPDATED       = "user_role_updated"
    33  	WEBSOCKET_EVENT_MEMBERROLE_UPDATED      = "memberrole_updated"
    34  	WEBSOCKET_EVENT_USER_REMOVED            = "user_removed"
    35  	WEBSOCKET_EVENT_PREFERENCE_CHANGED      = "preference_changed"
    36  	WEBSOCKET_EVENT_PREFERENCES_CHANGED     = "preferences_changed"
    37  	WEBSOCKET_EVENT_PREFERENCES_DELETED     = "preferences_deleted"
    38  	WEBSOCKET_EVENT_EPHEMERAL_MESSAGE       = "ephemeral_message"
    39  	WEBSOCKET_EVENT_STATUS_CHANGE           = "status_change"
    40  	WEBSOCKET_EVENT_HELLO                   = "hello"
    41  	WEBSOCKET_AUTHENTICATION_CHALLENGE      = "authentication_challenge"
    42  	WEBSOCKET_EVENT_REACTION_ADDED          = "reaction_added"
    43  	WEBSOCKET_EVENT_REACTION_REMOVED        = "reaction_removed"
    44  	WEBSOCKET_EVENT_RESPONSE                = "response"
    45  	WEBSOCKET_EVENT_EMOJI_ADDED             = "emoji_added"
    46  	WEBSOCKET_EVENT_CHANNEL_VIEWED          = "channel_viewed"
    47  	WEBSOCKET_EVENT_PLUGIN_STATUSES_CHANGED = "plugin_statuses_changed"
    48  	WEBSOCKET_EVENT_PLUGIN_ENABLED          = "plugin_enabled"
    49  	WEBSOCKET_EVENT_PLUGIN_DISABLED         = "plugin_disabled"
    50  	WEBSOCKET_EVENT_ROLE_UPDATED            = "role_updated"
    51  	WEBSOCKET_EVENT_LICENSE_CHANGED         = "license_changed"
    52  	WEBSOCKET_EVENT_CONFIG_CHANGED          = "config_changed"
    53  	WEBSOCKET_EVENT_OPEN_DIALOG             = "open_dialog"
    54  )
    55  
    56  type WebSocketMessage interface {
    57  	ToJson() string
    58  	IsValid() bool
    59  	EventType() string
    60  }
    61  
    62  type WebsocketBroadcast struct {
    63  	OmitUsers             map[string]bool `json:"omit_users"` // broadcast is omitted for users listed here
    64  	UserId                string          `json:"user_id"`    // broadcast only occurs for this user
    65  	ChannelId             string          `json:"channel_id"` // broadcast only occurs for users in this channel
    66  	TeamId                string          `json:"team_id"`    // broadcast only occurs for users in this team
    67  	ContainsSanitizedData bool            `json:"-"`
    68  	ContainsSensitiveData bool            `json:"-"`
    69  }
    70  
    71  type precomputedWebSocketEventJSON struct {
    72  	Event     json.RawMessage
    73  	Data      json.RawMessage
    74  	Broadcast json.RawMessage
    75  }
    76  
    77  type WebSocketEvent struct {
    78  	Event     string                 `json:"event"`
    79  	Data      map[string]interface{} `json:"data"`
    80  	Broadcast *WebsocketBroadcast    `json:"broadcast"`
    81  	Sequence  int64                  `json:"seq"`
    82  
    83  	precomputedJSON *precomputedWebSocketEventJSON
    84  }
    85  
    86  // PrecomputeJSON precomputes and stores the serialized JSON for all fields other than Sequence.
    87  // This makes ToJson much more efficient when sending the same event to multiple connections.
    88  func (m *WebSocketEvent) PrecomputeJSON() {
    89  	event, _ := json.Marshal(m.Event)
    90  	data, _ := json.Marshal(m.Data)
    91  	broadcast, _ := json.Marshal(m.Broadcast)
    92  	m.precomputedJSON = &precomputedWebSocketEventJSON{
    93  		Event:     json.RawMessage(event),
    94  		Data:      json.RawMessage(data),
    95  		Broadcast: json.RawMessage(broadcast),
    96  	}
    97  }
    98  
    99  func (m *WebSocketEvent) Add(key string, value interface{}) {
   100  	m.Data[key] = value
   101  }
   102  
   103  func NewWebSocketEvent(event, teamId, channelId, userId string, omitUsers map[string]bool) *WebSocketEvent {
   104  	return &WebSocketEvent{Event: event, Data: make(map[string]interface{}),
   105  		Broadcast: &WebsocketBroadcast{TeamId: teamId, ChannelId: channelId, UserId: userId, OmitUsers: omitUsers}}
   106  }
   107  
   108  func (o *WebSocketEvent) IsValid() bool {
   109  	return o.Event != ""
   110  }
   111  
   112  func (o *WebSocketEvent) EventType() string {
   113  	return o.Event
   114  }
   115  
   116  func (o *WebSocketEvent) ToJson() string {
   117  	if o.precomputedJSON != nil {
   118  		return fmt.Sprintf(`{"event": %s, "data": %s, "broadcast": %s, "seq": %d}`, o.precomputedJSON.Event, o.precomputedJSON.Data, o.precomputedJSON.Broadcast, o.Sequence)
   119  	}
   120  	b, _ := json.Marshal(o)
   121  	return string(b)
   122  }
   123  
   124  func WebSocketEventFromJson(data io.Reader) *WebSocketEvent {
   125  	var o *WebSocketEvent
   126  	json.NewDecoder(data).Decode(&o)
   127  	return o
   128  }
   129  
   130  type WebSocketResponse struct {
   131  	Status   string                 `json:"status"`
   132  	SeqReply int64                  `json:"seq_reply,omitempty"`
   133  	Data     map[string]interface{} `json:"data,omitempty"`
   134  	Error    *AppError              `json:"error,omitempty"`
   135  }
   136  
   137  func (m *WebSocketResponse) Add(key string, value interface{}) {
   138  	m.Data[key] = value
   139  }
   140  
   141  func NewWebSocketResponse(status string, seqReply int64, data map[string]interface{}) *WebSocketResponse {
   142  	return &WebSocketResponse{Status: status, SeqReply: seqReply, Data: data}
   143  }
   144  
   145  func NewWebSocketError(seqReply int64, err *AppError) *WebSocketResponse {
   146  	return &WebSocketResponse{Status: STATUS_FAIL, SeqReply: seqReply, Error: err}
   147  }
   148  
   149  func (o *WebSocketResponse) IsValid() bool {
   150  	return o.Status != ""
   151  }
   152  
   153  func (o *WebSocketResponse) EventType() string {
   154  	return WEBSOCKET_EVENT_RESPONSE
   155  }
   156  
   157  func (o *WebSocketResponse) ToJson() string {
   158  	b, _ := json.Marshal(o)
   159  	return string(b)
   160  }
   161  
   162  func WebSocketResponseFromJson(data io.Reader) *WebSocketResponse {
   163  	var o *WebSocketResponse
   164  	json.NewDecoder(data).Decode(&o)
   165  	return o
   166  }