From 6788a612fc2b0ee5c157bfbbbb0afd1f8e563802 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 1 Sep 2020 17:59:59 +0200 Subject: [PATCH] implement olm session storage --- src/matrix/storage/common.js | 1 + src/matrix/storage/idb/Transaction.js | 5 +++ src/matrix/storage/idb/schema.js | 6 +++ .../storage/idb/stores/OlmSessionStore.js | 45 +++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 src/matrix/storage/idb/stores/OlmSessionStore.js diff --git a/src/matrix/storage/common.js b/src/matrix/storage/common.js index 7d6fae09..73900af3 100644 --- a/src/matrix/storage/common.js +++ b/src/matrix/storage/common.js @@ -24,6 +24,7 @@ export const STORE_NAMES = Object.freeze([ "pendingEvents", "userIdentities", "deviceIdentities", + "olmSessions", ]); export const STORE_MAP = Object.freeze(STORE_NAMES.reduce((nameMap, name) => { diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index 921c23e2..370a5563 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -26,6 +26,7 @@ import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js"; import {PendingEventStore} from "./stores/PendingEventStore.js"; import {UserIdentityStore} from "./stores/UserIdentityStore.js"; import {DeviceIdentityStore} from "./stores/DeviceIdentityStore.js"; +import {OlmSessionStore} from "./stores/OlmSessionStore.js"; export class Transaction { constructor(txn, allowedStoreNames) { @@ -91,6 +92,10 @@ export class Transaction { return this._store("deviceIdentities", idbStore => new DeviceIdentityStore(idbStore)); } + get olmSessions() { + return this._store("olmSessions", idbStore => new OlmSessionStore(idbStore)); + } + complete() { return txnAsPromise(this._txn); } diff --git a/src/matrix/storage/idb/schema.js b/src/matrix/storage/idb/schema.js index d8aa81cc..8e34ac27 100644 --- a/src/matrix/storage/idb/schema.js +++ b/src/matrix/storage/idb/schema.js @@ -10,6 +10,7 @@ export const schema = [ createMemberStore, migrateSession, createIdentityStores, + createOlmSessionStore, ]; // TODO: how to deal with git merge conflicts of this array? @@ -70,3 +71,8 @@ function createIdentityStores(db) { db.createObjectStore("userIdentities", {keyPath: "userId"}); db.createObjectStore("deviceIdentities", {keyPath: "key"}); } + +//v5 +function createOlmSessionStore(db) { + db.createObjectStore("olmSessions", {keyPath: "key"}); +} diff --git a/src/matrix/storage/idb/stores/OlmSessionStore.js b/src/matrix/storage/idb/stores/OlmSessionStore.js new file mode 100644 index 00000000..c94b3bfd --- /dev/null +++ b/src/matrix/storage/idb/stores/OlmSessionStore.js @@ -0,0 +1,45 @@ +/* +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. +*/ + +function encodeKey(senderKey, sessionId) { + return `${senderKey}|${sessionId}`; +} + +export class OlmSessionStore { + constructor(store) { + this._store = store; + } + + getAll(senderKey) { + const range = IDBKeyRange.lowerBound(encodeKey(senderKey, "")); + return this._store.selectWhile(range, session => { + return session.senderKey === senderKey; + }); + } + + get(senderKey, sessionId) { + return this._store.get(encodeKey(senderKey, sessionId)); + } + + set(session) { + session.key = encodeKey(session.senderKey, session.sessionId); + return this._store.put(session); + } + + remove(senderKey, sessionId) { + return this._store.delete(encodeKey(senderKey, sessionId)); + } +}