Skip to content

Commit

Permalink
refactor: moves functions to new notifyListener format
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogarim committed May 17, 2024
1 parent 1a32c56 commit 63fbc5a
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 112 deletions.
82 changes: 81 additions & 1 deletion apps/meteor/app/lib/server/lib/notifyListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ import type {
IIntegration,
IPbxEvent,
LoginServiceConfiguration as LoginServiceConfigurationData,
ILivechatInquiryRecord,
} from '@rocket.chat/core-typings';
import { Rooms, Permissions, Settings, PbxEvents, Roles, Integrations, LoginServiceConfiguration } from '@rocket.chat/models';
import {
Rooms,
Permissions,
Settings,
PbxEvents,
Roles,
Integrations,
LoginServiceConfiguration,
LivechatInquiry,
} from '@rocket.chat/models';

type ClientAction = 'inserted' | 'updated' | 'removed';

Expand Down Expand Up @@ -240,3 +250,73 @@ export async function notifyOnIntegrationChangedByChannels<T extends IIntegratio
void api.broadcast('watch.integrations', { clientAction, id: item._id, data: item });
}
}

export async function notifyOnLivechatInquiryChanged(
data: ILivechatInquiryRecord | ILivechatInquiryRecord[],
clientAction: ClientAction = 'updated',
diff?: Partial<Record<keyof ILivechatInquiryRecord, unknown> & { queuedAt: unknown; takenAt: unknown }>,
): Promise<void> {
if (!dbWatchersDisabled) {
return;
}

const items = Array.isArray(data)? data : [data];

for (const item of items) {
void api.broadcast('watch.inquiries', { clientAction, inquiry: item, diff });
}
}

export async function notifyOnLivechatInquiryChangedById(
id: ILivechatInquiryRecord['_id'],
clientAction: ClientAction = 'updated',
diff?: Partial<Record<keyof ILivechatInquiryRecord, unknown> & { queuedAt: unknown; takenAt: unknown }>,
): Promise<void> {
if (!dbWatchersDisabled) {
return;
}

const inquiry = clientAction === 'removed' ? await LivechatInquiry.trashFindOneById(id) : await LivechatInquiry.findOneById(id);

if (!inquiry) {
return;
}

void api.broadcast('watch.inquiries', { clientAction, inquiry, diff });
}

export async function notifyOnLivechatInquiryChangedByRoom(
rid: ILivechatInquiryRecord['rid'],
clientAction: ClientAction = 'updated',
diff?: Partial<Record<keyof ILivechatInquiryRecord, unknown> & { queuedAt: unknown; takenAt: unknown }>,
): Promise<void> {
if (!dbWatchersDisabled) {
return;
}

const inquiry = await LivechatInquiry.findOneByRoomId(rid, {});

if (!inquiry) {
return;
}

void api.broadcast('watch.inquiries', { clientAction, inquiry, diff });
}

export async function notifyOnLivechatInquiryChangedByToken(
token: ILivechatInquiryRecord['v']['token'],
clientAction: ClientAction = 'updated',
diff?: Partial<Record<keyof ILivechatInquiryRecord, unknown> & { queuedAt: unknown; takenAt: unknown }>,
): Promise<void> {
if (!dbWatchersDisabled) {
return;
}

const inquiry = await LivechatInquiry.findOneByToken(token);

if (!inquiry) {
return;
}

void api.broadcast('watch.inquiries', { clientAction, inquiry, diff });
}

This file was deleted.

13 changes: 6 additions & 7 deletions apps/meteor/app/livechat/server/hooks/markRoomResponded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LivechatRooms, LivechatVisitors, LivechatInquiry } from '@rocket.chat/m
import moment from 'moment';

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

