-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathtables.ts
More file actions
73 lines (67 loc) · 2.32 KB
/
Copy pathtables.ts
File metadata and controls
73 lines (67 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { createLogger } from '@sim/logger'
import type { AuthenticatedSocket } from '@/middleware/auth'
import { verifyTableAccess } from '@/middleware/permissions'
import { type IRoomManager, tableRoomName } from '@/rooms/types'
const logger = createLogger('TableHandlers')
/**
* Wires `join-table` / `leave-table` socket events. Tables don't track presence
* or last-modified state — joining is a thin wrapper around `socket.join` so the
* Sim API → Realtime HTTP bridge can broadcast row updates back to subscribed clients.
*/
export function setupTableHandlers(socket: AuthenticatedSocket, _roomManager: IRoomManager) {
socket.on('join-table', async ({ tableId }: { tableId?: string }) => {
try {
if (!tableId || typeof tableId !== 'string') {
socket.emit('join-table-error', {
tableId: tableId ?? null,
error: 'tableId required',
code: 'INVALID_TABLE_ID',
retryable: false,
})
return
}
const userId = socket.userId
if (!userId) {
socket.emit('join-table-error', {
tableId,
error: 'Authentication required',
code: 'AUTHENTICATION_REQUIRED',
retryable: false,
})
return
}
const { hasAccess } = await verifyTableAccess(userId, tableId)
if (!hasAccess) {
socket.emit('join-table-error', {
tableId,
error: 'Access denied to table',
code: 'ACCESS_DENIED',
retryable: false,
})
return
}
const room = tableRoomName(tableId)
socket.join(room)
socket.emit('join-table-success', { tableId, socketId: socket.id })
logger.debug(`Socket ${socket.id} (user ${userId}) joined ${room}`)
} catch (error) {
logger.error(`Error joining table room:`, error)
socket.emit('join-table-error', {
tableId: null,
error: 'Failed to join table',
code: 'JOIN_TABLE_FAILED',
retryable: true,
})
}
})
socket.on('leave-table', async ({ tableId }: { tableId?: string }) => {
try {
if (!tableId || typeof tableId !== 'string') return
const room = tableRoomName(tableId)
socket.leave(room)
logger.debug(`Socket ${socket.id} left ${room}`)
} catch (error) {
logger.error(`Error leaving table room:`, error)
}
})
}