File size: 19,873 Bytes
13555f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//go:generate mockgen -destination=mocks/mockpluginapi.go -package mocks github.com/mattermost/mattermost-server/v6/plugin API
package ws

import (
	"fmt"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"github.com/mattermost/focalboard/server/auth"
	"github.com/mattermost/focalboard/server/model"
	"github.com/mattermost/focalboard/server/utils"

	mmModel "github.com/mattermost/mattermost/server/public/model"
	"github.com/mattermost/mattermost/server/public/shared/mlog"
)

const websocketMessagePrefix = "custom_focalboard_"

var errMissingTeamInCommand = fmt.Errorf("command doesn't contain teamId")

type PluginAdapterInterface interface {
	Adapter
	OnWebSocketConnect(webConnID, userID string)
	OnWebSocketDisconnect(webConnID, userID string)
	WebSocketMessageHasBeenPosted(webConnID, userID string, req *mmModel.WebSocketRequest)
	BroadcastConfigChange(clientConfig model.ClientConfig)
	BroadcastBlockChange(teamID string, block *model.Block)
	BroadcastBlockDelete(teamID, blockID, parentID string)
	BroadcastSubscriptionChange(teamID string, subscription *model.Subscription)
	BroadcastCardLimitTimestampChange(cardLimitTimestamp int64)
	HandleClusterEvent(ev mmModel.PluginClusterEvent)
}

type PluginAdapter struct {
	api            servicesAPI
	auth           auth.AuthInterface
	staleThreshold time.Duration
	store          Store
	logger         mlog.LoggerIFace

	listenersMU       sync.RWMutex
	listeners         map[string]*PluginAdapterClient
	listenersByUserID map[string][]*PluginAdapterClient

	subscriptionsMU  sync.RWMutex
	listenersByTeam  map[string][]*PluginAdapterClient
	listenersByBlock map[string][]*PluginAdapterClient
}

// servicesAPI is the interface required by the PluginAdapter to interact with
// the mattermost-server.
type servicesAPI interface {
	PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *mmModel.WebsocketBroadcast)
	PublishPluginClusterEvent(ev mmModel.PluginClusterEvent, opts mmModel.PluginClusterEventSendOptions) error
}

func NewPluginAdapter(api servicesAPI, auth auth.AuthInterface, store Store, logger mlog.LoggerIFace) *PluginAdapter {
	return &PluginAdapter{
		api:               api,
		auth:              auth,
		store:             store,
		staleThreshold:    5 * time.Minute,
		logger:            logger,
		listeners:         make(map[string]*PluginAdapterClient),
		listenersByUserID: make(map[string][]*PluginAdapterClient),
		listenersByTeam:   make(map[string][]*PluginAdapterClient),
		listenersByBlock:  make(map[string][]*PluginAdapterClient),
		listenersMU:       sync.RWMutex{},
		subscriptionsMU:   sync.RWMutex{},
	}
}

func (pa *PluginAdapter) GetListenerByWebConnID(webConnID string) (pac *PluginAdapterClient, ok bool) {
	pa.listenersMU.RLock()
	defer pa.listenersMU.RUnlock()

	pac, ok = pa.listeners[webConnID]
	return
}

func (pa *PluginAdapter) GetListenersByUserID(userID string) []*PluginAdapterClient {
	pa.listenersMU.RLock()
	defer pa.listenersMU.RUnlock()

	return pa.listenersByUserID[userID]
}

func (pa *PluginAdapter) GetListenersByTeam(teamID string) []*PluginAdapterClient {
	pa.subscriptionsMU.RLock()
	defer pa.subscriptionsMU.RUnlock()

	return pa.listenersByTeam[teamID]
}

func (pa *PluginAdapter) GetListenersByBlock(blockID string) []*PluginAdapterClient {
	pa.subscriptionsMU.RLock()
	defer pa.subscriptionsMU.RUnlock()

	return pa.listenersByBlock[blockID]
}

