From 7248092bceb9179df9cfd677bfbe6ecd0bbd6a1c Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Mon, 23 Jun 2025 14:48:44 -0300 Subject: [PATCH 1/4] chore: change method for `Roles.isUserInRoles` --- apps/meteor/app/models/client/models/Roles.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/app/models/client/models/Roles.ts b/apps/meteor/app/models/client/models/Roles.ts index aeeb5a5c7eb70..a0242c13b646b 100644 --- a/apps/meteor/app/models/client/models/Roles.ts +++ b/apps/meteor/app/models/client/models/Roles.ts @@ -11,7 +11,7 @@ class RolesCollection extends MinimongoCollection { isUserInRoles(userId: IUser['_id'], roles: IRole['_id'][] | IRole['_id'], scope?: string, ignoreSubscriptions = false) { roles = Array.isArray(roles) ? roles : [roles]; return roles.some((roleId) => { - const role = this.findOne(roleId); + const role = this.state.get(roleId); const roleScope = ignoreSubscriptions ? 'Users' : role?.scope || 'Users'; switch (roleScope) { From 9ac36728b237d58232de580da8e63058d887be4e Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Mon, 23 Jun 2025 14:50:35 -0300 Subject: [PATCH 2/4] chore: adapt `Roles` store --- apps/meteor/app/authorization/client/hasPermission.ts | 3 +-- apps/meteor/client/startup/roles.ts | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/apps/meteor/app/authorization/client/hasPermission.ts b/apps/meteor/app/authorization/client/hasPermission.ts index 4c48ead3bd1fb..9b87ce363a43f 100644 --- a/apps/meteor/app/authorization/client/hasPermission.ts +++ b/apps/meteor/app/authorization/client/hasPermission.ts @@ -32,8 +32,7 @@ const createPermissionValidator = const roles = permission?.roles ?? []; return roles.some((roleId) => { - const role = Models.Roles.findOne(roleId, { fields: { scope: 1 } }); - const roleScope = role?.scope; + const roleScope = Models.Roles.state.get(roleId)?.scope; if (!isValidScope(roleScope)) { return false; diff --git a/apps/meteor/client/startup/roles.ts b/apps/meteor/client/startup/roles.ts index 6ea36a3475f61..f0183c0bb7846 100644 --- a/apps/meteor/client/startup/roles.ts +++ b/apps/meteor/client/startup/roles.ts @@ -20,10 +20,10 @@ Meteor.startup(() => { const events: Record void) | undefined> = { changed: (role) => { delete role.type; - Roles.upsert({ _id: role._id }, role); + Roles.state.store(role); }, removed: (role) => { - Roles.remove({ _id: role._id }); + Roles.state.delete(role._id); }, }; From c39a7cbff4e964a8fe124944acbc1ac70c35ce2b Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Tue, 24 Jun 2025 10:26:30 -0300 Subject: [PATCH 3/4] chore: `createDocumentMapStore` in `Roles` model --- apps/meteor/app/models/client/models/Roles.ts | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/apps/meteor/app/models/client/models/Roles.ts b/apps/meteor/app/models/client/models/Roles.ts index a0242c13b646b..ae9f84fc9a909 100644 --- a/apps/meteor/app/models/client/models/Roles.ts +++ b/apps/meteor/app/models/client/models/Roles.ts @@ -1,17 +1,19 @@ import type { IRole, IUser } from '@rocket.chat/core-typings'; -import { ReactiveVar } from 'meteor/reactive-var'; import { Subscriptions } from './Subscriptions'; import { Users } from './Users'; -import { MinimongoCollection } from '../../../../client/lib/cachedCollections/MinimongoCollection'; +import { createDocumentMapStore } from '../../../../client/lib/cachedCollections/DocumentMapStore'; -class RolesCollection extends MinimongoCollection { - ready = new ReactiveVar(false); - - isUserInRoles(userId: IUser['_id'], roles: IRole['_id'][] | IRole['_id'], scope?: string, ignoreSubscriptions = false) { +/** @deprecated prefer fetching data from the REST API, listening to changes via streamer events, and storing the state in a Tanstack Query */ +export const Roles = { + use: createDocumentMapStore(), + get state() { + return Roles.use.getState(); + }, + isUserInRoles: (userId: IUser['_id'], roles: IRole['_id'][] | IRole['_id'], scope?: string, ignoreSubscriptions = false) => { roles = Array.isArray(roles) ? roles : [roles]; return roles.some((roleId) => { - const role = this.state.get(roleId); + const role = Roles.state.get(roleId); const roleScope = ignoreSubscriptions ? 'Users' : role?.scope || 'Users'; switch (roleScope) { @@ -25,8 +27,5 @@ class RolesCollection extends MinimongoCollection { return false; } }); - } -} - -/** @deprecated new code refer to Minimongo collections like this one; prefer fetching data from the REST API, listening to changes via streamer events, and storing the state in a Tanstack Query */ -export const Roles = new RolesCollection(); + }, +}; From 3bc880f9703c987b3cd38c9306b7842e0ab37022 Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Tue, 24 Jun 2025 10:26:45 -0300 Subject: [PATCH 4/4] chore: remove Roles.ready --- apps/meteor/client/startup/roles.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/meteor/client/startup/roles.ts b/apps/meteor/client/startup/roles.ts index f0183c0bb7846..cd6a1e2be5e49 100644 --- a/apps/meteor/client/startup/roles.ts +++ b/apps/meteor/client/startup/roles.ts @@ -11,8 +11,6 @@ Meteor.startup(() => { const { roles } = await sdk.rest.get('/v1/roles.list'); // if a role is checked before this collection is populated, it will return undefined Roles.state.replaceAll(roles); - - Roles.ready.set(true); }); type ClientAction = 'inserted' | 'updated' | 'removed' | 'changed';