Skip to content

Commit

Permalink
moved data counters to @rocket.chat/meteor
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-lehnen-rc committed Sep 22, 2023
1 parent 3e78a0e commit d5ded36
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
6 changes: 6 additions & 0 deletions apps/meteor/ee/app/license/server/startup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as License from '@rocket.chat/license';
import { Subscriptions, Users } from '@rocket.chat/models';

import { settings } from '../../../../app/settings/server';
import { callbacks } from '../../../../lib/callbacks';
Expand All @@ -14,5 +15,10 @@ callbacks.add('workspaceLicenseChanged', async (updatedLicense) => {
await License.setLicense(updatedLicense);
});

License.setLicenseLimitCounter('activeUsers', () => Users.getActiveLocalUserCount());
License.setLicenseLimitCounter('guestUsers', () => Users.getActiveLocalGuestCount());
License.setLicenseLimitCounter('roomsPerGuest', async (context) => (context?.userId ? Subscriptions.countByUserId(context.userId) : 0));
License.setLicenseLimitCounter('privateApps', () => getAppCount('private'));
License.setLicenseLimitCounter('marketplaceApps', () => getAppCount('marketplace'));
// #TODO: Get real value
License.setLicenseLimitCounter('monthlyActiveContacts', async () => 0);
3 changes: 1 addition & 2 deletions ee/packages/license/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"dependencies": {
"@rocket.chat/core-typings": "workspace:^",
"@rocket.chat/jwt": "workspace:^",
"@rocket.chat/logger": "workspace:^",
"@rocket.chat/models": "workspace:^"
"@rocket.chat/logger": "workspace:^"
}
}
5 changes: 3 additions & 2 deletions ee/packages/license/src/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { clearPendingLicense, hasPendingLicense, isPendingLicense, setPendingLic
import { showLicense } from './showLicense';
import { replaceTags } from './tags';
import { convertToV3 } from './v2/convertToV3';
import { hasAllDataCounters } from './validation/getCurrentValueForLicenseLimit';
import { getModulesToDisable } from './validation/getModulesToDisable';
import { isBehaviorsInResult } from './validation/isBehaviorsInResult';
import { runValidation } from './validation/runValidation';
Expand Down Expand Up @@ -88,8 +89,8 @@ const setLicenseV3 = async (newLicense: ILicenseV3, encryptedLicense: string, or
const setLicenseV2 = async (newLicense: ILicenseV2, encryptedLicense: string) =>
setLicenseV3(convertToV3(newLicense), encryptedLicense, newLicense);

// Can only validate licenses once the workspace URL is set
export const isReadyForValidation = () => Boolean(getWorkspaceUrl());
// Can only validate licenses once the workspace URL and the data counter functions are set
export const isReadyForValidation = () => Boolean(getWorkspaceUrl() && hasAllDataCounters());

export const setLicense = async (encryptedLicense: string, forceSet = false): Promise<boolean> => {
if (!encryptedLicense || String(encryptedLicense).trim() === '') {
Expand Down
2 changes: 1 addition & 1 deletion ee/packages/license/src/pendingLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const setPendingLicense = (encryptedLicense: string) => {
export const applyPendingLicense = async () => {
if (pendingLicense) {
logger.info('Applying pending license.');
await setLicense(pendingLicense, true);
await setLicense(pendingLicense);
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type { IUser } from '@rocket.chat/core-typings';
import { Subscriptions, Users } from '@rocket.chat/models';

import type { LicenseLimitKind } from '../definition/ILicenseV3';
import { logger } from '../logger';
import { applyPendingLicense, hasPendingLicense } from '../pendingLicense';

type LimitContext<T extends LicenseLimitKind> = T extends 'roomsPerGuest' ? { userId: IUser['_id'] } : Record<string, never>;

const dataCounters = new Map<LicenseLimitKind, (context?: LimitContext<LicenseLimitKind>) => Promise<number>>();

export const setLicenseLimitCounter = <T extends LicenseLimitKind>(limitKey: T, fn: (context?: LimitContext<T>) => Promise<number>) => {
dataCounters.set(limitKey, fn as (context?: LimitContext<LicenseLimitKind>) => Promise<number>);
};

setLicenseLimitCounter('activeUsers', () => Users.getActiveLocalUserCount());
setLicenseLimitCounter('guestUsers', () => Users.getActiveLocalGuestCount());
setLicenseLimitCounter('roomsPerGuest', async (context) => (context?.userId ? Subscriptions.countByUserId(context.userId) : 0));
if (hasPendingLicense() && hasAllDataCounters()) {
void applyPendingLicense();
}
};

export const getCurrentValueForLicenseLimit = async <T extends LicenseLimitKind>(
limitKey: T,
Expand All @@ -28,3 +28,8 @@ export const getCurrentValueForLicenseLimit = async <T extends LicenseLimitKind>

return 0;
};

export const hasAllDataCounters = () =>
(['activeUsers', 'guestUsers', 'roomsPerGuest', 'privateApps', 'marketplaceApps', 'monthlyActiveContacts'] as LicenseLimitKind[]).every(
(limitKey) => dataCounters.has(limitKey),
);
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8397,7 +8397,6 @@ __metadata:
"@rocket.chat/core-typings": "workspace:^"
"@rocket.chat/jwt": "workspace:^"
"@rocket.chat/logger": "workspace:^"
"@rocket.chat/models": "workspace:^"
"@types/jest": ~29.5.3
eslint: ~8.45.0
jest: ~29.6.1
Expand Down

0 comments on commit d5ded36

Please sign in to comment.