func (pa *PluginAdapter) addListener(pac *PluginAdapterClient) {
	pa.listenersMU.Lock()
	defer pa.listenersMU.Unlock()

	pa.listeners[pac.webConnID] = pac
	pa.listenersByUserID[pac.userID] = append(pa.listenersByUserID[pac.userID], pac)
}

func (pa *PluginAdapter) removeListener(pac *PluginAdapterClient) {
	pa.listenersMU.Lock()
	defer pa.listenersMU.Unlock()

	// team subscriptions
	for _, team := range pac.teams {
		pa.removeListenerFromTeam(pac, team)
	}

	// block subscriptions
	for _, block := range pac.blocks {
		pa.removeListenerFromBlock(pac, block)
	}

	// user ID list
	newUserListeners := []*PluginAdapterClient{}
	for _, listener := range pa.listenersByUserID[pac.userID] {
		if listener.webConnID != pac.webConnID {
			newUserListeners = append(newUserListeners, listener)
		}
	}
	pa.listenersByUserID[pac.userID] = newUserListeners

	delete(pa.listeners, pac.webConnID)
}

func (pa *PluginAdapter) removeExpiredForUserID(userID string) {
	for _, pac := range pa.GetListenersByUserID(userID) {
		if !pac.isActive() && pac.hasExpired(pa.staleThreshold) {
			pa.removeListener(pac)
		}
	}
}

func (pa *PluginAdapter) removeListenerFromTeam(pac *PluginAdapterClient, teamID string) {
	newTeamListeners := []*PluginAdapterClient{}
	for _, listener := range pa.GetListenersByTeam(teamID) {
		if listener.webConnID != pac.webConnID {
			newTeamListeners = append(newTeamListeners, listener)
		}
	}
	pa.subscriptionsMU.Lock()
	pa.listenersByTeam[teamID] = newTeamListeners
	pa.subscriptionsMU.Unlock()

	pac.unsubscribeFromTeam(teamID)
}

func (pa *PluginAdapter) removeListenerFromBlock(pac *PluginAdapterClient, blockID string) {
	newBlockListeners := []*PluginAdapterClient{}
	for _, listener := range pa.GetListenersByBlock(blockID) {
		if listener.webConnID != pac.webConnID {
			newBlockListeners = append(newBlockListeners, listener)
		}
	}
	pa.subscriptionsMU.Lock()
	pa.listenersByBlock[blockID] = newBlockListeners
	pa.subscriptionsMU.Unlock()

	pac.unsubscribeFromBlock(blockID)
}

func (pa *PluginAdapter) subscribeListenerToTeam(pac *PluginAdapterClient, teamID string) {
	if pac.isSubscribedToTeam(teamID) {
		return
	}

	pa.subscriptionsMU.Lock()
	pa.listenersByTeam[teamID] = append(pa.listenersByTeam[teamID], pac)
	pa.subscriptionsMU.Unlock()

	pac.subscribeToTeam(teamID)
}

func (pa *PluginAdapter) unsubscribeListenerFromTeam(pac *PluginAdapterClient, teamID string) {
	if !pac.isSubscribedToTeam(teamID) {
		return
	}

	pa.removeListenerFromTeam(pac, teamID)
}

func (pa *PluginAdapter) getUserIDsForTeam(teamID string) []string {
	userMap := map[string]bool{}
	for _, pac := range pa.GetListenersByTeam(teamID) {
		if pac.isActive() {
			userMap[pac.userID] = true
		}
	}

	userIDs := []string{}
	for userID := range userMap {
		if pa.auth.DoesUserHaveTeamAccess(userID, teamID) {
			userIDs = append(userIDs, userID)
		}
	}

	return userIDs
}

func (pa *PluginAdapter) getUserIDsForTeamAndBoard(teamID, boardID string, ensureUserIDs ...string) []string {
	userMap := map[string]bool{}
	for _, pac := range pa.GetListenersByTeam(teamID) {
		if pac.isActive() {
			userMap[pac.userID] = true
		}
	}

	members, err := pa.store.GetMembersForBoard(boardID)
	if err != nil {
		pa.logger.Error("error getting members for board",
			mlog.String("method", "getUserIDsForTeamAndBoard"),
			mlog.String("teamID", teamID),
			mlog.String("boardID", boardID),
		)
		return nil
	}

	// the list of users would be the intersection between the ones
	// that are connected to the team and the board members that need
	// to see the updates
	userIDs := []string{}
	for _, member := range members {
		for userID := range userMap {
			if userID == member.UserID && pa.auth.DoesUserHaveTeamAccess(userID, teamID) {
				userIDs = append(userIDs, userID)
			}
		}
	}

	// if we don't have to make sure that some IDs are included, we
	// can return at this point
	if len(ensureUserIDs) == 0 {
		return userIDs
	}

	completeUserMap := map[string]bool{}
	for _, id := range userIDs {
		completeUserMap[id] = true
	}
	for _, id := range ensureUserIDs {
		completeUserMap[id] = true
	}

	completeUserIDs := []string{}
	for id := range completeUserMap {
		completeUserIDs = append(completeUserIDs, id)
	}

	return completeUserIDs
}

//nolint:unused
func (pa *PluginAdapter) unsubscribeListenerFromBlocks(pac *PluginAdapterClient, blockIDs []string) {
	for _, blockID := range blockIDs {
		if pac.isSubscribedToBlock(blockID) {
			pa.removeListenerFromBlock(pac, blockID)
		}
	}
}

func (pa *PluginAdapter) OnWebSocketConnect(webConnID, userID string) {
	if existingPAC, ok := pa.GetListenerByWebConnID(webConnID); ok {
		pa.logger.Debug("inactive connection found for webconn, reusing",
			mlog.String("webConnID", webConnID),
			mlog.String("userID", userID),
		)
		atomic.StoreInt64(&existingPAC.inactiveAt, 0)
		return
	}

	newPAC := &PluginAdapterClient{
		inactiveAt: 0,
		webConnID:  webConnID,
		userID:     userID,
		teams:      []string{},
		blocks:     []string{},
	}

	pa.addListener(newPAC)
	pa.removeExpiredForUserID(userID)
}

func (pa *PluginAdapter) OnWebSocketDisconnect(webConnID, userID string) {
	pac, ok := pa.GetListenerByWebConnID(webConnID)
	if !ok {
		pa.logger.Debug("received a disconnect for an unregistered webconn",
			mlog.String("webConnID", webConnID),
			mlog.String("userID", userID),
		)
		return
	}

	atomic.StoreInt64(&pac.inactiveAt, mmModel.GetMillis())
}

func commandFromRequest(req *mmModel.WebSocketRequest) (*WebsocketCommand, error) {
	c := &WebsocketCommand{Action: strings.TrimPrefix(req.Action, websocketMessagePrefix)}

	if teamID, ok := req.Data["teamId"]; ok {
		c.TeamID = teamID.(string)
	} else {
		return nil, errMissingTeamInCommand
	}

	if readToken, ok := req.Data["readToken"]; ok {
		c.ReadToken = readToken.(string)
	}

	if blockIDs, ok := req.Data["blockIds"]; ok {
		c.BlockIDs = blockIDs.([]string)
	}

	return c, nil
}

