Skip to content

Commit

Permalink
Fix more function typings relating to key backup (#2086)
Browse files Browse the repository at this point in the history
* Fix more function typings relating to key backup

* Use function overloads to specify allowed params
  • Loading branch information
hughns authored Dec 23, 2021
1 parent ab19480 commit f30be87
Showing 1 changed file with 106 additions and 3 deletions.
109 changes: 106 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2658,7 +2658,14 @@ export class MatrixClient extends EventEmitter {
);
}

private makeKeyBackupPath(roomId: string, sessionId: string, version: string): IKeyBackupPath {
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,
version: string,
): IKeyBackupPath {
let path;
if (sessionId !== undefined) {
path = utils.encodeUri("/room_keys/keys/$roomId/$sessionId", {
Expand All @@ -2685,7 +2692,15 @@ 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<void> {
public sendKeyBackup(roomId: undefined, sessionId: undefined, version: string, data: IKeyBackup): Promise<void>;
public sendKeyBackup(roomId: string, sessionId: undefined, version: string, data: IKeyBackup): Promise<void>;
public sendKeyBackup(roomId: string, sessionId: string, version: string, data: IKeyBackup): Promise<void>;
public sendKeyBackup(
roomId: string,
sessionId: string | undefined,
version: string | undefined,
data: IKeyBackup,
): Promise<void> {
if (!this.crypto) {
throw new Error("End-to-end encryption disabled");
}
Expand Down Expand Up @@ -2771,12 +2786,33 @@ export class MatrixClient extends EventEmitter {
* @return {Promise<object>} Status of restoration with `total` and `imported`
* key counts.
*/
public async restoreKeyBackupWithPassword(
password: string,
targetRoomId: undefined,
targetSessionId: undefined,
backupInfo: IKeyBackupInfo,
opts: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
public async restoreKeyBackupWithPassword(
password: string,
targetRoomId: string,
targetSessionId: undefined,
backupInfo: IKeyBackupInfo,
opts: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
public async restoreKeyBackupWithPassword(
password: string,
targetRoomId: string,
targetSessionId: string,
backupInfo: IKeyBackupInfo,
opts: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
public async restoreKeyBackupWithPassword(
password: string,
targetRoomId: string | undefined,
targetSessionId: string | undefined,
backupInfo: IKeyBackupInfo,
opts: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult> {
const privKey = await keyFromAuthData(backupInfo.auth_data, password);
return this.restoreKeyBackup(
Expand Down Expand Up @@ -2833,22 +2869,61 @@ export class MatrixClient extends EventEmitter {
* @return {Promise<object>} Status of restoration with `total` and `imported`
* key counts.
*/
public restoreKeyBackupWithRecoveryKey(
recoveryKey: string,
targetRoomId: undefined,
targetSessionId: undefined,
backupInfo: IKeyBackupInfo,
opts: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
public restoreKeyBackupWithRecoveryKey(
recoveryKey: string,
targetRoomId: string,
targetSessionId: undefined,
backupInfo: IKeyBackupInfo,
opts: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
public restoreKeyBackupWithRecoveryKey(
recoveryKey: string,
targetRoomId: string,
targetSessionId: string,
backupInfo: IKeyBackupInfo,
opts: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
public restoreKeyBackupWithRecoveryKey(
recoveryKey: string,
targetRoomId: string | undefined,
targetSessionId: string | undefined,
backupInfo: IKeyBackupInfo,
opts: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult> {
const privKey = decodeRecoveryKey(recoveryKey);
return this.restoreKeyBackup(privKey, targetRoomId, targetSessionId, backupInfo, opts);
}

public async restoreKeyBackupWithCache(
targetRoomId: undefined,
targetSessionId: undefined,
backupInfo: IKeyBackupInfo,
opts?: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
public async restoreKeyBackupWithCache(
targetRoomId: string,
targetSessionId: undefined,
backupInfo: IKeyBackupInfo,
opts?: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
public async restoreKeyBackupWithCache(
targetRoomId: string,
targetSessionId: string,
backupInfo: IKeyBackupInfo,
opts?: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
public async restoreKeyBackupWithCache(
targetRoomId: string | undefined,
targetSessionId: string | undefined,
backupInfo: IKeyBackupInfo,
opts?: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult> {
const privKey = await this.crypto.getSessionBackupPrivateKey();
if (!privKey) {
Expand All @@ -2857,12 +2932,33 @@ export class MatrixClient extends EventEmitter {
return this.restoreKeyBackup(privKey, targetRoomId, targetSessionId, backupInfo, opts);
}

private async restoreKeyBackup(
privKey: ArrayLike<number>,
targetRoomId: undefined,
targetSessionId: undefined,
backupInfo: IKeyBackupInfo,
opts?: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
private async restoreKeyBackup(
privKey: ArrayLike<number>,
targetRoomId: string,
targetSessionId: undefined,
backupInfo: IKeyBackupInfo,
opts?: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
private async restoreKeyBackup(
privKey: ArrayLike<number>,
targetRoomId: string,
targetSessionId: string,
backupInfo: IKeyBackupInfo,
opts?: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult>;
private async restoreKeyBackup(
privKey: ArrayLike<number>,
targetRoomId: string | undefined,
targetSessionId: string | undefined,
backupInfo: IKeyBackupInfo,
opts?: IKeyBackupRestoreOpts,
): Promise<IKeyBackupRestoreResult> {
const cacheCompleteCallback = opts?.cacheCompleteCallback;
const progressCallback = opts?.progressCallback;
Expand Down Expand Up @@ -2953,7 +3049,14 @@ export class MatrixClient extends EventEmitter {
return { total: totalKeyCount, imported: keys.length };
}

public deleteKeysFromBackup(roomId: string, sessionId: string, version: string): Promise<void> {
public deleteKeysFromBackup(roomId: undefined, sessionId: undefined, version: string): Promise<void>;
public deleteKeysFromBackup(roomId: string, sessionId: undefined, version: string): Promise<void>;
public deleteKeysFromBackup(roomId: string, sessionId: string, version: string): Promise<void>;
public deleteKeysFromBackup(
roomId: string | undefined,
sessionId: string | undefined,
version: string,
): Promise<void> {
if (!this.crypto) {
throw new Error("End-to-end encryption disabled");
}
Expand Down

0 comments on commit f30be87

Please sign in to comment.