2023-03-05 19:57:05 +01:00
|
|
|
import authorized from "../middleware/authorized"
|
|
|
|
import Socket from "./websocket"
|
|
|
|
import { permissions } from "@budibase/backend-core"
|
|
|
|
import http from "http"
|
|
|
|
import Koa from "koa"
|
2023-05-12 17:13:32 +02:00
|
|
|
import { getTableId } from "../api/controllers/row/utils"
|
2023-05-22 16:59:44 +02:00
|
|
|
import { Row, Table } from "@budibase/types"
|
2023-03-05 19:57:05 +01:00
|
|
|
|
2023-04-20 09:02:49 +02:00
|
|
|
export default class GridSocket extends Socket {
|
2023-03-05 19:57:05 +01:00
|
|
|
constructor(app: Koa, server: http.Server) {
|
2023-04-20 09:02:49 +02:00
|
|
|
super(app, server, "/socket/grid", [authorized(permissions.BUILDER)])
|
2023-03-05 19:57:05 +01:00
|
|
|
|
|
|
|
this.io.on("connection", socket => {
|
|
|
|
const user = socket.data.user
|
2023-03-06 08:43:45 +01:00
|
|
|
|
|
|
|
// Socket state
|
|
|
|
let currentRoom: string
|
2023-03-05 19:57:05 +01:00
|
|
|
|
2023-04-18 11:46:35 +02:00
|
|
|
// Initial identification of connected spreadsheet
|
|
|
|
socket.on("select-table", async (tableId, callback) => {
|
2023-03-06 12:20:47 +01:00
|
|
|
// Leave current room
|
2023-03-06 08:43:45 +01:00
|
|
|
if (currentRoom) {
|
2023-05-26 10:24:53 +02:00
|
|
|
socket.to(currentRoom).emit("user-disconnect", user)
|
2023-03-06 08:43:45 +01:00
|
|
|
socket.leave(currentRoom)
|
|
|
|
}
|
2023-03-06 12:20:47 +01:00
|
|
|
|
|
|
|
// Join new room
|
2023-03-06 08:43:45 +01:00
|
|
|
currentRoom = tableId
|
2023-03-06 12:20:47 +01:00
|
|
|
socket.join(currentRoom)
|
2023-05-26 10:24:53 +02:00
|
|
|
socket.to(currentRoom).emit("user-update", user)
|
2023-03-06 08:43:45 +01:00
|
|
|
|
2023-03-06 12:20:47 +01:00
|
|
|
// Reply with all users in current room
|
|
|
|
const sockets = await this.io.in(currentRoom).fetchSockets()
|
2023-03-06 08:43:45 +01:00
|
|
|
callback({
|
|
|
|
users: sockets.map(socket => socket.data.user),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-03-06 12:20:47 +01:00
|
|
|
// Handle users selecting a new cell
|
2023-03-06 08:43:45 +01:00
|
|
|
socket.on("select-cell", cellId => {
|
2023-05-15 15:51:54 +02:00
|
|
|
socket.data.user.focusedCellId = cellId
|
2023-03-06 12:20:47 +01:00
|
|
|
if (currentRoom) {
|
2023-05-26 10:24:53 +02:00
|
|
|
socket.to(currentRoom).emit("user-update", user)
|
2023-03-06 12:20:47 +01:00
|
|
|
}
|
2023-03-05 19:57:05 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// Disconnection cleanup
|
2023-03-06 12:20:47 +01:00
|
|
|
socket.on("disconnect", () => {
|
|
|
|
if (currentRoom) {
|
2023-05-26 10:24:53 +02:00
|
|
|
socket.to(currentRoom).emit("user-disconnect", user)
|
2023-03-06 12:20:47 +01:00
|
|
|
}
|
2023-03-05 19:57:05 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2023-05-12 17:13:32 +02:00
|
|
|
|
|
|
|
emitRowUpdate(ctx: any, row: Row) {
|
|
|
|
const tableId = getTableId(ctx)
|
2023-05-22 16:59:44 +02:00
|
|
|
this.io.in(tableId).emit("row-change", { id: row._id, row })
|
2023-05-12 17:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emitRowDeletion(ctx: any, id: string) {
|
|
|
|
const tableId = getTableId(ctx)
|
2023-05-22 16:59:44 +02:00
|
|
|
this.io.in(tableId).emit("row-change", { id, row: null })
|
|
|
|
}
|
|
|
|
|
|
|
|
emitTableUpdate(table: Table) {
|
|
|
|
this.io.in(table._id!).emit("table-change", { id: table._id, table })
|
|
|
|
}
|
|
|
|
|
|
|
|
emitTableDeletion(id: string) {
|
|
|
|
this.io.in(id).emit("table-change", { id, table: null })
|
2023-05-12 17:13:32 +02:00
|
|
|
}
|
2023-03-05 19:57:05 +01:00
|
|
|
}
|