implement olm session storage

This commit is contained in:
Bruno Windels 2020-09-01 17:59:59 +02:00
parent 5fee7fedc3
commit 6788a612fc
4 changed files with 57 additions and 0 deletions

View File

@ -24,6 +24,7 @@ export const STORE_NAMES = Object.freeze([
"pendingEvents", "pendingEvents",
"userIdentities", "userIdentities",
"deviceIdentities", "deviceIdentities",
"olmSessions",
]); ]);
export const STORE_MAP = Object.freeze(STORE_NAMES.reduce((nameMap, name) => { export const STORE_MAP = Object.freeze(STORE_NAMES.reduce((nameMap, name) => {

View File

@ -26,6 +26,7 @@ import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js";
import {PendingEventStore} from "./stores/PendingEventStore.js"; import {PendingEventStore} from "./stores/PendingEventStore.js";
import {UserIdentityStore} from "./stores/UserIdentityStore.js"; import {UserIdentityStore} from "./stores/UserIdentityStore.js";
import {DeviceIdentityStore} from "./stores/DeviceIdentityStore.js"; import {DeviceIdentityStore} from "./stores/DeviceIdentityStore.js";
import {OlmSessionStore} from "./stores/OlmSessionStore.js";
export class Transaction { export class Transaction {
constructor(txn, allowedStoreNames) { constructor(txn, allowedStoreNames) {
@ -91,6 +92,10 @@ export class Transaction {
return this._store("deviceIdentities", idbStore => new DeviceIdentityStore(idbStore)); return this._store("deviceIdentities", idbStore => new DeviceIdentityStore(idbStore));
} }
get olmSessions() {
return this._store("olmSessions", idbStore => new OlmSessionStore(idbStore));
}
complete() { complete() {
return txnAsPromise(this._txn); return txnAsPromise(this._txn);
} }

View File

@ -10,6 +10,7 @@ export const schema = [
createMemberStore, createMemberStore,
migrateSession, migrateSession,
createIdentityStores, createIdentityStores,
createOlmSessionStore,
]; ];
// TODO: how to deal with git merge conflicts of this array? // 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("userIdentities", {keyPath: "userId"});
db.createObjectStore("deviceIdentities", {keyPath: "key"}); db.createObjectStore("deviceIdentities", {keyPath: "key"});
} }
//v5
function createOlmSessionStore(db) {
db.createObjectStore("olmSessions", {keyPath: "key"});
}

View File

@ -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));
}
}