Skip to content

Commit 2d381ad

Browse files
ajburarichvdh
andauthored
Report backup key import progress on start and improve types (#4711)
* report key import progress on start and improve types * fix lint * add documentation for exported types * link `ImportRoomKeyProgressData` type in `ImportRoomKeyStage` Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * link `ImportRoomKeyFetchProgress` in fetch stage doc Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * link `ImportRoomKeyLoadProgress` in load_keys stage Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * link `ImportRoomKeyProgressData` in `ImportRoomKeyFetchProgress` type doc Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * link `ImportRoomKeyProgressData` in `ImportRoomKeyLoadProgress` type doc Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * link `ImportRoomKeyStage.Fetch` Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * convert `ImportRoomKeyStage.LoadKeys` to link in `ImportRoomKeyLoadProgress` stage doc Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * remove whitespace * improve `ImportRoomKeyStage.Fetch` stage doc Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * improve `ImportRoomKeyStage.LoadKeys ` stage doc Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --------- Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
1 parent 266475c commit 2d381ad

File tree

4 files changed

+86
-18
lines changed

4 files changed

+86
-18
lines changed

spec/unit/rust-crypto/rust-crypto.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,10 @@ describe("RustCrypto", () => {
500500
const someRoomKeys = testData.MEGOLM_SESSION_DATA_ARRAY;
501501
let importTotal = 0;
502502
const opt: ImportRoomKeysOpts = {
503-
progressCallback: (stage) => {
504-
importTotal = stage.total ?? 0;
503+
progressCallback: (progress) => {
504+
if (progress.stage === "load_keys") {
505+
importTotal = progress.total;
506+
}
505507
},
506508
};
507509
await rustCrypto.importRoomKeys(someRoomKeys, opt);
@@ -523,8 +525,10 @@ describe("RustCrypto", () => {
523525
const someRoomKeys = testData.MEGOLM_SESSION_DATA_ARRAY;
524526
let importTotal = 0;
525527
const opt: ImportRoomKeysOpts = {
526-
progressCallback: (stage) => {
527-
importTotal = stage.total ?? 0;
528+
progressCallback: (progress) => {
529+
if (progress.stage === "load_keys") {
530+
importTotal = progress.total;
531+
}
528532
},
529533
};
530534
await rustCrypto.importRoomKeysAsJson(JSON.stringify(someRoomKeys), opt);

src/crypto-api/index.ts

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,18 +1000,72 @@ export class DeviceVerificationStatus {
10001000
}
10011001
}
10021002

1003+
/**
1004+
* Enum representing the different stages of importing room keys.
1005+
*
1006+
* This is the type of the `stage` property of {@link ImportRoomKeyProgressData}.
1007+
*/
1008+
export enum ImportRoomKeyStage {
1009+
/**
1010+
* The stage where room keys are being fetched.
1011+
*
1012+
* @see {@link ImportRoomKeyFetchProgress}.
1013+
*/
1014+
Fetch = "fetch",
1015+
/**
1016+
* The stage where room keys are being loaded.
1017+
*
1018+
* @see {@link ImportRoomKeyLoadProgress}.
1019+
*/
1020+
LoadKeys = "load_keys",
1021+
}
1022+
1023+
/**
1024+
* Type representing the progress during the 'fetch' stage of the room key import process.
1025+
*
1026+
* @see {@link ImportRoomKeyProgressData}.
1027+
*/
1028+
export type ImportRoomKeyFetchProgress = {
1029+
/**
1030+
* The current stage of the import process.
1031+
*/
1032+
stage: ImportRoomKeyStage.Fetch;
1033+
};
1034+
1035+
/**
1036+
* Type representing the progress during the 'load_keys' stage of the room key import process.
1037+
*
1038+
* @see {@link ImportRoomKeyProgressData}.
1039+
*/
1040+
export type ImportRoomKeyLoadProgress = {
1041+
/**
1042+
* The current stage of the import process.
1043+
*/
1044+
stage: ImportRoomKeyStage.LoadKeys;
1045+
1046+
/**
1047+
* The number of successfully loaded room keys so far.
1048+
*/
1049+
successes: number;
1050+
1051+
/**
1052+
* The number of room keys that failed to load so far.
1053+
*/
1054+
failures: number;
1055+
1056+
/**
1057+
* The total number of room keys being loaded.
1058+
*/
1059+
total: number;
1060+
};
1061+
10031062
/**
10041063
* Room key import progress report.
10051064
* Used when calling {@link CryptoApi#importRoomKeys},
10061065
* {@link CryptoApi#importRoomKeysAsJson} or {@link CryptoApi#restoreKeyBackup} as the parameter of
10071066
* the progressCallback. Used to display feedback.
10081067
*/
1009-
export interface ImportRoomKeyProgressData {
1010-
stage: string; // TODO: Enum
1011-
successes?: number;
1012-
failures?: number;
1013-
total?: number;
1014-
}
1068+
export type ImportRoomKeyProgressData = ImportRoomKeyFetchProgress | ImportRoomKeyLoadProgress;
10151069

10161070
/**
10171071
* Options object for {@link CryptoApi#importRoomKeys} and

src/rust-crypto/backup.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ import { encodeUri, logDuration } from "../utils.ts";
3535
import { type OutgoingRequestProcessor } from "./OutgoingRequestProcessor.ts";
3636
import { sleep } from "../utils.ts";
3737
import { type BackupDecryptor } from "../common-crypto/CryptoBackend.ts";
38-
import { type ImportRoomKeyProgressData, type ImportRoomKeysOpts, CryptoEvent } from "../crypto-api/index.ts";
38+
import {
39+
type ImportRoomKeyProgressData,
40+
type ImportRoomKeysOpts,
41+
CryptoEvent,
42+
ImportRoomKeyStage,
43+
} from "../crypto-api/index.ts";
3944
import { type AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
4045
import { type IMegolmSessionData } from "../@types/crypto.ts";
4146

@@ -235,7 +240,7 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
235240
const importOpt: ImportRoomKeyProgressData = {
236241
total: Number(total),
237242
successes: Number(progress),
238-
stage: "load_keys",
243+
stage: ImportRoomKeyStage.LoadKeys,
239244
failures: 0,
240245
};
241246
opts?.progressCallback?.(importOpt);
@@ -264,7 +269,7 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
264269
const importOpt: ImportRoomKeyProgressData = {
265270
total: Number(total),
266271
successes: Number(progress),
267-
stage: "load_keys",
272+
stage: ImportRoomKeyStage.LoadKeys,
268273
failures: Number(failures),
269274
};
270275
opts?.progressCallback?.(importOpt);
@@ -619,9 +624,6 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
619624
opts?: KeyBackupRestoreOpts,
620625
): Promise<KeyBackupRestoreResult> {
621626
const keyBackup = await this.downloadKeyBackup(backupVersion);
622-
opts?.progressCallback?.({
623-
stage: "load_keys",
624-
});
625627

626628
return this.importKeyBackup(keyBackup, backupVersion, backupDecryptor, opts);
627629
}
@@ -672,6 +674,13 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
672674
let totalImported = 0;
673675
let totalFailures = 0;
674676

677+
opts?.progressCallback?.({
678+
total: totalKeyCount,
679+
successes: totalImported,
680+
stage: ImportRoomKeyStage.LoadKeys,
681+
failures: totalFailures,
682+
});
683+
675684
/**
676685
* This method is called when we have enough chunks to decrypt.
677686
* It will decrypt the chunks and try to import the room keys.
@@ -704,7 +713,7 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
704713
opts?.progressCallback?.({
705714
total: totalKeyCount,
706715
successes: totalImported,
707-
stage: "load_keys",
716+
stage: ImportRoomKeyStage.LoadKeys,
708717
failures: totalFailures,
709718
});
710719
};

src/rust-crypto/rust-crypto.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import {
6767
type KeyBackupRestoreOpts,
6868
type KeyBackupRestoreResult,
6969
type StartDehydrationOpts,
70+
ImportRoomKeyStage,
7071
} from "../crypto-api/index.ts";
7172
import { deviceKeysToDeviceMap, rustDeviceToJsDevice } from "./device-converter.ts";
7273
import { type IDownloadKeyResult, type IQueryKeysRequest } from "../client.ts";
@@ -1340,7 +1341,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
13401341

13411342
try {
13421343
opts?.progressCallback?.({
1343-
stage: "fetch",
1344+
stage: ImportRoomKeyStage.Fetch,
13441345
});
13451346

13461347
return await this.backupManager.restoreKeyBackup(backupVersion, backupDecryptor, opts);

0 commit comments

Comments
 (0)