From 1a8da1c0afba51d1dcfbda92da67d026d70cbae2 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 17 Jul 2023 14:56:09 +0100 Subject: [PATCH] Move app enrichment of user sessions to users SDK --- .../server/src/api/controllers/application.ts | 27 ++----------- packages/server/src/sdk/users/index.ts | 2 + packages/server/src/sdk/users/sessions.ts | 38 +++++++++++++++++++ 3 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 packages/server/src/sdk/users/sessions.ts diff --git a/packages/server/src/api/controllers/application.ts b/packages/server/src/api/controllers/application.ts index 666dacc28c..2c53d3ad35 100644 --- a/packages/server/src/api/controllers/application.ts +++ b/packages/server/src/api/controllers/application.ts @@ -199,31 +199,10 @@ export async function fetch(ctx: UserCtx) { } } - // Get all builder sessions in each app - const sessions = await builderSocket?.getRoomSessions(appIds) - if (sessions?.length) { - let appSessionMap: Record = {} - sessions.forEach(session => { - const room = session.room - if (!room) { - return - } - if (!appSessionMap[room]) { - appSessionMap[room] = [] - } - appSessionMap[room].push(session) - }) - apps.forEach(app => { - const sessions = appSessionMap[app.appId] - if (sessions?.length) { - app.sessions = sessions - } else { - delete app.sessions - } - }) - } + // Enrich apps with all builder user sessions + const enrichedApps = await sdk.users.sessions.enrichApps(apps) - ctx.body = await checkAppMetadata(apps) + ctx.body = await checkAppMetadata(enrichedApps) } export async function fetchAppDefinition(ctx: UserCtx) { diff --git a/packages/server/src/sdk/users/index.ts b/packages/server/src/sdk/users/index.ts index c8431f7e61..c03eab7c2a 100644 --- a/packages/server/src/sdk/users/index.ts +++ b/packages/server/src/sdk/users/index.ts @@ -1,5 +1,7 @@ import * as utils from "./utils" +import * as sessions from "./sessions" export default { ...utils, + sessions, } diff --git a/packages/server/src/sdk/users/sessions.ts b/packages/server/src/sdk/users/sessions.ts new file mode 100644 index 0000000000..c413242277 --- /dev/null +++ b/packages/server/src/sdk/users/sessions.ts @@ -0,0 +1,38 @@ +import { builderSocket } from "../../websockets" +import { App, SocketSession } from "@budibase/types" + +export const enrichApps = async (apps: App[]) => { + // Sessions can only exist for dev app IDs + const devAppIds = apps + .filter((app: any) => app.status === "development") + .map((app: any) => app.appId) + + // Get all sessions for all apps and enrich app list + const sessions = await builderSocket?.getRoomSessions(devAppIds) + if (sessions?.length) { + let appSessionMap: Record = {} + sessions.forEach(session => { + const room = session.room + if (!room) { + return + } + if (!appSessionMap[room]) { + appSessionMap[room] = [] + } + appSessionMap[room].push(session) + }) + return apps.map(app => { + // Shallow clone to avoid mutating original reference + let enriched = { ...app } + const sessions = appSessionMap[app.appId] + if (sessions?.length) { + enriched.sessions = sessions + } else { + delete enriched.sessions + } + return enriched + }) + } else { + return apps + } +}