@@ -34,74 +34,82 @@ import { ROOM_SECURITY_TAB } from "./components/views/dialogs/RoomSettingsDialog
34
34
import { logger } from "matrix-js-sdk/src/logger" ;
35
35
import { removeDirectionOverrideChars } from 'matrix-js-sdk/src/utils' ;
36
36
import CallHandler from './CallHandler' ;
37
- import { RoomMember } from '../../ matrix-js-sdk/src' ;
37
+ import { RoomMember } from 'matrix-js-sdk/src/models/room-member ' ;
38
38
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
+ }
56
58
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
59
61
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 ( ) ;
65
67
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
+ }
71
73
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
+ } ;
78
80
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
+ } ) ;
87
89
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 ) => {
88
97
// If a native user was found, return their profile information
89
- if ( nativeUser ) {
98
+ if ( nativeUserId ) {
90
99
return getSenderNameForUserId ( nativeUser . userId ) ;
91
100
}
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
+ } ) ;
104
105
}
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 {
105
113
let isVoice = true ;
106
114
if ( event . getContent ( ) . offer && event . getContent ( ) . offer . sdp &&
107
115
event . getContent ( ) . offer . sdp . indexOf ( 'm=video' ) !== - 1 ) {
@@ -114,19 +122,19 @@ function textForCallInviteEvent(event: MatrixEvent): () => string | null {
114
122
// and more accurate, we break out the string-based variables to a couple booleans.
115
123
if ( isVoice && isSupported ) {
116
124
return ( ) => _t ( "%(senderName)s placed a voice call." , {
117
- senderName : getSenderName ( ) ,
125
+ senderName : getSenderNameFromPotentiallyVirtualUser ( event ) ,
118
126
} ) ;
119
127
} else if ( isVoice && ! isSupported ) {
120
128
return ( ) => _t ( "%(senderName)s placed a voice call. (not supported by this browser)" , {
121
- senderName : getSenderName ( ) ,
129
+ senderName : getSenderNameFromPotentiallyVirtualUser ( event ) ,
122
130
} ) ;
123
131
} else if ( ! isVoice && isSupported ) {
124
132
return ( ) => _t ( "%(senderName)s placed a video call." , {
125
- senderName : getSenderName ( ) ,
133
+ senderName : getSenderNameFromPotentiallyVirtualUser ( event ) ,
126
134
} ) ;
127
135
} else if ( ! isVoice && ! isSupported ) {
128
136
return ( ) => _t ( "%(senderName)s placed a video call. (not supported by this browser)" , {
129
- senderName : getSenderName ( ) ,
137
+ senderName : getSenderNameFromPotentiallyVirtualUser ( event ) ,
130
138
} ) ;
131
139
}
132
140
}
0 commit comments