Skip to content

Commit 5f075ea

Browse files
Dnouvd-gubert
andauthored
feat(apps): graduate getUserRoomIds experiment to the user bridge (#37547)
Co-authored-by: Douglas Gubert <1810309+d-gubert@users.noreply.github.com>
1 parent 3b92e81 commit 5f075ea

File tree

14 files changed

+54
-77
lines changed

14 files changed

+54
-77
lines changed

.changeset/calm-falcons-gather.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@rocket.chat/apps-engine": minor
3+
"@rocket.chat/meteor": minor
4+
---
5+
6+
Adds the `getUserRoomIds` method to the `UserRead` accessor in the Apps-Engine, graduating it from the experimental bridge to the stable user bridge.
Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,8 @@
11
import type { IAppServerOrchestrator } from '@rocket.chat/apps';
22
import { ExperimentalBridge } from '@rocket.chat/apps-engine/server/bridges';
3-
import { Subscriptions } from '@rocket.chat/models';
4-
5-
import { metrics } from '../../../metrics/server/lib/metrics';
63

74
export class AppExperimentalBridge extends ExperimentalBridge {
8-
constructor(private readonly orch: IAppServerOrchestrator) {
5+
constructor(protected readonly orch: IAppServerOrchestrator) {
96
super();
107
}
11-
12-
protected async getUserRoomIds(userId: string, appId: string): Promise<string[] | undefined> {
13-
const stopTimer = metrics.appBridgeMethods.startTimer({
14-
bridge: 'experimental',
15-
method: 'getUserRoomIds',
16-
app_id: appId,
17-
});
18-
19-
try {
20-
this.orch.debugLog(`The App ${appId} is getting the room ids for the user: "${userId}"`);
21-
22-
const subscriptions = await Subscriptions.findByUserId(userId, { projection: { rid: 1 } }).toArray();
23-
24-
const result = subscriptions.map((subscription) => subscription.rid);
25-
26-
return result;
27-
} finally {
28-
stopTimer();
29-
}
30-
}
318
}

apps/meteor/app/apps/server/bridges/users.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,12 @@ export class AppUserBridge extends UserBridge {
168168
protected async getUserUnreadMessageCount(uid: string): Promise<number> {
169169
return Subscriptions.getBadgeCount(uid);
170170
}
171+
172+
protected async getUserRoomIds(userId: string, appId: string): Promise<string[]> {
173+
this.orch.debugLog(`The App ${appId} is getting the room ids for the user: "${userId}"`);
174+
175+
const subscriptions = await Subscriptions.findByUserId(userId, { projection: { rid: 1 } }).toArray();
176+
177+
return subscriptions.map((subscription) => subscription.rid);
178+
}
171179
}

packages/apps-engine/src/definition/accessors/IExperimentalRead.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,5 @@
55
* team evaluates the proper signature, underlying implementation and performance
66
* impact of candidates for future APIs
77
*/
8-
export interface IExperimentalRead {
9-
/**
10-
* Fetches the IDs of the rooms that the user is a member of.
11-
*
12-
* @returns an array of room ids or undefined if the app doesn't have the proper permission
13-
* @experimental
14-
*/
15-
getUserRoomIds(userId: string): Promise<string[] | undefined>;
16-
}
8+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
9+
export interface IExperimentalRead {}

packages/apps-engine/src/definition/accessors/IUserRead.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ export interface IUserRead {
1919
* @param uid user's id
2020
*/
2121
getUserUnreadMessageCount(uid: string): Promise<number | undefined>;
22+
23+
/**
24+
* Fetches the IDs of the rooms that the user is a member of.
25+
*
26+
* @param userId the user whose memberships should be returned
27+
*/
28+
getUserRoomIds(userId: string): Promise<string[]>;
2229
}

packages/apps-engine/src/server/accessors/ExperimentalRead.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import type { ExperimentalBridge } from '../bridges';
33

44
export class ExperimentalRead implements IExperimentalRead {
55
constructor(
6-
private experimentalBridge: ExperimentalBridge,
7-
private appId: string,
6+
protected readonly experimentalBridge: ExperimentalBridge,
7+
protected readonly appId: string,
88
) {}
9-
10-
public async getUserRoomIds(userId: string): Promise<string[] | undefined> {
11-
return this.experimentalBridge.doGetUserRoomIds(userId, this.appId);
12-
}
139
}

packages/apps-engine/src/server/accessors/UserRead.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ export class UserRead implements IUserRead {
2323
public getUserUnreadMessageCount(uid: string): Promise<number> {
2424
return this.userBridge.doGetUserUnreadMessageCount(uid, this.appId);
2525
}
26+
27+
public getUserRoomIds(userId: string): Promise<string[]> {
28+
return this.userBridge.doGetUserRoomIds(userId, this.appId);
29+
}
2630
}
Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { BaseBridge } from './BaseBridge';
2-
import { PermissionDeniedError } from '../errors/PermissionDeniedError';
3-
import { AppPermissionManager } from '../managers/AppPermissionManager';
4-
import { AppPermissions } from '../permissions/AppPermissions';
52

63
/**
74
* @description
@@ -10,31 +7,4 @@ import { AppPermissions } from '../permissions/AppPermissions';
107
* team evaluates the proper signature, underlying implementation and performance
118
* impact of candidates for future APIs
129
*/
13-
export abstract class ExperimentalBridge extends BaseBridge {
14-
/**
15-
*
16-
* Candidate bridge: User bridge
17-
*/
18-
public async doGetUserRoomIds(userId: string, appId: string): Promise<string[] | undefined> {
19-
if (this.hasPermission('getUserRoomIds', appId)) {
20-
return this.getUserRoomIds(userId, appId);
21-
}
22-
}
23-
24-
protected abstract getUserRoomIds(userId: string, appId: string): Promise<string[] | undefined>;
25-
26-
private hasPermission(feature: keyof typeof AppPermissions.experimental, appId: string): boolean {
27-
if (AppPermissionManager.hasPermission(appId, AppPermissions.experimental[feature])) {
28-
return true;
29-
}
30-
31-
AppPermissionManager.notifyAboutError(
32-
new PermissionDeniedError({
33-
appId,
34-
missingPermissions: [AppPermissions.experimental[feature]],
35-
}),
36-
);
37-
38-
return false;
39-
}
40-
}
10+
export abstract class ExperimentalBridge extends BaseBridge {}

packages/apps-engine/src/server/bridges/UserBridge.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export abstract class UserBridge extends BaseBridge {
4545
}
4646
}
4747

48+
public async doGetUserRoomIds(userId: string, appId: string): Promise<string[]> {
49+
if (this.hasReadPermission(appId)) {
50+
return this.getUserRoomIds(userId, appId);
51+
}
52+
}
53+
4854
public async doDeleteUsersCreatedByApp(appId: string, type: UserType.BOT | UserType.APP): Promise<boolean> {
4955
if (this.hasWritePermission(appId)) {
5056
return this.deleteUsersCreatedByApp(appId, type);
@@ -67,6 +73,8 @@ export abstract class UserBridge extends BaseBridge {
6773

6874
protected abstract getUserUnreadMessageCount(uid: string, appId: string): Promise<number>;
6975

76+
protected abstract getUserRoomIds(userId: string, appId: string): Promise<string[]>;
77+
7078
/**
7179
* Creates a user.
7280
* @param data the essential data for creating a user

packages/apps-engine/src/server/permissions/AppPermissions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export const AppPermissions = {
123123
provide: { name: 'outbound-communication.provide' },
124124
},
125125
'experimental': {
126-
getUserRoomIds: { name: 'experimental.getUserRoomIds' },
126+
default: { name: 'experimental.default' },
127127
},
128128
};
129129

0 commit comments

Comments
 (0)