func (pa *PluginAdapter) WebSocketMessageHasBeenPosted(webConnID, userID string, req *mmModel.WebSocketRequest) {
	pac, ok := pa.GetListenerByWebConnID(webConnID)
	if !ok {
		pa.logger.Debug("received a message for an unregistered webconn",
			mlog.String("webConnID", webConnID),
			mlog.String("userID", userID),
			mlog.String("action", req.Action),
		)
		return
	}

	// only process messages using the plugin actions
	if !strings.HasPrefix(req.Action, websocketMessagePrefix) {
		return
	}

	command, err := commandFromRequest(req)
	if err != nil {
		pa.logger.Error("error getting command from request",
			mlog.String("action", req.Action),
			mlog.String("webConnID", webConnID),
			mlog.String("userID", userID),
			mlog.Err(err),
		)
		return
	}

	switch command.Action {
	// The block-related commands are not implemented in the adapter
	// as there is no such thing as unauthenticated websocket
	// connections in plugin mode. Only a debug line is logged
	case websocketActionSubscribeBlocks, websocketActionUnsubscribeBlocks:
		pa.logger.Debug(`Command not implemented in plugin mode`,
			mlog.String("command", command.Action),
			mlog.String("webConnID", webConnID),
			mlog.String("userID", userID),
			mlog.String("teamID", command.TeamID),
		)

	case websocketActionSubscribeTeam:
		pa.logger.Debug(`Command not implemented in plugin mode`,
			mlog.String("command", command.Action),
			mlog.String("webConnID", webConnID),
			mlog.String("userID", userID),
			mlog.String("teamID", command.TeamID),
		)

		if !pa.auth.DoesUserHaveTeamAccess(userID, command.TeamID) {
			return
		}

		pa.subscribeListenerToTeam(pac, command.TeamID)
	case websocketActionUnsubscribeTeam:
		pa.logger.Debug(`Command: UNSUBSCRIBE_WORKSPACE`,
			mlog.String("webConnID", webConnID),
			mlog.String("userID", userID),
			mlog.String("teamID", command.TeamID),
		)

		pa.unsubscribeListenerFromTeam(pac, command.TeamID)
	}
}

// sendMessageToAll will send a websocket message to all clients on all nodes.
func (pa *PluginAdapter) sendMessageToAll(event string, payload map[string]interface{}) {
	// Empty &mmModel.WebsocketBroadcast will send to all users
	pa.api.PublishWebSocketEvent(event, payload, &mmModel.WebsocketBroadcast{})
}

func (pa *PluginAdapter) BroadcastConfigChange(pluginConfig model.ClientConfig) {
	pa.sendMessageToAll(websocketActionUpdateConfig, utils.StructToMap(pluginConfig))
}

// sendUserMessageSkipCluster sends the message to specific users.
func (pa *PluginAdapter) sendUserMessageSkipCluster(event string, payload map[string]interface{}, userIDs ...string) {
	for _, userID := range userIDs {
		pa.api.PublishWebSocketEvent(event, payload, &mmModel.WebsocketBroadcast{UserId: userID})
	}
}

// sendTeamMessageSkipCluster sends a message to all the users
// with a websocket client subscribed to a given team.
func (pa *PluginAdapter) sendTeamMessageSkipCluster(event, teamID string, payload map[string]interface{}) {
	userIDs := pa.getUserIDsForTeam(teamID)
	pa.sendUserMessageSkipCluster(event, payload, userIDs...)
}

// sendTeamMessage sends and propagates a message that is aimed
// for all the users that are subscribed to a given team.
func (pa *PluginAdapter) sendTeamMessage(event, teamID string, payload map[string]interface{}, ensureUserIDs ...string) {
	go func() {
		clusterMessage := &ClusterMessage{
			TeamID:      teamID,
			Payload:     payload,
			EnsureUsers: ensureUserIDs,
		}

		pa.sendMessageToCluster(clusterMessage)
	}()

	pa.sendTeamMessageSkipCluster(event, teamID, payload)
}

// sendBoardMessageSkipCluster sends a message to all the users
// subscribed to a given team that belong to one of its boards.
func (pa *PluginAdapter) sendBoardMessageSkipCluster(teamID, boardID string, payload map[string]interface{}, ensureUserIDs ...string) {
	userIDs := pa.getUserIDsForTeamAndBoard(teamID, boardID, ensureUserIDs...)
	pa.sendUserMessageSkipCluster(websocketActionUpdateBoard, payload, userIDs...)
}

