From 8b7fdb2c61787dda69c2d4b7572f3fbf879ae11d Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 31 Aug 2020 14:38:03 +0200 Subject: [PATCH] create user & device identity stores --- src/matrix/storage/common.js | 2 + src/matrix/storage/idb/Transaction.js | 10 +++++ src/matrix/storage/idb/schema.js | 8 +++- .../storage/idb/stores/DeviceIdentityStore.js | 41 +++++++++++++++++++ .../storage/idb/stores/UserIdentityStore.js | 33 +++++++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/matrix/storage/idb/stores/DeviceIdentityStore.js create mode 100644 src/matrix/storage/idb/stores/UserIdentityStore.js diff --git a/src/matrix/storage/common.js b/src/matrix/storage/common.js index 0cf5b9b0..7d6fae09 100644 --- a/src/matrix/storage/common.js +++ b/src/matrix/storage/common.js @@ -22,6 +22,8 @@ export const STORE_NAMES = Object.freeze([ "timelineEvents", "timelineFragments", "pendingEvents", + "userIdentities", + "deviceIdentities", ]); 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 4f5e3af5..921c23e2 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -24,6 +24,8 @@ import {RoomStateStore} from "./stores/RoomStateStore.js"; import {RoomMemberStore} from "./stores/RoomMemberStore.js"; import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js"; import {PendingEventStore} from "./stores/PendingEventStore.js"; +import {UserIdentityStore} from "./stores/UserIdentityStore.js"; +import {DeviceIdentityStore} from "./stores/DeviceIdentityStore.js"; export class Transaction { constructor(txn, allowedStoreNames) { @@ -81,6 +83,14 @@ export class Transaction { return this._store("pendingEvents", idbStore => new PendingEventStore(idbStore)); } + get userIdentities() { + return this._store("userIdentities", idbStore => new UserIdentityStore(idbStore)); + } + + get deviceIdentities() { + return this._store("deviceIdentities", idbStore => new DeviceIdentityStore(idbStore)); + } + complete() { return txnAsPromise(this._txn); } diff --git a/src/matrix/storage/idb/schema.js b/src/matrix/storage/idb/schema.js index 0ab5707a..d8aa81cc 100644 --- a/src/matrix/storage/idb/schema.js +++ b/src/matrix/storage/idb/schema.js @@ -9,6 +9,7 @@ export const schema = [ createInitialStores, createMemberStore, migrateSession, + createIdentityStores, ]; // TODO: how to deal with git merge conflicts of this array? @@ -46,7 +47,7 @@ async function createMemberStore(db, txn) { } }); } - +//v3 async function migrateSession(db, txn) { const session = txn.objectStore("session"); try { @@ -64,3 +65,8 @@ async function migrateSession(db, txn) { console.error("could not migrate session", err.stack); } } +//v4 +function createIdentityStores(db) { + db.createObjectStore("userIdentities", {keyPath: "userId"}); + db.createObjectStore("deviceIdentities", {keyPath: "key"}); +} diff --git a/src/matrix/storage/idb/stores/DeviceIdentityStore.js b/src/matrix/storage/idb/stores/DeviceIdentityStore.js new file mode 100644 index 00000000..8bbab1b7 --- /dev/null +++ b/src/matrix/storage/idb/stores/DeviceIdentityStore.js @@ -0,0 +1,41 @@ +/* +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(userId, deviceId) { + return `${userId}|${deviceId}`; +} + +export class DeviceIdentityStore { + constructor(store) { + this._store = store; + } + + getAllForUserId(userId) { + const range = IDBKeyRange.lowerBound(encodeKey(userId, "")); + return this._store.selectWhile(range, device => { + return device.userId === userId; + }); + } + + get(userId, deviceId) { + return this._store.get(encodeKey(userId, deviceId)); + } + + set(deviceIdentity) { + deviceIdentity.key = encodeKey(deviceIdentity.userId, deviceIdentity.deviceId); + return this._store.set(deviceIdentity); + } +} diff --git a/src/matrix/storage/idb/stores/UserIdentityStore.js b/src/matrix/storage/idb/stores/UserIdentityStore.js new file mode 100644 index 00000000..2eadefec --- /dev/null +++ b/src/matrix/storage/idb/stores/UserIdentityStore.js @@ -0,0 +1,33 @@ +/* +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. +*/ + +export class UserIdentityStore { + constructor(store) { + this._store = store; + } + + get(userId) { + return this._store.get(userId); + } + + set(userIdentity) { + return this._store.set(userIdentity); + } + + remove(userId) { + return this._eventStore.delete(userId); + } +}