feat(apps): experimental member room IDs read bridge (#37057)
Co-authored-by: Douglas Gubert <1810309+d-gubert@users.noreply.github.com>pull/37254/head
parent
b85e96a2b7
commit
c253db3ece
@ -0,0 +1,6 @@ |
||||
--- |
||||
'@rocket.chat/apps-engine': minor |
||||
'@rocket.chat/meteor': minor |
||||
--- |
||||
|
||||
Adds an experimental API to the apps-engine that retrieves the ids of rooms the user is a member of |
||||
@ -0,0 +1,17 @@ |
||||
import type { IAppServerOrchestrator } from '@rocket.chat/apps'; |
||||
import { ExperimentalBridge } from '@rocket.chat/apps-engine/server/bridges'; |
||||
import { Subscriptions } from '@rocket.chat/models'; |
||||
|
||||
export class AppExperimentalBridge extends ExperimentalBridge { |
||||
constructor(private readonly orch: IAppServerOrchestrator) { |
||||
super(); |
||||
} |
||||
|
||||
protected async getUserRoomIds(userId: string, appId: string): Promise<string[] | undefined> { |
||||
this.orch.debugLog(`The App ${appId} is getting the room ids for the user: "${userId}"`); |
||||
|
||||
const subscriptions = await Subscriptions.findByUserId(userId, { projection: { rid: 1 } }).toArray(); |
||||
|
||||
return subscriptions.map((subscription) => subscription.rid); |
||||
} |
||||
} |
||||
@ -0,0 +1,16 @@ |
||||
/** |
||||
* @description |
||||
* Experimental bridge for experimental features. |
||||
* Methods in this class are not guaranteed to be stable between updates as the |
||||
* team evaluates the proper signature, underlying implementation and performance |
||||
* impact of candidates for future APIs |
||||
*/ |
||||
export interface IExperimentalRead { |
||||
/** |
||||
* Fetches the IDs of the rooms that the user is a member of. |
||||
* |
||||
* @returns an array of room ids or undefined if the app doesn't have the proper permission |
||||
* @experimental |
||||
*/ |
||||
getUserRoomIds(userId: string): Promise<string[] | undefined>; |
||||
} |
||||
@ -0,0 +1,13 @@ |
||||
import type { IExperimentalRead } from '../../definition/accessors'; |
||||
import type { ExperimentalBridge } from '../bridges'; |
||||
|
||||
export class ExperimentalRead implements IExperimentalRead { |
||||
constructor( |
||||
private experimentalBridge: ExperimentalBridge, |
||||
private appId: string, |
||||
) {} |
||||
|
||||
public async getUserRoomIds(userId: string): Promise<string[] | undefined> { |
||||
return this.experimentalBridge.doGetUserRoomIds(userId, this.appId); |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@ |
||||
import { BaseBridge } from './BaseBridge'; |
||||
import { PermissionDeniedError } from '../errors/PermissionDeniedError'; |
||||
import { AppPermissionManager } from '../managers/AppPermissionManager'; |
||||
import { AppPermissions } from '../permissions/AppPermissions'; |
||||
|
||||
/** |
||||
* @description |
||||
* Experimental bridge for experimental features. |
||||
* Methods in this class are not guaranteed to be stable between updates as the |
||||
* team evaluates the proper signature, underlying implementation and performance |
||||
* impact of candidates for future APIs |
||||
*/ |
||||
export abstract class ExperimentalBridge extends BaseBridge { |
||||
/** |
||||
* |
||||
* Candidate bridge: User bridge |
||||
*/ |
||||
public async doGetUserRoomIds(userId: string, appId: string): Promise<string[] | undefined> { |
||||
if (this.hasPermission('getUserRoomIds', appId)) { |
||||
return this.getUserRoomIds(userId, appId); |
||||
} |
||||
} |
||||
|
||||
protected abstract getUserRoomIds(userId: string, appId: string): Promise<string[] | undefined>; |
||||
|
||||
private hasPermission(feature: keyof typeof AppPermissions.experimental, appId: string): boolean { |
||||
if (AppPermissionManager.hasPermission(appId, AppPermissions.experimental[feature])) { |
||||
return true; |
||||
} |
||||
|
||||
AppPermissionManager.notifyAboutError( |
||||
new PermissionDeniedError({ |
||||
appId, |
||||
missingPermissions: [AppPermissions.experimental[feature]], |
||||
}), |
||||
); |
||||
|
||||
return false; |
||||
} |
||||
} |
||||
@ -0,0 +1,7 @@ |
||||
import { ExperimentalBridge } from '../../../src/server/bridges'; |
||||
|
||||
export class TestExperimentalBridge extends ExperimentalBridge { |
||||
protected getUserRoomIds(userId: string, appId: string): Promise<string[] | undefined> { |
||||
throw new Error('Method not implemented.'); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue