2020-09-04 19:01:00 +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 23:11:58 +05:30
|
|
|
import {Store} from "../Store";
|
2021-05-12 19:08:11 +05:30
|
|
|
|
2021-08-12 23:11:58 +05:30
|
|
|
function encodeKey(roomId: string, sessionId: string, messageIndex: number | string): string {
|
2020-09-04 20:10:39 +05:30
|
|
|
return `${roomId}|${sessionId}|${messageIndex}`;
|
2020-09-04 19:01:00 +05:30
|
|
|
}
|
|
|
|
|
2021-08-12 23:11:58 +05:30
|
|
|
interface GroupSessionDecryption {
|
|
|
|
eventId: string;
|
|
|
|
timestamp: number;
|
|
|
|
key: string;
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:01:00 +05:30
|
|
|
export class GroupSessionDecryptionStore {
|
2021-08-12 23:11:58 +05:30
|
|
|
private _store: Store<GroupSessionDecryption>;
|
|
|
|
|
|
|
|
constructor(store: Store<GroupSessionDecryption>) {
|
2020-09-04 19:01:00 +05:30
|
|
|
this._store = store;
|
|
|
|
}
|
|
|
|
|
2021-08-12 23:11:58 +05:30
|
|
|
get(roomId: string, sessionId: string, messageIndex: number): Promise<GroupSessionDecryption | null> {
|
2020-09-04 19:01:00 +05:30
|
|
|
return this._store.get(encodeKey(roomId, sessionId, messageIndex));
|
|
|
|
}
|
|
|
|
|
2021-09-01 03:18:38 +05:30
|
|
|
set(roomId: string, sessionId: string, messageIndex: number, decryption: GroupSessionDecryption): void {
|
2020-09-11 15:38:06 +05:30
|
|
|
decryption.key = encodeKey(roomId, sessionId, messageIndex);
|
2020-09-04 19:01:00 +05:30
|
|
|
this._store.put(decryption);
|
|
|
|
}
|
2021-05-12 19:08:11 +05:30
|
|
|
|
2021-08-12 23:11:58 +05:30
|
|
|
removeAllForRoom(roomId: string): Promise<undefined> {
|
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-08-12 23:11:58 +05:30
|
|
|
return this._store.delete(range);
|
2021-05-12 19:08:11 +05:30
|
|
|
}
|
2020-09-04 19:01:00 +05:30
|
|
|
}
|