Skip to content

Commit

Permalink
use license
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Oct 6, 2023
1 parent 184493d commit 5a23e7b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
4 changes: 0 additions & 4 deletions apps/meteor/app/livechat/server/api/v1/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ API.v1.addRoute('livechat/room', {
},
};

if (!(await Omnichannel.checkMACLimit())) {
throw new Error('error-mac-limit-reached');
}

const newRoom = await getRoom({ guest, rid, agent, roomInfo, extraParams });
return API.v1.success(newRoom);
}
Expand Down
9 changes: 7 additions & 2 deletions apps/meteor/ee/app/license/server/startup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { License } from '@rocket.chat/license';
import { Subscriptions, Users } from '@rocket.chat/models';
import { LivechatVisitors, Subscriptions, Users } from '@rocket.chat/models';
import moment from 'moment';

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

const getCurrentPeriod = () => {
moment.utc().format('YYYY-MM');
};

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);
License.setLicenseLimitCounter('monthlyActiveContacts', async () => LivechatVisitors.countVisitorsOnPeriod(getCurrentPeriod()));
17 changes: 8 additions & 9 deletions apps/meteor/server/services/omnichannel/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ServiceClassInternal } from '@rocket.chat/core-services';
import type { IOmnichannelService } from '@rocket.chat/core-services';
import type { AtLeast, IOmnichannelQueue, IOmnichannelRoom } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';
import moment from 'moment';

import { Livechat } from '../../../app/livechat/server';
Expand Down Expand Up @@ -30,16 +31,15 @@ export class OmnichannelService extends ServiceClassInternal implements IOmnicha
}
});

// TODO: Waiting for license definitions
/* this.onEvent('mac.limitreached', async (): Promise<void> => {
// void Livechat.notifyMacLimitReached();
License.onLimitReached('monthlyActiveContacts', async (): Promise<void> => {
void this.api?.broadcast('mac.LimitReached', {});
await this.queueWorker.stop();
});

this.onEvent('license.validated', async (): Promise<void> => {
// void Livechat.notifyLicenseChanged();
License.onValidateLicense(async (): Promise<void> => {
void this.api?.broadcast('mac.limitRestored', {});
await this.queueWorker.shouldStart();
}); */
});
}

async started() {
Expand All @@ -54,13 +54,12 @@ export class OmnichannelService extends ServiceClassInternal implements IOmnicha

async isRoomEnabled(room: AtLeast<IOmnichannelRoom, 'v'>): Promise<boolean> {
const currentMonth = moment.utc().format('YYYY-MM');
// return license.isMacOnLimit() || room.v.activity.includes(currentMonth)
// @ts-expect-error - v.activity
return false || room.v?.activity?.includes(currentMonth);
return room.v?.activity?.includes(currentMonth) || !(await License.shouldPreventAction('monthlyActiveContacts'));
}

async checkMACLimit(): Promise<boolean> {
// return license.isMacOnLimit();
return true;
return !(await License.shouldPreventAction('monthlyActiveContacts'));
}
}
2 changes: 2 additions & 0 deletions packages/core-services/src/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,6 @@ export type EventSignatures = {
'command.updated'(command: string): void;
'command.removed'(command: string): void;
'actions.changed'(): void;
'mac.limitReached'(): void;
'mac.limitRestored'(): void;
};

0 comments on commit 5a23e7b

Please sign in to comment.