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
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
2020-11-06 20:11:01 +02:00
|
|
|
"time"
|
2020-10-09 11:51:06 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
"github.com/gorilla/mux"
|
2020-10-09 11:51:06 +02:00
|
|
|
"github.com/gorilla/websocket"
|
2020-11-06 20:11:01 +02:00
|
|
|
"github.com/mattermost/mattermost-octo-tasks/server/model"
|
2020-10-09 11:51:06 +02:00
|
|
|
)
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// RegisterRoutes registers routes.
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ws *Server) RegisterRoutes(r *mux.Router) {
|
2020-10-16 16:36:08 +02:00
|
|
|
r.HandleFunc("/ws/onchange", ws.handleWebSocketOnChange)
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// AddListener adds a listener for a block's change.
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ws *Server) AddListener(client *websocket.Conn, blockIDs []string) {
|
2020-10-09 11:51:06 +02:00
|
|
|
ws.mu.Lock()
|
2020-10-21 21:36:43 +02:00
|
|
|
for _, blockID := range blockIDs {
|
|
|
|
if ws.listeners[blockID] == nil {
|
|
|
|
ws.listeners[blockID] = []*websocket.Conn{}
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
ws.listeners[blockID] = append(ws.listeners[blockID], client)
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
ws.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// RemoveListener removes a webSocket listener from all blocks.
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ws *Server) RemoveListener(client *websocket.Conn) {
|
2020-10-09 11:51:06 +02:00
|
|
|
ws.mu.Lock()
|
|
|
|
for key, clients := range ws.listeners {
|
2020-10-22 15:22:36 +02:00
|
|
|
listeners := []*websocket.Conn{}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-09 11:51:06 +02:00
|
|
|
for _, existingClient := range clients {
|
|
|
|
if client != existingClient {
|
|
|
|
listeners = append(listeners, existingClient)
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-09 11:51:06 +02:00
|
|
|
ws.listeners[key] = listeners
|
|
|
|
}
|
|
|
|
ws.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// RemoveListenerFromBlocks removes a webSocket listener from a set of block.
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ws *Server) RemoveListenerFromBlocks(client *websocket.Conn, blockIDs []string) {
|
2020-10-21 21:36:43 +02:00
|
|
|
ws.mu.Lock()
|
|
|
|
|
|
|
|
for _, blockID := range blockIDs {
|
|
|
|
listeners := ws.listeners[blockID]
|
|
|
|
if listeners == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the first instance of this client that's listening to this block
|
|
|
|
// Note: A client can listen multiple times to the same block
|
|
|
|
for index, listener := range listeners {
|
|
|
|
if client == listener {
|
|
|
|
newListeners := append(listeners[:index], listeners[index+1:]...)
|
|
|
|
ws.listeners[blockID] = newListeners
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// GetListeners returns the listeners to a blockID's changes.
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ws *Server) GetListeners(blockID string) []*websocket.Conn {
|
2020-10-09 11:51:06 +02:00
|
|
|
ws.mu.Lock()
|
|
|
|
listeners := ws.listeners[blockID]
|
|
|
|
ws.mu.Unlock()
|
|
|
|
|
|
|
|
return listeners
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// Server is a WebSocket server.
|
2020-10-22 13:34:42 +02:00
|
|
|
type Server struct {
|
2020-10-09 11:51:06 +02:00
|
|
|
upgrader websocket.Upgrader
|
|
|
|
listeners map[string][]*websocket.Conn
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// NewServer creates a new Server.
|
2020-10-22 13:34:42 +02:00
|
|
|
func NewServer() *Server {
|
|
|
|
return &Server{
|
2020-10-09 19:44:12 +02:00
|
|
|
listeners: make(map[string][]*websocket.Conn),
|
2020-10-09 11:51:06 +02:00
|
|
|
upgrader: websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 20:11:01 +02:00
|
|
|
// UpdateMsg is sent on block updates
|
|
|
|
type UpdateMsg struct {
|
|
|
|
Action string `json:"action"`
|
|
|
|
Block model.Block `json:"block"`
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// WebsocketCommand is an incoming command from the client.
|
2020-10-21 21:36:43 +02:00
|
|
|
type WebsocketCommand struct {
|
|
|
|
Action string `json:"action"`
|
|
|
|
BlockIDs []string `json:"blockIds"`
|
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
func (ws *Server) handleWebSocketOnChange(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 {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Auth
|
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
log.Printf("CONNECT WebSocket onChange, client: %s", client.RemoteAddr())
|
2020-10-09 11:51:06 +02:00
|
|
|
|
|
|
|
// Make sure we close the connection when the function returns
|
|
|
|
defer func() {
|
2020-10-21 21:36:43 +02:00
|
|
|
log.Printf("DISCONNECT WebSocket onChange, client: %s", client.RemoteAddr())
|
2020-10-09 11:51:06 +02:00
|
|
|
|
|
|
|
// Remove client from listeners
|
|
|
|
ws.RemoveListener(client)
|
|
|
|
|
|
|
|
client.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Simple message handling loop
|
|
|
|
for {
|
2020-10-21 21:36:43 +02:00
|
|
|
_, p, err := client.ReadMessage()
|
2020-10-09 11:51:06 +02:00
|
|
|
if err != nil {
|
2020-10-21 21:36:43 +02:00
|
|
|
log.Printf("ERROR WebSocket onChange, client: %s, err: %v", client.RemoteAddr(), err)
|
2020-10-09 11:51:06 +02:00
|
|
|
ws.RemoveListener(client)
|
2020-10-22 13:34:42 +02:00
|
|
|
|
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
|
|
|
|
log.Printf(`ERROR webSocket parsing command JSON: %v`, string(p))
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch command.Action {
|
|
|
|
case "ADD":
|
|
|
|
log.Printf(`Command: Add blockID: %v, client: %s`, command.BlockIDs, client.RemoteAddr())
|
|
|
|
ws.AddListener(client, command.BlockIDs)
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
case "REMOVE":
|
|
|
|
log.Printf(`Command: Remove blockID: %v, client: %s`, command.BlockIDs, client.RemoteAddr())
|
|
|
|
ws.RemoveListenerFromBlocks(client, command.BlockIDs)
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
default:
|
|
|
|
log.Printf(`ERROR webSocket command, invalid action: %v`, command.Action)
|
|
|
|
}
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 20:11:01 +02:00
|
|
|
// BroadcastBlockDelete broadcasts delete messages to clients
|
|
|
|
func (ws *Server) BroadcastBlockDelete(blockID string, parentID string) {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
block := model.Block{}
|
|
|
|
block.ID = blockID
|
|
|
|
block.ParentID = parentID
|
|
|
|
block.UpdateAt = now
|
|
|
|
block.DeleteAt = now
|
|
|
|
|
|
|
|
ws.BroadcastBlockChange(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastBlockChange broadcasts update messages to clients
|
|
|
|
func (ws *Server) BroadcastBlockChange(block model.Block) {
|
|
|
|
blockIDsToNotify := []string{block.ID, block.ParentID}
|
|
|
|
|
|
|
|
for _, blockID := range blockIDsToNotify {
|
|
|
|
listeners := ws.GetListeners(blockID)
|
|
|
|
log.Printf("%d listener(s) for blockID: %s", len(listeners), blockID)
|
|
|
|
|
|
|
|
if listeners != nil {
|
|
|
|
message := UpdateMsg{
|
|
|
|
Action: "UPDATE_BLOCK",
|
|
|
|
Block: block,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, listener := range listeners {
|
|
|
|
log.Printf("Broadcast change, blockID: %s, remoteAddr: %s", blockID, listener.RemoteAddr())
|
|
|
|
|
|
|
|
err := listener.WriteJSON(message)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("broadcast error: %v", err)
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|