From cc7de786229b7d6a360c29838a95557025c8c04a Mon Sep 17 00:00:00 2001 From: Timo Date: Wed, 8 Jan 2025 18:18:27 +0100 Subject: [PATCH] Remove methods and functions that are from MatrixRTCSession (they now live in MyMembershipManager) --- src/matrixrtc/MatrixRTCSession.ts | 217 ------------------------------ 1 file changed, 217 deletions(-) diff --git a/src/matrixrtc/MatrixRTCSession.ts b/src/matrixrtc/MatrixRTCSession.ts index 7483d09656..de00b2e666 100644 --- a/src/matrixrtc/MatrixRTCSession.ts +++ b/src/matrixrtc/MatrixRTCSession.ts @@ -852,195 +852,6 @@ export class MatrixRTCSession extends TypedEventEmitter => { - // TODO: Should this await on a shared promise? - if (this.updateCallMembershipRunning) { - this.needCallMembershipUpdate = true; - return; - } - - this.updateCallMembershipRunning = true; - try { - // if anything triggers an update while the update is running, do another update afterwards - do { - this.needCallMembershipUpdate = false; - await this.updateCallMembershipEvent(); - } while (this.needCallMembershipUpdate); - } finally { - this.updateCallMembershipRunning = false; - } - }; - - private async updateCallMembershipEvent(): Promise { - if (this.memberEventTimeout) { - clearTimeout(this.memberEventTimeout); - this.memberEventTimeout = undefined; - } - - const roomState = this.room.getLiveTimeline().getState(EventTimeline.FORWARDS); - if (!roomState) throw new Error("Couldn't get room state for room " + this.room.roomId); - - const localUserId = this.client.getUserId(); - const localDeviceId = this.client.getDeviceId(); - if (!localUserId || !localDeviceId) throw new Error("User ID or device ID was null!"); - - let newContent: {} | SessionMembershipData = {}; - // TODO: add back expiary logic to non-legacy events - // previously we checked here if the event is timed out and scheduled a check if not. - // maybe there is a better way. - newContent = this.makeNewMembership(localDeviceId); - - try { - if (this.isJoined()) { - const stateKey = this.makeMembershipStateKey(localUserId, localDeviceId); - const prepareDelayedDisconnection = async (): Promise => { - try { - const res = await resendIfRateLimited(() => - this.client._unstable_sendDelayedStateEvent( - this.room.roomId, - { - delay: this.membershipServerSideExpiryTimeout, - }, - EventType.GroupCallMemberPrefix, - {}, // leave event - stateKey, - ), - ); - this.disconnectDelayId = res.delay_id; - } catch (e) { - if ( - e instanceof MatrixError && - e.errcode === "M_UNKNOWN" && - e.data["org.matrix.msc4140.errcode"] === "M_MAX_DELAY_EXCEEDED" - ) { - const maxDelayAllowed = e.data["org.matrix.msc4140.max_delay"]; - if ( - typeof maxDelayAllowed === "number" && - this.membershipServerSideExpiryTimeout > maxDelayAllowed - ) { - this.membershipServerSideExpiryTimeoutOverride = maxDelayAllowed; - return prepareDelayedDisconnection(); - } - } - logger.error("Failed to prepare delayed disconnection event:", e); - } - }; - await prepareDelayedDisconnection(); - // Send join event _after_ preparing the delayed disconnection event - await resendIfRateLimited(() => - this.client.sendStateEvent(this.room.roomId, EventType.GroupCallMemberPrefix, newContent, stateKey), - ); - // If sending state cancels your own delayed state, prepare another delayed state - // TODO: Remove this once MSC4140 is stable & doesn't cancel own delayed state - if (this.disconnectDelayId !== undefined) { - try { - const knownDisconnectDelayId = this.disconnectDelayId; - await resendIfRateLimited(() => - this.client._unstable_updateDelayedEvent( - knownDisconnectDelayId, - UpdateDelayedEventAction.Restart, - ), - ); - } catch (e) { - if (e instanceof MatrixError && e.errcode === "M_NOT_FOUND") { - // If we get a M_NOT_FOUND we prepare a new delayed event. - // In other error cases we do not want to prepare anything since we do not have the guarantee, that the - // future is not still running. - logger.warn("Failed to update delayed disconnection event, prepare it again:", e); - this.disconnectDelayId = undefined; - await prepareDelayedDisconnection(); - } - } - } - if (this.disconnectDelayId !== undefined) { - this.scheduleDelayDisconnection(); - } - } else { - // Not joined - let sentDelayedDisconnect = false; - if (this.disconnectDelayId !== undefined) { - try { - const knownDisconnectDelayId = this.disconnectDelayId; - await resendIfRateLimited(() => - this.client._unstable_updateDelayedEvent( - knownDisconnectDelayId, - UpdateDelayedEventAction.Send, - ), - ); - sentDelayedDisconnect = true; - } catch (e) { - logger.error("Failed to send our delayed disconnection event:", e); - } - this.disconnectDelayId = undefined; - } - if (!sentDelayedDisconnect) { - await resendIfRateLimited(() => - this.client.sendStateEvent( - this.room.roomId, - EventType.GroupCallMemberPrefix, - {}, - this.makeMembershipStateKey(localUserId, localDeviceId), - ), - ); - } - } - logger.info("Sent updated call member event."); - } catch (e) { - const resendDelay = this.callMemberEventRetryDelayMinimum + Math.random() * this.callMemberEventRetryJitter; - logger.warn(`Failed to send call member event (retrying in ${resendDelay}): ${e}`); - await sleep(resendDelay); - await this.triggerCallMembershipEventUpdate(); - } - } - - private scheduleDelayDisconnection(): void { - this.memberEventTimeout = setTimeout(this.delayDisconnection, this.membershipKeepAlivePeriod); - } - - private readonly delayDisconnection = async (): Promise => { - try { - const knownDisconnectDelayId = this.disconnectDelayId!; - await resendIfRateLimited(() => - this.client._unstable_updateDelayedEvent(knownDisconnectDelayId, UpdateDelayedEventAction.Restart), - ); - this.scheduleDelayDisconnection(); - } catch (e) { - logger.error("Failed to delay our disconnection event:", e); - } - }; - - private makeMembershipStateKey(localUserId: string, localDeviceId: string): string { - const stateKey = `${localUserId}_${localDeviceId}`; - if (/^org\.matrix\.msc(3757|3779)\b/.exec(this.room.getVersion())) { - return stateKey; - } else { - return `_${stateKey}`; - } - } - private onRotateKeyTimeout = (): void => { if (!this.manageMediaKeys) return; @@ -1052,31 +863,3 @@ export class MatrixRTCSession extends TypedEventEmitter(func: () => Promise, numRetriesAllowed: number = 1): Promise { - // eslint-disable-next-line no-constant-condition - while (true) { - try { - return await func(); - } catch (e) { - if (numRetriesAllowed > 0 && e instanceof HTTPError && e.isRateLimitError()) { - numRetriesAllowed--; - let resendDelay: number; - const defaultMs = 5000; - try { - resendDelay = e.getRetryAfterMs() ?? defaultMs; - logger.info(`Rate limited by server, retrying in ${resendDelay}ms`); - } catch (e) { - logger.warn( - `Error while retrieving a rate-limit retry delay, retrying after default delay of ${defaultMs}`, - e, - ); - resendDelay = defaultMs; - } - await sleep(resendDelay); - } else { - throw e; - } - } - } -}