Skip to content

Commit

Permalink
Merge branch 'feat/single-contact-id' into fix/undefined-error-message
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustrb authored Oct 23, 2024
2 parents c790f86 + 593ad15 commit c2e2b43
Show file tree
Hide file tree
Showing 87 changed files with 1,678 additions and 1,208 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-jeans-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes banner breaking the UI with specific payloads
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/lib/maybeMigrateLivechatRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isOmnichannelRoom, type IRoom } from '@rocket.chat/core-typings';
import { Rooms } from '@rocket.chat/models';
import type { FindOptions } from 'mongodb';

import { migrateVisitorIfMissingContact } from '../../../livechat/server/lib/Contacts';
import { migrateVisitorIfMissingContact } from '../../../livechat/server/lib/contacts/migrateVisitorIfMissingContact';

export async function maybeMigrateLivechatRoom(room: IRoom | null, options: FindOptions<IRoom> = {}): Promise<IRoom | null> {
if (!room || !isOmnichannelRoom(room)) {
Expand Down
6 changes: 2 additions & 4 deletions apps/meteor/app/api/server/v1/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ API.v1.addRoute(
throw new Meteor.Error('error-role-protected', 'Cannot delete a protected role');
}

const existingUsers = await Roles.findUsersInRole(role._id);

if (existingUsers && (await existingUsers.count()) > 0) {
if ((await Roles.countUsersInRole(role._id)) > 0) {
throw new Meteor.Error('error-role-in-use', "Cannot delete role because it's in use");
}

Expand Down Expand Up @@ -217,7 +215,7 @@ API.v1.addRoute(
}

if (role._id === 'admin') {
const adminCount = await (await Roles.findUsersInRole('admin')).count();
const adminCount = await Roles.countUsersInRole('admin');
if (adminCount === 1) {
throw new Meteor.Error('error-admin-required', 'You need to have at least one admin');
}
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/apps/server/bridges/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { ContactBridge } from '@rocket.chat/apps-engine/server/bridges';
import type { IVisitor } from '@rocket.chat/core-typings';
import { LivechatContacts } from '@rocket.chat/models';

import { addContactEmail, verifyContactChannel } from '../../../livechat/server/lib/Contacts';
import { addContactEmail } from '../../../livechat/server/lib/contacts/addContactEmail';
import { verifyContactChannel } from '../../../livechat/server/lib/contacts/verifyContactChannel';

export class AppContactBridge extends ContactBridge {
constructor(private readonly orch: IAppServerOrchestrator) {
Expand Down
6 changes: 4 additions & 2 deletions apps/meteor/app/apps/server/bridges/livechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { LivechatVisitors, LivechatRooms, LivechatDepartment, Users } from '@roc

import { callbacks } from '../../../../lib/callbacks';
import { deasyncPromise } from '../../../../server/deasync/deasync';
import { type ILivechatMessage, Livechat as LivechatTyped } from '../../../livechat/server/lib/LivechatTyped';
import { Livechat as LivechatTyped } from '../../../livechat/server/lib/LivechatTyped';
import { getRoomMessages } from '../../../livechat/server/lib/getRoomMessages';
import type { ILivechatMessage } from '../../../livechat/server/lib/localTypes';
import { settings } from '../../../settings/server';

declare module '@rocket.chat/apps/dist/converters/IAppMessagesConverter' {
Expand Down Expand Up @@ -379,7 +381,7 @@ export class AppLivechatBridge extends LivechatBridge {
throw new Error('Could not get the message converter to process livechat room messages');
}

const livechatMessages = await LivechatTyped.getRoomMessages({ rid: roomId });
const livechatMessages = await getRoomMessages({ rid: roomId });
return Promise.all(await livechatMessages.map((message) => messageConverter.convertMessage(message, livechatMessages)).toArray());
}

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/authorization/server/methods/deleteRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Meteor.methods<ServerMethods>({
});
}

const users = await (await Roles.findUsersInRole(role._id)).count();
const users = await Roles.countUsersInRole(role._id);

if (users > 0) {
throw new Meteor.Error('error-role-in-use', "Cannot delete role because it's in use", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export class PendingAvatarImporter extends Importer {
this.logger.debug('start preparing import operation');
await super.updateProgress(ProgressStep.PREPARING_STARTED);

const users = Users.findAllUsersWithPendingAvatar();
const fileCount = await users.count();
const fileCount = await Users.countAllUsersWithPendingAvatar();

if (fileCount === 0) {
await super.updateProgress(ProgressStep.DONE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { IImportContact, IImportContactRecord } from '@rocket.chat/core-typings';
import { LivechatVisitors } from '@rocket.chat/models';

import { createContact, getAllowedCustomFields, validateCustomFields } from '../../../../livechat/server/lib/Contacts';
import { createContact } from '../../../../livechat/server/lib/contacts/createContact';
import { getAllowedCustomFields } from '../../../../livechat/server/lib/contacts/getAllowedCustomFields';
import { validateCustomFields } from '../../../../livechat/server/lib/contacts/validateCustomFields';
import { RecordConverter } from './RecordConverter';

export class ContactConverter extends RecordConverter<IImportContactRecord> {
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/lib/server/functions/closeLivechatRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { isOmnichannelRoom } from '@rocket.chat/core-typings';
import { LivechatRooms, Subscriptions } from '@rocket.chat/models';

import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import type { CloseRoomParams } from '../../../livechat/server/lib/LivechatTyped';
import { Livechat } from '../../../livechat/server/lib/LivechatTyped';
import type { CloseRoomParams } from '../../../livechat/server/lib/localTypes';
import { notifyOnSubscriptionChanged } from '../lib/notifyListener';

export const closeLivechatRoom = async (
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/app/lib/server/functions/updateGroupDMsName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const getName = (members: IUser[]): string => members.map(({ username }) => user

async function getUsersWhoAreInTheSameGroupDMsAs(user: IUser) {
// add all users to single array so we can fetch details from them all at once
const rooms = Rooms.findGroupDMsByUids([user._id], { projection: { uids: 1 } });
if ((await rooms.count()) === 0) {
if ((await Rooms.countGroupDMsByUids([user._id])) === 0) {
return;
}

const userIds = new Set();
const users = new Map();

const rooms = Rooms.findGroupDMsByUids([user._id], { projection: { uids: 1 } });
await rooms.forEach((room) => {
if (!room.uids) {
return;
Expand Down
3 changes: 1 addition & 2 deletions apps/meteor/app/lib/server/methods/leaveRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export const leaveRoomMethod = async (user: IUser, rid: string): Promise<void> =

// If user is room owner, check if there are other owners. If there isn't anyone else, warn user to set a new owner.
if (await hasRoleAsync(user._id, 'owner', room._id)) {
const cursor = await Roles.findUsersInRole('owner', room._id);
const numOwners = await cursor.count();
const numOwners = await Roles.countUsersInRole('owner', room._id);
if (numOwners === 1) {
throw new Meteor.Error('error-you-are-last-owner', 'You are the last owner. Please set new owner before leaving the room.', {
method: 'leaveRoom',
Expand Down
12 changes: 6 additions & 6 deletions apps/meteor/app/livechat/imports/server/rest/departments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
findArchivedDepartments,
} from '../../../server/api/lib/departments';
import { DepartmentHelper } from '../../../server/lib/Departments';
import { Livechat as LivechatTs } from '../../../server/lib/LivechatTyped';
import { saveDepartment, archiveDepartment, unarchiveDepartment, saveDepartmentAgents } from '../../../server/lib/departmentsLib';
import { isDepartmentCreationAvailable } from '../../../server/lib/isDepartmentCreationAvailable';

API.v1.addRoute(
Expand Down Expand Up @@ -62,7 +62,7 @@ API.v1.addRoute(

const agents = this.bodyParams.agents ? { upsert: this.bodyParams.agents } : {};
const { departmentUnit } = this.bodyParams;
const department = await LivechatTs.saveDepartment(
const department = await saveDepartment(
this.userId,
null,
this.bodyParams.department as ILivechatDepartment,
Expand Down Expand Up @@ -131,7 +131,7 @@ API.v1.addRoute(
}

const agentParam = permissionToAddAgents && agents ? { upsert: agents } : {};
await LivechatTs.saveDepartment(this.userId, _id, department, agentParam, departmentUnit || {});
await saveDepartment(this.userId, _id, department, agentParam, departmentUnit || {});

return API.v1.success({
department: await LivechatDepartment.findOneById(_id),
Expand Down Expand Up @@ -191,7 +191,7 @@ API.v1.addRoute(
},
{
async post() {
await LivechatTs.archiveDepartment(this.urlParams._id);
await archiveDepartment(this.urlParams._id);

return API.v1.success();
},
Expand All @@ -206,7 +206,7 @@ API.v1.addRoute(
},
{
async post() {
await LivechatTs.unarchiveDepartment(this.urlParams._id);
await unarchiveDepartment(this.urlParams._id);
return API.v1.success();
},
},
Expand Down Expand Up @@ -271,7 +271,7 @@ API.v1.addRoute(
remove: Array,
}),
);
await LivechatTs.saveDepartmentAgents(this.urlParams._id, this.bodyParams);
await saveDepartmentAgents(this.urlParams._id, this.bodyParams);

return API.v1.success();
},
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/imports/server/rest/sms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { FileUpload } from '../../../../file-upload/server';
import { checkUrlForSsrf } from '../../../../lib/server/functions/checkUrlForSsrf';
import { settings } from '../../../../settings/server';
import { setCustomField } from '../../../server/api/lib/customFields';
import type { ILivechatMessage } from '../../../server/lib/LivechatTyped';
import { Livechat as LivechatTyped } from '../../../server/lib/LivechatTyped';
import type { ILivechatMessage } from '../../../server/lib/localTypes';

const logger = new Logger('SMS');

Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/livechat/server/api/lib/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { FindOptions } from 'mongodb';

import { callbacks } from '../../../../../lib/callbacks';
import { canAccessRoomAsync } from '../../../../authorization/server/functions/canAccessRoom';
import { migrateVisitorToContactId, getContactIdByVisitorId } from '../../lib/Contacts';
import { getContactIdByVisitorId } from '../../lib/contacts/getContactIdByVisitorId';
import { migrateVisitorToContactId } from '../../lib/contacts/migrateVisitorToContactId';

export async function findVisitorInfo({ visitorId }: { visitorId: IVisitor['_id'] }) {
const visitor = await LivechatVisitors.findOneEnabledById(visitorId);
Expand Down
9 changes: 7 additions & 2 deletions apps/meteor/app/livechat/server/api/v1/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import { Meteor } from 'meteor/meteor';

import { API } from '../../../../api/server';
import { getPaginationItems } from '../../../../api/server/helpers/getPaginationItems';
import { getContactHistory, Contacts, createContact, getContact, updateContact, getContacts } from '../../lib/Contacts';
import { createContact } from '../../lib/contacts/createContact';
import { getContact } from '../../lib/contacts/getContact';
import { getContactHistory } from '../../lib/contacts/getContactHistory';
import { getContacts } from '../../lib/contacts/getContacts';
import { registerContact } from '../../lib/contacts/registerContact';
import { updateContact } from '../../lib/contacts/updateContact';

API.v1.addRoute(
'omnichannel/contact',
Expand All @@ -36,7 +41,7 @@ API.v1.addRoute(
}),
});

const contact = await Contacts.registerContact(this.bodyParams);
const contact = await registerContact(this.bodyParams);

return API.v1.success({ contact });
},
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/api/v1/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import { addUserToRoom } from '../../../../lib/server/functions/addUserToRoom';
import { closeLivechatRoom } from '../../../../lib/server/functions/closeLivechatRoom';
import { settings as rcSettings } from '../../../../settings/server';
import { normalizeTransferredByData } from '../../lib/Helper';
import type { CloseRoomParams } from '../../lib/LivechatTyped';
import { Livechat as LivechatTyped } from '../../lib/LivechatTyped';
import type { CloseRoomParams } from '../../lib/localTypes';
import { findGuest, findRoom, settings, findAgent, onCheckRoomParams } from '../lib/livechat';

const isAgentWithInfo = (agentObj: ILivechatAgent | { hiddenInfo: boolean }): agentObj is ILivechatAgent => !('hiddenInfo' in agentObj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isOmnichannelRoom } from '@rocket.chat/core-typings';
import { LivechatRooms } from '@rocket.chat/models';

import { callbacks } from '../../../../lib/callbacks';
import type { CloseRoomParams } from '../lib/LivechatTyped';
import type { CloseRoomParams } from '../lib/localTypes';
import { sendTranscript } from '../lib/sendTranscript';

type LivechatCloseCallbackParams = {
Expand Down
Loading

0 comments on commit c2e2b43

Please sign in to comment.