From 5a23e7b1790909afc38ad79015eb50b090d962ad Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Fri, 29 Sep 2023 11:57:53 -0600 Subject: [PATCH] use license --- apps/meteor/app/livechat/server/api/v1/room.ts | 4 ---- apps/meteor/ee/app/license/server/startup.ts | 9 +++++++-- .../server/services/omnichannel/service.ts | 17 ++++++++--------- packages/core-services/src/Events.ts | 2 ++ 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/apps/meteor/app/livechat/server/api/v1/room.ts b/apps/meteor/app/livechat/server/api/v1/room.ts index be3932a85bd99..43f264226020d 100644 --- a/apps/meteor/app/livechat/server/api/v1/room.ts +++ b/apps/meteor/app/livechat/server/api/v1/room.ts @@ -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); } diff --git a/apps/meteor/ee/app/license/server/startup.ts b/apps/meteor/ee/app/license/server/startup.ts index d3523282d1e87..eb1f216cdd826 100644 --- a/apps/meteor/ee/app/license/server/startup.ts +++ b/apps/meteor/ee/app/license/server/startup.ts @@ -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'; @@ -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())); diff --git a/apps/meteor/server/services/omnichannel/service.ts b/apps/meteor/server/services/omnichannel/service.ts index a01fcf73dd378..58a894477c59f 100644 --- a/apps/meteor/server/services/omnichannel/service.ts +++ b/apps/meteor/server/services/omnichannel/service.ts @@ -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'; @@ -30,16 +31,15 @@ export class OmnichannelService extends ServiceClassInternal implements IOmnicha } }); - // TODO: Waiting for license definitions - /* this.onEvent('mac.limitreached', async (): Promise => { - // void Livechat.notifyMacLimitReached(); + License.onLimitReached('monthlyActiveContacts', async (): Promise => { + void this.api?.broadcast('mac.LimitReached', {}); await this.queueWorker.stop(); }); - this.onEvent('license.validated', async (): Promise => { - // void Livechat.notifyLicenseChanged(); + License.onValidateLicense(async (): Promise => { + void this.api?.broadcast('mac.limitRestored', {}); await this.queueWorker.shouldStart(); - }); */ + }); } async started() { @@ -54,13 +54,12 @@ export class OmnichannelService extends ServiceClassInternal implements IOmnicha async isRoomEnabled(room: AtLeast): Promise { 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 { // return license.isMacOnLimit(); - return true; + return !(await License.shouldPreventAction('monthlyActiveContacts')); } } diff --git a/packages/core-services/src/Events.ts b/packages/core-services/src/Events.ts index 88ca1034b9c98..de3162c88f516 100644 --- a/packages/core-services/src/Events.ts +++ b/packages/core-services/src/Events.ts @@ -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; };