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

Commit 45bb2df

Browse files
committed
Move getSender to a separate function
Most of this commit can be safely ignored.
1 parent 04505b7 commit 45bb2df

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
@@ -34,74 +34,82 @@ import { ROOM_SECURITY_TAB } from "./components/views/dialogs/RoomSettingsDialog
3434
import { logger } from "matrix-js-sdk/src/logger";
3535
import { removeDirectionOverrideChars } from 'matrix-js-sdk/src/utils';
3636
import CallHandler from './CallHandler';
37-
import { RoomMember } from '../../matrix-js-sdk/src';
37+
import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
3838

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

57-
// Otherwise, assume the caller is a virtual user and attempt to look up the corresponding
58-
// native user
59+
// Otherwise, assume the caller is a virtual user and attempt to look up the corresponding
60+
// native user
5961

60-
// We can avoid a potentially expensive virtual -> native user lookup by checking if the native room
61-
// only has two people in it. If so, then the other user in that room is likely the matching native user.
62-
const nativeRoomId = window.mxVoipUserMapper.nativeRoomForVirtualRoom(potentiallyVirtualRoom.roomId);
63-
const nativeRoom = MatrixClientPeg.get().getRoom(nativeRoomId);
64-
const nativeRoomjoinedMembers = nativeRoom.getJoinedMembers();
62+
// We can avoid a potentially expensive virtual -> native user lookup by checking if the native room
63+
// only has two people in it. If so, then the other user in that room is likely the matching native user.
64+
const nativeRoomId = window.mxVoipUserMapper.nativeRoomForVirtualRoom(potentiallyVirtualRoom.roomId);
65+
const nativeRoom = MatrixClientPeg.get().getRoom(nativeRoomId);
66+
const nativeRoomjoinedMembers = nativeRoom.getJoinedMembers();
6567

66-
const getSenderNameForUserId = (userId) => {
67-
return MatrixClientPeg.get().getProfileInfo(userId).then((resp) => {
68-
if (resp.displayname) {
69-
return resp.displayname;
70-
}
68+
const getSenderNameForUserId = (userId) => {
69+
return MatrixClientPeg.get().getProfileInfo(userId).then((resp) => {
70+
if (resp.displayname) {
71+
return resp.displayname;
72+
}
7173

72-
return userId;
73-
}).catch((e) => {
74-
console.error('Error getting native user profile', e);
75-
return userId;
76-
});
77-
}
74+
return userId;
75+
}).catch((e) => {
76+
console.error('Error getting native user profile', e);
77+
return userId;
78+
});
79+
};
7880

79-
let nativeUser: RoomMember;
80-
if (nativeRoomjoinedMembers.length === 2) {
81-
// Assume the other user in the native room is the native user.
82-
// Find and pull them from the room's joined member list
83-
const myUserId = MatrixClientPeg.get().getUserId();
84-
nativeUser = nativeRoomjoinedMembers.find((roomMember) => {
85-
return roomMember.userId !== myUserId;
86-
});
81+
let nativeUser: RoomMember;
82+
if (nativeRoomjoinedMembers.length === 2) {
83+
// Assume the other user in the native room is the native user.
84+
// Find and pull them from the room's joined member list
85+
const myUserId = MatrixClientPeg.get().getUserId();
86+
nativeUser = nativeRoomjoinedMembers.find((roomMember) => {
87+
return roomMember.userId !== myUserId;
88+
});
8789

90+
// If a native user was found, return their profile information
91+
if (nativeUser) {
92+
return getSenderNameForUserId(nativeUser.userId);
93+
}
94+
} else {
95+
// Perform a virtual to native user third-party lookup
96+
return window.mxVoipUserMapper.virtualUserToNativeUser(event.sender.userId).then((nativeUserId) => {
8897
// If a native user was found, return their profile information
89-
if (nativeUser) {
98+
if (nativeUserId) {
9099
return getSenderNameForUserId(nativeUser.userId);
91100
}
92-
} else {
93-
// Perform a virtual to native user third-party lookup
94-
return window.mxVoipUserMapper.virtualUserToNativeUser(event.sender.userId).then((nativeUserId) => {
95-
// If a native user was found, return their profile information
96-
if (nativeUserId) {
97-
return getSenderNameForUserId(nativeUser.userId);
98-
}
99-
}).catch((e) => {
100-
console.error('Error lookup up native user for virtual user', e);
101-
return event.sender.userId;
102-
});
103-
}
101+
}).catch((e) => {
102+
console.error('Error lookup up native user for virtual user', e);
103+
return event.sender.userId;
104+
});
104105
}
106+
}
107+
108+
// These functions are frequently used just to check whether an event has
109+
// any text to display at all. For this reason they return deferred values
110+
// to avoid the expense of looking up translations when they're not needed.
111+
112+
function textForCallInviteEvent(event: MatrixEvent): () => string | null {
105113
let isVoice = true;
106114
if (event.getContent().offer && event.getContent().offer.sdp &&
107115
event.getContent().offer.sdp.indexOf('m=video') !== -1) {
@@ -114,19 +122,19 @@ function textForCallInviteEvent(event: MatrixEvent): () => string | null {
114122
// and more accurate, we break out the string-based variables to a couple booleans.
115123
if (isVoice && isSupported) {
116124
return () => _t("%(senderName)s placed a voice call.", {
117-
senderName: getSenderName(),
125+
senderName: getSenderNameFromPotentiallyVirtualUser(event),
118126
});
119127
} else if (isVoice && !isSupported) {
120128
return () => _t("%(senderName)s placed a voice call. (not supported by this browser)", {
121-
senderName: getSenderName(),
129+
senderName: getSenderNameFromPotentiallyVirtualUser(event),
122130
});
123131
} else if (!isVoice && isSupported) {
124132
return () => _t("%(senderName)s placed a video call.", {
125-
senderName: getSenderName(),
133+
senderName: getSenderNameFromPotentiallyVirtualUser(event),
126134
});
127135
} else if (!isVoice && !isSupported) {
128136
return () => _t("%(senderName)s placed a video call. (not supported by this browser)", {
129-
senderName: getSenderName(),
137+
senderName: getSenderNameFromPotentiallyVirtualUser(event),
130138
});
131139
}
132140
}

0 commit comments

Comments
 (0)