2023-03-05 19:57:05 +01:00
|
|
|
import authorized from "../middleware/authorized"
|
2023-06-27 12:33:23 +02:00
|
|
|
import currentApp from "../middleware/currentapp"
|
2023-05-31 11:21:50 +02:00
|
|
|
import { BaseSocket } from "./websocket"
|
2023-06-27 12:33:23 +02:00
|
|
|
import { auth, permissions } from "@budibase/backend-core"
|
2023-03-05 19:57:05 +01:00
|
|
|
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"
|
2023-05-31 18:03:14 +02:00
|
|
|
import { GridSocketEvent } from "@budibase/shared-core"
|
2023-06-27 12:33:23 +02:00
|
|
|
import { userAgent } from "koa-useragent"
|
|
|
|
import { createContext, runMiddlewares } from "./middleware"
|
2023-03-05 19:57:05 +01:00
|
|
|
|
2023-06-21 09:33:58 +02:00
|
|
|
const { PermissionType, PermissionLevel } = permissions
|
|
|
|
|
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-06-21 09:33:58 +02:00
|
|
|
super(app, server, "/socket/grid")
|
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
|
2023-06-21 09:33:58 +02:00
|
|
|
socket.on(
|
|
|
|
GridSocketEvent.SelectTable,
|
|
|
|
async ({ tableId, appId }, callback) => {
|
2023-06-21 09:51:43 +02:00
|
|
|
// Ignore if no table or app specified
|
|
|
|
if (!tableId || !appId) {
|
|
|
|
socket.disconnect(true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:33:23 +02:00
|
|
|
// Create context
|
|
|
|
const ctx = createContext(this.app, socket, {
|
2023-06-21 09:33:58 +02:00
|
|
|
resourceId: tableId,
|
2023-06-27 12:33:23 +02:00
|
|
|
appId,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Construct full middleware chain to assess permissions
|
|
|
|
const middlewares = [
|
|
|
|
userAgent,
|
|
|
|
auth.buildAuthMiddleware([], {
|
|
|
|
publicAllowed: true,
|
|
|
|
}),
|
|
|
|
currentApp,
|
|
|
|
authorized(PermissionType.TABLE, PermissionLevel.READ),
|
|
|
|
]
|
|
|
|
|
|
|
|
// Run all koa middlewares
|
|
|
|
try {
|
|
|
|
await runMiddlewares(ctx, middlewares, async () => {
|
|
|
|
// Middlewares are finished and we have permission
|
|
|
|
// Join room for this resource
|
2023-06-21 09:51:43 +02:00
|
|
|
const room = `${appId}-${tableId}`
|
|
|
|
await this.joinRoom(socket, room)
|
2023-03-06 08:43:45 +01:00
|
|
|
|
2023-06-21 09:33:58 +02:00
|
|
|
// Reply with all users in current room
|
2023-06-21 09:51:43 +02:00
|
|
|
const sessions = await this.getRoomSessions(room)
|
2023-06-21 09:33:58 +02:00
|
|
|
callback({ users: sessions })
|
|
|
|
})
|
2023-06-27 12:33:23 +02:00
|
|
|
} catch (error) {
|
|
|
|
socket.disconnect(true)
|
|
|
|
}
|
2023-06-21 09:33:58 +02:00
|
|
|
}
|
|
|
|
)
|
2023-03-05 19:57:05 +01:00
|
|
|
|
2023-05-31 11:21:50 +02:00
|
|
|
// Handle users selecting a new cell
|
2023-06-05 14:54:17 +02:00
|
|
|
socket.on(GridSocketEvent.SelectCell, ({ cellId }) => {
|
2023-05-31 11:21:50 +02:00
|
|
|
this.updateUser(socket, { focusedCellId: cellId })
|
2023-03-05 19:57:05 +01:00
|
|
|
})
|
|
|
|
}
|
2023-05-12 17:13:32 +02:00
|
|
|
|
2023-07-03 12:23:08 +02:00
|
|
|
async updateUser(socket: Socket, patch: Object) {
|
|
|
|
await super.updateUser(socket, {
|
|
|
|
...socket.data.gridMetadata,
|
|
|
|
...patch,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-12 17:13:32 +02:00
|
|
|
emitRowUpdate(ctx: any, row: Row) {
|
|
|
|
const tableId = getTableId(ctx)
|
2023-06-21 09:51:43 +02:00
|
|
|
const room = `${ctx.appId}-${tableId}`
|
|
|
|
this.emitToRoom(ctx, room, GridSocketEvent.RowChange, {
|
2023-06-01 18:14:32 +02:00
|
|
|
id: row._id,
|
|
|
|
row,
|
|
|
|
})
|
2023-05-12 17:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emitRowDeletion(ctx: any, id: string) {
|
|
|
|
const tableId = getTableId(ctx)
|
2023-06-21 09:51:43 +02:00
|
|
|
const room = `${ctx.appId}-${tableId}`
|
|
|
|
this.emitToRoom(ctx, room, GridSocketEvent.RowChange, { id, row: null })
|
2023-05-22 16:59:44 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:14:32 +02:00
|
|
|
emitTableUpdate(ctx: any, table: Table) {
|
2023-06-21 09:51:43 +02:00
|
|
|
const room = `${ctx.appId}-${table._id}`
|
|
|
|
this.emitToRoom(ctx, room, GridSocketEvent.TableChange, {
|
2023-06-01 18:14:32 +02:00
|
|
|
id: table._id,
|
|
|
|
table,
|
|
|
|
})
|
2023-05-22 16:59:44 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:14:32 +02:00
|
|
|
emitTableDeletion(ctx: any, id: string) {
|
2023-06-21 09:51:43 +02:00
|
|
|
const room = `${ctx.appId}-${id}`
|
|
|
|
this.emitToRoom(ctx, room, GridSocketEvent.TableChange, { id, table: null })
|
2023-05-12 17:13:32 +02:00
|
|
|
}
|
2023-03-05 19:57:05 +01:00
|
|
|
}
|