2023-03-05 19:57:05 +01:00
|
|
|
import authorized from "../middleware/authorized"
|
2023-05-31 11:21:50 +02:00
|
|
|
import { BaseSocket } from "./websocket"
|
2023-03-05 19:57:05 +01:00
|
|
|
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-05-31 11:21:50 +02:00
|
|
|
import { Socket } from "socket.io"
|
|
|
|
import { GridSocketEvents } from "@budibase/shared-core"
|
2023-03-05 19:57:05 +01:00
|
|
|
|
2023-05-31 11:21:50 +02:00
|
|
|
export default class GridSocket extends BaseSocket {
|
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-05-31 11:21:50 +02:00
|
|
|
}
|
2023-03-05 19:57:05 +01:00
|
|
|
|
2023-05-31 11:21:50 +02:00
|
|
|
async onConnect(socket: Socket) {
|
|
|
|
// Initial identification of connected spreadsheet
|
|
|
|
socket.on(GridSocketEvents.SelectTable, async (tableId, callback) => {
|
|
|
|
await this.joinRoom(socket, tableId)
|
2023-03-06 08:43:45 +01:00
|
|
|
|
2023-05-31 11:21:50 +02:00
|
|
|
// Reply with all users in current roome
|
|
|
|
const users = await this.getSocketUsers(tableId)
|
|
|
|
callback({ users })
|
|
|
|
})
|
2023-03-05 19:57:05 +01:00
|
|
|
|
2023-05-31 11:21:50 +02:00
|
|
|
// Handle users selecting a new cell
|
|
|
|
socket.on(GridSocketEvents.SelectCell, cellId => {
|
|
|
|
this.updateUser(socket, { focusedCellId: cellId })
|
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-31 11:21:50 +02:00
|
|
|
this.io.in(tableId).emit(GridSocketEvents.RowChange, { id: row._id, row })
|
2023-05-12 17:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emitRowDeletion(ctx: any, id: string) {
|
|
|
|
const tableId = getTableId(ctx)
|
2023-05-31 11:21:50 +02:00
|
|
|
this.io.in(tableId).emit(GridSocketEvents.RowChange, { id, row: null })
|
2023-05-22 16:59:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emitTableUpdate(table: Table) {
|
2023-05-31 11:21:50 +02:00
|
|
|
this.io
|
|
|
|
.in(table._id!)
|
|
|
|
.emit(GridSocketEvents.TableChange, { id: table._id, table })
|
2023-05-22 16:59:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emitTableDeletion(id: string) {
|
2023-05-31 11:21:50 +02:00
|
|
|
this.io.in(id).emit(GridSocketEvents.TableChange, { id, table: null })
|
2023-05-12 17:13:32 +02:00
|
|
|
}
|
2023-03-05 19:57:05 +01:00
|
|
|
}
|