1
0
mirror of https://github.com/element-hq/element-web.git synced 2025-08-08 03:42:14 +03:00

Cache the key backup status whether enabled or not (#29886)

This commit is contained in:
Andy Balaam
2025-05-07 12:24:43 +01:00
committed by GitHub
parent 36d25da288
commit 6063209fff
2 changed files with 31 additions and 11 deletions

View File

@@ -132,7 +132,7 @@ export default class DeviceListener {
this.dismissedThisDeviceToast = false; this.dismissedThisDeviceToast = false;
this.keyBackupInfo = null; this.keyBackupInfo = null;
this.keyBackupFetchedAt = null; this.keyBackupFetchedAt = null;
this.keyBackupStatusChecked = false; this.cachedKeyBackupStatus = undefined;
this.ourDeviceIdsAtStart = null; this.ourDeviceIdsAtStart = null;
this.displayingToastsForDeviceIds = new Set(); this.displayingToastsForDeviceIds = new Set();
this.client = undefined; this.client = undefined;
@@ -512,18 +512,36 @@ export default class DeviceListener {
* trigger an auto-rageshake). * trigger an auto-rageshake).
*/ */
private checkKeyBackupStatus = async (): Promise<void> => { private checkKeyBackupStatus = async (): Promise<void> => {
if (this.keyBackupStatusChecked || !this.client) { if (!(await this.getKeyBackupStatus())) {
return;
}
const activeKeyBackupVersion = await this.client.getCrypto()?.getActiveSessionBackupVersion();
// if key backup is enabled, no need to check this ever again (XXX: why only when it is enabled?)
this.keyBackupStatusChecked = !!activeKeyBackupVersion;
if (!activeKeyBackupVersion) {
dis.dispatch({ action: Action.ReportKeyBackupNotEnabled }); dis.dispatch({ action: Action.ReportKeyBackupNotEnabled });
} }
}; };
private keyBackupStatusChecked = false;
/**
* Is key backup enabled? Use a cached answer if we have one.
*/
private getKeyBackupStatus = async (): Promise<boolean> => {
if (!this.client) {
// To preserve existing behaviour, if there is no client, we
// pretend key storage is on.
//
// Someone looking to improve this code could try throwing an error
// here since we don't expect client to be undefined.
return true;
}
// If we've already cached the answer, return it.
if (this.cachedKeyBackupStatus !== undefined) {
return this.cachedKeyBackupStatus;
}
// Fetch the answer and cache it
const activeKeyBackupVersion = await this.client.getCrypto()?.getActiveSessionBackupVersion();
this.cachedKeyBackupStatus = !!activeKeyBackupVersion;
return this.cachedKeyBackupStatus;
};
private cachedKeyBackupStatus: boolean | undefined = undefined;
private onRecordClientInformationSettingChange: CallbackFn = ( private onRecordClientInformationSettingChange: CallbackFn = (
_originalSettingName, _originalSettingName,

View File

@@ -445,7 +445,9 @@ describe("DeviceListener", () => {
it("dispatches keybackup event when key backup is not enabled", async () => { it("dispatches keybackup event when key backup is not enabled", async () => {
mockCrypto.getActiveSessionBackupVersion.mockResolvedValue(null); mockCrypto.getActiveSessionBackupVersion.mockResolvedValue(null);
await createAndStart(); await createAndStart();
expect(mockDispatcher.dispatch).toHaveBeenCalledWith({ action: Action.ReportKeyBackupNotEnabled }); expect(mockDispatcher.dispatch).toHaveBeenCalledWith({
action: Action.ReportKeyBackupNotEnabled,
});
}); });
it("does not check key backup status again after check is complete", async () => { it("does not check key backup status again after check is complete", async () => {