This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 822
Fix displayed user in notifications for virtual rooms #6666
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b143676
Handle virtual rooms when showing a notification
anoadragon453 072874c
Show profile info of native users in notifs when a virtual user calls
anoadragon453 04505b7
Optimise for 2 person rooms, which should be 99% of cases
anoadragon453 45bb2df
Move getSender to a separate function
anoadragon453 6d25625
Merge remote-tracking branch 'origin/develop' into anoa/virtual_rooms…
dbkr 22a8f8f
s/sharedInstance()/instance/
dbkr 5f78129
Fix tests
dbkr 1ffa16a
Add jsdoc asterisk
dbkr db51e96
Add return type doc
dbkr 2fc3ce6
Sort out types
dbkr 768bbf6
Fix merge
dbkr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,9 @@ import { | |
PollStartEvent, | ||
} from "matrix-events-sdk"; | ||
import { LOCATION_EVENT_TYPE } from "matrix-js-sdk/src/@types/location"; | ||
import { RoomMember } from 'matrix-js-sdk/src/models/room-member'; | ||
|
||
import CallHandler from './CallHandler'; | ||
import { _t } from './languageHandler'; | ||
import * as Roles from './Roles'; | ||
import { isValid3pidInvite } from "./RoomInvite"; | ||
|
@@ -46,8 +48,73 @@ import AccessibleButton from './components/views/elements/AccessibleButton'; | |
import RightPanelStore from './stores/right-panel/RightPanelStore'; | ||
import UserIdentifierCustomisations from './customisations/UserIdentifier'; | ||
|
||
export function getSenderName(event: MatrixEvent): string { | ||
return event.sender?.name ?? event.getSender() ?? _t("Someone"); | ||
/** | ||
* Attempt to retrieve the displayname of a user from an event. If the user is a virtual user, | ||
* return the displayname of the native user instead. In either case, if a displayname cannot | ||
* be found, simply return the MXID of the user instead. | ||
* | ||
* @param event The event to extract the user from. | ||
*/ | ||
export function getSenderName(event: MatrixEvent): Promise<string> { | ||
// Retrieve the room ID from the event | ||
const potentiallyVirtualRoom = MatrixClientPeg.get().getRoom(event.getRoomId()); | ||
|
||
// Check if the event is from a virtual room | ||
if ( | ||
!CallHandler.instance.getSupportsVirtualRooms() || | ||
!window.mxVoipUserMapper.isVirtualRoom(potentiallyVirtualRoom) | ||
) { | ||
// If not, simply extract the sender information from the incoming event | ||
return Promise.resolve(event.sender?.name ?? event.getSender() ?? _t("Someone")); | ||
} | ||
|
||
// Otherwise, assume the caller is a virtual user and attempt to look up the corresponding | ||
// native user | ||
|
||
// We can avoid a potentially expensive virtual -> native user lookup by checking if the native room | ||
// only has two people in it. If so, then the other user in that room is likely the matching native user. | ||
const nativeRoomId = window.mxVoipUserMapper.nativeRoomForVirtualRoom(potentiallyVirtualRoom.roomId); | ||
const nativeRoom = MatrixClientPeg.get().getRoom(nativeRoomId); | ||
const nativeRoomjoinedMembers = nativeRoom.getJoinedMembers(); | ||
|
||
const getSenderNameForUserId = (userId: string): Promise<string> => { | ||
return MatrixClientPeg.get().getProfileInfo(userId).then((resp) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it doesn't matter in the long run, but we try to use |
||
if (resp.displayname) { | ||
return resp.displayname; | ||
} | ||
|
||
return userId; | ||
}).catch((e) => { | ||
console.error('Error getting native user profile', e); | ||
return userId; | ||
}); | ||
}; | ||
|
||
let nativeUser: RoomMember; | ||
if (nativeRoomjoinedMembers.length === 2) { | ||
// Assume the other user in the native room is the native user. | ||
// Find and pull them from the room's joined member list | ||
const myUserId = MatrixClientPeg.get().getUserId(); | ||
nativeUser = nativeRoomjoinedMembers.find((roomMember) => { | ||
return roomMember.userId !== myUserId; | ||
}); | ||
|
||
// If a native user was found, return their profile information | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and if not? The function ends up returning undefined - would be good to make that explicit |
||
if (nativeUser) { | ||
return getSenderNameForUserId(nativeUser.userId); | ||
} | ||
} else { | ||
// Perform a virtual to native user third-party lookup | ||
return window.mxVoipUserMapper.virtualUserToNativeUser(event.sender.userId).then((nativeUserId) => { | ||
// If a native user was found, return their profile information | ||
if (nativeUserId) { | ||
return getSenderNameForUserId(nativeUser.userId); | ||
} | ||
}).catch((e) => { | ||
console.error('Error lookup up native user for virtual user', e); | ||
return event.sender.userId; | ||
}); | ||
} | ||
} | ||
|
||
// These functions are frequently used just to check whether an event has | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@turt2live I'm actually not sure if this is a bug fix, since notifications never had support for virtual -> native user mapping in the first place.
And it is user visible... so I suppose this would be an enhancement, with a changelog entry of "Show details of the native room in notifications when new events occur in virtual rooms." Does that sound sensible?
My only worry is that this will make no sense to 99% of users 😋