@@ -27,74 +27,82 @@ import { SetRightPanelPhasePayload } from './dispatcher/payloads/SetRightPanelPh
27
27
import { MatrixEvent } from "matrix-js-sdk/src/models/event" ;
28
28
import { MatrixClientPeg } from "./MatrixClientPeg" ;
29
29
import CallHandler from './CallHandler' ;
30
- import { RoomMember } from '../../ matrix-js-sdk/src' ;
30
+ import { RoomMember } from 'matrix-js-sdk/src/models/room-member ' ;
31
31
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
+ }
49
51
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
52
54
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 ( ) ;
58
60
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
+ }
64
66
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
+ } ;
71
73
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
+ } ) ;
80
82
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 ) => {
81
90
// If a native user was found, return their profile information
82
- if ( nativeUser ) {
91
+ if ( nativeUserId ) {
83
92
return getSenderNameForUserId ( nativeUser . userId ) ;
84
93
}
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
+ } ) ;
97
98
}
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 {
98
106
let isVoice = true ;
99
107
if ( event . getContent ( ) . offer && event . getContent ( ) . offer . sdp &&
100
108
event . getContent ( ) . offer . sdp . indexOf ( 'm=video' ) !== - 1 ) {
@@ -107,19 +115,19 @@ function textForCallInviteEvent(event: MatrixEvent): () => string | null {
107
115
// and more accurate, we break out the string-based variables to a couple booleans.
108
116
if ( isVoice && isSupported ) {
109
117
return ( ) => _t ( "%(senderName)s placed a voice call." , {
110
- senderName : getSenderName ( ) ,
118
+ senderName : getSenderNameFromPotentiallyVirtualUser ( event ) ,
111
119
} ) ;
112
120
} else if ( isVoice && ! isSupported ) {
113
121
return ( ) => _t ( "%(senderName)s placed a voice call. (not supported by this browser)" , {
114
- senderName : getSenderName ( ) ,
122
+ senderName : getSenderNameFromPotentiallyVirtualUser ( event ) ,
115
123
} ) ;
116
124
} else if ( ! isVoice && isSupported ) {
117
125
return ( ) => _t ( "%(senderName)s placed a video call." , {
118
- senderName : getSenderName ( ) ,
126
+ senderName : getSenderNameFromPotentiallyVirtualUser ( event ) ,
119
127
} ) ;
120
128
} else if ( ! isVoice && ! isSupported ) {
121
129
return ( ) => _t ( "%(senderName)s placed a video call. (not supported by this browser)" , {
122
- senderName : getSenderName ( ) ,
130
+ senderName : getSenderNameFromPotentiallyVirtualUser ( event ) ,
123
131
} ) ;
124
132
}
125
133
}
0 commit comments