Skip to content

Commit

Permalink
Fix calls on homeservers without the unstable thirdparty endpoints
Browse files Browse the repository at this point in the history
Calling that endpoint throws an error and aborts the entire call. We do
check if an empty list or null is returned by that endpoint everywhere,
so returning an empty list simulates the thirdparty stuff just not being
found.

Checking for "this.supportsSipNativeVirtual" doesn't necessarily work,
since that might not be set yet and as such breaks calls that rely on
this functionality working.

fixes element-hq/element-web#21680

Signed-off-by: Nicolas Werner <[email protected]>
  • Loading branch information
deepbluev7 committed Jun 29, 2022
1 parent 80be02b commit 5e70710
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 19 deletions.
48 changes: 30 additions & 18 deletions src/CallHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,28 +265,40 @@ export default class CallHandler extends EventEmitter {
return this.supportsSipNativeVirtual;
}

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

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

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

private onCallIncoming = (call: MatrixCall): void => {
Expand Down
87 changes: 86 additions & 1 deletion test/CallHandler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('CallHandler', () => {
let pstnLookup: string;
let nativeLookup: string;

beforeEach(() => {
beforeEach(async () => {
stubClient();
MatrixClientPeg.get().createCall = roomId => {
if (fakeCall && fakeCall.roomId !== roomId) {
Expand Down Expand Up @@ -322,3 +322,88 @@ describe('CallHandler', () => {
expect(callHandler.getCallForRoom(NATIVE_ROOM_CHARLIE)).toBe(fakeCall);
});
});

describe('CallHandler without third party protocols', () => {
let dmRoomMap;
let callHandler;
let audioElement;
let fakeCall;

beforeEach(() => {
stubClient();
MatrixClientPeg.get().createCall = roomId => {
if (fakeCall && fakeCall.roomId !== roomId) {
throw new Error("Only one call is supported!");
}
fakeCall = new FakeCall(roomId);
return fakeCall;
};

MatrixClientPeg.get().getThirdpartyProtocols = () => {
throw new Error("Endpoint unsupported.");
};

callHandler = new CallHandler();
callHandler.start();

const nativeRoomAlice = mkStubDM(NATIVE_ROOM_ALICE, NATIVE_ALICE);

MatrixClientPeg.get().getRoom = roomId => {
switch (roomId) {
case NATIVE_ROOM_ALICE:
return nativeRoomAlice;
}
};

dmRoomMap = {
getUserIdForRoomId: roomId => {
if (roomId === NATIVE_ROOM_ALICE) {
return NATIVE_ALICE;
} else {
return null;
}
},
getDMRoomsForUserId: userId => {
if (userId === NATIVE_ALICE) {
return [NATIVE_ROOM_ALICE];
} else {
return [];
}
},
};
DMRoomMap.setShared(dmRoomMap);

MatrixClientPeg.get().getThirdpartyUser = (proto, params) => {
throw new Error("Endpoint unsupported.");
};

audioElement = document.createElement('audio');
audioElement.id = "remoteAudio";
document.body.appendChild(audioElement);
});

afterEach(() => {
callHandler.stop();
DMRoomMap.setShared(null);
// @ts-ignore
window.mxCallHandler = null;
fakeCall = null;
MatrixClientPeg.unset();

document.body.removeChild(audioElement);
SdkConfig.unset();
});

it('should still start a native call', async () => {
callHandler.placeCall(NATIVE_ROOM_ALICE, CallType.Voice);

await untilCallHandlerEvent(callHandler, CallHandlerEvent.CallState);

// Check that a call was started: its room on the protocol level
// should be the virtual room
expect(fakeCall.roomId).toEqual(NATIVE_ROOM_ALICE);

// but it should appear to the user to be in thw native room for Bob
expect(callHandler.roomIdForCall(fakeCall)).toEqual(NATIVE_ROOM_ALICE);
});
});

0 comments on commit 5e70710

Please sign in to comment.