callbacks.add(
'afterSaveMessage',
Expand Down Expand Up @@ -38,12 +38,11 @@ callbacks.add(
}

if (!room.v?.activity?.includes(monthYear)) {
await Promise.all([
LivechatRooms.markVisitorActiveForPeriod(room._id, monthYear),
LivechatInquiry.markInquiryActiveForPeriod(room._id, monthYear),
]);

void notifyListenerOnLivechatInquiryChangesByRoomId(room._id);
void LivechatRooms.markVisitorActiveForPeriod(room._id, monthYear);
const livechatInquiry = await LivechatInquiry.markInquiryActiveForPeriod(room._id, monthYear);
if (livechatInquiry) {
void notifyOnLivechatInquiryChanged(livechatInquiry, 'updated', { v: livechatInquiry.v });
}
}

if (room.responseBy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isOmnichannelRoom, isEditedMessage } from '@rocket.chat/core-typings';
import { LivechatInquiry } from '@rocket.chat/models';

import { callbacks } from '../../../../lib/callbacks';
import { notifyListenerOnLivechatInquiryChangesByRoomId } from '../../../lib/server/lib/notifyListenerOnLivechatInquiryChanges';
import { notifyOnLivechatInquiryChanged } from '../../../lib/server/lib/notifyListener';
import { settings } from '../../../settings/server';
import { RoutingManager } from '../lib/RoutingManager';

Expand All @@ -22,9 +22,10 @@ callbacks.add(
return message;
}

await LivechatInquiry.setLastMessageByRoomId(room._id, message);

void notifyListenerOnLivechatInquiryChangesByRoomId(room._id);
const livechatInquiry = await LivechatInquiry.setLastMessageByRoomId(room._id, message);
if (livechatInquiry) {
void notifyOnLivechatInquiryChanged(livechatInquiry, 'updated', { lastMessage: message });
}

return message;
},
Expand Down
10 changes: 2 additions & 8 deletions apps/meteor/app/livechat/server/lib/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import type { MatchKeysAndValues, OnlyFieldsOfType } from 'mongodb';

import { callbacks } from '../../../../lib/callbacks';
import { trim } from '../../../../lib/utils/stringUtils';
import { notifyListenerOnLivechatInquiryChangesByRoomId } from '../../../lib/server/lib/notifyListenerOnLivechatInquiryChanges';
import { notifyOnRoomChangedById } from '../../../lib/server/lib/notifyListener';
import { notifyOnRoomChangedById, notifyOnLivechatInquiryChangedByRoom } from '../../../lib/server/lib/notifyListener';
import { i18n } from '../../../utils/lib/i18n';

