2020-09-02 17:54:38 +05:30
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-08-12 02:24:33 +05:30
|
|
|
import {MIN_UNICODE, MAX_UNICODE} from "./common";
|
2021-08-12 22:54:47 +05:30
|
|
|
import {Store} from "../Store";
|
|
|
|
|
2022-01-27 20:30:46 +05:30
|
|
|
export enum BackupStatus {
|
|
|
|
NotBackedUp = 0,
|
|
|
|
BackedUp = 1
|
|
|
|
}
|
|
|
|
|
2021-10-20 18:44:17 +05:30
|
|
|
export interface InboundGroupSessionEntry {
|
2021-08-12 22:54:47 +05:30
|
|
|
roomId: string;
|
|
|
|
senderKey: string;
|
|
|
|
sessionId: string;
|
|
|
|
session?: string;
|
|
|
|
claimedKeys?: { [algorithm : string] : string };
|
|
|
|
eventIds?: string[];
|
2022-01-27 20:30:46 +05:30
|
|
|
backup: BackupStatus
|
2021-08-12 22:54:47 +05:30
|
|
|
}
|
2021-05-12 19:08:11 +05:30
|
|
|
|
2021-10-20 18:44:17 +05:30
|
|
|
type InboundGroupSessionStorageEntry = InboundGroupSessionEntry & { key: string };
|
|
|
|
|
|
|
|
|
2021-08-12 22:54:47 +05:30
|
|
|
function encodeKey(roomId: string, senderKey: string, sessionId: string): string {
|
2020-09-02 17:54:38 +05:30
|
|
|
return `${roomId}|${senderKey}|${sessionId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class InboundGroupSessionStore {
|
2021-10-20 18:44:17 +05:30
|
|
|
private _store: Store<InboundGroupSessionStorageEntry>;
|
2021-08-12 22:54:47 +05:30
|
|
|
|
2021-10-20 18:44:17 +05:30
|
|
|
constructor(store: Store<InboundGroupSessionStorageEntry>) {
|
2020-09-02 17:54:38 +05:30
|
|
|
this._store = store;
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:54:47 +05:30
|
|
|
async has(roomId: string, senderKey: string, sessionId: string): Promise<boolean> {
|
2020-09-02 17:54:38 +05:30
|
|
|
const key = encodeKey(roomId, senderKey, sessionId);
|
|
|
|
const fetchedKey = await this._store.getKey(key);
|
|
|
|
return key === fetchedKey;
|
|
|
|
}
|
|
|
|
|
2022-01-25 23:13:44 +05:30
|
|
|
get(roomId: string, senderKey: string, sessionId: string): Promise<InboundGroupSessionEntry | undefined> {
|
2020-09-04 19:01:00 +05:30
|
|
|
return this._store.get(encodeKey(roomId, senderKey, sessionId));
|
|
|
|
}
|
|
|
|
|
2021-10-20 18:44:17 +05:30
|
|
|
set(session: InboundGroupSessionEntry): void {
|
|
|
|
const storageEntry = session as InboundGroupSessionStorageEntry;
|
|
|
|
storageEntry.key = encodeKey(session.roomId, session.senderKey, session.sessionId);
|
|
|
|
this._store.put(storageEntry);
|
2020-09-02 17:54:38 +05:30
|
|
|
}
|
2021-05-12 19:08:11 +05:30
|
|
|
|
2021-09-17 21:54:24 +05:30
|
|
|
removeAllForRoom(roomId: string) {
|
2021-06-02 16:01:13 +05:30
|
|
|
const range = this._store.IDBKeyRange.bound(
|
2021-05-12 19:08:11 +05:30
|
|
|
encodeKey(roomId, MIN_UNICODE, MIN_UNICODE),
|
|
|
|
encodeKey(roomId, MAX_UNICODE, MAX_UNICODE)
|
|
|
|
);
|
2021-09-17 21:54:24 +05:30
|
|
|
this._store.delete(range);
|
2021-05-12 19:08:11 +05:30
|
|
|
}
|
2022-01-27 20:30:46 +05:30
|
|
|
countNonBackedUpSessions(): Promise<number> {
|
|
|
|
return this._store.index("byBackup").count();
|
|
|
|
}
|
|
|
|
|
|
|
|
getFirstNonBackedUpSessions(amount: number): Promise<InboundGroupSessionEntry[]> {
|
|
|
|
return this._store.index("byBackup").selectLimit(0, amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
async markAsBackedUp(roomId: string, senderKey: string, sessionId: string): Promise<void> {
|
|
|
|
const entry = await this._store.get(encodeKey(roomId, senderKey, sessionId));
|
|
|
|
if (entry) {
|
|
|
|
entry.backup = BackupStatus.BackedUp;
|
|
|
|
this._store.put(entry);
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 17:54:38 +05:30
|
|
|
}
|