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

Commit

Permalink
Optimise for 2 person rooms, which should be 99% of cases
Browse files Browse the repository at this point in the history
Performing a virtual user -> native user lookup is rather expensive. It
requires making a third-party lookup API call in the background. We
would rather not need to do so every time a call comes in.

Translating a virtual room to a native room is rather cheap in contrast
- the client already knows about it. Thus, we can make an optimisation
by checking if the corresponding native room has only two users in it.
If so, we can likely assume that whichever user in the native that's not
us, is probably the same user that calls us via the virtual room.

Thus in that case, we just return that user's profile information.
  • Loading branch information
anoadragon453 committed Nov 17, 2021
1 parent 072874c commit 04505b7
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/TextForEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,74 @@ import { ROOM_SECURITY_TAB } from "./components/views/dialogs/RoomSettingsDialog
import { logger } from "matrix-js-sdk/src/logger";
import { removeDirectionOverrideChars } from 'matrix-js-sdk/src/utils';
import CallHandler from './CallHandler';
import { RoomMember } from '../../matrix-js-sdk/src';

// These functions are frequently used just to check whether an event has
// any text to display at all. For this reason they return deferred values
// to avoid the expense of looking up translations when they're not needed.

function textForCallInviteEvent(event: MatrixEvent): () => string | null {
const getSenderName = () => {
const room = MatrixClientPeg.get().getRoom(event.getRoomId());
// 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.sharedInstance().getSupportsVirtualRooms() || !window.mxVoipUserMapper.isVirtualRoom(room)) {
if (
!CallHandler.sharedInstance().getSupportsVirtualRooms() ||
!window.mxVoipUserMapper.isVirtualRoom(potentiallyVirtualRoom)
) {
// If not, simply extract the sender information from the incoming event
return event.sender ? event.sender.name : _t('Someone');
}

// Otherwise, assume the caller is a virtual user and attempt to look up the corresponding
// native user
const senderMxid = event.getSender();
let nativeUserId: string = null;

// Perform a virtual to native user third-party lookup
window.mxVoipUserMapper.virtualUserToNativeUser(senderMxid).then((userId) => {
nativeUserId = userId;
});
// 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();

// If a native user was found, return their information instead
if (nativeUserId) {
// TODO: Optimize for 2 people rooms
MatrixClientPeg.get().getProfileInfo(nativeUserId).then((resp) => {
const getSenderNameForUserId = (userId) => {
return MatrixClientPeg.get().getProfileInfo(userId).then((resp) => {
if (resp.displayname) {
return resp.displayname;
}

return nativeUserId;
return userId;
}).catch((e) => {
console.error('Error getting native user profile', e);
return nativeUserId;
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;
});

// FIXME: Find a better way to determine this from the event?
// If a native user was found, return their profile information
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;
});
}
}
let isVoice = true;
if (event.getContent().offer && event.getContent().offer.sdp &&
event.getContent().offer.sdp.indexOf('m=video') !== -1) {
Expand Down

0 comments on commit 04505b7

Please sign in to comment.