2020-09-01 21:29:59 +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 22:49:09 +05:30
|
|
|
import {Store} from "../Store";
|
2020-09-01 21:29:59 +05:30
|
|
|
|
2021-08-12 22:49:09 +05:30
|
|
|
function encodeKey(senderKey: string, sessionId: string): string {
|
2020-09-01 21:29:59 +05:30
|
|
|
return `${senderKey}|${sessionId}`;
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:49:09 +05:30
|
|
|
function decodeKey(key: string): { senderKey: string, sessionId: string } {
|
2020-09-03 19:03:03 +05:30
|
|
|
const [senderKey, sessionId] = key.split("|");
|
|
|
|
return {senderKey, sessionId};
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:49:09 +05:30
|
|
|
interface OlmSession {
|
|
|
|
session: string;
|
|
|
|
sessionId: string;
|
|
|
|
senderKey: string;
|
|
|
|
lastUsed: number;
|
|
|
|
key: string;
|
|
|
|
}
|
|
|
|
|
2020-09-01 21:29:59 +05:30
|
|
|
export class OlmSessionStore {
|
2021-08-12 22:49:09 +05:30
|
|
|
private _store: Store<OlmSession>;
|
|
|
|
|
|
|
|
constructor(store: Store<OlmSession>) {
|
2020-09-01 21:29:59 +05:30
|
|
|
this._store = store;
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:49:09 +05:30
|
|
|
async getSessionIds(senderKey: string): Promise<string[]> {
|
|
|
|
const sessionIds: string[] = [];
|
2021-06-02 16:01:13 +05:30
|
|
|
const range = this._store.IDBKeyRange.lowerBound(encodeKey(senderKey, ""));
|
2020-09-03 19:03:03 +05:30
|
|
|
await this._store.iterateKeys(range, key => {
|
2021-08-12 22:49:09 +05:30
|
|
|
const decodedKey = decodeKey(key as string);
|
2020-09-03 19:03:03 +05:30
|
|
|
// prevent running into the next room
|
|
|
|
if (decodedKey.senderKey === senderKey) {
|
|
|
|
sessionIds.push(decodedKey.sessionId);
|
|
|
|
return false; // fetch more
|
|
|
|
}
|
|
|
|
return true; // done
|
|
|
|
});
|
|
|
|
return sessionIds;
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:49:09 +05:30
|
|
|
getAll(senderKey: string): Promise<OlmSession[]> {
|
2021-06-02 16:01:13 +05:30
|
|
|
const range = this._store.IDBKeyRange.lowerBound(encodeKey(senderKey, ""));
|
2020-09-01 21:29:59 +05:30
|
|
|
return this._store.selectWhile(range, session => {
|
|
|
|
return session.senderKey === senderKey;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:49:09 +05:30
|
|
|
get(senderKey: string, sessionId: string): Promise<OlmSession | null> {
|
2020-09-01 21:29:59 +05:30
|
|
|
return this._store.get(encodeKey(senderKey, sessionId));
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:49:09 +05:30
|
|
|
set(session: OlmSession): Promise<IDBValidKey> {
|
2020-09-01 21:29:59 +05:30
|
|
|
session.key = encodeKey(session.senderKey, session.sessionId);
|
|
|
|
return this._store.put(session);
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:49:09 +05:30
|
|
|
remove(senderKey: string, sessionId: string): Promise<undefined> {
|
2020-09-01 21:29:59 +05:30
|
|
|
return this._store.delete(encodeKey(senderKey, sessionId));
|
|
|
|
}
|
|
|
|
}
|