Skip to content

Commit

Permalink
Emit unknown device errors for group call participants without e2e (#…
Browse files Browse the repository at this point in the history
…2447)

* Emit unknown device errors for group call participants without e2e

There are a number of different cases here: there were some before
when dealing with versions that didn't send deviceId. This catches
all of them and makes all these cases emit the same error.

* Add type
  • Loading branch information
dbkr committed Jun 10, 2022
1 parent 5e76697 commit b97b862
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { MatrixClient } from "../client";
import { ISendEventResponse } from "../@types/requests";
import { EventEmitterEvents, TypedEventEmitter } from "../models/typed-event-emitter";
import { DeviceInfo } from '../crypto/deviceinfo';
import { GroupCallUnknownDeviceError } from './groupCall';

// events: hangup, error(err), replaced(call), state(state, oldState)

Expand Down Expand Up @@ -521,7 +522,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
const deviceInfoMap = await this.client.crypto.deviceList.downloadKeys([userId], false);
this.opponentDeviceInfo = deviceInfoMap[userId][this.opponentDeviceId];
if (this.opponentDeviceInfo === undefined) {
throw new Error(`No keys found for opponent device ${this.opponentDeviceId}!`);
throw new GroupCallUnknownDeviceError(userId);
}
}

Expand Down
18 changes: 11 additions & 7 deletions src/webrtc/callEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { CallDirection, CallErrorCode, CallState, createNewMatrixCall, MatrixCal
import { EventType } from '../@types/event';
import { ClientEvent, MatrixClient } from '../client';
import { MCallAnswer, MCallHangupReject } from "./callEventTypes";
import { GroupCallError, GroupCallErrorCode, GroupCallEvent } from './groupCall';
import { GroupCall, GroupCallErrorCode, GroupCallEvent, GroupCallUnknownDeviceError } from './groupCall';
import { RoomEvent } from "../models/room";

// Don't ring unless we'd be ringing for at least 3 seconds: the user needs some
Expand Down Expand Up @@ -210,8 +210,9 @@ export class CallEventHandler {

let opponentDeviceId: string | undefined;

let groupCall: GroupCall;
if (groupCallId) {
const groupCall = this.client.groupCallEventHandler.getGroupCallById(groupCallId);
groupCall = this.client.groupCallEventHandler.getGroupCallById(groupCallId);

if (!groupCall) {
logger.warn(`Cannot find a group call ${groupCallId} for event ${type}. Ignoring event.`);
Expand All @@ -224,10 +225,7 @@ export class CallEventHandler {
logger.warn(`Cannot find a device id for ${senderId}. Ignoring event.`);
groupCall.emit(
GroupCallEvent.Error,
new GroupCallError(
GroupCallErrorCode.UnknownDevice,
`Incoming Call: No opponent device found for ${senderId}, ignoring.`,
),
new GroupCallUnknownDeviceError(senderId),
);
return;
}
Expand Down Expand Up @@ -282,7 +280,13 @@ export class CallEventHandler {
}

call.callId = content.call_id;
await call.initWithInvite(event);
try {
await call.initWithInvite(event);
} catch (e) {
if (e.code === GroupCallErrorCode.UnknownDevice) {
groupCall?.emit(GroupCallEvent.Error, e);
}
}
this.calls.set(call.callId, call);

// if we stashed candidate events for that call ID, play them back now
Expand Down
31 changes: 19 additions & 12 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export enum GroupCallEvent {
LocalScreenshareStateChanged = "local_screenshare_state_changed",
LocalMuteStateChanged = "local_mute_state_changed",
ParticipantsChanged = "participants_changed",
Error = "error"
Error = "error",
}

export type GroupCallEventHandlerMap = {
Expand Down Expand Up @@ -77,6 +77,12 @@ export class GroupCallError extends Error {
}
}

export class GroupCallUnknownDeviceError extends GroupCallError {
constructor(public userId: string) {
super(GroupCallErrorCode.UnknownDevice, "No device found for " + userId);
}
}

export class OtherUserSpeakingError extends Error {
constructor() {
super("Cannot unmute: another user is speaking");
Expand Down Expand Up @@ -750,10 +756,7 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
logger.warn(`No opponent device found for ${member.userId}, ignoring.`);
this.emit(
GroupCallEvent.Error,
new GroupCallError(
GroupCallErrorCode.UnknownDevice,
`Outgoing Call: No opponent device found for ${member.userId}, ignoring.`,
),
new GroupCallUnknownDeviceError(member.userId),
);
return;
}
Expand Down Expand Up @@ -791,13 +794,17 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
);
} catch (e) {
logger.warn(`Failed to place call to ${member.userId}!`, e);
this.emit(
GroupCallEvent.Error,
new GroupCallError(
GroupCallErrorCode.PlaceCallFailed,
`Failed to place call to ${member.userId}.`,
),
);
if (e.code === GroupCallErrorCode.UnknownDevice) {
this.emit(GroupCallEvent.Error, e);
} else {
this.emit(
GroupCallEvent.Error,
new GroupCallError(
GroupCallErrorCode.PlaceCallFailed,
`Failed to place call to ${member.userId}.`,
),
);
}
return;
}

Expand Down

0 comments on commit b97b862

Please sign in to comment.