Skip to content

Commit

Permalink
block meteor methods
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Oct 6, 2023
1 parent 159b0b0 commit 184493d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
6 changes: 3 additions & 3 deletions apps/meteor/app/lib/server/methods/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function executeSendMessage(uid: IUser['_id'], message: AtLeast<IMe
const room = await canSendMessageAsync(rid, { uid, username: user.username, type: user.type });

metrics.messagesSent.inc(); // TODO This line needs to be moved to it's proper place. See the comments on: https://github.com/RocketChat/Rocket.Chat/pull/5736
return sendMessage(user, message, room, false, previewUrls);
return await sendMessage(user, message, room, false, previewUrls);
} catch (err: any) {
SystemLogger.error({ msg: 'Error sending message:', err });

Expand All @@ -107,7 +107,7 @@ declare module '@rocket.chat/ui-contexts' {
}

Meteor.methods<ServerMethods>({
sendMessage(message, previewUrls) {
async sendMessage(message, previewUrls) {
check(message, Object);

const uid = Meteor.userId();
Expand All @@ -118,7 +118,7 @@ Meteor.methods<ServerMethods>({
}

try {
return executeSendMessage(uid, message, previewUrls);
return await executeSendMessage(uid, message, previewUrls);
} catch (error: any) {
if ((error.error || error.message) === 'error-not-allowed') {
throw new Meteor.Error(error.error || error.message, error.reason, {
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/livechat/server/hooks/checkMAC.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Omnichannel } from '@rocket.chat/core-services';
import type { IOmnichannelRoom } from '@rocket.chat/core-typings';
import { isEditedMessage } from '@rocket.chat/core-typings';

import { callbacks } from '../../../../lib/callbacks';
Expand All @@ -16,7 +17,7 @@ callbacks.add('beforeSaveMessage', async (message, room) => {
return message;
}

const canSendMessage = await Omnichannel.isRoomEnabled(room);
const canSendMessage = await Omnichannel.isRoomEnabled(room as IOmnichannelRoom);
if (!canSendMessage) {
throw new Error('error-mac-limit-reached');
}
Expand Down
6 changes: 5 additions & 1 deletion apps/meteor/app/livechat/server/lib/LivechatTyped.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message } from '@rocket.chat/core-services';
import { Message, Omnichannel } from '@rocket.chat/core-services';
import type {
IOmnichannelRoom,
IOmnichannelRoomClosingInfo,
Expand Down Expand Up @@ -463,6 +463,10 @@ class LivechatClass {
throw new Error('error-invalid-room');
}

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

const showAgentInfo = settings.get<string>('Livechat_show_agent_info');
const closingMessage = await Messages.findLivechatClosingMessage(rid, { projection: { ts: 1 } });
const ignoredMessageTypes: MessageTypesValues[] = [
Expand Down
5 changes: 5 additions & 0 deletions apps/meteor/app/livechat/server/methods/returnAsInquiry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Omnichannel } from '@rocket.chat/core-services';
import type { ILivechatDepartment, IRoom } from '@rocket.chat/core-typings';
import { LivechatRooms } from '@rocket.chat/models';
import type { ServerMethods } from '@rocket.chat/ui-contexts';
Expand Down Expand Up @@ -29,6 +30,10 @@ Meteor.methods<ServerMethods>({
});
}

if (!(await Omnichannel.isRoomEnabled(room))) {
throw new Meteor.Error('error-mac-limit-reached', 'MAC limit reached', { method: 'livechat:returnAsInquiry' });
}

if (!room.open) {
throw new Meteor.Error('room-closed', 'Room closed', { method: 'livechat:returnAsInquiry' });
}
Expand Down
5 changes: 5 additions & 0 deletions apps/meteor/app/livechat/server/methods/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Omnichannel } from '@rocket.chat/core-services';
import type { IUser } from '@rocket.chat/core-typings';
import { LivechatVisitors, LivechatRooms, Subscriptions, Users } from '@rocket.chat/models';
import type { ServerMethods } from '@rocket.chat/ui-contexts';
Expand Down Expand Up @@ -49,6 +50,10 @@ Meteor.methods<ServerMethods>({
throw new Meteor.Error('room-closed', 'Room closed', { method: 'livechat:transfer' });
}

if (!(await Omnichannel.isRoomEnabled(room))) {
throw new Meteor.Error('error-mac-limit-reached', 'MAC limit reached', { method: 'livechat:transfer' });
}

const subscription = await Subscriptions.findOneByRoomIdAndUserId(room._id, uid, {
projection: { _id: 1 },
});
Expand Down
8 changes: 5 additions & 3 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 moment from 'moment';

import { Livechat } from '../../../app/livechat/server';
import { RoutingManager } from '../../../app/livechat/server/lib/RoutingManager';
Expand Down Expand Up @@ -51,10 +52,11 @@ export class OmnichannelService extends ServiceClassInternal implements IOmnicha
return this.queueWorker;
}

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

async checkMACLimit(): Promise<boolean> {
Expand Down

0 comments on commit 184493d

Please sign in to comment.