2020-10-16 19:12:53 +02:00
|
|
|
package ws
|
2020-10-09 11:51:06 +02:00
|
|
|
|
|
|
|
import (
|
2020-10-21 21:36:43 +02:00
|
|
|
"encoding/json"
|
2020-10-09 11:51:06 +02:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
"github.com/gorilla/mux"
|
2020-10-09 11:51:06 +02:00
|
|
|
"github.com/gorilla/websocket"
|
2021-02-02 21:06:28 +01:00
|
|
|
"github.com/mattermost/focalboard/server/auth"
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-10-07 13:51:01 +02:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-10-09 11:51:06 +02:00
|
|
|
)
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
const singleUserID = "single-user-id"
|
2021-05-24 19:06:11 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (wss *websocketSession) WriteJSON(v interface{}) error {
|
|
|
|
wss.mu.Lock()
|
|
|
|
defer wss.mu.Unlock()
|
|
|
|
err := wss.conn.WriteJSON(v)
|
2021-07-01 08:11:29 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (wss *websocketSession) isSubscribedToTeam(teamID string) bool {
|
|
|
|
for _, id := range wss.teams {
|
|
|
|
if id == teamID {
|
2021-07-19 11:34:17 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (wss *websocketSession) isSubscribedToBlock(blockID string) bool {
|
|
|
|
for _, id := range wss.blocks {
|
2021-07-19 11:34:17 +02:00
|
|
|
if id == blockID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-02 21:06:28 +01:00
|
|
|
// Server is a WebSocket server.
|
|
|
|
type Server struct {
|
2022-03-22 15:24:34 +01:00
|
|
|
upgrader websocket.Upgrader
|
|
|
|
listeners map[*websocketSession]bool
|
|
|
|
listenersByTeam map[string][]*websocketSession
|
|
|
|
listenersByBlock map[string][]*websocketSession
|
|
|
|
mu sync.RWMutex
|
|
|
|
auth *auth.Auth
|
|
|
|
singleUserToken string
|
|
|
|
isMattermostAuth bool
|
|
|
|
logger *mlog.Logger
|
|
|
|
store Store
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-16 21:31:02 +02:00
|
|
|
// UpdateClientConfig is sent on block updates.
|
|
|
|
type UpdateClientConfig struct {
|
|
|
|
Action string `json:"action"`
|
|
|
|
ClientConfig model.ClientConfig `json:"clientconfig"`
|
|
|
|
}
|
|
|
|
|
2021-02-02 21:06:28 +01:00
|
|
|
type websocketSession struct {
|
2022-03-22 15:24:34 +01:00
|
|
|
conn *websocket.Conn
|
2021-07-19 11:34:17 +02:00
|
|
|
userID string
|
2022-03-22 15:24:34 +01:00
|
|
|
mu sync.Mutex
|
|
|
|
teams []string
|
|
|
|
blocks []string
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wss *websocketSession) isAuthenticated() bool {
|
|
|
|
return wss.userID != ""
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// NewServer creates a new Server.
|
2022-03-22 15:24:34 +01:00
|
|
|
func NewServer(auth *auth.Auth, singleUserToken string, isMattermostAuth bool, logger *mlog.Logger, store Store) *Server {
|
2020-10-22 13:34:42 +02:00
|
|
|
return &Server{
|
2022-03-22 15:24:34 +01:00
|
|
|
listeners: make(map[*websocketSession]bool),
|
|
|
|
listenersByTeam: make(map[string][]*websocketSession),
|
|
|
|
listenersByBlock: make(map[string][]*websocketSession),
|
2020-10-09 11:51:06 +02:00
|
|
|
upgrader: websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
2021-05-24 19:06:11 +02:00
|
|
|
auth: auth,
|
|
|
|
singleUserToken: singleUserToken,
|
|
|
|
isMattermostAuth: isMattermostAuth,
|
2021-05-29 08:23:10 +02:00
|
|
|
logger: logger,
|
2022-03-22 15:24:34 +01:00
|
|
|
store: store,
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 21:06:28 +01:00
|
|
|
// RegisterRoutes registers routes.
|
|
|
|
func (ws *Server) RegisterRoutes(r *mux.Router) {
|
2021-07-19 11:34:17 +02:00
|
|
|
r.HandleFunc("/ws", ws.handleWebSocket)
|
2020-10-21 21:36:43 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
func (ws *Server) handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
2020-10-09 11:51:06 +02:00
|
|
|
// Upgrade initial GET request to a websocket
|
|
|
|
client, err := ws.upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Error("ERROR upgrading to websocket", mlog.Err(err))
|
2021-03-27 02:04:23 +01:00
|
|
|
return
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// create an empty session with websocket client
|
2022-03-22 15:24:34 +01:00
|
|
|
wsSession := &websocketSession{
|
|
|
|
conn: client,
|
2021-07-19 11:34:17 +02:00
|
|
|
userID: "",
|
2022-03-22 15:24:34 +01:00
|
|
|
mu: sync.Mutex{},
|
|
|
|
teams: []string{},
|
|
|
|
blocks: []string{},
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
if ws.isMattermostAuth {
|
|
|
|
wsSession.userID = r.Header.Get("Mattermost-User-Id")
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.addListener(wsSession)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2021-07-01 08:11:29 +02:00
|
|
|
// Make sure we close the connection when the function returns
|
|
|
|
defer func() {
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.logger.Debug("DISCONNECT WebSocket", mlog.Stringer("client", wsSession.conn.RemoteAddr()))
|
2021-07-01 08:11:29 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// Remove session from listeners
|
|
|
|
ws.removeListener(wsSession)
|
|
|
|
wsSession.conn.Close()
|
2021-07-01 08:11:29 +02:00
|
|
|
}()
|
|
|
|
|
2020-10-09 11:51:06 +02:00
|
|
|
// Simple message handling loop
|
|
|
|
for {
|
2022-03-22 15:24:34 +01:00
|
|
|
_, p, err := wsSession.conn.ReadMessage()
|
2020-10-09 11:51:06 +02:00
|
|
|
if err != nil {
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.logger.Error("ERROR WebSocket",
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.Stringer("client", wsSession.conn.RemoteAddr()),
|
2021-05-29 08:23:10 +02:00
|
|
|
mlog.Err(err),
|
|
|
|
)
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.removeListener(wsSession)
|
2020-10-09 11:51:06 +02:00
|
|
|
break
|
|
|
|
}
|
2020-10-21 21:36:43 +02:00
|
|
|
|
|
|
|
var command WebsocketCommand
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
err = json.Unmarshal(p, &command)
|
|
|
|
if err != nil {
|
|
|
|
// handle this error
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Error(`ERROR webSocket parsing command`, mlog.String("json", string(p)))
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
if command.Action == websocketActionAuth {
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.logger.Debug(`Command: AUTH`, mlog.Stringer("client", wsSession.conn.RemoteAddr()))
|
|
|
|
ws.authenticateListener(wsSession, command.Token)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the client wants to subscribe to a set of blocks and it
|
|
|
|
// is sending a read token, we don't need to check for
|
|
|
|
// authentication
|
|
|
|
if command.Action == websocketActionSubscribeBlocks {
|
|
|
|
ws.logger.Debug(`Command: SUBSCRIBE_BLOCKS`,
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("teamID", command.TeamID),
|
|
|
|
mlog.Stringer("client", wsSession.conn.RemoteAddr()),
|
2021-07-19 11:34:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if !ws.isCommandReadTokenValid(command) {
|
|
|
|
ws.logger.Error(`Rejected invalid read token`,
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.Stringer("client", wsSession.conn.RemoteAddr()),
|
2021-07-19 11:34:17 +02:00
|
|
|
mlog.String("action", command.Action),
|
|
|
|
mlog.String("readToken", command.ReadToken),
|
|
|
|
)
|
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-07-19 11:34:17 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.subscribeListenerToBlocks(wsSession, command.BlockIDs)
|
2021-07-19 11:34:17 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:14:18 +02:00
|
|
|
if command.Action == websocketActionUnsubscribeBlocks {
|
|
|
|
ws.logger.Debug(`Command: UNSUBSCRIBE_BLOCKS`,
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("teamID", command.TeamID),
|
|
|
|
mlog.Stringer("client", wsSession.conn.RemoteAddr()),
|
2021-07-28 18:14:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if !ws.isCommandReadTokenValid(command) {
|
|
|
|
ws.logger.Error(`Rejected invalid read token`,
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.Stringer("client", wsSession.conn.RemoteAddr()),
|
2021-07-28 18:14:18 +02:00
|
|
|
mlog.String("action", command.Action),
|
|
|
|
mlog.String("readToken", command.ReadToken),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.unsubscribeListenerFromBlocks(wsSession, command.BlockIDs)
|
2021-07-28 18:14:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// if the command is not authenticated at this point, it will
|
|
|
|
// not be processed
|
|
|
|
if !wsSession.isAuthenticated() {
|
|
|
|
ws.logger.Error(`Rejected unauthenticated message`,
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.Stringer("client", wsSession.conn.RemoteAddr()),
|
2021-07-19 11:34:17 +02:00
|
|
|
mlog.String("action", command.Action),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
switch command.Action {
|
2022-03-22 15:24:34 +01:00
|
|
|
case websocketActionSubscribeTeam:
|
|
|
|
ws.logger.Debug(`Command: SUBSCRIBE_TEAM`,
|
|
|
|
mlog.String("teamID", command.TeamID),
|
|
|
|
mlog.Stringer("client", wsSession.conn.RemoteAddr()),
|
2021-05-29 08:23:10 +02:00
|
|
|
)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
// if single user mode, check that the userID is valid and
|
|
|
|
// assume that the user has permission if so
|
|
|
|
if len(ws.singleUserToken) != 0 {
|
|
|
|
if wsSession.userID != singleUserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not in single user mode validate that the session
|
2022-03-22 15:24:34 +01:00
|
|
|
// has permissions to the team
|
2021-07-19 11:34:17 +02:00
|
|
|
} else {
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.logger.Debug("Not single user mode")
|
|
|
|
if !ws.auth.DoesUserHaveTeamAccess(wsSession.userID, command.TeamID) {
|
|
|
|
ws.logger.Error("WS user doesn't have team access", mlog.String("teamID", command.TeamID), mlog.String("userID", wsSession.userID))
|
2021-07-19 11:34:17 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.subscribeListenerToTeam(wsSession, command.TeamID)
|
|
|
|
case websocketActionUnsubscribeTeam:
|
|
|
|
ws.logger.Debug(`Command: UNSUBSCRIBE_TEAM`,
|
|
|
|
mlog.String("teamID", command.TeamID),
|
|
|
|
mlog.Stringer("client", wsSession.conn.RemoteAddr()),
|
2021-05-29 08:23:10 +02:00
|
|
|
)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.unsubscribeListenerFromTeam(wsSession, command.TeamID)
|
2020-10-21 21:36:43 +02:00
|
|
|
default:
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Error(`ERROR webSocket command, invalid action`, mlog.String("action", command.Action))
|
2020-10-21 21:36:43 +02:00
|
|
|
}
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// isCommandReadTokenValid ensures that a command contains a read
|
|
|
|
// token and a set of block ids that said token is valid for.
|
|
|
|
func (ws *Server) isCommandReadTokenValid(command WebsocketCommand) bool {
|
2022-03-22 15:24:34 +01:00
|
|
|
if len(command.TeamID) == 0 {
|
2021-03-27 02:04:23 +01:00
|
|
|
return false
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := ""
|
|
|
|
// all the blocks must be part of the same board
|
|
|
|
for _, blockID := range command.BlockIDs {
|
|
|
|
block, err := ws.store.GetBlock(blockID)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-03-27 02:04:23 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if boardID == "" {
|
|
|
|
boardID = block.BoardID
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if boardID != block.BoardID {
|
|
|
|
return false
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// the read token must be valid for the board
|
|
|
|
isValid, err := ws.auth.IsValidReadToken(boardID, command.ReadToken)
|
|
|
|
if err != nil {
|
|
|
|
ws.logger.Error(`ERROR when checking token validity`,
|
|
|
|
mlog.String("teamID", command.TeamID),
|
|
|
|
mlog.Err(err),
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return isValid
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// addListener adds a listener to the websocket server. The listener
|
|
|
|
// should not receive any update from the server until it subscribes
|
|
|
|
// itself to some entity changes. Adding a listener to the server
|
|
|
|
// doesn't mean that it's authenticated in any way.
|
2022-03-22 15:24:34 +01:00
|
|
|
func (ws *Server) addListener(listener *websocketSession) {
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.listeners[listener] = true
|
2021-07-09 03:09:02 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// removeListener removes a listener and all its subscriptions, if
|
|
|
|
// any, from the websockets server.
|
2022-03-22 15:24:34 +01:00
|
|
|
func (ws *Server) removeListener(listener *websocketSession) {
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2021-07-09 03:09:02 +02:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// remove the listener from its subscriptions, if any
|
2021-03-26 19:01:54 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// team subscriptions
|
|
|
|
for _, team := range listener.teams {
|
|
|
|
ws.removeListenerFromTeam(listener, team)
|
2021-03-29 21:54:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// block subscriptions
|
2022-03-22 15:24:34 +01:00
|
|
|
for _, block := range listener.blocks {
|
|
|
|
ws.removeListenerFromBlock(listener, block)
|
2021-03-26 19:01:54 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
delete(ws.listeners, listener)
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// subscribeListenerToTeam safely modifies the listener and the
|
|
|
|
// server to subscribe the listener to a given team updates.
|
|
|
|
func (ws *Server) subscribeListenerToTeam(listener *websocketSession, teamID string) {
|
|
|
|
if listener.isSubscribedToTeam(teamID) {
|
2021-07-19 11:34:17 +02:00
|
|
|
return
|
2021-02-03 03:15:03 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2021-02-03 03:15:03 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.listenersByTeam[teamID] = append(ws.listenersByTeam[teamID], listener)
|
|
|
|
listener.teams = append(listener.teams, teamID)
|
2021-03-27 02:04:23 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// unsubscribeListenerFromTeam safely modifies the listener and
|
2021-07-19 11:34:17 +02:00
|
|
|
// the server data structures to remove the link between the listener
|
2022-03-22 15:24:34 +01:00
|
|
|
// and a given team ID.
|
|
|
|
func (ws *Server) unsubscribeListenerFromTeam(listener *websocketSession, teamID string) {
|
|
|
|
if !listener.isSubscribedToTeam(teamID) {
|
2021-02-02 21:06:28 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.mu.Lock()
|
2021-07-19 11:34:17 +02:00
|
|
|
defer ws.mu.Unlock()
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.removeListenerFromTeam(listener, teamID)
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// subscribeListenerToBlocks safely modifies the listener and the
|
|
|
|
// server to subscribe the listener to a given set of block updates.
|
2022-03-22 15:24:34 +01:00
|
|
|
func (ws *Server) subscribeListenerToBlocks(listener *websocketSession, blockIDs []string) {
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
|
|
|
|
|
|
|
for _, blockID := range blockIDs {
|
2022-03-22 15:24:34 +01:00
|
|
|
if listener.isSubscribedToBlock(blockID) {
|
2021-07-19 11:34:17 +02:00
|
|
|
continue
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.listenersByBlock[blockID] = append(ws.listenersByBlock[blockID], listener)
|
|
|
|
listener.blocks = append(listener.blocks, blockID)
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// unsubscribeListenerFromBlocks safely modifies the listener and the
|
|
|
|
// server data structures to remove the link between the listener and
|
|
|
|
// a given set of block IDs.
|
2022-03-22 15:24:34 +01:00
|
|
|
func (ws *Server) unsubscribeListenerFromBlocks(listener *websocketSession, blockIDs []string) {
|
2021-02-02 21:06:28 +01:00
|
|
|
ws.mu.Lock()
|
2021-07-19 11:34:17 +02:00
|
|
|
defer ws.mu.Unlock()
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
for _, blockID := range blockIDs {
|
2022-03-22 15:24:34 +01:00
|
|
|
if listener.isSubscribedToBlock(blockID) {
|
|
|
|
ws.removeListenerFromBlock(listener, blockID)
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// removeListenerFromTeam removes the listener from both its own
|
|
|
|
// block subscribed list and the server listeners by team map.
|
|
|
|
func (ws *Server) removeListenerFromTeam(listener *websocketSession, teamID string) {
|
|
|
|
// we remove the listener from the team index
|
|
|
|
newTeamListeners := []*websocketSession{}
|
|
|
|
for _, l := range ws.listenersByTeam[teamID] {
|
|
|
|
if l != listener {
|
|
|
|
newTeamListeners = append(newTeamListeners, l)
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.listenersByTeam[teamID] = newTeamListeners
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// we remove the team from the listener subscription list
|
|
|
|
newListenerTeams := []string{}
|
|
|
|
for _, id := range listener.teams {
|
|
|
|
if id != teamID {
|
|
|
|
newListenerTeams = append(newListenerTeams, id)
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
listener.teams = newListenerTeams
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// removeListenerFromBlock removes the listener from both its own
|
|
|
|
// block subscribed list and the server listeners by block map.
|
2022-03-22 15:24:34 +01:00
|
|
|
func (ws *Server) removeListenerFromBlock(listener *websocketSession, blockID string) {
|
2021-07-19 11:34:17 +02:00
|
|
|
// we remove the listener from the block index
|
2022-03-22 15:24:34 +01:00
|
|
|
newBlockListeners := []*websocketSession{}
|
|
|
|
for _, l := range ws.listenersByBlock[blockID] {
|
|
|
|
if l != listener {
|
|
|
|
newBlockListeners = append(newBlockListeners, l)
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.listenersByBlock[blockID] = newBlockListeners
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// we remove the block from the listener subscription list
|
2022-03-22 15:24:34 +01:00
|
|
|
newListenerBlocks := []string{}
|
|
|
|
for _, id := range listener.blocks {
|
2021-07-19 11:34:17 +02:00
|
|
|
if id != blockID {
|
2022-03-22 15:24:34 +01:00
|
|
|
newListenerBlocks = append(newListenerBlocks, id)
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
listener.blocks = newListenerBlocks
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
func (ws *Server) getUserIDForToken(token string) string {
|
|
|
|
if len(ws.singleUserToken) > 0 {
|
|
|
|
if token == ws.singleUserToken {
|
|
|
|
return singleUserID
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := ws.auth.GetSession(token)
|
|
|
|
if session == nil || err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.UserID
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
func (ws *Server) authenticateListener(wsSession *websocketSession, token string) {
|
2021-08-27 10:59:14 +02:00
|
|
|
ws.logger.Debug("authenticateListener",
|
|
|
|
mlog.String("token", token),
|
|
|
|
mlog.String("wsSession.userID", wsSession.userID),
|
|
|
|
)
|
2021-07-19 11:34:17 +02:00
|
|
|
if wsSession.isAuthenticated() {
|
|
|
|
// Do not allow multiple auth calls (for security)
|
|
|
|
ws.logger.Debug(
|
|
|
|
"authenticateListener: Ignoring already authenticated session",
|
|
|
|
mlog.String("userID", wsSession.userID),
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.Stringer("client", wsSession.conn.RemoteAddr()),
|
2021-07-19 11:34:17 +02:00
|
|
|
)
|
|
|
|
return
|
2021-02-02 22:17:34 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// Authenticate session
|
|
|
|
userID := ws.getUserIDForToken(token)
|
|
|
|
if userID == "" {
|
2022-03-22 15:24:34 +01:00
|
|
|
wsSession.conn.Close()
|
2021-07-19 11:34:17 +02:00
|
|
|
return
|
2021-02-02 22:17:34 +01:00
|
|
|
}
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
// Authenticated
|
|
|
|
wsSession.userID = userID
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.logger.Debug("authenticateListener: Authenticated", mlog.String("userID", userID), mlog.Stringer("client", wsSession.conn.RemoteAddr()))
|
2021-02-02 22:17:34 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// getListenersForBlock returns the listeners subscribed to a
|
|
|
|
// block changes.
|
2022-03-22 15:24:34 +01:00
|
|
|
func (ws *Server) getListenersForBlock(blockID string) []*websocketSession {
|
2021-07-19 11:34:17 +02:00
|
|
|
return ws.listenersByBlock[blockID]
|
|
|
|
}
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// getListenersForTeam returns the listeners subscribed to a
|
|
|
|
// team changes.
|
|
|
|
func (ws *Server) getListenersForTeam(teamID string) []*websocketSession {
|
|
|
|
return ws.listenersByTeam[teamID]
|
|
|
|
}
|
|
|
|
|
|
|
|
// getListenersForTeamAndBoard returns the listeners subscribed to a
|
|
|
|
// team changes and members of a given board.
|
|
|
|
func (ws *Server) getListenersForTeamAndBoard(teamID, boardID string, ensureUsers ...string) []*websocketSession {
|
|
|
|
members, err := ws.store.GetMembersForBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
ws.logger.Error("error getting members for board",
|
|
|
|
mlog.String("method", "getListenersForTeamAndBoard"),
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
memberMap := map[string]bool{}
|
|
|
|
for _, member := range members {
|
|
|
|
memberMap[member.UserID] = true
|
|
|
|
}
|
|
|
|
for _, id := range ensureUsers {
|
|
|
|
memberMap[id] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
memberIDs := []string{}
|
|
|
|
for id := range memberMap {
|
|
|
|
memberIDs = append(memberIDs, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners := []*websocketSession{}
|
|
|
|
for _, memberID := range memberIDs {
|
|
|
|
for _, listener := range ws.listenersByTeam[teamID] {
|
|
|
|
if listener.userID == memberID {
|
|
|
|
listeners = append(listeners, listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return listeners
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// BroadcastBlockDelete broadcasts delete messages to clients.
|
2022-03-22 15:24:34 +01:00
|
|
|
func (ws *Server) BroadcastBlockDelete(teamID, blockID, boardID string) {
|
2021-10-07 13:51:01 +02:00
|
|
|
now := utils.GetMillis()
|
2020-11-06 19:11:01 +01:00
|
|
|
block := model.Block{}
|
|
|
|
block.ID = blockID
|
2022-03-22 15:24:34 +01:00
|
|
|
block.BoardID = boardID
|
2020-11-06 19:11:01 +01:00
|
|
|
block.UpdateAt = now
|
|
|
|
block.DeleteAt = now
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.BroadcastBlockChange(teamID, block)
|
2020-11-06 19:11:01 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// BroadcastBlockChange broadcasts update messages to clients.
|
2022-03-22 15:24:34 +01:00
|
|
|
func (ws *Server) BroadcastBlockChange(teamID string, block model.Block) {
|
2020-11-06 19:11:01 +01:00
|
|
|
blockIDsToNotify := []string{block.ID, block.ParentID}
|
|
|
|
|
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-07-19 11:34:17 +02:00
|
|
|
Block: block,
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
listeners := ws.getListenersForTeamAndBoard(teamID, block.BoardID)
|
|
|
|
ws.logger.Trace("listener(s) for teamID",
|
2021-07-19 11:34:17 +02:00
|
|
|
mlog.Int("listener_count", len(listeners)),
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", block.BoardID),
|
2021-07-19 11:34:17 +02:00
|
|
|
)
|
|
|
|
|
2020-11-06 19:11:01 +01:00
|
|
|
for _, blockID := range blockIDsToNotify {
|
2021-07-19 11:34:17 +02:00
|
|
|
listeners = append(listeners, ws.getListenersForBlock(blockID)...)
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.logger.Trace("listener(s) for blockID",
|
2021-05-29 08:23:10 +02:00
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("blockID", blockID),
|
|
|
|
)
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
2020-11-06 19:11:01 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
for _, listener := range listeners {
|
2022-03-22 15:24:34 +01:00
|
|
|
ws.logger.Debug("Broadcast block change",
|
|
|
|
mlog.String("teamID", teamID),
|
2021-07-19 11:34:17 +02:00
|
|
|
mlog.String("blockID", block.ID),
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.Stringer("remoteAddr", listener.conn.RemoteAddr()),
|
2021-07-19 11:34:17 +02:00
|
|
|
)
|
2021-06-21 11:21:42 +02:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
err := listener.WriteJSON(message)
|
|
|
|
if err != nil {
|
|
|
|
ws.logger.Error("broadcast error", mlog.Err(err))
|
2022-03-22 15:24:34 +01:00
|
|
|
listener.conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *Server) BroadcastCategoryChange(category model.Category) {
|
|
|
|
message := UpdateCategoryMessage{
|
|
|
|
Action: websocketActionUpdateCategory,
|
|
|
|
TeamID: category.TeamID,
|
|
|
|
Category: &category,
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners := ws.getListenersForTeam(category.TeamID)
|
|
|
|
ws.logger.Debug("listener(s) for teamID",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("teamID", category.TeamID),
|
|
|
|
mlog.String("categoryID", category.ID),
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, listener := range listeners {
|
|
|
|
ws.logger.Debug("Broadcast block change",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("teamID", category.TeamID),
|
|
|
|
mlog.String("categoryID", category.ID),
|
|
|
|
mlog.Stringer("remoteAddr", listener.conn.RemoteAddr()),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := listener.WriteJSON(message); err != nil {
|
|
|
|
ws.logger.Error("broadcast category change error", mlog.Err(err))
|
|
|
|
listener.conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *Server) BroadcastCategoryBlockChange(teamID, userID string, blockCategory model.BlockCategoryWebsocketData) {
|
|
|
|
message := UpdateCategoryMessage{
|
|
|
|
Action: websocketActionUpdateCategoryBlock,
|
|
|
|
TeamID: teamID,
|
|
|
|
BlockCategories: &blockCategory,
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners := ws.getListenersForTeam(teamID)
|
|
|
|
ws.logger.Debug("listener(s) for teamID",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("categoryID", blockCategory.CategoryID),
|
|
|
|
mlog.String("blockID", blockCategory.BlockID),
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, listener := range listeners {
|
|
|
|
ws.logger.Debug("Broadcast block change",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("categoryID", blockCategory.CategoryID),
|
|
|
|
mlog.String("blockID", blockCategory.BlockID),
|
|
|
|
mlog.Stringer("remoteAddr", listener.conn.RemoteAddr()),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := listener.WriteJSON(message); err != nil {
|
|
|
|
ws.logger.Error("broadcast category change error", mlog.Err(err))
|
|
|
|
listener.conn.Close()
|
2020-11-06 19:11:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 21:31:02 +02:00
|
|
|
|
|
|
|
// BroadcastConfigChange broadcasts update messages to clients.
|
|
|
|
func (ws *Server) BroadcastConfigChange(clientConfig model.ClientConfig) {
|
|
|
|
message := UpdateClientConfig{
|
|
|
|
Action: websocketActionUpdateConfig,
|
|
|
|
ClientConfig: clientConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners := ws.listeners
|
|
|
|
ws.logger.Debug("broadcasting config change to listener(s)",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
)
|
|
|
|
|
|
|
|
for listener := range listeners {
|
|
|
|
ws.logger.Debug("Broadcast Config change",
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.Stringer("remoteAddr", listener.conn.RemoteAddr()),
|
|
|
|
)
|
|
|
|
err := listener.WriteJSON(message)
|
|
|
|
if err != nil {
|
|
|
|
ws.logger.Error("broadcast error", mlog.Err(err))
|
|
|
|
listener.conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *Server) BroadcastBoardChange(teamID string, board *model.Board) {
|
|
|
|
message := UpdateBoardMsg{
|
|
|
|
Action: websocketActionUpdateBoard,
|
|
|
|
TeamID: teamID,
|
|
|
|
Board: board,
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners := ws.getListenersForTeamAndBoard(teamID, board.ID)
|
|
|
|
ws.logger.Trace("listener(s) for teamID and boardID",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", board.ID),
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, listener := range listeners {
|
|
|
|
ws.logger.Debug("Broadcast board change",
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", board.ID),
|
|
|
|
mlog.Stringer("remoteAddr", listener.conn.RemoteAddr()),
|
2021-09-16 21:31:02 +02:00
|
|
|
)
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
err := listener.WriteJSON(message)
|
|
|
|
if err != nil {
|
|
|
|
ws.logger.Error("broadcast error", mlog.Err(err))
|
|
|
|
listener.conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *Server) BroadcastBoardDelete(teamID, boardID string) {
|
|
|
|
now := utils.GetMillis()
|
|
|
|
board := &model.Board{}
|
|
|
|
board.ID = boardID
|
|
|
|
board.TeamID = teamID
|
|
|
|
board.UpdateAt = now
|
|
|
|
board.DeleteAt = now
|
|
|
|
|
|
|
|
ws.BroadcastBoardChange(teamID, board)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *Server) BroadcastMemberChange(teamID, boardID string, member *model.BoardMember) {
|
|
|
|
message := UpdateMemberMsg{
|
|
|
|
Action: websocketActionUpdateMember,
|
|
|
|
TeamID: teamID,
|
|
|
|
Member: member,
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners := ws.getListenersForTeamAndBoard(teamID, boardID)
|
|
|
|
ws.logger.Trace("listener(s) for teamID and boardID",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, listener := range listeners {
|
|
|
|
ws.logger.Debug("Broadcast member change",
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
mlog.Stringer("remoteAddr", listener.conn.RemoteAddr()),
|
|
|
|
)
|
|
|
|
|
|
|
|
err := listener.WriteJSON(message)
|
|
|
|
if err != nil {
|
|
|
|
ws.logger.Error("broadcast error", mlog.Err(err))
|
|
|
|
listener.conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *Server) BroadcastMemberDelete(teamID, boardID, userID string) {
|
|
|
|
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
|
|
|
|
listeners := ws.getListenersForTeamAndBoard(teamID, boardID, userID)
|
|
|
|
ws.logger.Trace("listener(s) for teamID and boardID",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, listener := range listeners {
|
|
|
|
ws.logger.Debug("Broadcast member removal",
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
mlog.Stringer("remoteAddr", listener.conn.RemoteAddr()),
|
|
|
|
)
|
|
|
|
|
2021-09-16 21:31:02 +02:00
|
|
|
err := listener.WriteJSON(message)
|
|
|
|
if err != nil {
|
|
|
|
ws.logger.Error("broadcast error", mlog.Err(err))
|
2022-03-22 15:24:34 +01:00
|
|
|
listener.conn.Close()
|
2021-09-16 21:31:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-10 16:46:37 +01:00
|
|
|
|
|
|
|
func (ws *Server) BroadcastSubscriptionChange(workspaceID string, subscription *model.Subscription) {
|
|
|
|
// not implemented for standalone server.
|
|
|
|
}
|