diff --git a/src/matrix/storage/idb/schema.js b/src/matrix/storage/idb/schema.js index fc88b42f..011f1e02 100644 --- a/src/matrix/storage/idb/schema.js +++ b/src/matrix/storage/idb/schema.js @@ -1,5 +1,6 @@ import {iterateCursor} from "./utils.js"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/RoomMember.js"; +import {RoomMemberStore} from "./stores/RoomMemberStore.js"; // FUNCTIONS SHOULD ONLY BE APPENDED!! // the index in the array is the database version @@ -30,10 +31,7 @@ function createInitialStores(db) { } //v2 async function createMemberStore(db, txn) { - const roomMembers = db.createObjectStore("roomMembers", {keyPath: [ - "roomId", - "userId" - ]}); + const roomMembers = new RoomMemberStore(db.createObjectStore("roomMembers", {keyPath: "key"})); // migrate existing member state events over const roomState = txn.objectStore("roomState"); await iterateCursor(roomState.openCursor(), entry => { @@ -41,25 +39,8 @@ async function createMemberStore(db, txn) { roomState.delete(entry.key); const member = RoomMember.fromMemberEvent(entry.roomId, entry.event); if (member) { - roomMembers.add(member.serialize()); + roomMembers.set(member.serialize()); } } }); } - -function migrateKeyPathToArray(db, isNew) { - if (isNew) { - // create the new stores with the final name - } else { - // create the new stores with a tmp name - // migrate the data over - // change the name - } - - // maybe it is ok to just run all the migration steps? - // it might be a bit slower to create a store twice ... - // but at least the path of migration or creating a new store - // will go through the same code - // - // might not even be slower, as this is all happening within one transaction -} diff --git a/src/matrix/storage/idb/stores/RoomMemberStore.js b/src/matrix/storage/idb/stores/RoomMemberStore.js index 67527ce2..677f41f0 100644 --- a/src/matrix/storage/idb/stores/RoomMemberStore.js +++ b/src/matrix/storage/idb/stores/RoomMemberStore.js @@ -1,24 +1,37 @@ -// no historical members for now +/* +Copyright 2020 Bruno Windels +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(roomId, userId) { + return `${roomId}|${userId}`; +} + +// no historical members export class RoomMemberStore { constructor(roomMembersStore) { this._roomMembersStore = roomMembersStore; } get(roomId, userId) { - return this._roomMembersStore.get([roomId, userId]); + return this._roomMembersStore.get(encodeKey(roomId, userId)); } async set(member) { + member.key = encodeKey(member.roomId, member.userId); return this._roomMembersStore.put(member); } - /* - async getMemberAtSortKey(roomId, userId, sortKey) { - - } - - async getSortedMembers(roomId, offset, amount) { - - } - */ }