Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into sele…
Browse files Browse the repository at this point in the history
…cting

* 'develop' of github.com:RocketChat/Rocket.Chat:
  Regression: Show username and real name on the message system (#25254)
  [IMPROVE] Performance for some Omnichannel features (#25217)
  [FIX] room creation fails if app framework is disabled (#25200)
  [IMPROVE] Add OTR Room States (#24565)
  [FIX] Client disconnection on network loss (#25170)
  [FIX] Read receipts show with color gray when not read yet (#25244)
  [FIX] VoIP disabled/enabled  sequence puts voip agent in error state (#25230)
  [NEW] Add expire index to integration history (#25087)
  • Loading branch information
gabriellsh committed Apr 20, 2022
2 parents 1f762bb + fcbb58c commit b3800ee
Show file tree
Hide file tree
Showing 51 changed files with 874 additions and 353 deletions.
6 changes: 3 additions & 3 deletions apps/meteor/app/lib/server/functions/createRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ export const createRoom = function <T extends RoomType>(
throw new Meteor.Error('error-app-prevented', 'A Rocket.Chat App prevented the room creation.');
}

const { _USERNAMES, ...result } = Promise.await(
const eventResult = Promise.await(
Apps.triggerEvent('IPreRoomCreateModify', Promise.await(Apps.triggerEvent('IPreRoomCreateExtend', tmp))),
);

if (typeof result === 'object') {
Object.assign(roomProps, result);
if (eventResult && typeof eventResult === 'object' && delete eventResult._USERNAMES) {
Object.assign(roomProps, eventResult);
}

if (type === 'c') {
Expand Down
8 changes: 8 additions & 0 deletions apps/meteor/app/lib/server/startup/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3209,6 +3209,14 @@ settingsRegistry.addGroup('Call_Center', function () {
value: true,
},
});
this.add('VoIP_Retry_Count', -1, {
type: 'int',
public: true,
enableQuery: {
_id: 'VoIP_Enabled',
value: true,
},
});
});

this.section('Management_Server', function () {
Expand Down
8 changes: 6 additions & 2 deletions apps/meteor/app/livechat/server/api/lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function findQueueMetrics({ userId, agentId, includeOfflineAgents,
throw new Error('error-not-authorized');
}

const queue = await LivechatRooms.getQueueMetrics({
const result = await LivechatRooms.getQueueMetrics({
departmentId,
agentId,
includeOfflineAgents,
Expand All @@ -16,7 +16,11 @@ export async function findQueueMetrics({ userId, agentId, includeOfflineAgents,
count,
},
});
const total = (await LivechatRooms.getQueueMetrics({ departmentId, agentId, includeOfflineAgents })).length;

const {
sortedResults: queue,
totalCount: [{ total } = { total: 0 }],
} = result[0];

return {
queue,
Expand Down
5 changes: 4 additions & 1 deletion apps/meteor/app/livechat/server/api/v1/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Match, check } from 'meteor/check';
import mem from 'mem';

import { API } from '../../../../api/server';
import { Livechat } from '../../lib/Livechat';
import { settings, findOpenRoom, getExtraConfigInfo, findAgent } from '../lib/livechat';

const cachedSettings = mem(settings, { maxAge: 1000, cacheKey: JSON.stringify });

API.v1.addRoute('livechat/config', {
async get() {
try {
Expand All @@ -20,7 +23,7 @@ API.v1.addRoute('livechat/config', {

const { token, department, businessUnit } = this.queryParams;

const config = await settings({ businessUnit });
const config = await cachedSettings({ businessUnit });

const status = Livechat.online(department);
const guest = token && Livechat.findGuest(token);
Expand Down
8 changes: 5 additions & 3 deletions apps/meteor/app/livechat/server/lib/Contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const Contacts = {
registerContact({ token, name, email, phone, username, customFields = {}, contactManager = {} } = {}) {
check(token, String);

const visitorEmail = s.trim(email).toLowerCase();

let contactId;
const updateUser = {
$set: {
Expand All @@ -25,7 +27,7 @@ export const Contacts = {

let existingUser = null;

if (s.trim(email) !== '' && (existingUser = LivechatVisitors.findOneGuestByEmailAddress(email))) {
if (visitorEmail !== '' && (existingUser = LivechatVisitors.findOneGuestByEmailAddress(visitorEmail))) {
contactId = existingUser._id;
} else {
const userData = {
Expand All @@ -39,9 +41,9 @@ export const Contacts = {

updateUser.$set.name = name;
updateUser.$set.phone = (phone && [{ phoneNumber: phone }]) || null;
updateUser.$set.visitorEmails = (email && [{ address: email }]) || null;
updateUser.$set.visitorEmails = (visitorEmail && [{ address: visitorEmail }]) || null;

const allowedCF = LivechatCustomField.find({ scope: 'visitor' }).map(({ _id }) => _id);
const allowedCF = LivechatCustomField.find({ scope: 'visitor' }, { fields: { _id: 1 } }).map(({ _id }) => _id);

const livechatData = Object.keys(customFields)
.filter((key) => allowedCF.includes(key) && customFields[key] !== '' && customFields[key] !== undefined)
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export const Livechat = {
};

if (email) {
email = email.trim();
email = email.trim().toLowerCase();
validateEmail(email);
updateUser.$set.visitorEmails = [{ address: email }];
}
Expand Down
2 changes: 2 additions & 0 deletions apps/meteor/app/models/server/models/LivechatCustomField.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Base } from './_Base';
export class LivechatCustomField extends Base {
constructor() {
super('livechat_custom_field');

this.tryEnsureIndex({ scope: 1 });
}

// FIND
Expand Down
2 changes: 2 additions & 0 deletions apps/meteor/app/models/server/models/LivechatDepartment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export class LivechatDepartment extends Base {
numAgents: 1,
enabled: 1,
});
this.tryEnsureIndex({ parentId: 1 }, { sparse: true });
this.tryEnsureIndex({ ancestors: 1 }, { sparse: true });
}

// FIND
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/app/models/server/models/LivechatInquiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class LivechatInquiry extends Base {
this.tryEnsureIndex({ department: 1 });
this.tryEnsureIndex({ status: 1 }); // 'ready', 'queued', 'taken'
this.tryEnsureIndex({ queueOrder: 1, estimatedWaitingTimeQueue: 1, estimatedServiceTimeAt: 1 });
this.tryEnsureIndex({ 'v.token': 1, 'status': 1 }); // visitor token and status
}

findOneById(inquiryId) {
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/app/models/server/models/LivechatRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class LivechatRooms extends Base {
this.tryEnsureIndex({ 'v._id': 1 }, { sparse: true });
this.tryEnsureIndex({ t: 1, departmentId: 1, closedAt: 1 }, { partialFilterExpression: { closedAt: { $exists: true } } });
this.tryEnsureIndex({ source: 1 }, { sparse: true });
this.tryEnsureIndex({ departmentAncestors: 1 }, { sparse: true });
}

findLivechat(filter = {}, offset = 0, limit = 20) {
Expand Down
6 changes: 4 additions & 2 deletions apps/meteor/app/models/server/models/LivechatVisitors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import _ from 'underscore';
import s from 'underscore.string';
import { escapeRegExp } from '@rocket.chat/string-helpers';

import { Base } from './_Base';
import Settings from './Settings';
Expand All @@ -11,6 +10,9 @@ export class LivechatVisitors extends Base {

this.tryEnsureIndex({ token: 1 });
this.tryEnsureIndex({ 'phone.phoneNumber': 1 }, { sparse: true });
this.tryEnsureIndex({ 'visitorEmails.address': 1 }, { sparse: true });
this.tryEnsureIndex({ name: 1 }, { sparse: true });
this.tryEnsureIndex({ username: 1 });
}

/**
Expand Down Expand Up @@ -200,7 +202,7 @@ export class LivechatVisitors extends Base {

findOneGuestByEmailAddress(emailAddress) {
const query = {
'visitorEmails.address': new RegExp(`^${escapeRegExp(emailAddress)}$`, 'i'),
'visitorEmails.address': String(emailAddress).toLowerCase(),
};

return this.findOne(query);
Expand Down
1 change: 0 additions & 1 deletion apps/meteor/app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class Users extends Base {
this.tryEnsureIndex({ statusConnection: 1 }, { sparse: 1 });
this.tryEnsureIndex({ appId: 1 }, { sparse: 1 });
this.tryEnsureIndex({ type: 1 });
this.tryEnsureIndex({ 'visitorEmails.address': 1 });
this.tryEnsureIndex({ federation: 1 }, { sparse: true });
this.tryEnsureIndex({ isRemote: 1 }, { sparse: true });
this.tryEnsureIndex({ 'services.saml.inResponseTo': 1 });
Expand Down
5 changes: 4 additions & 1 deletion apps/meteor/app/models/server/raw/IntegrationHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { BaseRaw, IndexSpecification } from './BaseRaw';

export class IntegrationHistoryRaw extends BaseRaw<IIntegrationHistory> {
protected modelIndexes(): IndexSpecification[] {
return [{ key: { 'integration._id': 1, 'integration._createdBy._id': 1 } }];
return [
{ key: { 'integration._id': 1, 'integration._createdBy._id': 1 } },
{ key: { _updatedAt: 1 }, expireAfterSeconds: 30 * 24 * 60 * 60 },
];
}

removeByIntegrationId(integrationId: string): ReturnType<BaseRaw<IIntegrationHistory>['deleteMany']> {
Expand Down
16 changes: 13 additions & 3 deletions apps/meteor/app/models/server/raw/LivechatRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,23 @@ export class LivechatRoomsRaw extends BaseRaw {
firstParams.push(matchUsers);
}
const sort = { $sort: options.sort || { chats: -1 } };
const params = [...firstParams, usersGroup, project, sort];
const pagination = [sort];

if (options.offset) {
params.push({ $skip: options.offset });
pagination.push({ $skip: options.offset });
}
if (options.count) {
params.push({ $limit: options.count });
pagination.push({ $limit: options.count });
}

const facet = {
$facet: {
sortedResults: pagination,
totalCount: [{ $group: { _id: null, total: { $sum: 1 } } }],
},
};

const params = [...firstParams, usersGroup, project, facet];
return this.col.aggregate(params).toArray();
}

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/models/server/raw/LivechatVisitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> {
const query = {
$or: [
{
'visitorEmails.address': filter,
'visitorEmails.address': _emailOrPhoneOrNameOrUsername,
},
{
'phone.phoneNumber': _emailOrPhoneOrNameOrUsername,
Expand Down
9 changes: 9 additions & 0 deletions apps/meteor/app/otr/client/OtrRoomState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum OtrRoomState {
DISABLED = 'DISABLED',
NOT_STARTED = 'NOT_STARTED',
ESTABLISHING = 'ESTABLISHING',
ESTABLISHED = 'ESTABLISHED',
ERROR = 'ERROR',
TIMEOUT = 'TIMEOUT',
DECLINED = 'DECLINED',
}
13 changes: 11 additions & 2 deletions apps/meteor/app/otr/client/rocketchat.otr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Notifications } from '../../notifications';
import { t } from '../../utils';
import { onClientMessageReceived } from '../../../client/lib/onClientMessageReceived';
import { onClientBeforeSendMessage } from '../../../client/lib/onClientBeforeSendMessage';
import { OtrRoomState } from './OtrRoomState';

class OTRClass {
constructor() {
Expand Down Expand Up @@ -56,7 +57,11 @@ Meteor.startup(function () {
});

onClientBeforeSendMessage.use(function (message) {
if (message.rid && OTR.getInstanceByRoomId(message.rid) && OTR.getInstanceByRoomId(message.rid).established.get()) {
if (
message.rid &&
OTR.getInstanceByRoomId(message.rid) &&
OTR.getInstanceByRoomId(message.rid).state.get() === OtrRoomState.ESTABLISHED
) {
return OTR.getInstanceByRoomId(message.rid)
.encrypt(message)
.then((msg) => {
Expand All @@ -69,7 +74,11 @@ Meteor.startup(function () {
});

onClientMessageReceived.use(function (message) {
if (message.rid && OTR.getInstanceByRoomId(message.rid) && OTR.getInstanceByRoomId(message.rid).established.get()) {
if (
message.rid &&
OTR.getInstanceByRoomId(message.rid) &&
OTR.getInstanceByRoomId(message.rid).state.get() === OtrRoomState.ESTABLISHED
) {
if (message.notification) {
message.msg = t('Encrypted_message');
return Promise.resolve(message);
Expand Down
Loading

0 comments on commit b3800ee

Please sign in to comment.