Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import CountlyAnalytics from "../CountlyAnalytics";
import UserActivity from "../UserActivity";
import {ModalWidgetStore} from "../stores/ModalWidgetStore";
import { WidgetLayoutStore } from "../stores/widgets/WidgetLayoutStore";
import VoipUserMapper from "../VoipUserMapper";

declare global {
interface Window {
Expand Down Expand Up @@ -66,6 +67,7 @@ declare global {
mxCountlyAnalytics: typeof CountlyAnalytics;
mxUserActivity: UserActivity;
mxModalWidgetStore: ModalWidgetStore;
mxVoipUserMapper: VoipUserMapper;
}

interface Document {
Expand Down
106 changes: 91 additions & 15 deletions src/CallHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,19 @@ import { CallError } from "matrix-js-sdk/src/webrtc/call";
import { logger } from 'matrix-js-sdk/src/logger';
import DesktopCapturerSourcePicker from "./components/views/elements/DesktopCapturerSourcePicker"
import { Action } from './dispatcher/actions';
import { roomForVirtualRoom, getOrCreateVirtualRoomForRoom } from './VoipUserMapper';
import VoipUserMapper from './VoipUserMapper';
import { addManagedHybridWidget, isManagedHybridWidgetEnabled } from './widgets/ManagedHybrid';
import { randomString } from "matrix-js-sdk/src/randomstring";

const CHECK_PSTN_SUPPORT_ATTEMPTS = 3;
export const PROTOCOL_PSTN = 'm.protocol.pstn';
export const PROTOCOL_PSTN_PREFIXED = 'im.vector.protocol.pstn';
export const PROTOCOL_SIP_NATIVE = 'im.vector.protocol.sip_native';
export const PROTOCOL_SIP_VIRTUAL = 'im.vector.protocol.sip_virtual';

const CHECK_PROTOCOLS_ATTEMPTS = 3;
// Event type for room account data and room creation content used to mark rooms as virtual rooms
// (and store the ID of their native room)
export const VIRTUAL_ROOM_EVENT_TYPE = 'im.vector.is_virtual_room';

enum AudioID {
Ring = 'ringAudio',
Expand All @@ -96,6 +104,29 @@ enum AudioID {
Busy = 'busyAudio',
}

interface ThirdpartyLookupResponseFields {
/* eslint-disable camelcase */

// im.vector.sip_native
virtual_mxid?: string;
is_virtual?: boolean;

// im.vector.sip_virtual
native_mxid?: string;
is_native?: boolean;

// common
lookup_success?: boolean;

/* eslint-enable camelcase */
}

interface ThirdpartyLookupResponse {
userid: string,
protocol: string,
fields: ThirdpartyLookupResponseFields,
}

// Unlike 'CallType' in js-sdk, this one includes screen sharing
// (because a screen sharing call is only a screen sharing call to the caller,
// to the callee it's just a video call, at least as far as the current impl
Expand Down Expand Up @@ -126,7 +157,12 @@ export default class CallHandler {
private audioPromises = new Map<AudioID, Promise<void>>();
private dispatcherRef: string = null;
private supportsPstnProtocol = null;
private pstnSupportPrefixed = null; // True if the server only support the prefixed pstn protocol
private supportsSipNativeVirtual = null; // im.vector.protocol.sip_virtual and im.vector.protocol.sip_native
private pstnSupportCheckTimer: NodeJS.Timeout; // number actually because we're in the browser
// For rooms we've been invited to, true if they're from virtual user, false if we've checked and they aren't.
private invitedRoomsAreVirtual = new Map<string, boolean>();
private invitedRoomCheckInProgress = false;

static sharedInstance() {
if (!window.mxCallHandler) {
Expand All @@ -140,9 +176,9 @@ export default class CallHandler {
* Gets the user-facing room associated with a call (call.roomId may be the call "virtual room"
* if a voip_mxid_translate_pattern is set in the config)
*/
public static roomIdForCall(call: MatrixCall) {
public static roomIdForCall(call: MatrixCall): string {
if (!call) return null;
return roomForVirtualRoom(call.roomId) || call.roomId;
return VoipUserMapper.sharedInstance().nativeRoomForVirtualRoom(call.roomId) || call.roomId;
}

start() {
Expand All @@ -163,7 +199,7 @@ export default class CallHandler {
MatrixClientPeg.get().on('Call.incoming', this.onCallIncoming);
}

this.checkForPstnSupport(CHECK_PSTN_SUPPORT_ATTEMPTS);
this.checkProtocols(CHECK_PROTOCOLS_ATTEMPTS);
}

stop() {
Expand All @@ -177,33 +213,73 @@ export default class CallHandler {
}
}

private async checkForPstnSupport(maxTries) {
private async checkProtocols(maxTries) {
try {
const protocols = await MatrixClientPeg.get().getThirdpartyProtocols();
if (protocols['im.vector.protocol.pstn'] !== undefined) {
this.supportsPstnProtocol = protocols['im.vector.protocol.pstn'];
} else if (protocols['m.protocol.pstn'] !== undefined) {
this.supportsPstnProtocol = protocols['m.protocol.pstn'];

if (protocols[PROTOCOL_PSTN] !== undefined) {
this.supportsPstnProtocol = Boolean(protocols[PROTOCOL_PSTN]);
if (this.supportsPstnProtocol) this.pstnSupportPrefixed = false;
} else if (protocols[PROTOCOL_PSTN_PREFIXED] !== undefined) {
this.supportsPstnProtocol = Boolean(protocols[PROTOCOL_PSTN_PREFIXED]);
if (this.supportsPstnProtocol) this.pstnSupportPrefixed = true;
} else {
this.supportsPstnProtocol = null;
}

dis.dispatch({action: Action.PstnSupportUpdated});

if (protocols[PROTOCOL_SIP_NATIVE] !== undefined && protocols[PROTOCOL_SIP_VIRTUAL] !== undefined) {
this.supportsSipNativeVirtual = Boolean(
protocols[PROTOCOL_SIP_NATIVE] && protocols[PROTOCOL_SIP_VIRTUAL],
);
}

dis.dispatch({action: Action.VirtualRoomSupportUpdated});
} catch (e) {
if (maxTries === 1) {
console.log("Failed to check for pstn protocol support and no retries remain: assuming no support", e);
console.log("Failed to check for protocol support and no retries remain: assuming no support", e);
} else {
console.log("Failed to check for pstn protocol support: will retry", e);
console.log("Failed to check for protocol support: will retry", e);
this.pstnSupportCheckTimer = setTimeout(() => {
this.checkForPstnSupport(maxTries - 1);
this.checkProtocols(maxTries - 1);
}, 10000);
}
}
}

getSupportsPstnProtocol() {
public getSupportsPstnProtocol() {
return this.supportsPstnProtocol;
}

public getSupportsVirtualRooms() {
return this.supportsPstnProtocol;
}

public pstnLookup(phoneNumber: string): Promise<ThirdpartyLookupResponse[]> {
return MatrixClientPeg.get().getThirdpartyUser(
this.pstnSupportPrefixed ? PROTOCOL_PSTN_PREFIXED : PROTOCOL_PSTN, {
'm.id.phone': phoneNumber,
},
);
}

public sipVirtualLookup(nativeMxid: string): Promise<ThirdpartyLookupResponse[]> {
return MatrixClientPeg.get().getThirdpartyUser(
PROTOCOL_SIP_VIRTUAL, {
'native_mxid': nativeMxid,
},
);
}

public sipNativeLookup(virtualMxid: string): Promise<ThirdpartyLookupResponse[]> {
return MatrixClientPeg.get().getThirdpartyUser(
PROTOCOL_SIP_NATIVE, {
'virtual_mxid': virtualMxid,
},
);
}

private onCallIncoming = (call) => {
// we dispatch this synchronously to make sure that the event
// handlers on the call are set up immediately (so that if
Expand Down Expand Up @@ -550,7 +626,7 @@ export default class CallHandler {
Analytics.trackEvent('voip', 'placeCall', 'type', type);
CountlyAnalytics.instance.trackStartCall(roomId, type === PlaceCallType.Video, false);

const mappedRoomId = (await getOrCreateVirtualRoomForRoom(roomId)) || roomId;
const mappedRoomId = (await VoipUserMapper.sharedInstance().getOrCreateVirtualRoomForRoom(roomId)) || roomId;
logger.debug("Mapped real room " + roomId + " to room ID " + mappedRoomId);

const call = createNewMatrixCall(MatrixClientPeg.get(), mappedRoomId);
Expand Down
4 changes: 1 addition & 3 deletions src/SlashCommands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,7 @@ export const Commands = [

return success((async () => {
if (isPhoneNumber) {
const results = await MatrixClientPeg.get().getThirdpartyUser('im.vector.protocol.pstn', {
'm.id.phone': userId,
});
const results = await CallHandler.sharedInstance().pstnLookup(this.state.value);
if (!results || results.length === 0 || !results[0].userid) {
throw new Error("Unable to find Matrix ID for phone number");
}
Expand Down
127 changes: 79 additions & 48 deletions src/VoipUserMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,97 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { ensureDMExists, findDMForUser } from './createRoom';
import { ensureVirtualRoomExists, findDMForUser } from './createRoom';
import { MatrixClientPeg } from "./MatrixClientPeg";
import DMRoomMap from "./utils/DMRoomMap";
import SdkConfig from "./SdkConfig";
import CallHandler, { VIRTUAL_ROOM_EVENT_TYPE } from './CallHandler';
import { Room } from 'matrix-js-sdk/src/models/room';

// Functions for mapping users & rooms for the voip_mxid_translate_pattern
// config option
// Functions for mapping virtual users & rooms. Currently the only lookup
// is sip virtual: there could be others in the future.

export function voipUserMapperEnabled(): boolean {
return SdkConfig.get()['voip_mxid_translate_pattern'] !== undefined;
}

// only exported for tests
export function userToVirtualUser(userId: string, templateString?: string): string {
if (templateString === undefined) templateString = SdkConfig.get()['voip_mxid_translate_pattern'];
if (!templateString) return null;
return templateString.replace('${mxid}', encodeURIComponent(userId).replace(/%/g, '=').toLowerCase());
}
export default class VoipUserMapper {
private virtualRoomIdCache = new Set<string>();

// only exported for tests
export function virtualUserToUser(userId: string, templateString?: string): string {
if (templateString === undefined) templateString = SdkConfig.get()['voip_mxid_translate_pattern'];
if (!templateString) return null;
public static sharedInstance(): VoipUserMapper {
if (window.mxVoipUserMapper === undefined) window.mxVoipUserMapper = new VoipUserMapper();
return window.mxVoipUserMapper;
}

const regexString = templateString.replace('${mxid}', '(.+)');
private async userToVirtualUser(userId: string): Promise<string> {
const results = await CallHandler.sharedInstance().sipVirtualLookup(userId);
if (results.length === 0) return null;
return results[0].userid;
}

const match = userId.match('^' + regexString + '$');
if (!match) return null;
public async getOrCreateVirtualRoomForRoom(roomId: string):Promise<string> {
const userId = DMRoomMap.shared().getUserIdForRoomId(roomId);
if (!userId) return null;

return decodeURIComponent(match[1].replace(/=/g, '%'));
}
const virtualUser = await this.userToVirtualUser(userId);
if (!virtualUser) return null;

async function getOrCreateVirtualRoomForUser(userId: string):Promise<string> {
const virtualUser = userToVirtualUser(userId);
if (!virtualUser) return null;
const virtualRoomId = await ensureVirtualRoomExists(MatrixClientPeg.get(), virtualUser, roomId);
MatrixClientPeg.get().setRoomAccountData(virtualRoomId, VIRTUAL_ROOM_EVENT_TYPE, {
native_room: roomId,
});

return await ensureDMExists(MatrixClientPeg.get(), virtualUser);
}
return virtualRoomId;
}

export async function getOrCreateVirtualRoomForRoom(roomId: string):Promise<string> {
const user = DMRoomMap.shared().getUserIdForRoomId(roomId);
if (!user) return null;
return getOrCreateVirtualRoomForUser(user);
}
public nativeRoomForVirtualRoom(roomId: string):string {
const virtualRoom = MatrixClientPeg.get().getRoom(roomId);
if (!virtualRoom) return null;
const virtualRoomEvent = virtualRoom.getAccountData(VIRTUAL_ROOM_EVENT_TYPE);
if (!virtualRoomEvent || !virtualRoomEvent.getContent()) return null;
return virtualRoomEvent.getContent()['native_room'] || null;
}

export function roomForVirtualRoom(roomId: string):string {
const virtualUser = DMRoomMap.shared().getUserIdForRoomId(roomId);
if (!virtualUser) return null;
const realUser = virtualUserToUser(virtualUser);
const room = findDMForUser(MatrixClientPeg.get(), realUser);
if (room) {
return room.roomId;
} else {
return null;
public isVirtualRoom(room: Room):boolean {
if (this.nativeRoomForVirtualRoom(room.roomId)) return true;

if (this.virtualRoomIdCache.has(room.roomId)) return true;

// also look in the create event for the claimed native room ID, which is the only
// way we can recognise a virtual room we've created when it first arrives down
// our stream. We don't trust this in general though, as it could be faked by an
// inviter: our main source of truth is the DM state.
const roomCreateEvent = room.currentState.getStateEvents("m.room.create", "");
if (!roomCreateEvent || !roomCreateEvent.getContent()) return false;
// we only look at this for rooms we created (so inviters can't just cause rooms
// to be invisible)
if (roomCreateEvent.getSender() !== MatrixClientPeg.get().getUserId()) return false;
const claimedNativeRoomId = roomCreateEvent.getContent()[VIRTUAL_ROOM_EVENT_TYPE];
return Boolean(claimedNativeRoomId);
}
}

export function isVirtualRoom(roomId: string):boolean {
const virtualUser = DMRoomMap.shared().getUserIdForRoomId(roomId);
if (!virtualUser) return null;
const realUser = virtualUserToUser(virtualUser);
return Boolean(realUser);
public async onNewInvitedRoom(invitedRoom: Room) {
const inviterId = invitedRoom.getDMInviter();
console.log(`Checking virtual-ness of room ID ${invitedRoom.roomId}, invited by ${inviterId}`);
const result = await CallHandler.sharedInstance().sipNativeLookup(inviterId);
if (result.length === 0) {
return true;
}

if (result[0].fields.is_virtual) {
const nativeUser = result[0].userid;
const nativeRoom = findDMForUser(MatrixClientPeg.get(), nativeUser);
if (nativeRoom) {
// It's a virtual room with a matching native room, so set the room account data. This
// will make sure we know where how to map calls and also allow us know not to display
// it in the future.
MatrixClientPeg.get().setRoomAccountData(invitedRoom.roomId, VIRTUAL_ROOM_EVENT_TYPE, {
native_room: nativeRoom.roomId,
});
// also auto-join the virtual room if we have a matching native room
// (possibly we should only join if we've also joined the native room, then we'd also have
// to make sure we joined virtual rooms on joining a native one)
MatrixClientPeg.get().joinRoom(invitedRoom.roomId);
}

// also put this room in the virtual room ID cache so isVirtualRoom return the right answer
// in however long it takes for the echo of setAccountData to come down the sync
this.virtualRoomIdCache.add(invitedRoom.roomId);
}
}
}
5 changes: 2 additions & 3 deletions src/components/views/voip/DialPadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import DialPad from './DialPad';
import dis from '../../../dispatcher/dispatcher';
import Modal from "../../../Modal";
import ErrorDialog from "../../views/dialogs/ErrorDialog";
import CallHandler from "../../../CallHandler";

interface IProps {
onFinished: (boolean) => void;
Expand Down Expand Up @@ -64,9 +65,7 @@ export default class DialpadModal extends React.PureComponent<IProps, IState> {
}

onDialPress = async () => {
const results = await MatrixClientPeg.get().getThirdpartyUser('im.vector.protocol.pstn', {
'm.id.phone': this.state.value,
});
const results = await CallHandler.sharedInstance().pstnLookup(this.state.value);
if (!results || results.length === 0 || !results[0].userid) {
Modal.createTrackedDialog('', '', ErrorDialog, {
title: _t("Unable to look up phone number"),
Expand Down
Loading