Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/strange-ants-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rocket.chat/model-typings': patch
'@rocket.chat/models': patch
'@rocket.chat/meteor': patch
---

Makes roomsPerGuest exclude DMs when counting subscriptions, ensuring guest limits apply only to non-DM rooms as per expected behavior.
4 changes: 3 additions & 1 deletion apps/meteor/ee/app/license/server/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export const startLicense = async () => {

License.setLicenseLimitCounter('activeUsers', () => Users.getActiveLocalUserCount());
License.setLicenseLimitCounter('guestUsers', () => Users.getActiveLocalGuestCount());
License.setLicenseLimitCounter('roomsPerGuest', async (context) => (context?.userId ? Subscriptions.countByUserId(context.userId) : 0));
License.setLicenseLimitCounter('roomsPerGuest', async (context) =>
context?.userId ? Subscriptions.countByUserIdExceptType(context.userId, 'd') : 0,
);
License.setLicenseLimitCounter('privateApps', () => getAppCount('private'));
License.setLicenseLimitCounter('marketplaceApps', () => getAppCount('marketplace'));
License.setLicenseLimitCounter('monthlyActiveContacts', () => LivechatContacts.countContactsOnPeriod(moment.utc().format('YYYY-MM')));
Expand Down
4 changes: 3 additions & 1 deletion apps/meteor/ee/server/startup/maxRoomsPerGuest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ callbacks.add(
'beforeAddedToRoom',
async ({ user }) => {
if (user.roles?.includes('guest')) {
if (await License.shouldPreventAction('roomsPerGuest', 0, { userId: user._id })) {
// extraCount = 1 checks if adding one more room would exceed the limit
// (not if they've already exceeded it, since this runs before adding them to the room)
if (await License.shouldPreventAction('roomsPerGuest', 1, { userId: user._id })) {
throw new Meteor.Error('error-max-rooms-per-guest-reached', i18n.t('error-max-rooms-per-guest-reached'));
}
}
Expand Down
26 changes: 26 additions & 0 deletions ee/packages/license/src/license.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,32 @@ describe('Validate License Limits', () => {
expect(fairUsageCallback).toHaveBeenCalledTimes(0);
expect(preventActionCallback).toHaveBeenCalledTimes(0);
});

it('should check roomsPerGuest with per-user context', async () => {
const licenseManager = await getReadyLicenseManager();
const license = new MockedLicenseBuilder().withLimits('roomsPerGuest', [
{
max: 3,
behavior: 'prevent_action',
},
]);

await expect(licenseManager.setLicense(await license.sign())).resolves.toBe(true);

licenseManager.setLicenseLimitCounter('roomsPerGuest', (context) => {
switch (context?.userId) {
case 'user1':
return 2;
case 'user2':
return 3;
default:
return 0;
}
});

await expect(licenseManager.shouldPreventAction('roomsPerGuest', 1, { userId: 'user1' })).resolves.toBe(false);
await expect(licenseManager.shouldPreventAction('roomsPerGuest', 1, { userId: 'user2' })).resolves.toBe(true);
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/model-typings/src/models/ISubscriptionsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export interface ISubscriptionsModel extends IBaseModel<ISubscription> {

countByRoomIdAndRoles(roomId: string, roles: string[]): Promise<number>;
countByRoomId(roomId: string, options?: CountDocumentsOptions): Promise<number>;
countByUserId(userId: string): Promise<number>;
countByUserIdExceptType(userId: string, typeException: ISubscription['t']): Promise<number>;
openByRoomIdAndUserId(roomId: string, userId: string): Promise<UpdateResult>;
countByRoomIdAndNotUserId(rid: string, uid: string): Promise<number>;
countByRoomIdWhenUsernameExists(rid: string): Promise<number>;
Expand Down
7 changes: 5 additions & 2 deletions packages/models/src/models/Subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,8 +1172,11 @@ export class SubscriptionsRaw extends BaseRaw<ISubscription> implements ISubscri
return this.countDocuments(query);
}

countByUserId(userId: string): Promise<number> {
const query = { 'u._id': userId };
countByUserIdExceptType(userId: string, typeException: ISubscription['t']): Promise<number> {
const query: Filter<ISubscription> = {
'u._id': userId,
't': { $ne: typeException },
};

return this.countDocuments(query);
}
Expand Down
Loading