Skip to content

Commit 17496df

Browse files
chore: save users' names to call history (#37867)
1 parent 56f1239 commit 17496df

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

apps/meteor/app/lib/server/functions/saveUserIdentity.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { IUser } from '@rocket.chat/core-typings';
22
import type { Updater } from '@rocket.chat/models';
3-
import { Messages, VideoConference, LivechatDepartmentAgents, Rooms, Subscriptions, Users } from '@rocket.chat/models';
3+
import { Messages, VideoConference, LivechatDepartmentAgents, Rooms, Subscriptions, Users, CallHistory } from '@rocket.chat/models';
44
import type { ClientSession } from 'mongodb';
55

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

182182
// update name and username of users on video conferences
183183
await VideoConference.updateUserReferences(user._id, username || previousUsername, name || previousName);
184+
185+
// update name and username of users on call history
186+
await CallHistory.updateUserReferences(user._id, username || previousUsername, name || previousName);
184187
}
185188
}

apps/meteor/server/services/media-call/service.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ export class MediaCallService extends ServiceClassInternal implements IMediaCall
129129
await CallHistory.insertOne(historyItem).catch((err: unknown) => logger.error({ msg: 'Failed to insert item into Call History', err }));
130130
}
131131

132+
private getContactDataForInternalHistory(
133+
contact: IMediaCall['caller'] | IMediaCall['callee'],
134+
): Pick<IInternalMediaCallHistoryItem, 'contactId' | 'contactName' | 'contactUsername'> {
135+
return {
136+
contactId: contact.id,
137+
contactName: contact.displayName,
138+
contactUsername: contact.username,
139+
};
140+
}
141+
132142
private async saveInternalCallToHistory(call: IMediaCall): Promise<void> {
133143
if (call.caller.type !== 'user' || call.callee.type !== 'user') {
134144
logger.warn({ msg: 'Attempt to save an internal call history with a call that is not internal', callId: call._id });
@@ -158,14 +168,14 @@ export class MediaCallService extends ServiceClassInternal implements IMediaCall
158168
...sharedData,
159169
uid: call.caller.id,
160170
direction: 'outbound',
161-
contactId: call.callee.id,
171+
...this.getContactDataForInternalHistory(call.callee),
162172
} as const;
163173

164174
const inboundHistoryItem = {
165175
...sharedData,
166176
uid: call.callee.id,
167177
direction: 'inbound',
168-
contactId: call.caller.id,
178+
...this.getContactDataForInternalHistory(call.caller),
169179
} as const;
170180

171181
await CallHistory.insertMany([outboundHistoryItem, inboundHistoryItem]).catch((err: unknown) =>

apps/meteor/tests/unit/server/users/saveUserIdentity.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const stubs = {
1010
updateAllUsernamesByUserId: sinon.stub(),
1111
updateDirectNameAndFnameByName: sinon.stub(),
1212
updateUserReferences: sinon.stub(),
13+
updateHistoryReferences: sinon.stub(),
1314
setUsername: sinon.stub(),
1415
setRealName: sinon.stub(),
1516
validateName: sinon.stub(),
@@ -32,6 +33,9 @@ const { saveUserIdentity } = proxyquire.noCallThru().load('../../../../app/lib/s
3233
VideoConference: {
3334
updateUserReferences: stubs.updateUserReferences,
3435
},
36+
CallHistory: {
37+
updateUserReferences: stubs.updateHistoryReferences,
38+
},
3539
},
3640
'meteor/meteor': {
3741
'Meteor': sinon.stub(),
@@ -110,6 +114,7 @@ describe('Users - saveUserIdentity', () => {
110114
expect(stubs.updateUsernameAndMessageOfMentionByIdAndOldUsername.called).to.be.false;
111115
expect(stubs.updateDirectNameAndFnameByName.called).to.be.false;
112116
expect(stubs.updateUserReferences.called).to.be.false;
117+
expect(stubs.updateHistoryReferences.called).to.be.false;
113118
});
114119

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

124-
it('should update Subscriptions and VideoConference if name changes', async () => {
129+
it('should update Subscriptions, VideoConference and Call History if name changes', async () => {
125130
stubs.findOneUserById.returns({ name: 'oldName', username: 'oldUsername' });
126131
stubs.setRealName.returns(true);
127132
const result = await saveUserIdentity({ _id: 'valid_id', name: 'name', username: 'oldUsername' });
@@ -131,6 +136,7 @@ describe('Users - saveUserIdentity', () => {
131136
expect(stubs.updateUsernameOfEditByUserId.called).to.be.false;
132137
expect(stubs.updateDirectNameAndFnameByName.called).to.be.true;
133138
expect(stubs.updateUserReferences.called).to.be.true;
139+
expect(stubs.updateHistoryReferences.called).to.be.true;
134140
expect(result).to.be.true;
135141
});
136142
});

packages/core-typings/src/ICallHistoryItem.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ interface IMediaCallHistoryItem extends ICallHistoryItem {
3737
export interface IInternalMediaCallHistoryItem extends IMediaCallHistoryItem {
3838
external: false;
3939
contactId: IUser['_id'];
40+
contactName?: IUser['name'];
41+
contactUsername?: IUser['username'];
4042

4143
rid?: IRoom['_id'];
4244
messageId?: IMessage['_id']; // Id of the message that was sent after the call ended

packages/model-typings/src/models/ICallHistoryModel.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CallHistoryItem } from '@rocket.chat/core-typings';
1+
import type { CallHistoryItem, IRegisterUser } from '@rocket.chat/core-typings';
22
import type { FindOptions } from 'mongodb';
33

44
import type { IBaseModel } from './IBaseModel';
@@ -15,4 +15,6 @@ export interface ICallHistoryModel extends IBaseModel<CallHistoryItem> {
1515
uid: CallHistoryItem['uid'],
1616
options?: FindOptions<CallHistoryItem>,
1717
): Promise<CallHistoryItem | null>;
18+
19+
updateUserReferences(userId: IRegisterUser['_id'], username: IRegisterUser['username'], name?: IRegisterUser['name']): Promise<void>;
1820
}

packages/models/src/models/CallHistory.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CallHistoryItem } from '@rocket.chat/core-typings';
1+
import type { CallHistoryItem, IRegisterUser } from '@rocket.chat/core-typings';
22
import type { ICallHistoryModel } from '@rocket.chat/model-typings';
33
import type { Db, FindOptions, IndexDescription } from 'mongodb';
44

@@ -28,4 +28,22 @@ export class CallHistoryRaw extends BaseRaw<CallHistoryItem> implements ICallHis
2828
): Promise<CallHistoryItem | null> {
2929
return this.findOne({ callId, uid }, options);
3030
}
31+
32+
public async updateUserReferences(
33+
userId: IRegisterUser['_id'],
34+
username: IRegisterUser['username'],
35+
name?: IRegisterUser['name'],
36+
): Promise<void> {
37+
await this.updateMany(
38+
{
39+
contactId: userId,
40+
},
41+
{
42+
$set: {
43+
contactUsername: username,
44+
...(name && { contactName: name }),
45+
},
46+
},
47+
);
48+
}
3149
}

0 commit comments

Comments
 (0)