From 77f75fd968349c3e47ca2205301d7373942f4c02 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 12 Aug 2021 11:05:55 -0700 Subject: [PATCH] Migrate OperationStore to TypeScript --- src/matrix/storage/idb/Transaction.js | 2 +- src/matrix/storage/idb/schema.js | 2 +- .../{OperationStore.js => OperationStore.ts} | 45 ++++++++++++++----- 3 files changed, 35 insertions(+), 14 deletions(-) rename src/matrix/storage/idb/stores/{OperationStore.js => OperationStore.ts} (60%) diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index 8b19af37..7d5f8af9 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -32,7 +32,7 @@ import {OlmSessionStore} from "./stores/OlmSessionStore.js"; import {InboundGroupSessionStore} from "./stores/InboundGroupSessionStore.js"; import {OutboundGroupSessionStore} from "./stores/OutboundGroupSessionStore"; import {GroupSessionDecryptionStore} from "./stores/GroupSessionDecryptionStore"; -import {OperationStore} from "./stores/OperationStore.js"; +import {OperationStore} from "./stores/OperationStore"; import {AccountDataStore} from "./stores/AccountDataStore.js"; export class Transaction { diff --git a/src/matrix/storage/idb/schema.js b/src/matrix/storage/idb/schema.js index d0d9cf16..7df2cb3e 100644 --- a/src/matrix/storage/idb/schema.js +++ b/src/matrix/storage/idb/schema.js @@ -2,7 +2,7 @@ import {iterateCursor, reqAsPromise} from "./utils"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js"; import {RoomMemberStore} from "./stores/RoomMemberStore"; import {SessionStore} from "./stores/SessionStore"; -import {encodeScopeTypeKey} from "./stores/OperationStore.js"; +import {encodeScopeTypeKey} from "./stores/OperationStore"; // FUNCTIONS SHOULD ONLY BE APPENDED!! // the index in the array is the database version diff --git a/src/matrix/storage/idb/stores/OperationStore.js b/src/matrix/storage/idb/stores/OperationStore.ts similarity index 60% rename from src/matrix/storage/idb/stores/OperationStore.js rename to src/matrix/storage/idb/stores/OperationStore.ts index 961e36af..2be6c3d7 100644 --- a/src/matrix/storage/idb/stores/OperationStore.js +++ b/src/matrix/storage/idb/stores/OperationStore.ts @@ -14,23 +14,43 @@ See the License for the specific language governing permissions and limitations under the License. */ import {MIN_UNICODE, MAX_UNICODE} from "./common"; +import {Store} from "../Store"; -export function encodeScopeTypeKey(scope, type) { +export function encodeScopeTypeKey(scope: string, type: string): string { return `${scope}|${type}`; } +interface Operation { + id: string; + type: string; + scope: string; + userIds: string[]; + scopeTypeKey: string; + roomKeyMessage: RoomKeyMessage; +} + +interface RoomKeyMessage { + room_id: string; + session_id: string; + session_key: string; + algorithm: string; + chain_index: number; +} + export class OperationStore { - constructor(store) { + private _store: Store; + + constructor(store: Store) { this._store = store; } - getAll() { + getAll(): Promise { return this._store.selectAll(); } - async getAllByTypeAndScope(type, scope) { + async getAllByTypeAndScope(type: string, scope: string): Promise { const key = encodeScopeTypeKey(scope, type); - const results = []; + const results: Operation[] = []; await this._store.index("byScopeAndType").iterateWhile(key, value => { if (value.scopeTypeKey !== key) { return false; @@ -41,20 +61,20 @@ export class OperationStore { return results; } - add(operation) { + add(operation: Operation): Promise { operation.scopeTypeKey = encodeScopeTypeKey(operation.scope, operation.type); - this._store.add(operation); + return this._store.add(operation); } - update(operation) { - this._store.put(operation); + update(operation: Operation): Promise { + return this._store.put(operation); } - remove(id) { - this._store.delete(id); + remove(id: string): Promise { + return this._store.delete(id); } - async removeAllForScope(scope) { + async removeAllForScope(scope: string): Promise { const range = this._store.IDBKeyRange.bound( encodeScopeTypeKey(scope, MIN_UNICODE), encodeScopeTypeKey(scope, MAX_UNICODE) @@ -64,5 +84,6 @@ export class OperationStore { cur.delete(); return true; }); + return; } }