From a7a7cb56fe0171a32f58f9deff5c1fa0177dfeb7 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 20 Jan 2022 09:32:15 +0000 Subject: [PATCH] Cancel pending events in virtual room when call placed (#7583) As the comment hopefully explains. Also add public qualifiers to the methods in Resend which lacked any visibility specifiers. Fixes https://github.com/vector-im/element-web/issues/17594 --- src/CallHandler.tsx | 13 +++++++++++++ src/Resend.ts | 8 ++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/CallHandler.tsx b/src/CallHandler.tsx index f4bf86c6b2..76e9d50d69 100644 --- a/src/CallHandler.tsx +++ b/src/CallHandler.tsx @@ -54,6 +54,7 @@ import { WidgetLayoutStore, Container } from './stores/widgets/WidgetLayoutStore import { getIncomingCallToastKey } from './toasts/IncomingCallToast'; import ToastStore from './stores/ToastStore'; import IncomingCallToast from "./toasts/IncomingCallToast"; +import Resend from './Resend'; export const PROTOCOL_PSTN = 'm.protocol.pstn'; export const PROTOCOL_PSTN_PREFIXED = 'im.vector.protocol.pstn'; @@ -737,6 +738,18 @@ export default class CallHandler extends EventEmitter { const mappedRoomId = (await VoipUserMapper.sharedInstance().getOrCreateVirtualRoomForRoom(roomId)) || roomId; logger.debug("Mapped real room " + roomId + " to room ID " + mappedRoomId); + // If we're using a virtual room nd there are any events pending, try to resend them, + // otherwise the call will fail and because its a virtual room, the user won't be able + // to see it to either retry or clear the pending events. There will only be call events + // in this queue, and since we're about to place a new call, they can only be events from + // previous calls that are probably stale by now, so just cancel them. + if (mappedRoomId !== roomId) { + const mappedRoom = MatrixClientPeg.get().getRoom(mappedRoomId); + if (mappedRoom.getPendingEvents().length > 0) { + Resend.cancelUnsentEvents(mappedRoom); + } + } + const timeUntilTurnCresExpire = MatrixClientPeg.get().getTurnServersExpiry() - Date.now(); logger.log("Current turn creds expire in " + timeUntilTurnCresExpire + " ms"); const call = MatrixClientPeg.get().createCall(mappedRoomId); diff --git a/src/Resend.ts b/src/Resend.ts index 241fb7f89b..eefd999bd7 100644 --- a/src/Resend.ts +++ b/src/Resend.ts @@ -22,7 +22,7 @@ import { MatrixClientPeg } from './MatrixClientPeg'; import dis from './dispatcher/dispatcher'; export default class Resend { - static resendUnsentEvents(room: Room): Promise { + public static resendUnsentEvents(room: Room): Promise { return Promise.all(room.getPendingEvents().filter(function(ev: MatrixEvent) { return ev.status === EventStatus.NOT_SENT; }).map(function(event: MatrixEvent) { @@ -30,7 +30,7 @@ export default class Resend { })); } - static cancelUnsentEvents(room: Room): void { + public static cancelUnsentEvents(room: Room): void { room.getPendingEvents().filter(function(ev: MatrixEvent) { return ev.status === EventStatus.NOT_SENT; }).forEach(function(event: MatrixEvent) { @@ -38,7 +38,7 @@ export default class Resend { }); } - static resend(event: MatrixEvent): Promise { + public static resend(event: MatrixEvent): Promise { const room = MatrixClientPeg.get().getRoom(event.getRoomId()); return MatrixClientPeg.get().resendEvent(event, room).then(function(res) { dis.dispatch({ @@ -52,7 +52,7 @@ export default class Resend { }); } - static removeFromQueue(event: MatrixEvent): void { + public static removeFromQueue(event: MatrixEvent): void { MatrixClientPeg.get().cancelPendingEvent(event); } }