Skip to content

Commit

Permalink
refactor: Move units check outside of model for finds (#29253)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Jun 14, 2023
1 parent 956dbb5 commit 7832a40
Show file tree
Hide file tree
Showing 26 changed files with 306 additions and 163 deletions.
6 changes: 6 additions & 0 deletions .changeset/breezy-drinks-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/model-typings": patch
---

refactor: Move units check outside of model for finds
7 changes: 5 additions & 2 deletions apps/meteor/app/apps/server/bridges/livechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getRoom } from '../../../livechat/server/api/lib/livechat';
import { Livechat } from '../../../livechat/server/lib/Livechat';
import type { AppServerOrchestrator } from '../../../../ee/server/apps/orchestrator';
import { Livechat as LivechatTyped } from '../../../livechat/server/lib/LivechatTyped';
import { callbacks } from '../../../../lib/callbacks';
import { deasyncPromise } from '../../../../server/deasync/deasync';

export class AppLivechatBridge extends LivechatBridge {
Expand Down Expand Up @@ -143,10 +144,12 @@ export class AppLivechatBridge extends LivechatBridge {

let result;

const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});

if (departmentId) {
result = await LivechatRooms.findOpenByVisitorTokenAndDepartmentId(visitor.token, departmentId, {}).toArray();
result = await LivechatRooms.findOpenByVisitorTokenAndDepartmentId(visitor.token, departmentId, {}, extraQuery).toArray();
} else {
result = await LivechatRooms.findOpenByVisitorToken(visitor.token, {}).toArray();
result = await LivechatRooms.findOpenByVisitorToken(visitor.token, {}, extraQuery).toArray();
}

