Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 725f223

Browse files
committed
Move getSender to a separate function
Most of this commit can be safely ignored.
1 parent 772995b commit 725f223

File tree

1 file changed

+69
-61
lines changed

1 file changed

+69
-61
lines changed

src/TextForEvent.tsx

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,74 +27,82 @@ import { SetRightPanelPhasePayload } from './dispatcher/payloads/SetRightPanelPh
2727
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
2828
import { MatrixClientPeg } from "./MatrixClientPeg";
2929
import CallHandler from './CallHandler';
30-
import { RoomMember } from '../../matrix-js-sdk/src';
30+
import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
3131

32-
// These functions are frequently used just to check whether an event has
33-
// any text to display at all. For this reason they return deferred values
34-
// to avoid the expense of looking up translations when they're not needed.
35-
36-
function textForCallInviteEvent(event: MatrixEvent): () => string | null {
37-
const getSenderName = () => {
38-
// Retrieve the room ID from the event
39-
const potentiallyVirtualRoom = MatrixClientPeg.get().getRoom(event.getRoomId());
40-
41-
// Check if the event is from a virtual room
42-
if (
43-
!CallHandler.sharedInstance().getSupportsVirtualRooms() ||
44-
!window.mxVoipUserMapper.isVirtualRoom(potentiallyVirtualRoom)
45-
) {
46-
// If not, simply extract the sender information from the incoming event
47-
return event.sender ? event.sender.name : _t('Someone');
48-
}
32+
/*
33+
* Attempt to retrieve the displayname of a user from an event. If the user is a virtual user,
34+
* return the displayname of the native user instead. In either case, if a displayname cannot
35+
* be found, simply return the MXID of the user instead.
36+
*
37+
* @param event The event to extract the user from.
38+
*/
39+
function getSenderNameFromPotentiallyVirtualUser(event: MatrixEvent) {
40+
// Retrieve the room ID from the event
41+
const potentiallyVirtualRoom = MatrixClientPeg.get().getRoom(event.getRoomId());
42+
43+
// Check if the event is from a virtual room
44+
if (
45+
!CallHandler.sharedInstance().getSupportsVirtualRooms() ||
46+
!window.mxVoipUserMapper.isVirtualRoom(potentiallyVirtualRoom)
47+
) {
48+
// If not, simply extract the sender information from the incoming event
49+
return event.sender ? event.sender.name : _t('Someone');
50+
}
4951

50-
// Otherwise, assume the caller is a virtual user and attempt to look up the corresponding
51-
// native user
52+
// Otherwise, assume the caller is a virtual user and attempt to look up the corresponding
53+
// native user
5254

53-
// We can avoid a potentially expensive virtual -> native user lookup by checking if the native room
54-
// only has two people in it. If so, then the other user in that room is likely the matching native user.
55-
const nativeRoomId = window.mxVoipUserMapper.nativeRoomForVirtualRoom(potentiallyVirtualRoom.roomId);
56-
const nativeRoom = MatrixClientPeg.get().getRoom(nativeRoomId);
57-
const nativeRoomjoinedMembers = nativeRoom.getJoinedMembers();
55+
// We can avoid a potentially expensive virtual -> native user lookup by checking if the native room
56+
// only has two people in it. If so, then the other user in that room is likely the matching native user.
57+
const nativeRoomId = window.mxVoipUserMapper.nativeRoomForVirtualRoom(potentiallyVirtualRoom.roomId);
58+
const nativeRoom = MatrixClientPeg.get().getRoom(nativeRoomId);
59+
const nativeRoomjoinedMembers = nativeRoom.getJoinedMembers();
5860

59-
const getSenderNameForUserId = (userId) => {
60-
return MatrixClientPeg.get().getProfileInfo(userId).then((resp) => {
61-
if (resp.displayname) {
62-
return resp.displayname;
63-
}
61+
const getSenderNameForUserId = (userId) => {
62+
return MatrixClientPeg.get().getProfileInfo(userId).then((resp) => {
63+
if (resp.displayname) {
64+
return resp.displayname;
65+
}
6466

65-
return userId;
66-
}).catch((e) => {
67-
console.error('Error getting native user profile', e);
68-
return userId;
69-
});
70-
}
67+
return userId;
68+
}).catch((e) => {
69+
console.error('Error getting native user profile', e);
70+
return userId;
71+
});
72+
};
7173

72-
let nativeUser: RoomMember;
73-
if (nativeRoomjoinedMembers.length === 2) {
74-
// Assume the other user in the native room is the native user.
75-
// Find and pull them from the room's joined member list
76-
const myUserId = MatrixClientPeg.get().getUserId();
77-
nativeUser = nativeRoomjoinedMembers.find((roomMember) => {
78-
return roomMember.userId !== myUserId;
79-
});
74+
let nativeUser: RoomMember;
75+
if (nativeRoomjoinedMembers.length === 2) {
76+
// Assume the other user in the native room is the native user.
77+
// Find and pull them from the room's joined member list
78+
const myUserId = MatrixClientPeg.get().getUserId();
79+
nativeUser = nativeRoomjoinedMembers.find((roomMember) => {
80+
return roomMember.userId !== myUserId;
81+
});
8082

83+
// If a native user was found, return their profile information
84+
if (nativeUser) {
85+
return getSenderNameForUserId(nativeUser.userId);
86+
}
87+
} else {
88+
// Perform a virtual to native user third-party lookup
89+
return window.mxVoipUserMapper.virtualUserToNativeUser(event.sender.userId).then((nativeUserId) => {
8190
// If a native user was found, return their profile information
82-
if (nativeUser) {
91+
if (nativeUserId) {
8392
return getSenderNameForUserId(nativeUser.userId);
8493
}
85-
} else {
86-
// Perform a virtual to native user third-party lookup
87-
return window.mxVoipUserMapper.virtualUserToNativeUser(event.sender.userId).then((nativeUserId) => {
88-
// If a native user was found, return their profile information
89-
if (nativeUserId) {
90-
return getSenderNameForUserId(nativeUser.userId);
91-
}
92-
}).catch((e) => {
93-
console.error('Error lookup up native user for virtual user', e);
94-
return event.sender.userId;
95-
});
96-
}
94+
}).catch((e) => {
95+
console.error('Error lookup up native user for virtual user', e);
96+
return event.sender.userId;
97+
});
9798
}
99+
}
100+
101+
// These functions are frequently used just to check whether an event has
102+
// any text to display at all. For this reason they return deferred values
103+
// to avoid the expense of looking up translations when they're not needed.
104+
105+
function textForCallInviteEvent(event: MatrixEvent): () => string | null {
98106
let isVoice = true;
99107
if (event.getContent().offer && event.getContent().offer.sdp &&
100108
event.getContent().offer.sdp.indexOf('m=video') !== -1) {
@@ -107,19 +115,19 @@ function textForCallInviteEvent(event: MatrixEvent): () => string | null {
107115
// and more accurate, we break out the string-based variables to a couple booleans.
108116
if (isVoice && isSupported) {
109117
return () => _t("%(senderName)s placed a voice call.", {
110-
senderName: getSenderName(),
118+
senderName: getSenderNameFromPotentiallyVirtualUser(event),
111119
});
112120
} else if (isVoice && !isSupported) {
113121
return () => _t("%(senderName)s placed a voice call. (not supported by this browser)", {
114-
senderName: getSenderName(),
122+
senderName: getSenderNameFromPotentiallyVirtualUser(event),
115123
});
116124
} else if (!isVoice && isSupported) {
117125
return () => _t("%(senderName)s placed a video call.", {
118-
senderName: getSenderName(),
126+
senderName: getSenderNameFromPotentiallyVirtualUser(event),
119127
});
120128
} else if (!isVoice && !isSupported) {
121129
return () => _t("%(senderName)s placed a video call. (not supported by this browser)", {
122-
senderName: getSenderName(),
130+
senderName: getSenderNameFromPotentiallyVirtualUser(event),
123131
});
124132
}
125133
}

0 commit comments

Comments
 (0)