Schedule dehydration on reload if the dehydration key is already cached locally (#29021)

* Schedule dehydration on reload

* fix test and use the right function to check dehydration is enabled

* use dehydration helper function when scheduling dehydration on restart

* fix test by passing in client object
This commit is contained in:
Hubert Chathi
2025-01-31 13:29:59 -05:00
committed by GitHub
parent b64471e4f6
commit 4cba79ddcc
5 changed files with 48 additions and 9 deletions

View File

@@ -7,8 +7,9 @@ Please see LICENSE files in the repository root for full details.
*/
import { logger } from "matrix-js-sdk/src/logger";
import { CryptoApi } from "matrix-js-sdk/src/crypto-api";
import { CryptoApi, StartDehydrationOpts } from "matrix-js-sdk/src/crypto-api";
import type { MatrixClient } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from "../../MatrixClientPeg";
/**
@@ -21,14 +22,14 @@ import { MatrixClientPeg } from "../../MatrixClientPeg";
*
* Dehydration can currently only be enabled by setting a flag in the .well-known file.
*/
async function deviceDehydrationEnabled(crypto: CryptoApi | undefined): Promise<boolean> {
async function deviceDehydrationEnabled(client: MatrixClient, crypto: CryptoApi | undefined): Promise<boolean> {
if (!crypto) {
return false;
}
if (!(await crypto.isDehydrationSupported())) {
return false;
}
const wellknown = await MatrixClientPeg.safeGet().waitForClientWellKnown();
const wellknown = await client.waitForClientWellKnown();
return !!wellknown?.["org.matrix.msc3814"];
}
@@ -40,10 +41,11 @@ async function deviceDehydrationEnabled(crypto: CryptoApi | undefined): Promise<
* @param createNewKey: force a new dehydration key to be created, even if one
* already exists. This is used when we reset secret storage.
*/
export async function initialiseDehydration(createNewKey: boolean = false): Promise<void> {
const crypto = MatrixClientPeg.safeGet().getCrypto();
if (await deviceDehydrationEnabled(crypto)) {
export async function initialiseDehydration(opts: StartDehydrationOpts = {}, client?: MatrixClient): Promise<void> {
client = client || MatrixClientPeg.safeGet();
const crypto = client.getCrypto();
if (await deviceDehydrationEnabled(client, crypto)) {
logger.log("Device dehydration enabled");
await crypto!.startDehydration(createNewKey);
await crypto!.startDehydration(opts);
}
}