Update grid socket middleware to support V2 views
This commit is contained in:
parent
1ddd0bca21
commit
a6da5fd2bb
|
@ -7,7 +7,7 @@ export const createGridWebsocket = context => {
|
||||||
const socket = createWebsocket("/socket/grid")
|
const socket = createWebsocket("/socket/grid")
|
||||||
|
|
||||||
const connectToDatasource = datasource => {
|
const connectToDatasource = datasource => {
|
||||||
if (!socket.connected || datasource?.type !== "table") {
|
if (!socket.connected) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Identify which table we are editing
|
// Identify which table we are editing
|
||||||
|
|
|
@ -20,53 +20,49 @@ export default class GridSocket extends BaseSocket {
|
||||||
|
|
||||||
async onConnect(socket: Socket) {
|
async onConnect(socket: Socket) {
|
||||||
// Initial identification of connected spreadsheet
|
// Initial identification of connected spreadsheet
|
||||||
socket.on(
|
socket.on(GridSocketEvent.SelectDatasource, async (payload, callback) => {
|
||||||
GridSocketEvent.SelectDatasource,
|
const ds = payload.datasource
|
||||||
async ({ datasource, appId }, callback) => {
|
const appId = payload.appId
|
||||||
if (datasource?.type !== "table") {
|
const resourceId = ds?.type === "table" ? ds?.tableId : ds?.id
|
||||||
return
|
|
||||||
}
|
|
||||||
const tableId = datasource.tableId
|
|
||||||
|
|
||||||
// Ignore if no table or app specified
|
// Ignore if no table or app specified
|
||||||
if (!tableId || !appId) {
|
if (!resourceId || !appId) {
|
||||||
socket.disconnect(true)
|
socket.disconnect(true)
|
||||||
return
|
return
|
||||||
}
|
|
||||||
|
|
||||||
// Create context
|
|
||||||
const ctx = createContext(this.app, socket, {
|
|
||||||
resourceId: tableId,
|
|
||||||
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
|
|
||||||
const room = `${appId}-${tableId}`
|
|
||||||
await this.joinRoom(socket, room)
|
|
||||||
|
|
||||||
// Reply with all users in current room
|
|
||||||
const sessions = await this.getRoomSessions(room)
|
|
||||||
callback({ users: sessions })
|
|
||||||
})
|
|
||||||
} catch (error) {
|
|
||||||
socket.disconnect(true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
// Create context
|
||||||
|
const ctx = createContext(this.app, socket, {
|
||||||
|
resourceId,
|
||||||
|
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
|
||||||
|
const room = `${appId}-${resourceId}`
|
||||||
|
await this.joinRoom(socket, room)
|
||||||
|
|
||||||
|
// Reply with all users in current room
|
||||||
|
const sessions = await this.getRoomSessions(room)
|
||||||
|
callback({ users: sessions })
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
socket.disconnect(true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Handle users selecting a new cell
|
// Handle users selecting a new cell
|
||||||
socket.on(GridSocketEvent.SelectCell, ({ cellId }) => {
|
socket.on(GridSocketEvent.SelectCell, ({ cellId }) => {
|
||||||
|
|
Loading…
Reference in New Issue