From 1403ee59ce4108654b6e814afc29fe26f667bcc8 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 21 Jun 2023 08:51:43 +0100 Subject: [PATCH] Use app ID as part of room key for grid websocket to fix issues with dev/prod tables having the same table ID but being different tables --- packages/server/src/websockets/grid.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/server/src/websockets/grid.ts b/packages/server/src/websockets/grid.ts index a1f790fc68..2a797623cf 100644 --- a/packages/server/src/websockets/grid.ts +++ b/packages/server/src/websockets/grid.ts @@ -20,6 +20,12 @@ export default class GridSocket extends BaseSocket { socket.on( GridSocketEvent.SelectTable, async ({ tableId, appId }, callback) => { + // Ignore if no table or app specified + if (!tableId || !appId) { + socket.disconnect(true) + return + } + // Check if the user has permission to read this resource const middleware = authorized( PermissionType.TABLE, @@ -42,10 +48,11 @@ export default class GridSocket extends BaseSocket { } await context.doInAppContext(appId, async () => { await middleware(ctx, async () => { - await this.joinRoom(socket, tableId) + const room = `${appId}-${tableId}` + await this.joinRoom(socket, room) // Reply with all users in current room - const sessions = await this.getRoomSessions(tableId) + const sessions = await this.getRoomSessions(room) callback({ users: sessions }) }) }) @@ -60,7 +67,8 @@ export default class GridSocket extends BaseSocket { emitRowUpdate(ctx: any, row: Row) { const tableId = getTableId(ctx) - this.emitToRoom(ctx, tableId, GridSocketEvent.RowChange, { + const room = `${ctx.appId}-${tableId}` + this.emitToRoom(ctx, room, GridSocketEvent.RowChange, { id: row._id, row, }) @@ -68,17 +76,20 @@ export default class GridSocket extends BaseSocket { emitRowDeletion(ctx: any, id: string) { const tableId = getTableId(ctx) - this.emitToRoom(ctx, tableId, GridSocketEvent.RowChange, { id, row: null }) + const room = `${ctx.appId}-${tableId}` + this.emitToRoom(ctx, room, GridSocketEvent.RowChange, { id, row: null }) } emitTableUpdate(ctx: any, table: Table) { - this.emitToRoom(ctx, table._id!, GridSocketEvent.TableChange, { + const room = `${ctx.appId}-${table._id}` + this.emitToRoom(ctx, room, GridSocketEvent.TableChange, { id: table._id, table, }) } emitTableDeletion(ctx: any, id: string) { - this.emitToRoom(ctx, id, GridSocketEvent.TableChange, { id, table: null }) + const room = `${ctx.appId}-${id}` + this.emitToRoom(ctx, room, GridSocketEvent.TableChange, { id, table: null }) } }