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";
|
|
|
|
|
|
|
|
interface InboundGroupSession {
|
|
|
|
roomId: string;
|
|
|
|
senderKey: string;
|
|
|
|
sessionId: string;
|
|
|
|
session?: string;
|
|
|
|
claimedKeys?: { [algorithm : string] : string };
|
|
|
|
eventIds?: string[];
|
|
|
|
key: string;
|
|
|
|
}
|
2021-05-12 19:08:11 +05:30
|
|
|
|
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-08-12 22:54:47 +05:30
|
|
|
private _store: Store<InboundGroupSession>;
|
|
|
|
|
|
|
|
constructor(store: Store<InboundGroupSession>) {
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:54:47 +05:30
|
|
|
get(roomId: string, senderKey: string, sessionId: string): Promise<InboundGroupSession | null> {
|
2020-09-04 19:01:00 +05:30
|
|
|
return this._store.get(encodeKey(roomId, senderKey, sessionId));
|
|
|
|
}
|
|
|
|
|
2021-09-01 03:18:38 +05:30
|
|
|
set(session: InboundGroupSession): void {
|
2020-09-02 17:54:38 +05:30
|
|
|
session.key = encodeKey(session.roomId, session.senderKey, session.sessionId);
|
|
|
|
this._store.put(session);
|
|
|
|
}
|
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
|
|
|
}
|
2020-09-02 17:54:38 +05:30
|
|
|
}
|