return Promise.all((result as unknown as ILivechatRoom[]).map((room) => this.orch.getConverters()?.get('rooms').convertRoom(room)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import { LivechatRooms } from '@rocket.chat/models';
import { settings } from '../../../settings/server';
import { Livechat } from '../../../livechat/server/lib/LivechatTyped';
import { i18n } from '../../../../server/lib/i18n';
import { callbacks } from '../../../../lib/callbacks';

type SubscribedRooms = {
rid: string;
t: string;
};

export const closeOmnichannelConversations = async (user: IUser, subscribedRooms: SubscribedRooms[]): Promise<void> => {
const roomsInfo = await LivechatRooms.findByIds(subscribedRooms.map(({ rid }) => rid));
const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
const roomsInfo = await LivechatRooms.findByIds(
subscribedRooms.map(({ rid }) => rid),
{},
extraQuery,
);
const language = settings.get<string>('Language') || 'en';
const comment = i18n.t('Agent_deactivated', { lng: language });

Expand Down
5 changes: 3 additions & 2 deletions apps/meteor/app/livechat/server/api/lib/livechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ export async function findOpenRoom(token: string, departmentId?: string): Promis
},
};

const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
const rooms = departmentId
? await LivechatRooms.findOpenByVisitorTokenAndDepartmentId(token, departmentId, options).toArray()
: await LivechatRooms.findOpenByVisitorToken(token, options).toArray();
? await LivechatRooms.findOpenByVisitorTokenAndDepartmentId(token, departmentId, options, extraQuery).toArray()
: await LivechatRooms.findOpenByVisitorToken(token, options, extraQuery).toArray();
if (rooms && rooms.length > 0) {
return rooms[0];
}
Expand Down
4 changes: 4 additions & 0 deletions apps/meteor/app/livechat/server/api/lib/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { ILivechatDepartment, IOmnichannelRoom } from '@rocket.chat/core-ty
import { LivechatRooms, LivechatDepartment } from '@rocket.chat/models';
import type { PaginatedResult } from '@rocket.chat/rest-typings';

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

export async function findRooms({
agents,
roomName,
Expand Down Expand Up @@ -31,6 +33,7 @@ export async function findRooms({
onhold?: string | boolean;
options: { offset: number; count: number; fields: Record<string, number>; sort: Record<string, number> };
}): Promise<PaginatedResult<{ rooms: Array<IOmnichannelRoom> }>> {
const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
const { cursor, totalCount } = LivechatRooms.findRoomsWithCriteria({
agents,
roomName,
Expand All @@ -47,6 +50,7 @@ export async function findRooms({
count,
fields,
},
extraQuery,
});

const [rooms, total] = await Promise.all([cursor.toArray(), totalCount]);
Expand Down
16 changes: 11 additions & 5 deletions apps/meteor/app/livechat/server/api/lib/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LivechatVisitors, Messages, LivechatRooms, LivechatCustomField } from '
import type { FindOptions } from 'mongodb';

import { canAccessRoomAsync } from '../../../../authorization/server/functions/canAccessRoom';
import { callbacks } from '../../../../../lib/callbacks';

export async function findVisitorInfo({ visitorId }: { visitorId: IVisitor['_id'] }) {
const visitor = await LivechatVisitors.findOneById(visitorId);
Expand Down Expand Up @@ -61,11 +62,16 @@ export async function findChatHistory({
throw new Error('error-not-allowed');
}

const { cursor, totalCount } = LivechatRooms.findPaginatedByVisitorId(visitorId, {
sort: sort || { ts: -1 },
skip: offset,
limit: count,
});
const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
const { cursor, totalCount } = LivechatRooms.findPaginatedByVisitorId(
visitorId,
{
sort: sort || { ts: -1 },
skip: offset,
limit: count,
},
extraQuery,
);

const [history, total] = await Promise.all([cursor.toArray(), totalCount]);

Expand Down
4 changes: 3 additions & 1 deletion apps/meteor/app/livechat/server/api/v1/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { normalizeMessageFileUpload } from '../../../../utils/server/functions/n
import { settings } from '../../../../settings/server';
import { getPaginationItems } from '../../../../api/server/helpers/getPaginationItems';
import { isWidget } from '../../../../api/server/helpers/isWidget';
import { callbacks } from '../../../../../lib/callbacks';

API.v1.addRoute(
'livechat/message',
Expand Down Expand Up @@ -254,7 +255,8 @@ API.v1.addRoute(
let visitor = await LivechatVisitors.getVisitorByToken(visitorToken, {});
let rid: string;
if (visitor) {
const rooms = await LivechatRooms.findOpenByVisitorToken(visitorToken).toArray();
const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
const rooms = await LivechatRooms.findOpenByVisitorToken(visitorToken, {}, extraQuery).toArray();
if (rooms && rooms.length > 0) {
rid = rooms[0]._id;
} else {
Expand Down
51 changes: 31 additions & 20 deletions apps/meteor/app/livechat/server/api/v1/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { findGuest, normalizeHttpHeaderData } from '../lib/livechat';
import { Livechat } from '../../lib/Livechat';
import { Livechat as LivechatTyped } from '../../lib/LivechatTyped';
import { settings } from '../../../../settings/server';
import { callbacks } from '../../../../../lib/callbacks';

API.v1.addRoute('livechat/visitor', {
async post() {
Expand Down Expand Up @@ -46,8 +47,9 @@ API.v1.addRoute('livechat/visitor', {

let visitor = await VisitorsRaw.findOneById(visitorId, {});
if (visitor) {
const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
// If it's updating an existing visitor, it must also update the roomInfo
const rooms = await LivechatRooms.findOpenByVisitorToken(visitor?.token).toArray();
const rooms = await LivechatRooms.findOpenByVisitorToken(visitor?.token, {}, extraQuery).toArray();
await Promise.all(rooms.map((room: IRoom) => Livechat.saveRoomInfo(room, visitor)));
}

Expand Down Expand Up @@ -97,17 +99,21 @@ API.v1.addRoute('livechat/visitor/:token', {
if (!visitor) {
throw new Meteor.Error('invalid-token');
}

const rooms = await LivechatRooms.findOpenByVisitorToken(this.urlParams.token, {
projection: {
name: 1,
t: 1,
cl: 1,
u: 1,
usernames: 1,
servedBy: 1,
const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
const rooms = await LivechatRooms.findOpenByVisitorToken(
this.urlParams.token,
{
projection: {
name: 1,
t: 1,
cl: 1,
u: 1,
usernames: 1,
servedBy: 1,
},
},
}).toArray();
extraQuery,
).toArray();

// if gdpr is enabled, bypass rooms check
if (rooms?.length && !settings.get('Livechat_Allow_collect_and_store_HTTP_header_informations')) {
Expand All @@ -134,16 +140,21 @@ API.v1.addRoute(
{ authRequired: true, permissionsRequired: ['view-livechat-manager'] },
{
async get() {
const rooms = await LivechatRooms.findOpenByVisitorToken(this.urlParams.token, {
projection: {
name: 1,
t: 1,
cl: 1,
u: 1,
usernames: 1,
servedBy: 1,
const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
const rooms = await LivechatRooms.findOpenByVisitorToken(
this.urlParams.token,
{
projection: {
name: 1,
t: 1,
cl: 1,
u: 1,
usernames: 1,
servedBy: 1,
},
},
}).toArray();
extraQuery,
).toArray();
return API.v1.success({ rooms });
},
},
Expand Down
Loading

0 comments on commit 7832a40

Please sign in to comment.