Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion apps/meteor/app/lib/server/functions/saveUserIdentity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IUser } from '@rocket.chat/core-typings';
import type { Updater } from '@rocket.chat/models';
import { Messages, VideoConference, LivechatDepartmentAgents, Rooms, Subscriptions, Users } from '@rocket.chat/models';
import { Messages, VideoConference, LivechatDepartmentAgents, Rooms, Subscriptions, Users, CallHistory } from '@rocket.chat/models';
import type { ClientSession } from 'mongodb';

import { _setRealName } from './setRealName';
Expand Down Expand Up @@ -181,5 +181,8 @@ async function updateUsernameReferences({

// update name and username of users on video conferences
await VideoConference.updateUserReferences(user._id, username || previousUsername, name || previousName);

// update name and username of users on call history
await CallHistory.updateUserReferences(user._id, username || previousUsername, name || previousName);
}
}
14 changes: 12 additions & 2 deletions apps/meteor/server/services/media-call/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ export class MediaCallService extends ServiceClassInternal implements IMediaCall
await CallHistory.insertOne(historyItem).catch((err: unknown) => logger.error({ msg: 'Failed to insert item into Call History', err }));
}

private getContactDataForInternalHistory(
contact: IMediaCall['caller'] | IMediaCall['callee'],
): Pick<IInternalMediaCallHistoryItem, 'contactId' | 'contactName' | 'contactUsername'> {
return {
contactId: contact.id,
contactName: contact.displayName,
contactUsername: contact.username,
};
}

private async saveInternalCallToHistory(call: IMediaCall): Promise<void> {
if (call.caller.type !== 'user' || call.callee.type !== 'user') {
logger.warn({ msg: 'Attempt to save an internal call history with a call that is not internal', callId: call._id });
Expand Down Expand Up @@ -161,14 +171,14 @@ export class MediaCallService extends ServiceClassInternal implements IMediaCall
...sharedData,
uid: call.caller.id,
direction: 'outbound',
contactId: call.callee.id,
...this.getContactDataForInternalHistory(call.callee),
} as const;

const inboundHistoryItem = {
...sharedData,
uid: call.callee.id,
direction: 'inbound',
contactId: call.caller.id,
...this.getContactDataForInternalHistory(call.caller),
} as const;

await CallHistory.insertMany([outboundHistoryItem, inboundHistoryItem]).catch((err: unknown) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const stubs = {
updateAllUsernamesByUserId: sinon.stub(),
updateDirectNameAndFnameByName: sinon.stub(),
updateUserReferences: sinon.stub(),
updateHistoryReferences: sinon.stub(),
setUsername: sinon.stub(),
setRealName: sinon.stub(),
validateName: sinon.stub(),
Expand All @@ -32,6 +33,9 @@ const { saveUserIdentity } = proxyquire.noCallThru().load('../../../../app/lib/s
VideoConference: {
updateUserReferences: stubs.updateUserReferences,
},
CallHistory: {
updateUserReferences: stubs.updateHistoryReferences,
},
},
'meteor/meteor': {
'Meteor': sinon.stub(),
Expand Down Expand Up @@ -110,6 +114,7 @@ describe('Users - saveUserIdentity', () => {
expect(stubs.updateUsernameAndMessageOfMentionByIdAndOldUsername.called).to.be.false;
expect(stubs.updateDirectNameAndFnameByName.called).to.be.false;
expect(stubs.updateUserReferences.called).to.be.false;
expect(stubs.updateHistoryReferences.called).to.be.false;
});

it('should return false if _setName fails', async () => {
Expand All @@ -121,7 +126,7 @@ describe('Users - saveUserIdentity', () => {
expect(result).to.be.false;
});

it('should update Subscriptions and VideoConference if name changes', async () => {
it('should update Subscriptions, VideoConference and Call History if name changes', async () => {
stubs.findOneUserById.returns({ name: 'oldName', username: 'oldUsername' });
stubs.setRealName.returns(true);
const result = await saveUserIdentity({ _id: 'valid_id', name: 'name', username: 'oldUsername' });
Expand All @@ -131,6 +136,7 @@ describe('Users - saveUserIdentity', () => {
expect(stubs.updateUsernameOfEditByUserId.called).to.be.false;
expect(stubs.updateDirectNameAndFnameByName.called).to.be.true;
expect(stubs.updateUserReferences.called).to.be.true;
expect(stubs.updateHistoryReferences.called).to.be.true;
expect(result).to.be.true;
});
});
2 changes: 2 additions & 0 deletions packages/core-typings/src/ICallHistoryItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ interface IMediaCallHistoryItem extends ICallHistoryItem {
export interface IInternalMediaCallHistoryItem extends IMediaCallHistoryItem {
external: false;
contactId: IUser['_id'];
contactName?: IUser['name'];
contactUsername?: IUser['username'];

rid?: IRoom['_id'];
messageId?: IMessage['_id']; // Id of the message that was sent after the call ended
Expand Down
4 changes: 3 additions & 1 deletion packages/model-typings/src/models/ICallHistoryModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CallHistoryItem } from '@rocket.chat/core-typings';
import type { CallHistoryItem, IRegisterUser } from '@rocket.chat/core-typings';
import type { FindOptions } from 'mongodb';

import type { IBaseModel } from './IBaseModel';
Expand All @@ -15,4 +15,6 @@ export interface ICallHistoryModel extends IBaseModel<CallHistoryItem> {
uid: CallHistoryItem['uid'],
options?: FindOptions<CallHistoryItem>,
): Promise<CallHistoryItem | null>;

updateUserReferences(userId: IRegisterUser['_id'], username: IRegisterUser['username'], name?: IRegisterUser['name']): Promise<void>;
}
20 changes: 19 additions & 1 deletion packages/models/src/models/CallHistory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CallHistoryItem } from '@rocket.chat/core-typings';
import type { CallHistoryItem, IRegisterUser } from '@rocket.chat/core-typings';
import type { ICallHistoryModel } from '@rocket.chat/model-typings';
import type { Db, FindOptions, IndexDescription } from 'mongodb';

Expand Down Expand Up @@ -28,4 +28,22 @@ export class CallHistoryRaw extends BaseRaw<CallHistoryItem> implements ICallHis
): Promise<CallHistoryItem | null> {
return this.findOne({ callId, uid }, options);
}

public async updateUserReferences(
userId: IRegisterUser['_id'],
username: IRegisterUser['username'],
name?: IRegisterUser['name'],
): Promise<void> {
await this.updateMany(
{
contactId: userId,
},
{
$set: {
contactUsername: username,
...(name && { contactName: name }),
},
},
);
}
}
Loading