2021-09-29 18:19:34 +02:00
|
|
|
//go:generate mockgen --build_flags=--mod=mod -destination=mocks/mockpluginapi.go -package mocks github.com/mattermost/mattermost-server/v6/plugin API
|
2021-08-27 10:59:14 +02:00
|
|
|
package ws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-09-29 18:19:34 +02:00
|
|
|
"sync/atomic"
|
2021-08-27 10:59:14 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/auth"
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-09-16 21:31:02 +02:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-10-07 13:51:01 +02:00
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
mmModel "github.com/mattermost/mattermost-server/v6/model"
|
|
|
|
"github.com/mattermost/mattermost-server/v6/plugin"
|
2022-02-08 00:04:24 +01:00
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2021-08-27 10:59:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const websocketMessagePrefix = "custom_focalboard_"
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
var errMissingTeamInCommand = fmt.Errorf("command doesn't contain teamId")
|
2021-08-27 10:59:14 +02:00
|
|
|
|
2021-09-16 21:31:02 +02:00
|
|
|
type PluginAdapterInterface interface {
|
2022-03-22 15:24:34 +01:00
|
|
|
Adapter
|
2021-09-16 21:31:02 +02:00
|
|
|
OnWebSocketConnect(webConnID, userID string)
|
|
|
|
OnWebSocketDisconnect(webConnID, userID string)
|
|
|
|
WebSocketMessageHasBeenPosted(webConnID, userID string, req *mmModel.WebSocketRequest)
|
|
|
|
BroadcastConfigChange(clientConfig model.ClientConfig)
|
2022-03-22 15:24:34 +01:00
|
|
|
BroadcastBlockChange(teamID string, block model.Block)
|
|
|
|
BroadcastBlockDelete(teamID, blockID, parentID string)
|
|
|
|
BroadcastSubscriptionChange(teamID string, subscription *model.Subscription)
|
2022-06-15 12:17:44 +02:00
|
|
|
BroadcastCardLimitTimestampChange(cardLimitTimestamp int64)
|
2021-09-16 21:31:02 +02:00
|
|
|
HandleClusterEvent(ev mmModel.PluginClusterEvent)
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
type PluginAdapter struct {
|
2021-09-29 18:19:34 +02:00
|
|
|
api plugin.API
|
|
|
|
auth auth.AuthInterface
|
|
|
|
staleThreshold time.Duration
|
2022-03-22 15:24:34 +01:00
|
|
|
store Store
|
2022-02-08 00:04:24 +01:00
|
|
|
logger *mlog.Logger
|
2021-08-27 10:59:14 +02:00
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
listenersMU sync.RWMutex
|
|
|
|
listeners map[string]*PluginAdapterClient
|
|
|
|
listenersByUserID map[string][]*PluginAdapterClient
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
subscriptionsMU sync.RWMutex
|
|
|
|
listenersByTeam map[string][]*PluginAdapterClient
|
|
|
|
listenersByBlock map[string][]*PluginAdapterClient
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func NewPluginAdapter(api plugin.API, auth auth.AuthInterface, store Store, logger *mlog.Logger) *PluginAdapter {
|
2021-08-27 10:59:14 +02:00
|
|
|
return &PluginAdapter{
|
2022-03-22 15:24:34 +01:00
|
|
|
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{},
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (pa *PluginAdapter) GetListenersByTeam(teamID string) []*PluginAdapterClient {
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.subscriptionsMU.RLock()
|
|
|
|
defer pa.subscriptionsMU.RUnlock()
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
return pa.listenersByTeam[teamID]
|
2021-09-29 18:19:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) GetListenersByBlock(blockID string) []*PluginAdapterClient {
|
|
|
|
pa.subscriptionsMU.RLock()
|
|
|
|
defer pa.subscriptionsMU.RUnlock()
|
|
|
|
|
|
|
|
return pa.listenersByBlock[blockID]
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
func (pa *PluginAdapter) addListener(pac *PluginAdapterClient) {
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.listenersMU.Lock()
|
|
|
|
defer pa.listenersMU.Unlock()
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
pa.listeners[pac.webConnID] = pac
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.listenersByUserID[pac.userID] = append(pa.listenersByUserID[pac.userID], pac)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) removeListener(pac *PluginAdapterClient) {
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.listenersMU.Lock()
|
|
|
|
defer pa.listenersMU.Unlock()
|
2021-08-27 10:59:14 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// team subscriptions
|
|
|
|
for _, team := range pac.teams {
|
|
|
|
pa.removeListenerFromTeam(pac, team)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// block subscriptions
|
|
|
|
for _, block := range pac.blocks {
|
|
|
|
pa.removeListenerFromBlock(pac, block)
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
// 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
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
delete(pa.listeners, pac.webConnID)
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
func (pa *PluginAdapter) removeExpiredForUserID(userID string) {
|
|
|
|
for _, pac := range pa.GetListenersByUserID(userID) {
|
|
|
|
if !pac.isActive() && pac.hasExpired(pa.staleThreshold) {
|
|
|
|
pa.removeListener(pac)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (pa *PluginAdapter) removeListenerFromTeam(pac *PluginAdapterClient, teamID string) {
|
|
|
|
newTeamListeners := []*PluginAdapterClient{}
|
|
|
|
for _, listener := range pa.GetListenersByTeam(teamID) {
|
2021-08-27 10:59:14 +02:00
|
|
|
if listener.webConnID != pac.webConnID {
|
2022-03-22 15:24:34 +01:00
|
|
|
newTeamListeners = append(newTeamListeners, listener)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.subscriptionsMU.Lock()
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.listenersByTeam[teamID] = newTeamListeners
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.subscriptionsMU.Unlock()
|
2021-08-27 10:59:14 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
pac.unsubscribeFromTeam(teamID)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) removeListenerFromBlock(pac *PluginAdapterClient, blockID string) {
|
|
|
|
newBlockListeners := []*PluginAdapterClient{}
|
2021-09-29 18:19:34 +02:00
|
|
|
for _, listener := range pa.GetListenersByBlock(blockID) {
|
2021-08-27 10:59:14 +02:00
|
|
|
if listener.webConnID != pac.webConnID {
|
|
|
|
newBlockListeners = append(newBlockListeners, listener)
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.subscriptionsMU.Lock()
|
2021-08-27 10:59:14 +02:00
|
|
|
pa.listenersByBlock[blockID] = newBlockListeners
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.subscriptionsMU.Unlock()
|
2021-08-27 10:59:14 +02:00
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
pac.unsubscribeFromBlock(blockID)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (pa *PluginAdapter) subscribeListenerToTeam(pac *PluginAdapterClient, teamID string) {
|
|
|
|
if pac.isSubscribedToTeam(teamID) {
|
2021-08-27 10:59:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.subscriptionsMU.Lock()
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.listenersByTeam[teamID] = append(pa.listenersByTeam[teamID], pac)
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.subscriptionsMU.Unlock()
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
pac.subscribeToTeam(teamID)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (pa *PluginAdapter) unsubscribeListenerFromTeam(pac *PluginAdapterClient, teamID string) {
|
|
|
|
if !pac.isSubscribedToTeam(teamID) {
|
2021-08-27 10:59:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.removeListenerFromTeam(pac, teamID)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (pa *PluginAdapter) getUserIDsForTeam(teamID string) []string {
|
2021-09-29 18:19:34 +02:00
|
|
|
userMap := map[string]bool{}
|
2022-03-22 15:24:34 +01:00
|
|
|
for _, pac := range pa.GetListenersByTeam(teamID) {
|
2021-09-29 18:19:34 +02:00
|
|
|
if pac.isActive() {
|
|
|
|
userMap[pac.userID] = true
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 10:59:14 +02:00
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
userIDs := []string{}
|
|
|
|
for userID := range userMap {
|
2022-04-25 17:39:40 +02:00
|
|
|
if pa.auth.DoesUserHaveTeamAccess(userID, teamID) {
|
|
|
|
userIDs = append(userIDs, userID)
|
|
|
|
}
|
2021-09-29 18:19:34 +02:00
|
|
|
}
|
2022-04-25 17:39:40 +02:00
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
return userIDs
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
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 {
|
2022-04-25 17:39:40 +02:00
|
|
|
if userID == member.UserID && pa.auth.DoesUserHaveTeamAccess(userID, teamID) {
|
2022-03-22 15:24:34 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
//nolint:unused
|
|
|
|
func (pa *PluginAdapter) unsubscribeListenerFromBlocks(pac *PluginAdapterClient, blockIDs []string) {
|
2021-08-27 10:59:14 +02:00
|
|
|
for _, blockID := range blockIDs {
|
|
|
|
if pac.isSubscribedToBlock(blockID) {
|
|
|
|
pa.removeListenerFromBlock(pac, blockID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) OnWebSocketConnect(webConnID, userID string) {
|
2021-09-29 18:19:34 +02:00
|
|
|
if existingPAC, ok := pa.GetListenerByWebConnID(webConnID); ok {
|
2022-02-08 00:04:24 +01:00
|
|
|
pa.logger.Debug("inactive connection found for webconn, reusing",
|
|
|
|
mlog.String("webConnID", webConnID),
|
|
|
|
mlog.String("userID", userID),
|
2021-09-29 18:19:34 +02:00
|
|
|
)
|
|
|
|
atomic.StoreInt64(&existingPAC.inactiveAt, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newPAC := &PluginAdapterClient{
|
|
|
|
inactiveAt: 0,
|
2021-08-27 10:59:14 +02:00
|
|
|
webConnID: webConnID,
|
|
|
|
userID: userID,
|
2022-03-22 15:24:34 +01:00
|
|
|
teams: []string{},
|
2021-08-27 10:59:14 +02:00
|
|
|
blocks: []string{},
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
pa.addListener(newPAC)
|
|
|
|
pa.removeExpiredForUserID(userID)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) OnWebSocketDisconnect(webConnID, userID string) {
|
2021-09-29 18:19:34 +02:00
|
|
|
pac, ok := pa.GetListenerByWebConnID(webConnID)
|
2021-08-27 10:59:14 +02:00
|
|
|
if !ok {
|
2022-02-08 00:04:24 +01:00
|
|
|
pa.logger.Debug("received a disconnect for an unregistered webconn",
|
|
|
|
mlog.String("webConnID", webConnID),
|
|
|
|
mlog.String("userID", userID),
|
2021-08-27 10:59:14 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
atomic.StoreInt64(&pac.inactiveAt, mmModel.GetMillis())
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func commandFromRequest(req *mmModel.WebSocketRequest) (*WebsocketCommand, error) {
|
|
|
|
c := &WebsocketCommand{Action: strings.TrimPrefix(req.Action, websocketMessagePrefix)}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if teamID, ok := req.Data["teamId"]; ok {
|
|
|
|
c.TeamID = teamID.(string)
|
2021-08-27 10:59:14 +02:00
|
|
|
} else {
|
2022-03-22 15:24:34 +01:00
|
|
|
return nil, errMissingTeamInCommand
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-09-29 18:19:34 +02:00
|
|
|
pac, ok := pa.GetListenerByWebConnID(webConnID)
|
2021-08-27 10:59:14 +02:00
|
|
|
if !ok {
|
2022-02-08 00:04:24 +01:00
|
|
|
pa.logger.Debug("received a message for an unregistered webconn",
|
|
|
|
mlog.String("webConnID", webConnID),
|
|
|
|
mlog.String("userID", userID),
|
|
|
|
mlog.String("action", req.Action),
|
2021-08-27 10:59:14 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only process messages using the plugin actions
|
|
|
|
if !strings.HasPrefix(req.Action, websocketMessagePrefix) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
command, err := commandFromRequest(req)
|
|
|
|
if err != nil {
|
2022-02-08 00:04:24 +01:00
|
|
|
pa.logger.Error("error getting command from request",
|
|
|
|
mlog.String("action", req.Action),
|
|
|
|
mlog.String("webConnID", webConnID),
|
|
|
|
mlog.String("userID", userID),
|
|
|
|
mlog.Err(err),
|
2021-08-27 10:59:14 +02:00
|
|
|
)
|
|
|
|
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:
|
2022-02-08 00:04:24 +01:00
|
|
|
pa.logger.Debug(`Command not implemented in plugin mode`,
|
|
|
|
mlog.String("command", command.Action),
|
|
|
|
mlog.String("webConnID", webConnID),
|
|
|
|
mlog.String("userID", userID),
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("teamID", command.TeamID),
|
2021-08-27 10:59:14 +02:00
|
|
|
)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
case websocketActionSubscribeTeam:
|
|
|
|
pa.logger.Debug(`Command not implemented in plugin mode`,
|
|
|
|
mlog.String("command", command.Action),
|
2022-02-08 00:04:24 +01:00
|
|
|
mlog.String("webConnID", webConnID),
|
|
|
|
mlog.String("userID", userID),
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("teamID", command.TeamID),
|
2021-08-27 10:59:14 +02:00
|
|
|
)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if !pa.auth.DoesUserHaveTeamAccess(userID, command.TeamID) {
|
2021-08-27 10:59:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.subscribeListenerToTeam(pac, command.TeamID)
|
|
|
|
case websocketActionUnsubscribeTeam:
|
2022-02-08 00:04:24 +01:00
|
|
|
pa.logger.Debug(`Command: UNSUBSCRIBE_WORKSPACE`,
|
|
|
|
mlog.String("webConnID", webConnID),
|
|
|
|
mlog.String("userID", userID),
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("teamID", command.TeamID),
|
2021-08-27 10:59:14 +02:00
|
|
|
)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.unsubscribeListenerFromTeam(pac, command.TeamID)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 21:59:04 +02:00
|
|
|
// sendMessageToAll will send a websocket message to all clients on all nodes.
|
|
|
|
func (pa *PluginAdapter) sendMessageToAll(event string, payload map[string]interface{}) {
|
2021-09-16 21:31:02 +02:00
|
|
|
// Empty &mmModel.WebsocketBroadcast will send to all users
|
2022-05-19 21:59:04 +02:00
|
|
|
pa.api.PublishWebSocketEvent(event, payload, &mmModel.WebsocketBroadcast{})
|
2021-09-16 21:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) BroadcastConfigChange(pluginConfig model.ClientConfig) {
|
2022-05-19 21:59:04 +02:00
|
|
|
pa.sendMessageToAll(websocketActionUpdateConfig, utils.StructToMap(pluginConfig))
|
2021-09-16 21:31:02 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 12:03:56 +02:00
|
|
|
// sendUserMessageSkipCluster sends the message to specific users.
|
|
|
|
func (pa *PluginAdapter) sendUserMessageSkipCluster(event string, payload map[string]interface{}, userIDs ...string) {
|
2021-09-16 12:18:11 +02:00
|
|
|
for _, userID := range userIDs {
|
2021-12-10 16:46:37 +01:00
|
|
|
pa.api.PublishWebSocketEvent(event, payload, &mmModel.WebsocketBroadcast{UserId: userID})
|
2021-09-16 12:18:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 17:39:40 +02:00
|
|
|
// 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...)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// sendTeamMessage sends and propagates a message that is aimed
|
|
|
|
// for all the users that are subscribed to a given team.
|
2022-04-07 12:03:56 +02:00
|
|
|
func (pa *PluginAdapter) sendTeamMessage(event, teamID string, payload map[string]interface{}, ensureUserIDs ...string) {
|
2022-03-22 15:24:34 +01:00
|
|
|
go func() {
|
|
|
|
clusterMessage := &ClusterMessage{
|
2022-04-07 12:03:56 +02:00
|
|
|
TeamID: teamID,
|
|
|
|
Payload: payload,
|
|
|
|
EnsureUsers: ensureUserIDs,
|
2022-03-22 15:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pa.sendMessageToCluster("websocket_message", 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...)
|
2022-04-07 12:03:56 +02:00
|
|
|
pa.sendUserMessageSkipCluster(websocketActionUpdateBoard, payload, userIDs...)
|
2022-03-22 15:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2021-09-16 12:18:11 +02:00
|
|
|
go func() {
|
|
|
|
clusterMessage := &ClusterMessage{
|
2022-03-22 15:24:34 +01:00
|
|
|
TeamID: teamID,
|
|
|
|
BoardID: boardID,
|
2021-09-16 12:18:11 +02:00
|
|
|
Payload: payload,
|
2022-03-22 15:24:34 +01:00
|
|
|
EnsureUsers: ensureUserIDs,
|
2021-09-16 12:18:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pa.sendMessageToCluster("websocket_message", clusterMessage)
|
|
|
|
}()
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.sendBoardMessageSkipCluster(teamID, boardID, payload, ensureUserIDs...)
|
2021-09-16 12:18:11 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (pa *PluginAdapter) BroadcastBlockChange(teamID string, block model.Block) {
|
2022-02-08 00:04:24 +01:00
|
|
|
pa.logger.Debug("BroadcastingBlockChange",
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", block.BoardID),
|
2022-02-08 00:04:24 +01:00
|
|
|
mlog.String("blockID", block.ID),
|
2021-08-27 10:59:14 +02:00
|
|
|
)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
message := UpdateBlockMsg{
|
2021-08-27 10:59:14 +02:00
|
|
|
Action: websocketActionUpdateBlock,
|
2022-03-22 15:24:34 +01:00
|
|
|
TeamID: teamID,
|
2021-08-27 10:59:14 +02:00
|
|
|
Block: block,
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.sendBoardMessage(teamID, block.BoardID, utils.StructToMap(message))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) BroadcastCategoryChange(category model.Category) {
|
|
|
|
pa.logger.Debug("BroadcastCategoryChange",
|
2022-04-07 12:03:56 +02:00
|
|
|
mlog.String("userID", category.UserID),
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("teamID", category.TeamID),
|
|
|
|
mlog.String("categoryID", category.ID),
|
|
|
|
)
|
|
|
|
|
|
|
|
message := UpdateCategoryMessage{
|
|
|
|
Action: websocketActionUpdateCategory,
|
|
|
|
TeamID: category.TeamID,
|
|
|
|
Category: &category,
|
|
|
|
}
|
|
|
|
|
2022-04-13 12:16:35 +02:00
|
|
|
payload := utils.StructToMap(message)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
clusterMessage := &ClusterMessage{
|
|
|
|
Payload: payload,
|
|
|
|
EnsureUsers: []string{category.UserID},
|
|
|
|
}
|
|
|
|
|
|
|
|
pa.sendMessageToCluster("websocket_message", clusterMessage)
|
|
|
|
}()
|
|
|
|
|
|
|
|
pa.sendUserMessageSkipCluster(websocketActionUpdateCategory, payload, category.UserID)
|
2022-03-22 15:24:34 +01:00
|
|
|
}
|
|
|
|
|
2022-04-14 00:10:53 +02:00
|
|
|
func (pa *PluginAdapter) BroadcastCategoryBoardChange(teamID, userID string, boardCategory model.BoardCategoryWebsocketData) {
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.logger.Debug(
|
2022-04-14 00:10:53 +02:00
|
|
|
"BroadcastCategoryBoardChange",
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("userID", userID),
|
|
|
|
mlog.String("teamID", teamID),
|
2022-04-14 00:10:53 +02:00
|
|
|
mlog.String("categoryID", boardCategory.CategoryID),
|
|
|
|
mlog.String("blockID", boardCategory.BoardID),
|
2022-03-22 15:24:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
message := UpdateCategoryMessage{
|
2022-04-14 00:10:53 +02:00
|
|
|
Action: websocketActionUpdateCategoryBoard,
|
2022-03-22 15:24:34 +01:00
|
|
|
TeamID: teamID,
|
2022-04-14 00:10:53 +02:00
|
|
|
BoardCategories: &boardCategory,
|
2022-03-22 15:24:34 +01:00
|
|
|
}
|
|
|
|
|
2022-04-14 00:10:53 +02:00
|
|
|
pa.sendTeamMessage(websocketActionUpdateCategoryBoard, teamID, utils.StructToMap(message))
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (pa *PluginAdapter) BroadcastBlockDelete(teamID, blockID, boardID string) {
|
2021-10-07 13:51:01 +02:00
|
|
|
now := utils.GetMillis()
|
2021-08-27 10:59:14 +02:00
|
|
|
block := model.Block{}
|
|
|
|
block.ID = blockID
|
2022-03-22 15:24:34 +01:00
|
|
|
block.BoardID = boardID
|
2021-08-27 10:59:14 +02:00
|
|
|
block.UpdateAt = now
|
|
|
|
block.DeleteAt = now
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.BroadcastBlockChange(teamID, block)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) BroadcastBoardChange(teamID string, board *model.Board) {
|
2022-04-18 14:21:44 +02:00
|
|
|
pa.logger.Debug("BroadcastingBoardChange",
|
2022-03-22 15:24:34 +01:00
|
|
|
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) {
|
2022-04-18 14:21:44 +02:00
|
|
|
pa.logger.Debug("BroadcastingMemberChange",
|
2022-03-22 15:24:34 +01:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) BroadcastMemberDelete(teamID, boardID, userID string) {
|
2022-04-18 14:21:44 +02:00
|
|
|
pa.logger.Debug("BroadcastingMemberDelete",
|
2022-03-22 15:24:34 +01:00
|
|
|
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)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
2021-12-10 16:46:37 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (pa *PluginAdapter) BroadcastSubscriptionChange(teamID string, subscription *model.Subscription) {
|
2022-02-08 00:04:24 +01:00
|
|
|
pa.logger.Debug("BroadcastingSubscriptionChange",
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("TeamID", teamID),
|
2022-02-08 00:04:24 +01:00
|
|
|
mlog.String("blockID", subscription.BlockID),
|
|
|
|
mlog.String("subscriberID", subscription.SubscriberID),
|
2021-12-10 16:46:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
message := UpdateSubscription{
|
|
|
|
Action: websocketActionUpdateSubscription,
|
|
|
|
Subscription: subscription,
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
pa.sendTeamMessage(websocketActionUpdateSubscription, teamID, utils.StructToMap(message))
|
2021-12-10 16:46:37 +01:00
|
|
|
}
|
2022-06-15 12:17:44 +02:00
|
|
|
|
|
|
|
func (pa *PluginAdapter) BroadcastCardLimitTimestampChange(cardLimitTimestamp int64) {
|
|
|
|
pa.logger.Debug("BroadcastCardLimitTimestampChange",
|
|
|
|
mlog.Int64("cardLimitTimestamp", cardLimitTimestamp),
|
|
|
|
)
|
|
|
|
|
|
|
|
message := UpdateCardLimitTimestamp{
|
|
|
|
Action: websocketActionUpdateCardLimitTimestamp,
|
|
|
|
Timestamp: cardLimitTimestamp,
|
|
|
|
}
|
|
|
|
|
|
|
|
pa.sendMessageToAll(websocketActionUpdateCardLimitTimestamp, utils.StructToMap(message))
|
|
|
|
}
|