// sendBoardMessage sends and propagates a message that is aimed for
// all the users that are subscribed to the board's team and are
// members of it too.
func (pa *PluginAdapter) sendBoardMessage(teamID, boardID string, payload map[string]interface{}, ensureUserIDs ...string) {
	go func() {
		clusterMessage := &ClusterMessage{
			TeamID:      teamID,
			BoardID:     boardID,
			Payload:     payload,
			EnsureUsers: ensureUserIDs,
		}

		pa.sendMessageToCluster(clusterMessage)
	}()

	pa.sendBoardMessageSkipCluster(teamID, boardID, payload, ensureUserIDs...)
}

func (pa *PluginAdapter) BroadcastBlockChange(teamID string, block *model.Block) {
	pa.logger.Trace("BroadcastingBlockChange",
		mlog.String("teamID", teamID),
		mlog.String("boardID", block.BoardID),
		mlog.String("blockID", block.ID),
	)

	message := UpdateBlockMsg{
		Action: websocketActionUpdateBlock,
		TeamID: teamID,
		Block:  block,
	}

	pa.sendBoardMessage(teamID, block.BoardID, utils.StructToMap(message))
}

func (pa *PluginAdapter) BroadcastCategoryChange(category model.Category) {
	pa.logger.Debug("BroadcastCategoryChange",
		mlog.String("userID", category.UserID),
		mlog.String("teamID", category.TeamID),
		mlog.String("categoryID", category.ID),
	)

	message := UpdateCategoryMessage{
		Action:   websocketActionUpdateCategory,
		TeamID:   category.TeamID,
		Category: &category,
	}

	payload := utils.StructToMap(message)

	go func() {
		clusterMessage := &ClusterMessage{
			Payload: payload,
			UserID:  category.UserID,
		}

		pa.sendMessageToCluster(clusterMessage)
	}()

	pa.sendUserMessageSkipCluster(websocketActionUpdateCategory, payload, category.UserID)
}

func (pa *PluginAdapter) BroadcastCategoryReorder(teamID, userID string, categoryOrder []string) {
	pa.logger.Debug("BroadcastCategoryReorder",
		mlog.String("userID", userID),
		mlog.String("teamID", teamID),
	)

	message := CategoryReorderMessage{
		Action:        websocketActionReorderCategories,
		CategoryOrder: categoryOrder,
		TeamID:        teamID,
	}
	payload := utils.StructToMap(message)
	go func() {
		clusterMessage := &ClusterMessage{
			Payload: payload,
			UserID:  userID,
		}

		pa.sendMessageToCluster(clusterMessage)
	}()

	pa.sendUserMessageSkipCluster(message.Action, payload, userID)
}

func (pa *PluginAdapter) BroadcastCategoryBoardsReorder(teamID, userID, categoryID string, boardsOrder []string) {
	pa.logger.Debug("BroadcastCategoryBoardsReorder",
		mlog.String("userID", userID),
		mlog.String("teamID", teamID),
		mlog.String("categoryID", categoryID),
	)

	message := CategoryBoardReorderMessage{
		Action:     websocketActionReorderCategoryBoards,
		CategoryID: categoryID,
		BoardOrder: boardsOrder,
		TeamID:     teamID,
	}
	payload := utils.StructToMap(message)
	go func() {
		clusterMessage := &ClusterMessage{
			Payload: payload,
			UserID:  userID,
		}

		pa.sendMessageToCluster(clusterMessage)
	}()

	pa.sendUserMessageSkipCluster(message.Action, payload, userID)
}

func (pa *PluginAdapter) BroadcastCategoryBoardChange(teamID, userID string, boardCategories []*model.BoardCategoryWebsocketData) {
	pa.logger.Debug(
		"BroadcastCategoryBoardChange",
		mlog.String("userID", userID),
		mlog.String("teamID", teamID),
		mlog.Int("numEntries", len(boardCategories)),
	)

	message := UpdateCategoryMessage{
		Action:          websocketActionUpdateCategoryBoard,
		TeamID:          teamID,
		BoardCategories: boardCategories,
	}

	payload := utils.StructToMap(message)

	go func() {
		clusterMessage := &ClusterMessage{
			Payload: payload,
			UserID:  userID,
		}

		pa.sendMessageToCluster(clusterMessage)
	}()

	pa.sendUserMessageSkipCluster(websocketActionUpdateCategoryBoard, utils.StructToMap(message), userID)
}

