From a13ed24375071388bf63eedc490ad697abf0e84b Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Wed, 22 Dec 2021 11:25:50 +0000 Subject: [PATCH 1/2] Fix more function typings relating to key backup --- src/client.ts | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/client.ts b/src/client.ts index 926f21c9797..0b06bb09d91 100644 --- a/src/client.ts +++ b/src/client.ts @@ -2658,7 +2658,11 @@ export class MatrixClient extends EventEmitter { ); } - private makeKeyBackupPath(roomId: string, sessionId: string, version: string): IKeyBackupPath { + private makeKeyBackupPath( + roomId: string | undefined, + sessionId: string | undefined, + version: string, + ): IKeyBackupPath { let path; if (sessionId !== undefined) { path = utils.encodeUri("/room_keys/keys/$roomId/$sessionId", { @@ -2685,7 +2689,12 @@ export class MatrixClient extends EventEmitter { * @return {Promise} a promise that will resolve when the keys * are uploaded */ - public sendKeyBackup(roomId: string, sessionId: string, version: string, data: IKeyBackup): Promise { + public sendKeyBackup( + roomId: string | undefined, + sessionId: string | undefined, + version: string, + data: IKeyBackup, + ): Promise { if (!this.crypto) { throw new Error("End-to-end encryption disabled"); } @@ -2773,8 +2782,8 @@ export class MatrixClient extends EventEmitter { */ public async restoreKeyBackupWithPassword( password: string, - targetRoomId: string, - targetSessionId: string, + targetRoomId: string | undefined, + targetSessionId: string | undefined, backupInfo: IKeyBackupInfo, opts: IKeyBackupRestoreOpts, ): Promise { @@ -2835,8 +2844,8 @@ export class MatrixClient extends EventEmitter { */ public restoreKeyBackupWithRecoveryKey( recoveryKey: string, - targetRoomId: string, - targetSessionId: string, + targetRoomId: string | undefined, + targetSessionId: string | undefined, backupInfo: IKeyBackupInfo, opts: IKeyBackupRestoreOpts, ): Promise { @@ -2845,8 +2854,8 @@ export class MatrixClient extends EventEmitter { } public async restoreKeyBackupWithCache( - targetRoomId: string, - targetSessionId: string, + targetRoomId: string | undefined, + targetSessionId: string | undefined, backupInfo: IKeyBackupInfo, opts?: IKeyBackupRestoreOpts, ): Promise { @@ -2859,8 +2868,8 @@ export class MatrixClient extends EventEmitter { private async restoreKeyBackup( privKey: ArrayLike, - targetRoomId: string, - targetSessionId: string, + targetRoomId: string | undefined, + targetSessionId: string | undefined, backupInfo: IKeyBackupInfo, opts?: IKeyBackupRestoreOpts, ): Promise { @@ -2953,7 +2962,11 @@ export class MatrixClient extends EventEmitter { return { total: totalKeyCount, imported: keys.length }; } - public deleteKeysFromBackup(roomId: string, sessionId: string, version: string): Promise { + public deleteKeysFromBackup( + roomId: string | undefined, + sessionId: string | undefined, + version: string, + ): Promise { if (!this.crypto) { throw new Error("End-to-end encryption disabled"); } From 4213fe9cdc1be5e982bc7c778f454e9a2bb6bcc5 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Wed, 22 Dec 2021 16:58:38 +0000 Subject: [PATCH 2/2] Use function overloads to specify allowed params --- src/client.ts | 94 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 0b06bb09d91..aa5fc530092 100644 --- a/src/client.ts +++ b/src/client.ts @@ -2658,6 +2658,9 @@ export class MatrixClient extends EventEmitter { ); } + private makeKeyBackupPath(roomId: undefined, sessionId: undefined, version: string): IKeyBackupPath; + private makeKeyBackupPath(roomId: string, sessionId: undefined, version: string): IKeyBackupPath; + private makeKeyBackupPath(roomId: string, sessionId: string, version: string): IKeyBackupPath; private makeKeyBackupPath( roomId: string | undefined, sessionId: string | undefined, @@ -2689,10 +2692,13 @@ export class MatrixClient extends EventEmitter { * @return {Promise} a promise that will resolve when the keys * are uploaded */ + public sendKeyBackup(roomId: undefined, sessionId: undefined, version: string, data: IKeyBackup): Promise; + public sendKeyBackup(roomId: string, sessionId: undefined, version: string, data: IKeyBackup): Promise; + public sendKeyBackup(roomId: string, sessionId: string, version: string, data: IKeyBackup): Promise; public sendKeyBackup( - roomId: string | undefined, + roomId: string, sessionId: string | undefined, - version: string, + version: string | undefined, data: IKeyBackup, ): Promise { if (!this.crypto) { @@ -2780,6 +2786,27 @@ export class MatrixClient extends EventEmitter { * @return {Promise} Status of restoration with `total` and `imported` * key counts. */ + public async restoreKeyBackupWithPassword( + password: string, + targetRoomId: undefined, + targetSessionId: undefined, + backupInfo: IKeyBackupInfo, + opts: IKeyBackupRestoreOpts, + ): Promise; + public async restoreKeyBackupWithPassword( + password: string, + targetRoomId: string, + targetSessionId: undefined, + backupInfo: IKeyBackupInfo, + opts: IKeyBackupRestoreOpts, + ): Promise; + public async restoreKeyBackupWithPassword( + password: string, + targetRoomId: string, + targetSessionId: string, + backupInfo: IKeyBackupInfo, + opts: IKeyBackupRestoreOpts, + ): Promise; public async restoreKeyBackupWithPassword( password: string, targetRoomId: string | undefined, @@ -2842,6 +2869,27 @@ export class MatrixClient extends EventEmitter { * @return {Promise} Status of restoration with `total` and `imported` * key counts. */ + public restoreKeyBackupWithRecoveryKey( + recoveryKey: string, + targetRoomId: undefined, + targetSessionId: undefined, + backupInfo: IKeyBackupInfo, + opts: IKeyBackupRestoreOpts, + ): Promise; + public restoreKeyBackupWithRecoveryKey( + recoveryKey: string, + targetRoomId: string, + targetSessionId: undefined, + backupInfo: IKeyBackupInfo, + opts: IKeyBackupRestoreOpts, + ): Promise; + public restoreKeyBackupWithRecoveryKey( + recoveryKey: string, + targetRoomId: string, + targetSessionId: string, + backupInfo: IKeyBackupInfo, + opts: IKeyBackupRestoreOpts, + ): Promise; public restoreKeyBackupWithRecoveryKey( recoveryKey: string, targetRoomId: string | undefined, @@ -2853,6 +2901,24 @@ export class MatrixClient extends EventEmitter { return this.restoreKeyBackup(privKey, targetRoomId, targetSessionId, backupInfo, opts); } + public async restoreKeyBackupWithCache( + targetRoomId: undefined, + targetSessionId: undefined, + backupInfo: IKeyBackupInfo, + opts?: IKeyBackupRestoreOpts, + ): Promise; + public async restoreKeyBackupWithCache( + targetRoomId: string, + targetSessionId: undefined, + backupInfo: IKeyBackupInfo, + opts?: IKeyBackupRestoreOpts, + ): Promise; + public async restoreKeyBackupWithCache( + targetRoomId: string, + targetSessionId: string, + backupInfo: IKeyBackupInfo, + opts?: IKeyBackupRestoreOpts, + ): Promise; public async restoreKeyBackupWithCache( targetRoomId: string | undefined, targetSessionId: string | undefined, @@ -2866,6 +2932,27 @@ export class MatrixClient extends EventEmitter { return this.restoreKeyBackup(privKey, targetRoomId, targetSessionId, backupInfo, opts); } + private async restoreKeyBackup( + privKey: ArrayLike, + targetRoomId: undefined, + targetSessionId: undefined, + backupInfo: IKeyBackupInfo, + opts?: IKeyBackupRestoreOpts, + ): Promise; + private async restoreKeyBackup( + privKey: ArrayLike, + targetRoomId: string, + targetSessionId: undefined, + backupInfo: IKeyBackupInfo, + opts?: IKeyBackupRestoreOpts, + ): Promise; + private async restoreKeyBackup( + privKey: ArrayLike, + targetRoomId: string, + targetSessionId: string, + backupInfo: IKeyBackupInfo, + opts?: IKeyBackupRestoreOpts, + ): Promise; private async restoreKeyBackup( privKey: ArrayLike, targetRoomId: string | undefined, @@ -2962,6 +3049,9 @@ export class MatrixClient extends EventEmitter { return { total: totalKeyCount, imported: keys.length }; } + public deleteKeysFromBackup(roomId: undefined, sessionId: undefined, version: string): Promise; + public deleteKeysFromBackup(roomId: string, sessionId: undefined, version: string): Promise; + public deleteKeysFromBackup(roomId: string, sessionId: string, version: string): Promise; public deleteKeysFromBackup( roomId: string | undefined, sessionId: string | undefined,