Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create afterOmnichannelSaveMessage #32970

Merged
merged 1 commit into from
Aug 2, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { isOmnichannelRoom } from '@rocket.chat/core-typings';

import { callbacks } from '../../../../lib/callbacks';

callbacks.add(
'afterSaveMessage',
async (message, room) => {
// only call webhook if it is a livechat room
if (!isOmnichannelRoom(room)) {
return message;
}
return callbacks.run('afterOmnichannelSaveMessage', message, { room });
},
callbacks.priority.MEDIUM,
'after-omnichannel-save-message',
);
10 changes: 3 additions & 7 deletions apps/meteor/app/livechat/server/hooks/leadCapture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IMessage, IOmnichannelRoom } from '@rocket.chat/core-typings';
import { isEditedMessage, isOmnichannelRoom } from '@rocket.chat/core-typings';
import { isEditedMessage } from '@rocket.chat/core-typings';
import { LivechatVisitors } from '@rocket.chat/models';

import { callbacks } from '../../../../lib/callbacks';
Expand Down Expand Up @@ -31,12 +31,8 @@ function validateMessage(message: IMessage, room: IOmnichannelRoom) {
}

callbacks.add(
'afterSaveMessage',
async (message, room) => {
if (!isOmnichannelRoom(room)) {
return message;
}

'afterOmnichannelSaveMessage',
async (message, { room }) => {
if (!validateMessage(message, room)) {
return message;
}
Expand Down
12 changes: 4 additions & 8 deletions apps/meteor/app/livechat/server/hooks/markRoomNotResponded.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { isOmnichannelRoom, isEditedMessage } from '@rocket.chat/core-typings';
import { isEditedMessage } from '@rocket.chat/core-typings';
import { LivechatRooms } from '@rocket.chat/models';

import { callbacks } from '../../../../lib/callbacks';

callbacks.add(
'afterSaveMessage',
async (message, room) => {
if (!isOmnichannelRoom(room)) {
return message;
}

'afterOmnichannelSaveMessage',
async (message, { room }) => {
// skips this callback if the message was edited
if (!message || isEditedMessage(message)) {
return message;
Expand All @@ -21,7 +17,7 @@ callbacks.add(
}

// check if room is yet awaiting for response
if (typeof room.t !== 'undefined' && room.t === 'l' && room.waitingResponse) {
if (room.waitingResponse) {
return message;
}

Expand Down
10 changes: 3 additions & 7 deletions apps/meteor/app/livechat/server/hooks/markRoomResponded.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import type { IOmnichannelRoom } from '@rocket.chat/core-typings';
import { isOmnichannelRoom, isEditedMessage } from '@rocket.chat/core-typings';
import { isEditedMessage } from '@rocket.chat/core-typings';
import { LivechatRooms, LivechatVisitors, LivechatInquiry } from '@rocket.chat/models';
import moment from 'moment';

import { callbacks } from '../../../../lib/callbacks';
import { notifyOnLivechatInquiryChanged } from '../../../lib/server/lib/notifyListener';

callbacks.add(
'afterSaveMessage',
async (message, room) => {
if (!isOmnichannelRoom(room)) {
return message;
}

'afterOmnichannelSaveMessage',
async (message, { room }) => {
// skips this callback if the message was edited
if (!message || isEditedMessage(message)) {
return message;
Expand Down
13 changes: 4 additions & 9 deletions apps/meteor/app/livechat/server/hooks/saveAnalyticsData.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { isEditedMessage, isOmnichannelRoom } from '@rocket.chat/core-typings';
import { isEditedMessage, isMessageFromVisitor } from '@rocket.chat/core-typings';
import { LivechatRooms } from '@rocket.chat/models';

import { callbacks } from '../../../../lib/callbacks';
import { normalizeMessageFileUpload } from '../../../utils/server/functions/normalizeMessageFileUpload';

callbacks.add(
'afterSaveMessage',
async (message, room) => {
// check if room is livechat
if (!isOmnichannelRoom(room)) {
return message;
}

'afterOmnichannelSaveMessage',
async (message, { room }) => {
// skips this callback if the message was edited
if (!message || isEditedMessage(message)) {
return message;
}

// if the message has a token, it was sent by the visitor
if (message.token) {
if (isMessageFromVisitor(message)) {
// When visitor sends a mesage, most metrics wont be calculated/served.
// But, v.lq (last query) will be updated to the message time. This has to be done
// As not doing it will cause the metrics to be crazy and not have real values.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isOmnichannelRoom, isEditedMessage } from '@rocket.chat/core-typings';
import { isEditedMessage } from '@rocket.chat/core-typings';
import { LivechatInquiry } from '@rocket.chat/models';

import { callbacks } from '../../../../lib/callbacks';
Expand All @@ -7,9 +7,9 @@ import { settings } from '../../../settings/server';
import { RoutingManager } from '../lib/RoutingManager';

callbacks.add(
'afterSaveMessage',
async (message, room) => {
if (!isOmnichannelRoom(room) || isEditedMessage(message) || message.t) {
'afterOmnichannelSaveMessage',
async (message, { room }) => {
if (isEditedMessage(message) || message.t) {
return message;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { isOmnichannelRoom } from '@rocket.chat/core-typings';
import { isMessageFromVisitor } from '@rocket.chat/core-typings';
import { LivechatRooms } from '@rocket.chat/models';

import { callbacks } from '../../../../lib/callbacks';

callbacks.add(
'afterSaveMessage',
async (message, room) => {
if (!(isOmnichannelRoom(room) && room.v.token)) {
return message;
}
'afterOmnichannelSaveMessage',
async (message, { room }) => {
if (message.t) {
return message;
}
if (!message.token) {
if (!isMessageFromVisitor(message)) {
return message;
}

Expand Down
9 changes: 2 additions & 7 deletions apps/meteor/app/livechat/server/hooks/sendToCRM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,8 @@ callbacks.add(
);

callbacks.add(
'afterSaveMessage',
async (message, room) => {
// only call webhook if it is a livechat room
if (!isOmnichannelRoom(room) || !room?.v?.token) {
return message;
}

'afterOmnichannelSaveMessage',
async (message, { room }) => {
// if the message has a token, it was sent from the visitor
// if not, it was sent from the agent
if (message.token && !settings.get('Livechat_webhook_on_visitor_message')) {
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/app/livechat/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import './hooks/saveContactLastChat';
import './hooks/saveLastMessageToInquiry';
import './hooks/afterUserActions';
import './hooks/afterAgentRemoved';
import './hooks/afterSaveOmnichannelMessage';
import './methods/addAgent';
import './methods/addManager';
import './methods/changeLivechatStatus';
Expand Down
8 changes: 4 additions & 4 deletions apps/meteor/app/livechat/server/sendMessageBySMS.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OmnichannelIntegration } from '@rocket.chat/core-services';
import { isEditedMessage, isOmnichannelRoom } from '@rocket.chat/core-typings';
import { isEditedMessage } from '@rocket.chat/core-typings';
import { LivechatVisitors } from '@rocket.chat/models';

import { callbacks } from '../../../lib/callbacks';
Expand All @@ -8,8 +8,8 @@ import { normalizeMessageFileUpload } from '../../utils/server/functions/normali
import { callbackLogger } from './lib/logger';

callbacks.add(
'afterSaveMessage',
async (message, room) => {
'afterOmnichannelSaveMessage',
async (message, { room }) => {
// skips this callback if the message was edited
if (isEditedMessage(message)) {
return message;
Expand All @@ -20,7 +20,7 @@ callbacks.add(
}

// only send the sms by SMS if it is a livechat room with SMS set to true
if (!(isOmnichannelRoom(room) && room.sms && room.v && room.v.token)) {
if (!(room.sms && room.v && room.v.token)) {
return message;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OmnichannelEEService } from '@rocket.chat/core-services';
import type { ILivechatVisitor, IMessage, IOmnichannelRoom, IRoom, IUser } from '@rocket.chat/core-typings';
import { isEditedMessage, isOmnichannelRoom } from '@rocket.chat/core-typings';
import type { ILivechatVisitor, IMessage, IOmnichannelRoom, IUser } from '@rocket.chat/core-typings';
import { isMessageFromVisitor, isEditedMessage } from '@rocket.chat/core-typings';
import { LivechatRooms, LivechatVisitors, Users } from '@rocket.chat/models';

import { callbackLogger } from '../../../../../app/livechat/server/lib/logger';
Expand All @@ -16,7 +16,7 @@ const resumeOnHoldCommentAndUser = async (room: IOmnichannelRoom): Promise<{ com
projection: { name: 1, username: 1 },
});
if (!visitor) {
callbackLogger.error(`[afterSaveMessage] Visitor Not found for room ${rid} while trying to resume on hold`);
callbackLogger.error(`[afterOmnichannelSaveMessage] Visitor Not found for room ${rid} while trying to resume on hold`);
throw new Error('Visitor not found while trying to resume on hold');
}

Expand All @@ -26,43 +26,46 @@ const resumeOnHoldCommentAndUser = async (room: IOmnichannelRoom): Promise<{ com

const resumedBy = await Users.findOneById('rocket.cat');
if (!resumedBy) {
callbackLogger.error(`[afterSaveMessage] User Not found for room ${rid} while trying to resume on hold`);
callbackLogger.error(`[afterOmnichannelSaveMessage] User Not found for room ${rid} while trying to resume on hold`);
throw new Error(`User not found while trying to resume on hold`);
}

return { comment: resumeChatComment, resumedBy };
};

const handleAfterSaveMessage = async (message: IMessage, room: IRoom) => {
if (isEditedMessage(message) || message.t || !isOmnichannelRoom(room)) {
return message;
}
callbacks.add(
'afterOmnichannelSaveMessage',
async (message: IMessage, { room }) => {
if (isEditedMessage(message) || message.t) {
return message;
}

const { _id: rid, v: roomVisitor } = room;
const { _id: rid, v: roomVisitor } = room;

if (!roomVisitor?._id) {
return message;
}

// Need to read the room every time, the room object is not updated
const updatedRoom = await LivechatRooms.findOneById(rid);
if (!updatedRoom) {
return message;
}

if (message.token && room.onHold) {
callbackLogger.debug(`[afterSaveMessage] Room ${rid} is on hold, resuming it now since visitor sent a message`);
if (!roomVisitor?._id) {
return message;
}

try {
const { comment: resumeChatComment, resumedBy } = await resumeOnHoldCommentAndUser(updatedRoom);
await OmnichannelEEService.resumeRoomOnHold(updatedRoom, resumeChatComment, resumedBy);
} catch (error) {
callbackLogger.error(`[afterSaveMessage] Error while resuming room ${rid} on hold: Error: `, error);
// Need to read the room every time, the room object is not updated
const updatedRoom = await LivechatRooms.findOneById(rid);
if (!updatedRoom) {
return message;
}
}

return message;
};
if (isMessageFromVisitor(message) && room.onHold) {
callbackLogger.debug(`[afterOmnichannelSaveMessage] Room ${rid} is on hold, resuming it now since visitor sent a message`);

try {
const { comment: resumeChatComment, resumedBy } = await resumeOnHoldCommentAndUser(updatedRoom);
await OmnichannelEEService.resumeRoomOnHold(updatedRoom, resumeChatComment, resumedBy);
} catch (error) {
callbackLogger.error(`[afterOmnichannelSaveMessage] Error while resuming room ${rid} on hold: Error: `, error);
return message;
}
}

callbacks.add('afterSaveMessage', handleAfterSaveMessage, callbacks.priority.HIGH, 'livechat-resume-on-hold');
return message;
},
callbacks.priority.HIGH,
'livechat-resume-on-hold',
);
Loading
Loading