add sessionsNeedingBackup store

This commit is contained in:
Bruno Windels 2022-01-25 18:46:51 +01:00
parent a3e294bb60
commit 290aaad63a
4 changed files with 69 additions and 1 deletions

View file

@ -33,6 +33,7 @@ export enum StoreNames {
groupSessionDecryptions = "groupSessionDecryptions",
operations = "operations",
accountData = "accountData",
sessionsNeedingBackup = "sessionsNeedingBackup",
}
export const STORE_NAMES: Readonly<StoreNames[]> = Object.values(StoreNames);

View file

@ -36,6 +36,7 @@ import {OutboundGroupSessionStore} from "./stores/OutboundGroupSessionStore";
import {GroupSessionDecryptionStore} from "./stores/GroupSessionDecryptionStore";
import {OperationStore} from "./stores/OperationStore";
import {AccountDataStore} from "./stores/AccountDataStore";
import {SessionNeedingBackupStore} from "./stores/SessionNeedingBackupStore";
import type {ILogger, ILogItem} from "../../../logging/types";
export type IDBKey = IDBValidKey | IDBKeyRange;
@ -152,6 +153,10 @@ export class Transaction {
return this._store(StoreNames.inboundGroupSessions, idbStore => new InboundGroupSessionStore(idbStore));
}
get sessionsNeedingBackup(): SessionNeedingBackupStore {
return this._store(StoreNames.sessionsNeedingBackup, idbStore => new SessionNeedingBackupStore(idbStore));
}
get outboundGroupSessions(): OutboundGroupSessionStore {
return this._store(StoreNames.outboundGroupSessions, idbStore => new OutboundGroupSessionStore(idbStore));
}

View file

@ -31,7 +31,8 @@ export const schema: MigrationFunc[] = [
fixMissingRoomsInUserIdentities,
changeSSSSKeyPrefix,
backupAndRestoreE2EEAccountToLocalStorage,
clearAllStores
clearAllStores,
createSessionsNeedingBackup
];
// TODO: how to deal with git merge conflicts of this array?
@ -270,3 +271,8 @@ async function clearAllStores(db: IDBDatabase, txn: IDBTransaction) {
}
}
}
// v15 adds the sessionsNeedingBackup store, for session backup
function createSessionsNeedingBackup(db: IDBDatabase): void {
db.createObjectStore("sessionsNeedingBackup", {keyPath: "key"});
}

View file

@ -0,0 +1,56 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import type {Store} from "../Store";
export type BackupEntry = {
roomId: string;
senderKey: string;
sessionId: string;
};
type StorageEntry = {
key: string
};
function encodeKey(roomId: string, senderKey: string, sessionId: string): string {
return `${roomId}|${senderKey}|${sessionId}`;
}
function decodeKey(key: string): BackupEntry {
const [roomId, senderKey, sessionId] = key.split("|");
return {roomId, senderKey, sessionId};
}
export class SessionNeedingBackupStore {
constructor(private store: Store<StorageEntry>) {}
async getFirstEntries(amount: number): Promise<BackupEntry[]> {
const storageEntries = await this.store.selectLimit(undefined, amount);
return storageEntries.map(s => decodeKey(s.key));
}
set(roomId: string, senderKey: string, sessionId: string): void {
const storageEntry : StorageEntry = {
key: encodeKey(roomId, senderKey, sessionId),
};
this.store.put(storageEntry);
}
remove(roomId: string, senderKey: string, sessionId: string): void {
this.store.delete(encodeKey(roomId, senderKey, sessionId));
}
}