core: gateway: add metadata field

This commit is contained in:
undefined committed 2025-06-16 13:22:21 +08:00
1 parent 436733f621
commit 1bff3ecce0
2 files changed
+16 -11

No files matched your search

+15 -10
View File
@@ -33,24 +33,29 @@ class WebsocketEventsConnectionManagerHandler extends ConnectionHandler {
} }
async message(payload: any) { async message(payload: any) {
if (!['resume', 'subscribe'].includes(payload.operation)) return;
const accept = []; const accept = [];
const reject = []; const reject = [];
const session = payload?.credential const session = payload.credential
? await TokenModel.get(payload.credential, TokenModel.TYPE_SESSION) ? await TokenModel.get(payload.credential, TokenModel.TYPE_SESSION)
: null; : null;
if (!session && payload.operation !== 'resume') return; const op = payload.operation || '';
if (payload.operation === 'resume' && !this.privileged) return; if (!session && op !== 'resume') return;
const user = payload.operation === 'resume' ? null : await UserModel.getById('system', session.uid); if (op === 'resume' && !this.privileged) return;
for (const channel of payload.channels) { const user = op === 'resume' ? null : await UserModel.getById('system', session.uid);
for (const channel of payload.channels || []) {
try { try {
const result = payload.operation === 'resume' const result = op === 'resume'
? { ok: true, channel } ? { ok: true, channel }
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
: await this.ctx.bail('subscription/subscribe', channel, user); : await this.ctx.bail('subscription/subscribe', channel, user, this.privileged ? payload.metadata || {} : {});
if (result?.ok) accept.push(result.channel); if (result?.ok) {
else reject.push(channel); // eslint-disable-next-line no-await-in-loop
await this.accept(result.channel);
accept.push(result.channel);
} else reject.push(channel);
} catch (e) { } catch (e) {
logger.error('Error subscribing to channel %s for user %s: %s', channel, user._id, e); logger.error('Error subscribing to channel %s for user %s: %s', channel, user?._id, e);
reject.push(channel); reject.push(channel);
} }
} }
+1 -1
View File
@@ -23,7 +23,7 @@ export interface EventMap {
'app/reload': (entries: Set<string>) => VoidReturn 'app/reload': (entries: Set<string>) => VoidReturn
'subscription/init': (h: ConnectionHandler<Context>, privileged: boolean) => VoidReturn 'subscription/init': (h: ConnectionHandler<Context>, privileged: boolean) => VoidReturn
'subscription/subscribe': (channel: string, user: User) => VoidReturn 'subscription/subscribe': (channel: string, user: User, metadata: Record<string, string>) => VoidReturn
'subscription/enable': (channel: string, h: ConnectionHandler<Context>, privileged: boolean) => VoidReturn 'subscription/enable': (channel: string, h: ConnectionHandler<Context>, privileged: boolean) => VoidReturn
'app/watch/change': (path: string) => VoidReturn 'app/watch/change': (path: string) => VoidReturn