type RegisterContactProps = {
Expand Down Expand Up @@ -138,19 +137,14 @@ export const Contacts = {
if (rooms?.length) {
for await (const room of rooms) {
const { _id: rid } = room;
if (await Rooms.setFnameById(rid, name)) {
if (await LivechatInquiry.setNameByRoomId(rid, name)) {
void notifyListenerOnLivechatInquiryChangesByRoomId(rid);
await Subscriptions.updateDisplayNameByRoomId(rid, name);
}
}

await Promise.all([
Rooms.setFnameById(rid, name),
LivechatInquiry.setNameByRoomId(rid, name),
Subscriptions.updateDisplayNameByRoomId(rid, name),
]);

void notifyOnLivechatInquiryChangedByRoom(rid, 'updated', { name });
void notifyOnRoomChangedById(rid);
}
}
Expand Down
4 changes: 0 additions & 4 deletions apps/meteor/app/livechat/server/lib/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,6 @@ export const updateChatDepartment = async ({
Subscriptions.changeDepartmentByRoomId(rid, newDepartmentId),
]);

// void notifyListenerOnLivechatInquiryChangesByRoomId(rid);

setImmediate(() => {
void Apps.self?.triggerEvent(AppEvents.IPostLivechatRoomTransferred, {
type: LivechatTransferEventType.DEPARTMENT,
Expand Down Expand Up @@ -631,8 +629,6 @@ export const forwardRoomToDepartment = async (room: IOmnichannelRoom, guest: ILi
throw new Error('error-invalid-inquiry');
}

// void notifyListenerOnLivechatInquiryChanges(inquiry._id);

await queueInquiry(newInquiry);
logger.debug(`Inquiry ${inquiry._id} queued succesfully`);
}
Expand Down
29 changes: 14 additions & 15 deletions apps/meteor/app/livechat/server/lib/LivechatTyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ import { deleteMessage } from '../../../lib/server/functions/deleteMessage';
import { sendMessage } from '../../../lib/server/functions/sendMessage';
import { updateMessage } from '../../../lib/server/functions/updateMessage';
import {
notifyListenerOnLivechatInquiryChangesByRoomId,
notifyListenerOnLivechatInquiryChangesByToken,
} from '../../../lib/server/lib/notifyListenerOnLivechatInquiryChanges';
import { notifyOnRoomChangedById } from '../../../lib/server/lib/notifyListener';
notifyOnLivechatInquiryChanged,
notifyOnLivechatInquiryChangedByRoom,
notifyOnRoomChangedById,
notifyOnLivechatInquiryChangedByToken,
} from '../../../lib/server/lib/notifyListener';
import * as Mailer from '../../../mailer/server/api';
import { metrics } from '../../../metrics/server';
import { settings } from '../../../settings/server';
Expand Down Expand Up @@ -301,7 +302,7 @@ class LivechatClass {
if (removedInquiry && removedInquiry.deletedCount !== 1) {
throw new Error('Error removing inquiry');
}
void notifyListenerOnLivechatInquiryChangesByRoomId(rid, 'removed');
void notifyOnLivechatInquiryChangedByRoom(rid, 'removed');

const updatedRoom = await LivechatRooms.closeRoomById(rid, closeData);
if (!updatedRoom || updatedRoom.modifiedCount !== 1) {
Expand Down Expand Up @@ -513,7 +514,7 @@ class LivechatClass {
LivechatRooms.removeById(rid),
]);

void notifyListenerOnLivechatInquiryChangesByRoomId(rid, 'removed');
void notifyOnLivechatInquiryChangedByRoom(rid, 'removed');

for (const r of result) {
if (r.status === 'rejected') {
Expand Down Expand Up @@ -1272,13 +1273,11 @@ class LivechatClass {
]);
}

await Promise.all([
Subscriptions.removeByVisitorToken(token),
LivechatRooms.removeByVisitorToken(token),
LivechatInquiry.removeByVisitorToken(token),
]);
await Promise.all([Subscriptions.removeByVisitorToken(token), LivechatRooms.removeByVisitorToken(token)]);

void notifyListenerOnLivechatInquiryChangesByToken(token);
const livechatInquiries = await LivechatInquiry.findIdsByVisitorToken(token).toArray();
await LivechatInquiry.removeByIds(livechatInquiries.map(({ _id }) => _id));
void notifyOnLivechatInquiryChanged(livechatInquiries, 'removed');
}

async deleteMessage({ guest, message }: { guest: ILivechatVisitor; message: IMessage }) {
Expand Down Expand Up @@ -1672,7 +1671,7 @@ class LivechatClass {
const inquiryVisitorStatus = await LivechatInquiry.updateVisitorStatus(token, status);

if (inquiryVisitorStatus.modifiedCount) {
void notifyListenerOnLivechatInquiryChangesByToken(token);
void notifyOnLivechatInquiryChangedByToken(token, 'updated', { v: { status } });
}
}

Expand Down Expand Up @@ -1824,12 +1823,12 @@ class LivechatClass {
LivechatInquiry.setNameByRoomId(rid, name),
Subscriptions.updateDisplayNameByRoomId(rid, name),
]);
}

void notifyListenerOnLivechatInquiryChangesByRoomId(rid);
void notifyOnLivechatInquiryChangedByRoom(rid, 'updated', { name });

return true;
}

void notifyOnRoomChangedById(roomData._id);
}

Expand Down
19 changes: 15 additions & 4 deletions apps/meteor/app/livechat/server/lib/QueueManager.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Apps, AppEvents } from '@rocket.chat/apps';
import { Omnichannel } from '@rocket.chat/core-services';
import type { ILivechatInquiryRecord, ILivechatVisitor, IMessage, IOmnichannelRoom, SelectedAgent } from '@rocket.chat/core-typings';
import {
LivechatInquiryStatus,
type ILivechatInquiryRecord,
type ILivechatVisitor,
type IMessage,
type IOmnichannelRoom,
type SelectedAgent,
} from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
import { LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models';
import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

import { callbacks } from '../../../../lib/callbacks';
import { notifyListenerOnLivechatInquiryChanges } from '../../../lib/server/lib/notifyListenerOnLivechatInquiryChanges';
import { notifyOnLivechatInquiryChangedById, notifyOnLivechatInquiryChanged } from '../../../lib/server/lib/notifyListener';
import { checkServiceStatus, createLivechatRoom, createLivechatInquiry } from './Helper';
import { RoutingManager } from './RoutingManager';

Expand All @@ -16,7 +23,11 @@ const logger = new Logger('QueueManager');
export const saveQueueInquiry = async (inquiry: ILivechatInquiryRecord) => {
await LivechatInquiry.queueInquiry(inquiry._id);
await callbacks.run('livechat.afterInquiryQueued', inquiry);
void notifyListenerOnLivechatInquiryChanges(inquiry._id);
void notifyOnLivechatInquiryChanged(inquiry, 'updated', {
status: LivechatInquiryStatus.QUEUED,
queuedAt: new Date(),
takenAt: undefined,
});
};

export const queueInquiry = async (inquiry: ILivechatInquiryRecord, defaultAgent?: SelectedAgent) => {
Expand Down Expand Up @@ -139,7 +150,7 @@ export const QueueManager: queueManager = {
if (oldInquiry) {
logger.debug(`Removing old inquiry (${oldInquiry._id}) for room ${rid}`);
await LivechatInquiry.removeByRoomId(rid);
void notifyListenerOnLivechatInquiryChanges(rid, 'removed');
void notifyOnLivechatInquiryChangedById(rid, 'removed');
}

const guest = {
Expand Down

0 comments on commit 63fbc5a

Please sign in to comment.