2024-04-15 11:47:15 -04:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2024-04-15 11:47:15 -04:00
|
|
|
Copyright 2024 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2024-04-15 11:47:15 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type CryptoApi, type StartDehydrationOpts } from "matrix-js-sdk/src/crypto-api";
|
2024-04-15 11:47:15 -04:00
|
|
|
|
2025-01-31 13:29:59 -05:00
|
|
|
import type { MatrixClient } from "matrix-js-sdk/src/matrix";
|
2024-04-15 11:47:15 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if device dehydration is enabled.
|
|
|
|
|
*
|
|
|
|
|
* Note that this doesn't necessarily mean that device dehydration has been initialised
|
|
|
|
|
* (yet) on this client; rather, it means that the server supports it, the crypto backend
|
|
|
|
|
* supports it, and the application configuration suggests that it *should* be
|
|
|
|
|
* initialised on this device.
|
|
|
|
|
*
|
|
|
|
|
* Dehydration can currently only be enabled by setting a flag in the .well-known file.
|
|
|
|
|
*/
|
2025-01-31 13:29:59 -05:00
|
|
|
async function deviceDehydrationEnabled(client: MatrixClient, crypto: CryptoApi | undefined): Promise<boolean> {
|
2024-04-15 11:47:15 -04:00
|
|
|
if (!crypto) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!(await crypto.isDehydrationSupported())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-01-31 13:29:59 -05:00
|
|
|
const wellknown = await client.waitForClientWellKnown();
|
2024-04-15 11:47:15 -04:00
|
|
|
return !!wellknown?.["org.matrix.msc3814"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If dehydration is enabled (i.e., it is supported by the server and enabled in
|
|
|
|
|
* the configuration), rehydrate a device (if available) and create
|
|
|
|
|
* a new dehydrated device.
|
|
|
|
|
*
|
2025-02-14 10:59:02 +00:00
|
|
|
* @param client - MatrixClient to use for the operation
|
|
|
|
|
* @param opts - options for the startDehydration operation, if one is performed.
|
2024-04-15 11:47:15 -04:00
|
|
|
*/
|
2025-02-14 10:59:02 +00:00
|
|
|
export async function initialiseDehydrationIfEnabled(
|
|
|
|
|
client: MatrixClient,
|
|
|
|
|
opts: StartDehydrationOpts = {},
|
|
|
|
|
): Promise<void> {
|
2025-01-31 13:29:59 -05:00
|
|
|
const crypto = client.getCrypto();
|
|
|
|
|
if (await deviceDehydrationEnabled(client, crypto)) {
|
2024-04-15 11:47:15 -04:00
|
|
|
logger.log("Device dehydration enabled");
|
2025-01-31 13:29:59 -05:00
|
|
|
await crypto!.startDehydration(opts);
|
2024-04-15 11:47:15 -04:00
|
|
|
}
|
|
|
|
|
}
|