func (pa *PluginAdapter) BroadcastBlockDelete(teamID, blockID, boardID string) {
	now := utils.GetMillis()
	block := &model.Block{}
	block.ID = blockID
	block.BoardID = boardID
	block.UpdateAt = now
	block.DeleteAt = now

	pa.BroadcastBlockChange(teamID, block)
}

func (pa *PluginAdapter) BroadcastBoardChange(teamID string, board *model.Board) {
	pa.logger.Debug("BroadcastingBoardChange",
		mlog.String("teamID", teamID),
		mlog.String("boardID", board.ID),
	)

	message := UpdateBoardMsg{
		Action: websocketActionUpdateBoard,
		TeamID: teamID,
		Board:  board,
	}

	pa.sendBoardMessage(teamID, board.ID, utils.StructToMap(message))
}

func (pa *PluginAdapter) BroadcastBoardDelete(teamID, boardID string) {
	now := utils.GetMillis()
	board := &model.Board{}
	board.ID = boardID
	board.TeamID = teamID
	board.UpdateAt = now
	board.DeleteAt = now

	pa.BroadcastBoardChange(teamID, board)
}

func (pa *PluginAdapter) BroadcastMemberChange(teamID, boardID string, member *model.BoardMember) {
	pa.logger.Debug("BroadcastingMemberChange",
		mlog.String("teamID", teamID),
		mlog.String("boardID", boardID),
		mlog.String("userID", member.UserID),
	)

	message := UpdateMemberMsg{
		Action: websocketActionUpdateMember,
		TeamID: teamID,
		Member: member,
	}

	pa.sendBoardMessage(teamID, boardID, utils.StructToMap(message), member.UserID)
}

func (pa *PluginAdapter) BroadcastMemberDelete(teamID, boardID, userID string) {
	pa.logger.Debug("BroadcastingMemberDelete",
		mlog.String("teamID", teamID),
		mlog.String("boardID", boardID),
		mlog.String("userID", userID),
	)

	message := UpdateMemberMsg{
		Action: websocketActionDeleteMember,
		TeamID: teamID,
		Member: &model.BoardMember{UserID: userID, BoardID: boardID},
	}

	// when fetching the members of the board that should receive the
	// member deletion message, the deleted member will not be one of
	// them, so we need to ensure they receive the message
	pa.sendBoardMessage(teamID, boardID, utils.StructToMap(message), userID)
}

func (pa *PluginAdapter) BroadcastSubscriptionChange(teamID string, subscription *model.Subscription) {
	pa.logger.Debug("BroadcastingSubscriptionChange",
		mlog.String("TeamID", teamID),
		mlog.String("blockID", subscription.BlockID),
		mlog.String("subscriberID", subscription.SubscriberID),
	)

	message := UpdateSubscription{
		Action:       websocketActionUpdateSubscription,
		Subscription: subscription,
	}

	pa.sendTeamMessage(websocketActionUpdateSubscription, teamID, utils.StructToMap(message))
}

func (pa *PluginAdapter) BroadcastCardLimitTimestampChange(cardLimitTimestamp int64) {
	pa.logger.Debug("BroadcastCardLimitTimestampChange",
		mlog.Int("cardLimitTimestamp", cardLimitTimestamp),
	)

	message := UpdateCardLimitTimestamp{
		Action:    websocketActionUpdateCardLimitTimestamp,
		Timestamp: cardLimitTimestamp,
	}

	pa.sendMessageToAll(websocketActionUpdateCardLimitTimestamp, utils.StructToMap(message))
}