Matrix: consolidate decrypt retry wiring and harden sdk imports

This commit is contained in:
gustavo
2026-02-08 15:39:53 -05:00
parent afd46ce9b8
commit ba8e08186d
3 changed files with 35 additions and 19 deletions

View File

@@ -7,8 +7,8 @@ import {
type MatrixClient as MatrixJsClient,
type MatrixEvent,
} from "matrix-js-sdk";
import { CryptoEvent } from "matrix-js-sdk/src/crypto-api/CryptoEvent.ts";
import { VerificationMethod } from "matrix-js-sdk/src/types.ts";
import { CryptoEvent } from "matrix-js-sdk/lib/crypto-api/CryptoEvent.js";
import { VerificationMethod } from "matrix-js-sdk/lib/types.js";
import { EventEmitter } from "node:events";
import fs from "node:fs";
import path from "node:path";
@@ -438,21 +438,7 @@ export class MatrixClient {
}
});
const triggerDecryptRetry = (reason: string): void => {
this.decryptBridge.retryPendingNow(reason);
};
crypto.on(CryptoEvent.KeyBackupDecryptionKeyCached, () => {
triggerDecryptRetry("crypto.keyBackupDecryptionKeyCached");
});
crypto.on(CryptoEvent.RehydrationCompleted, () => {
triggerDecryptRetry("dehydration.RehydrationCompleted");
});
crypto.on(CryptoEvent.DevicesUpdated, () => {
triggerDecryptRetry("crypto.devicesUpdated");
});
crypto.on(CryptoEvent.KeysChanged, () => {
triggerDecryptRetry("crossSigning.keysChanged");
});
this.decryptBridge.bindCryptoRetrySignals(crypto);
LogService.info("MatrixClientLite", "Verification request handler registered");
}

View File

@@ -1,4 +1,5 @@
import { MatrixEventEvent, type MatrixEvent } from "matrix-js-sdk";
import { CryptoEvent } from "matrix-js-sdk/lib/crypto-api/CryptoEvent.js";
import { LogService, noop } from "./logger.js";
type MatrixDecryptIfNeededClient = {
@@ -23,6 +24,10 @@ type DecryptBridgeRawEvent = {
event_id: string;
};
type MatrixCryptoRetrySignalSource = {
on: (eventName: string, listener: (...args: unknown[]) => void) => void;
};
const MATRIX_DECRYPT_RETRY_BASE_DELAY_MS = 1_500;
const MATRIX_DECRYPT_RETRY_MAX_DELAY_MS = 30_000;
const MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS = 8;
@@ -46,6 +51,7 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
private readonly decryptedMessageDedupe = new Map<string, number>();
private readonly decryptRetries = new Map<string, MatrixDecryptRetryState>();
private readonly failedDecryptionsNotified = new Set<string>();
private cryptoRetrySignalsBound = false;
constructor(
private readonly deps: {
@@ -103,6 +109,30 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
}
}
bindCryptoRetrySignals(crypto: MatrixCryptoRetrySignalSource | undefined): void {
if (!crypto || this.cryptoRetrySignalsBound) {
return;
}
this.cryptoRetrySignalsBound = true;
const trigger = (reason: string): void => {
this.retryPendingNow(reason);
};
crypto.on(CryptoEvent.KeyBackupDecryptionKeyCached, () => {
trigger("crypto.keyBackupDecryptionKeyCached");
});
crypto.on(CryptoEvent.RehydrationCompleted, () => {
trigger("dehydration.RehydrationCompleted");
});
crypto.on(CryptoEvent.DevicesUpdated, () => {
trigger("crypto.devicesUpdated");
});
crypto.on(CryptoEvent.KeysChanged, () => {
trigger("crossSigning.keysChanged");
});
}
stop(): void {
for (const retryKey of this.decryptRetries.keys()) {
this.clearDecryptRetry(retryKey);

View File

@@ -2,8 +2,8 @@ import {
VerificationPhase,
VerificationRequestEvent,
VerifierEvent,
} from "matrix-js-sdk/src/crypto-api/verification.ts";
import { VerificationMethod } from "matrix-js-sdk/src/types.ts";
} from "matrix-js-sdk/lib/crypto-api/verification.js";
import { VerificationMethod } from "matrix-js-sdk/lib/types.js";
export type MatrixVerificationMethod = "sas" | "show-qr" | "scan-qr";