From 144e391c82ff57dfef42e75be14fffea08f93f45 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 10 Aug 2021 16:10:55 -0700 Subject: [PATCH 01/11] Migrate SessionStore.js to TypeScript. --- src/matrix/storage/idb/Transaction.js | 2 +- src/matrix/storage/idb/schema.js | 2 +- src/matrix/storage/idb/stores/SessionStore.js | 40 ---------------- src/matrix/storage/idb/stores/SessionStore.ts | 48 +++++++++++++++++++ 4 files changed, 50 insertions(+), 42 deletions(-) delete mode 100644 src/matrix/storage/idb/stores/SessionStore.js create mode 100644 src/matrix/storage/idb/stores/SessionStore.ts diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index 4c543f4c..d4949b02 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -17,7 +17,7 @@ limitations under the License. import {txnAsPromise} from "./utils"; import {StorageError} from "../common"; import {Store} from "./Store"; -import {SessionStore} from "./stores/SessionStore.js"; +import {SessionStore} from "./stores/SessionStore"; import {RoomSummaryStore} from "./stores/RoomSummaryStore.js"; import {InviteStore} from "./stores/InviteStore.js"; import {TimelineEventStore} from "./stores/TimelineEventStore.js"; diff --git a/src/matrix/storage/idb/schema.js b/src/matrix/storage/idb/schema.js index d593f6b9..fdfc78d7 100644 --- a/src/matrix/storage/idb/schema.js +++ b/src/matrix/storage/idb/schema.js @@ -1,7 +1,7 @@ import {iterateCursor, reqAsPromise} from "./utils"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js"; import {RoomMemberStore} from "./stores/RoomMemberStore.js"; -import {SessionStore} from "./stores/SessionStore.js"; +import {SessionStore} from "./stores/SessionStore"; import {encodeScopeTypeKey} from "./stores/OperationStore.js"; // FUNCTIONS SHOULD ONLY BE APPENDED!! diff --git a/src/matrix/storage/idb/stores/SessionStore.js b/src/matrix/storage/idb/stores/SessionStore.js deleted file mode 100644 index 3be8e875..00000000 --- a/src/matrix/storage/idb/stores/SessionStore.js +++ /dev/null @@ -1,40 +0,0 @@ -/* -Copyright 2020 Bruno Windels - -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 SessionStore { - constructor(sessionStore) { - this._sessionStore = sessionStore; - } - - async get(key) { - const entry = await this._sessionStore.get(key); - if (entry) { - return entry.value; - } - } - - set(key, value) { - this._sessionStore.put({key, value}); - } - - add(key, value) { - this._sessionStore.add({key, value}); - } - - remove(key) { - this._sessionStore.delete(key); - } -} diff --git a/src/matrix/storage/idb/stores/SessionStore.ts b/src/matrix/storage/idb/stores/SessionStore.ts new file mode 100644 index 00000000..bf3d6d11 --- /dev/null +++ b/src/matrix/storage/idb/stores/SessionStore.ts @@ -0,0 +1,48 @@ +/* +Copyright 2020 Bruno Windels + +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. +*/ +import {Store} from "../Store"; + +export interface SessionEntry { + key: string; + value: any; +} + +export class SessionStore { + private _sessionStore: Store + + constructor(sessionStore: Store) { + this._sessionStore = sessionStore; + } + + async get(key: IDBValidKey): Promise { + const entry = await this._sessionStore.get(key); + if (entry) { + return entry.value; + } + } + + set(key: string, value: any): Promise { + return this._sessionStore.put({key, value}); + } + + add(key: string, value: any): Promise { + return this._sessionStore.add({key, value}); + } + + remove(key: IDBValidKey): Promise { + return this._sessionStore.delete(key); + } +} From e284224cc883e4037e2bedaa32589e3809fe7acc Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 10 Aug 2021 16:28:47 -0700 Subject: [PATCH 02/11] Migrate RoomSummaryStore.js to TypeScript. --- src/matrix/storage/idb/Transaction.js | 2 +- ...oomSummaryStore.js => RoomSummaryStore.ts} | 38 ++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) rename src/matrix/storage/idb/stores/{RoomSummaryStore.js => RoomSummaryStore.ts} (57%) diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index d4949b02..c92999b8 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -18,7 +18,7 @@ import {txnAsPromise} from "./utils"; import {StorageError} from "../common"; import {Store} from "./Store"; import {SessionStore} from "./stores/SessionStore"; -import {RoomSummaryStore} from "./stores/RoomSummaryStore.js"; +import {RoomSummaryStore} from "./stores/RoomSummaryStore"; import {InviteStore} from "./stores/InviteStore.js"; import {TimelineEventStore} from "./stores/TimelineEventStore.js"; import {TimelineRelationStore} from "./stores/TimelineRelationStore.js"; diff --git a/src/matrix/storage/idb/stores/RoomSummaryStore.js b/src/matrix/storage/idb/stores/RoomSummaryStore.ts similarity index 57% rename from src/matrix/storage/idb/stores/RoomSummaryStore.js rename to src/matrix/storage/idb/stores/RoomSummaryStore.ts index 426a01bb..19e847b1 100644 --- a/src/matrix/storage/idb/stores/RoomSummaryStore.js +++ b/src/matrix/storage/idb/stores/RoomSummaryStore.ts @@ -27,31 +27,35 @@ store contains: inviteCount joinCount */ +import {Store} from "../Store"; +import {SummaryData} from "../../../room/RoomSummary"; /** Used for both roomSummary and archivedRoomSummary stores */ export class RoomSummaryStore { - constructor(summaryStore) { - this._summaryStore = summaryStore; - } + private _summaryStore: Store; - getAll() { - return this._summaryStore.selectAll(); - } + constructor(summaryStore: Store) { + this._summaryStore = summaryStore; + } - set(summary) { - return this._summaryStore.put(summary); - } + getAll(): Promise { + return this._summaryStore.selectAll(); + } - get(roomId) { - return this._summaryStore.get(roomId); - } + set(summary: SummaryData): Promise { + return this._summaryStore.put(summary); + } - async has(roomId) { + get(roomId: string): Promise { + return this._summaryStore.get(roomId); + } + + async has(roomId: string): Promise { const fetchedKey = await this._summaryStore.getKey(roomId); return roomId === fetchedKey; - } + } - remove(roomId) { - return this._summaryStore.delete(roomId); - } + remove(roomId: string): Promise { + return this._summaryStore.delete(roomId); + } } From 38a38e828722b9c981c2d796a9b1e5aac0f2d2d5 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 11 Aug 2021 13:54:33 -0700 Subject: [PATCH 03/11] Migrate common.ts --- src/matrix/storage/idb/stores/DeviceIdentityStore.js | 2 +- src/matrix/storage/idb/stores/GroupSessionDecryptionStore.js | 2 +- src/matrix/storage/idb/stores/InboundGroupSessionStore.js | 2 +- src/matrix/storage/idb/stores/OperationStore.js | 2 +- src/matrix/storage/idb/stores/RoomMemberStore.js | 2 +- src/matrix/storage/idb/stores/RoomStateStore.js | 2 +- src/matrix/storage/idb/stores/TimelineRelationStore.js | 2 +- src/matrix/storage/idb/stores/{common.js => common.ts} | 0 8 files changed, 7 insertions(+), 7 deletions(-) rename src/matrix/storage/idb/stores/{common.js => common.ts} (100%) diff --git a/src/matrix/storage/idb/stores/DeviceIdentityStore.js b/src/matrix/storage/idb/stores/DeviceIdentityStore.js index bfc5b30b..207cfb20 100644 --- a/src/matrix/storage/idb/stores/DeviceIdentityStore.js +++ b/src/matrix/storage/idb/stores/DeviceIdentityStore.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {MAX_UNICODE, MIN_UNICODE} from "./common.js"; +import {MAX_UNICODE, MIN_UNICODE} from "./common"; function encodeKey(userId, deviceId) { return `${userId}|${deviceId}`; diff --git a/src/matrix/storage/idb/stores/GroupSessionDecryptionStore.js b/src/matrix/storage/idb/stores/GroupSessionDecryptionStore.js index f3a721f1..da47639d 100644 --- a/src/matrix/storage/idb/stores/GroupSessionDecryptionStore.js +++ b/src/matrix/storage/idb/stores/GroupSessionDecryptionStore.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {MIN_UNICODE, MAX_UNICODE} from "./common.js"; +import {MIN_UNICODE, MAX_UNICODE} from "./common"; function encodeKey(roomId, sessionId, messageIndex) { return `${roomId}|${sessionId}|${messageIndex}`; diff --git a/src/matrix/storage/idb/stores/InboundGroupSessionStore.js b/src/matrix/storage/idb/stores/InboundGroupSessionStore.js index f5dcdc39..488f2510 100644 --- a/src/matrix/storage/idb/stores/InboundGroupSessionStore.js +++ b/src/matrix/storage/idb/stores/InboundGroupSessionStore.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {MIN_UNICODE, MAX_UNICODE} from "./common.js"; +import {MIN_UNICODE, MAX_UNICODE} from "./common"; function encodeKey(roomId, senderKey, sessionId) { return `${roomId}|${senderKey}|${sessionId}`; diff --git a/src/matrix/storage/idb/stores/OperationStore.js b/src/matrix/storage/idb/stores/OperationStore.js index e2040e30..961e36af 100644 --- a/src/matrix/storage/idb/stores/OperationStore.js +++ b/src/matrix/storage/idb/stores/OperationStore.js @@ -13,7 +13,7 @@ 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. */ -import {MIN_UNICODE, MAX_UNICODE} from "./common.js"; +import {MIN_UNICODE, MAX_UNICODE} from "./common"; export function encodeScopeTypeKey(scope, type) { return `${scope}|${type}`; diff --git a/src/matrix/storage/idb/stores/RoomMemberStore.js b/src/matrix/storage/idb/stores/RoomMemberStore.js index f900ff0b..7b2d2032 100644 --- a/src/matrix/storage/idb/stores/RoomMemberStore.js +++ b/src/matrix/storage/idb/stores/RoomMemberStore.js @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {MAX_UNICODE} from "./common.js"; +import {MAX_UNICODE} from "./common"; function encodeKey(roomId, userId) { return `${roomId}|${userId}`; diff --git a/src/matrix/storage/idb/stores/RoomStateStore.js b/src/matrix/storage/idb/stores/RoomStateStore.js index 99fc23f7..db54458f 100644 --- a/src/matrix/storage/idb/stores/RoomStateStore.js +++ b/src/matrix/storage/idb/stores/RoomStateStore.js @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {MAX_UNICODE} from "./common.js"; +import {MAX_UNICODE} from "./common"; function encodeKey(roomId, eventType, stateKey) { return `${roomId}|${eventType}|${stateKey}`; diff --git a/src/matrix/storage/idb/stores/TimelineRelationStore.js b/src/matrix/storage/idb/stores/TimelineRelationStore.js index bba24fc3..5e802b65 100644 --- a/src/matrix/storage/idb/stores/TimelineRelationStore.js +++ b/src/matrix/storage/idb/stores/TimelineRelationStore.js @@ -13,7 +13,7 @@ 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. */ -import {MIN_UNICODE, MAX_UNICODE} from "./common.js"; +import {MIN_UNICODE, MAX_UNICODE} from "./common"; function encodeKey(roomId, targetEventId, relType, sourceEventId) { return `${roomId}|${targetEventId}|${relType}|${sourceEventId}`; diff --git a/src/matrix/storage/idb/stores/common.js b/src/matrix/storage/idb/stores/common.ts similarity index 100% rename from src/matrix/storage/idb/stores/common.js rename to src/matrix/storage/idb/stores/common.ts From 7c56ac774617b92a36bd71513f35da7aa4ef30c9 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 11 Aug 2021 14:59:52 -0700 Subject: [PATCH 04/11] Migrate RoomMemberStore.js to TypeScript --- src/matrix/storage/idb/Transaction.js | 2 +- src/matrix/storage/idb/schema.js | 2 +- ...{RoomMemberStore.js => RoomMemberStore.ts} | 45 ++++++++++++------- 3 files changed, 32 insertions(+), 17 deletions(-) rename src/matrix/storage/idb/stores/{RoomMemberStore.js => RoomMemberStore.ts} (60%) diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index c92999b8..e8e52b11 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -23,7 +23,7 @@ import {InviteStore} from "./stores/InviteStore.js"; import {TimelineEventStore} from "./stores/TimelineEventStore.js"; import {TimelineRelationStore} from "./stores/TimelineRelationStore.js"; import {RoomStateStore} from "./stores/RoomStateStore.js"; -import {RoomMemberStore} from "./stores/RoomMemberStore.js"; +import {RoomMemberStore} from "./stores/RoomMemberStore"; import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js"; import {PendingEventStore} from "./stores/PendingEventStore.js"; import {UserIdentityStore} from "./stores/UserIdentityStore.js"; diff --git a/src/matrix/storage/idb/schema.js b/src/matrix/storage/idb/schema.js index fdfc78d7..d0d9cf16 100644 --- a/src/matrix/storage/idb/schema.js +++ b/src/matrix/storage/idb/schema.js @@ -1,6 +1,6 @@ import {iterateCursor, reqAsPromise} from "./utils"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js"; -import {RoomMemberStore} from "./stores/RoomMemberStore.js"; +import {RoomMemberStore} from "./stores/RoomMemberStore"; import {SessionStore} from "./stores/SessionStore"; import {encodeScopeTypeKey} from "./stores/OperationStore.js"; diff --git a/src/matrix/storage/idb/stores/RoomMemberStore.js b/src/matrix/storage/idb/stores/RoomMemberStore.ts similarity index 60% rename from src/matrix/storage/idb/stores/RoomMemberStore.js rename to src/matrix/storage/idb/stores/RoomMemberStore.ts index 7b2d2032..847e8dae 100644 --- a/src/matrix/storage/idb/stores/RoomMemberStore.js +++ b/src/matrix/storage/idb/stores/RoomMemberStore.ts @@ -16,43 +16,58 @@ limitations under the License. */ import {MAX_UNICODE} from "./common"; +import {Store} from "../Store"; -function encodeKey(roomId, userId) { +function encodeKey(roomId: string, userId: string) { return `${roomId}|${userId}`; } -function decodeKey(key) { +function decodeKey(key: string): { roomId: string, userId: string } { const [roomId, userId] = key.split("|"); return {roomId, userId}; } +// TODO: Move to RoomMember when that's TypeScript. +export interface MemberData { + roomId: string; + userId: string; + avatarUrl: string; + displayName: string; + membership: "join" | "leave" | "invite" | "ban"; +} + +type MemberStorageEntry = MemberData & { key: string } + // no historical members export class RoomMemberStore { - constructor(roomMembersStore) { + private _roomMembersStore: Store; + + constructor(roomMembersStore: Store) { this._roomMembersStore = roomMembersStore; } - get(roomId, userId) { + get(roomId: string, userId: string): Promise { return this._roomMembersStore.get(encodeKey(roomId, userId)); - } + } - async set(member) { - member.key = encodeKey(member.roomId, member.userId); - return this._roomMembersStore.put(member); - } + async set(member: MemberData): Promise { + // Object.assign would be more typesafe, but small objects + (member as any).key = encodeKey(member.roomId, member.userId); + return this._roomMembersStore.put(member as MemberStorageEntry); + } - getAll(roomId) { + getAll(roomId: string): Promise { const range = this._roomMembersStore.IDBKeyRange.lowerBound(encodeKey(roomId, "")); return this._roomMembersStore.selectWhile(range, member => { return member.roomId === roomId; }); } - async getAllUserIds(roomId) { - const userIds = []; + async getAllUserIds(roomId: string): Promise { + const userIds: string[] = []; const range = this._roomMembersStore.IDBKeyRange.lowerBound(encodeKey(roomId, "")); await this._roomMembersStore.iterateKeys(range, key => { - const decodedKey = decodeKey(key); + const decodedKey = decodeKey(key as string); // prevent running into the next room if (decodedKey.roomId === roomId) { userIds.push(decodedKey.userId); @@ -63,10 +78,10 @@ export class RoomMemberStore { return userIds; } - removeAllForRoom(roomId) { + removeAllForRoom(roomId: string): Promise { // exclude both keys as they are theoretical min and max, // but we should't have a match for just the room id, or room id with max const range = this._roomMembersStore.IDBKeyRange.bound(roomId, `${roomId}|${MAX_UNICODE}`, true, true); - this._roomMembersStore.delete(range); + return this._roomMembersStore.delete(range); } } From 7de704ef86d0d7d7cc3c5b7d36495f75cc228822 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 11 Aug 2021 12:03:06 -0700 Subject: [PATCH 05/11] Migrate InviteStore.js to TypeScript. --- src/matrix/storage/idb/Transaction.js | 2 +- .../stores/{InviteStore.js => InviteStore.ts} | 26 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) rename src/matrix/storage/idb/stores/{InviteStore.js => InviteStore.ts} (56%) diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index e8e52b11..72716fe5 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -19,7 +19,7 @@ import {StorageError} from "../common"; import {Store} from "./Store"; import {SessionStore} from "./stores/SessionStore"; import {RoomSummaryStore} from "./stores/RoomSummaryStore"; -import {InviteStore} from "./stores/InviteStore.js"; +import {InviteStore} from "./stores/InviteStore"; import {TimelineEventStore} from "./stores/TimelineEventStore.js"; import {TimelineRelationStore} from "./stores/TimelineRelationStore.js"; import {RoomStateStore} from "./stores/RoomStateStore.js"; diff --git a/src/matrix/storage/idb/stores/InviteStore.js b/src/matrix/storage/idb/stores/InviteStore.ts similarity index 56% rename from src/matrix/storage/idb/stores/InviteStore.js rename to src/matrix/storage/idb/stores/InviteStore.ts index b0eefe60..f8841a71 100644 --- a/src/matrix/storage/idb/stores/InviteStore.js +++ b/src/matrix/storage/idb/stores/InviteStore.ts @@ -13,21 +13,39 @@ 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. */ +import {Store} from "../Store"; +import {MemberData} from "./RoomMemberStore"; + +// TODO: Move to Invite when that's TypeScript. +export interface InviteData { + roomId: string; + isEncrypted: boolean; + isDirectMessage: boolean; + name?: string; + avatarUrl?: string; + avatarColorId: number; + canonicalAlias?: string; + timestamp: number; + joinRule: string; + inviter?: MemberData; +} export class InviteStore { - constructor(inviteStore) { + private _inviteStore: Store; + + constructor(inviteStore: Store) { this._inviteStore = inviteStore; } - getAll() { + getAll(): Promise { return this._inviteStore.selectAll(); } - set(invite) { + set(invite: InviteData): Promise { return this._inviteStore.put(invite); } - remove(roomId) { + remove(roomId: string): void { this._inviteStore.delete(roomId); } } From e3b1d034f0286a061869770f7cd2bc1896ca187e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 11 Aug 2021 13:33:25 -0700 Subject: [PATCH 06/11] Migrate TimelineEventStore.js to TypeScript --- src/matrix/storage/idb/Transaction.js | 2 +- ...ineEventStore.js => TimelineEventStore.ts} | 154 +++++++++++------- src/matrix/storage/types.ts | 28 ++++ 3 files changed, 121 insertions(+), 63 deletions(-) rename src/matrix/storage/idb/stores/{TimelineEventStore.js => TimelineEventStore.ts} (64%) create mode 100644 src/matrix/storage/types.ts diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index 72716fe5..a047a7a2 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -20,7 +20,7 @@ import {Store} from "./Store"; import {SessionStore} from "./stores/SessionStore"; import {RoomSummaryStore} from "./stores/RoomSummaryStore"; import {InviteStore} from "./stores/InviteStore"; -import {TimelineEventStore} from "./stores/TimelineEventStore.js"; +import {TimelineEventStore} from "./stores/TimelineEventStore"; import {TimelineRelationStore} from "./stores/TimelineRelationStore.js"; import {RoomStateStore} from "./stores/RoomStateStore.js"; import {RoomMemberStore} from "./stores/RoomMemberStore"; diff --git a/src/matrix/storage/idb/stores/TimelineEventStore.js b/src/matrix/storage/idb/stores/TimelineEventStore.ts similarity index 64% rename from src/matrix/storage/idb/stores/TimelineEventStore.js rename to src/matrix/storage/idb/stores/TimelineEventStore.ts index 8ff445f0..764b886b 100644 --- a/src/matrix/storage/idb/stores/TimelineEventStore.js +++ b/src/matrix/storage/idb/stores/TimelineEventStore.ts @@ -18,22 +18,49 @@ import {EventKey} from "../../../room/timeline/EventKey.js"; import { StorageError } from "../../common"; import { encodeUint32 } from "../utils"; import {KeyLimits} from "../../common"; +import {Store} from "../Store"; +import {RoomEvent, StateEvent} from "../../types"; -function encodeKey(roomId, fragmentId, eventIndex) { +interface Annotation { + count: number; + me: boolean; + firstTimestamp: number; +} + +interface StorageEntry { + roomId: string; + fragmentId: number; + eventIndex: number; + event: RoomEvent | StateEvent; + displayName?: string; + avatarUrl?: string; + annotations?: { [key : string]: Annotation }; + key: string; + eventIdKey: string; +} + +function encodeKey(roomId: string, fragmentId: number, eventIndex: number): string { return `${roomId}|${encodeUint32(fragmentId)}|${encodeUint32(eventIndex)}`; } -function encodeEventIdKey(roomId, eventId) { +function encodeEventIdKey(roomId: string, eventId: string): string { return `${roomId}|${eventId}`; } -function decodeEventIdKey(eventIdKey) { +function decodeEventIdKey(eventIdKey: string): { roomId: string, eventId: string } { const [roomId, eventId] = eventIdKey.split("|"); return {roomId, eventId}; } class Range { - constructor(IDBKeyRange, only, lower, upper, lowerOpen, upperOpen) { + private _IDBKeyRange: any; // TODO what's the appropriate representation here? + private _only?: EventKey; + private _lower?: EventKey; + private _upper?: EventKey; + private _lowerOpen: boolean; + private _upperOpen: boolean; + + constructor(IDBKeyRange: any, only?: EventKey, lower?: EventKey, upper?: EventKey, lowerOpen: boolean = false, upperOpen: boolean = false) { this._IDBKeyRange = IDBKeyRange; this._only = only; this._lower = lower; @@ -42,7 +69,7 @@ class Range { this._upperOpen = upperOpen; } - asIDBKeyRange(roomId) { + asIDBKeyRange(roomId: string): IDBKeyRange | undefined { try { // only if (this._only) { @@ -99,66 +126,68 @@ class Range { * @property {?Gap} gap if a gap entry, the gap */ export class TimelineEventStore { - constructor(timelineStore) { + private _timelineStore: Store; + + constructor(timelineStore: Store) { this._timelineStore = timelineStore; } /** Creates a range that only includes the given key - * @param {EventKey} eventKey the key - * @return {Range} the created range + * @param eventKey the key + * @return the created range */ - onlyRange(eventKey) { + onlyRange(eventKey: EventKey): Range { return new Range(this._timelineStore.IDBKeyRange, eventKey); } /** Creates a range that includes all keys before eventKey, and optionally also the key itself. - * @param {EventKey} eventKey the key - * @param {boolean} [open=false] whether the key is included (false) or excluded (true) from the range at the upper end. - * @return {Range} the created range + * @param eventKey the key + * @param [open=false] whether the key is included (false) or excluded (true) from the range at the upper end. + * @return the created range */ - upperBoundRange(eventKey, open=false) { + upperBoundRange(eventKey: EventKey, open=false): Range { return new Range(this._timelineStore.IDBKeyRange, undefined, undefined, eventKey, undefined, open); } /** Creates a range that includes all keys after eventKey, and optionally also the key itself. - * @param {EventKey} eventKey the key - * @param {boolean} [open=false] whether the key is included (false) or excluded (true) from the range at the lower end. - * @return {Range} the created range + * @param eventKey the key + * @param [open=false] whether the key is included (false) or excluded (true) from the range at the lower end. + * @return the created range */ - lowerBoundRange(eventKey, open=false) { + lowerBoundRange(eventKey: EventKey, open=false): Range { return new Range(this._timelineStore.IDBKeyRange, undefined, eventKey, undefined, open); } /** Creates a range that includes all keys between `lower` and `upper`, and optionally the given keys as well. - * @param {EventKey} lower the lower key - * @param {EventKey} upper the upper key - * @param {boolean} [lowerOpen=false] whether the lower key is included (false) or excluded (true) from the range. - * @param {boolean} [upperOpen=false] whether the upper key is included (false) or excluded (true) from the range. - * @return {Range} the created range + * @param lower the lower key + * @param upper the upper key + * @param [lowerOpen=false] whether the lower key is included (false) or excluded (true) from the range. + * @param [upperOpen=false] whether the upper key is included (false) or excluded (true) from the range. + * @return the created range */ - boundRange(lower, upper, lowerOpen=false, upperOpen=false) { + boundRange(lower: EventKey, upper: EventKey, lowerOpen=false, upperOpen=false): Range { return new Range(this._timelineStore.IDBKeyRange, undefined, lower, upper, lowerOpen, upperOpen); } /** Looks up the last `amount` entries in the timeline for `roomId`. - * @param {string} roomId - * @param {number} fragmentId - * @param {number} amount - * @return {Promise} a promise resolving to an array with 0 or more entries, in ascending order. + * @param roomId + * @param fragmentId + * @param amount + * @return a promise resolving to an array with 0 or more entries, in ascending order. */ - async lastEvents(roomId, fragmentId, amount) { + async lastEvents(roomId: string, fragmentId: number, amount: number): Promise { const eventKey = EventKey.maxKey; eventKey.fragmentId = fragmentId; return this.eventsBefore(roomId, eventKey, amount); } /** Looks up the first `amount` entries in the timeline for `roomId`. - * @param {string} roomId - * @param {number} fragmentId - * @param {number} amount - * @return {Promise} a promise resolving to an array with 0 or more entries, in ascending order. + * @param roomId + * @param fragmentId + * @param amount + * @return a promise resolving to an array with 0 or more entries, in ascending order. */ - async firstEvents(roomId, fragmentId, amount) { + async firstEvents(roomId: string, fragmentId: number, amount: number): Promise { const eventKey = EventKey.minKey; eventKey.fragmentId = fragmentId; return this.eventsAfter(roomId, eventKey, amount); @@ -166,24 +195,24 @@ export class TimelineEventStore { /** Looks up `amount` entries after `eventKey` in the timeline for `roomId` within the same fragment. * The entry for `eventKey` is not included. - * @param {string} roomId - * @param {EventKey} eventKey - * @param {number} amount - * @return {Promise} a promise resolving to an array with 0 or more entries, in ascending order. + * @param roomId + * @param eventKey + * @param amount + * @return a promise resolving to an array with 0 or more entries, in ascending order. */ - eventsAfter(roomId, eventKey, amount) { + eventsAfter(roomId: string, eventKey: EventKey, amount: number): Promise { const idbRange = this.lowerBoundRange(eventKey, true).asIDBKeyRange(roomId); return this._timelineStore.selectLimit(idbRange, amount); } /** Looks up `amount` entries before `eventKey` in the timeline for `roomId` within the same fragment. * The entry for `eventKey` is not included. - * @param {string} roomId - * @param {EventKey} eventKey - * @param {number} amount - * @return {Promise} a promise resolving to an array with 0 or more entries, in ascending order. + * @param roomId + * @param eventKey + * @param amount + * @return a promise resolving to an array with 0 or more entries, in ascending order. */ - async eventsBefore(roomId, eventKey, amount) { + async eventsBefore(roomId: string, eventKey: EventKey, amount: number): Promise { const range = this.upperBoundRange(eventKey, true).asIDBKeyRange(roomId); const events = await this._timelineStore.selectLimitReverse(range, amount); events.reverse(); // because we fetched them backwards @@ -195,23 +224,23 @@ export class TimelineEventStore { * * The order in which results are returned might be different than `eventIds`. * Call the return value to obtain the next {id, event} pair. - * @param {string} roomId - * @param {string[]} eventIds - * @return {Function} + * @param roomId + * @param eventIds + * @return */ // performance comment from above refers to the fact that there *might* // be a correlation between event_id sorting order and chronology. // In that case we could avoid running over all eventIds, as the reported order by findExistingKeys // would match the order of eventIds. That's why findLast is also passed as backwards to keysExist. // also passing them in chronological order makes sense as that's how we'll receive them almost always. - async findFirstOccurringEventId(roomId, eventIds) { + async findFirstOccurringEventId(roomId: string, eventIds: string[]): Promise { const byEventId = this._timelineStore.index("byEventId"); const keys = eventIds.map(eventId => encodeEventIdKey(roomId, eventId)); const results = new Array(keys.length); - let firstFoundKey; + let firstFoundKey: string | undefined; // find first result that is found and has no undefined results before it - function firstFoundAndPrecedingResolved() { + function firstFoundAndPrecedingResolved(): string | undefined { for(let i = 0; i < results.length; ++i) { if (results[i] === undefined) { return; @@ -222,7 +251,8 @@ export class TimelineEventStore { } await byEventId.findExistingKeys(keys, false, (key, found) => { - const index = keys.indexOf(key); + // T[].search(T, number), but we want T[].search(R, number), so cast + const index = (keys as IDBValidKey[]).indexOf(key); results[index] = found; firstFoundKey = firstFoundAndPrecedingResolved(); return !!firstFoundKey; @@ -231,38 +261,38 @@ export class TimelineEventStore { } /** Inserts a new entry into the store. The combination of roomId and eventKey should not exist yet, or an error is thrown. - * @param {Entry} entry the entry to insert - * @return {Promise<>} a promise resolving to undefined if the operation was successful, or a StorageError if not. + * @param entry the entry to insert + * @return a promise resolving to undefined if the operation was successful, or a StorageError if not. * @throws {StorageError} ... */ - insert(entry) { + insert(entry: StorageEntry): Promise { entry.key = encodeKey(entry.roomId, entry.fragmentId, entry.eventIndex); entry.eventIdKey = encodeEventIdKey(entry.roomId, entry.event.event_id); // TODO: map error? or in idb/store? - this._timelineStore.add(entry); + return this._timelineStore.add(entry); } /** Updates the entry into the store with the given [roomId, eventKey] combination. * If not yet present, will insert. Might be slower than add. - * @param {Entry} entry the entry to update. - * @return {Promise<>} a promise resolving to undefined if the operation was successful, or a StorageError if not. + * @param entry the entry to update. + * @return a promise resolving to undefined if the operation was successful, or a StorageError if not. */ - update(entry) { - this._timelineStore.put(entry); + update(entry: StorageEntry): Promise { + return this._timelineStore.put(entry); } - get(roomId, eventKey) { + get(roomId: string, eventKey: EventKey): Promise { return this._timelineStore.get(encodeKey(roomId, eventKey.fragmentId, eventKey.eventIndex)); } - getByEventId(roomId, eventId) { + getByEventId(roomId: string, eventId: string): Promise { return this._timelineStore.index("byEventId").get(encodeEventIdKey(roomId, eventId)); } - removeAllForRoom(roomId) { + removeAllForRoom(roomId: string): Promise { const minKey = encodeKey(roomId, KeyLimits.minStorageKey, KeyLimits.minStorageKey); const maxKey = encodeKey(roomId, KeyLimits.maxStorageKey, KeyLimits.maxStorageKey); const range = this._timelineStore.IDBKeyRange.bound(minKey, maxKey); - this._timelineStore.delete(range); + return this._timelineStore.delete(range); } } diff --git a/src/matrix/storage/types.ts b/src/matrix/storage/types.ts new file mode 100644 index 00000000..bc61e1f8 --- /dev/null +++ b/src/matrix/storage/types.ts @@ -0,0 +1,28 @@ +/* +Copyright 2021 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 type Content = { [key: string]: any } + +export interface RoomEvent { + content: Content; + type: string; + event_id: string; + sender: string; + origin_server_ts: number; + unsigned?: Content; +} + +export type StateEvent = RoomEvent & { prev_content?: Content, state_key: string } From 69953e5277594122c7cd02b33423d77101379a41 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 11 Aug 2021 14:07:44 -0700 Subject: [PATCH 07/11] Migrate TimelineRelationStore.js to TypeScript --- src/matrix/storage/idb/Transaction.js | 2 +- ...ationStore.js => TimelineRelationStore.ts} | 26 +++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) rename src/matrix/storage/idb/stores/{TimelineRelationStore.js => TimelineRelationStore.ts} (72%) diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index a047a7a2..71513730 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -21,7 +21,7 @@ import {SessionStore} from "./stores/SessionStore"; import {RoomSummaryStore} from "./stores/RoomSummaryStore"; import {InviteStore} from "./stores/InviteStore"; import {TimelineEventStore} from "./stores/TimelineEventStore"; -import {TimelineRelationStore} from "./stores/TimelineRelationStore.js"; +import {TimelineRelationStore} from "./stores/TimelineRelationStore"; import {RoomStateStore} from "./stores/RoomStateStore.js"; import {RoomMemberStore} from "./stores/RoomMemberStore"; import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js"; diff --git a/src/matrix/storage/idb/stores/TimelineRelationStore.js b/src/matrix/storage/idb/stores/TimelineRelationStore.ts similarity index 72% rename from src/matrix/storage/idb/stores/TimelineRelationStore.js rename to src/matrix/storage/idb/stores/TimelineRelationStore.ts index 5e802b65..0905eb0c 100644 --- a/src/matrix/storage/idb/stores/TimelineRelationStore.js +++ b/src/matrix/storage/idb/stores/TimelineRelationStore.ts @@ -14,30 +14,40 @@ 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"; -function encodeKey(roomId, targetEventId, relType, sourceEventId) { +function encodeKey(roomId: string, targetEventId: string, relType: string, sourceEventId: string): string { return `${roomId}|${targetEventId}|${relType}|${sourceEventId}`; } -function decodeKey(key) { +interface RelationEntry { + roomId: string; + targetEventId: string; + sourceEventId: string; + relType: string; +} + +function decodeKey(key: string): RelationEntry { const [roomId, targetEventId, relType, sourceEventId] = key.split("|"); return {roomId, targetEventId, relType, sourceEventId}; } export class TimelineRelationStore { - constructor(store) { + private _store: Store<{ key: string }>; + + constructor(store: Store<{ key: string }>) { this._store = store; } - add(roomId, targetEventId, relType, sourceEventId) { + add(roomId: string, targetEventId: string, relType: string, sourceEventId: string): Promise { return this._store.add({key: encodeKey(roomId, targetEventId, relType, sourceEventId)}); } - remove(roomId, targetEventId, relType, sourceEventId) { + remove(roomId: string, targetEventId: string, relType: string, sourceEventId: string): Promise { return this._store.delete(encodeKey(roomId, targetEventId, relType, sourceEventId)); } - removeAllForTarget(roomId, targetId) { + removeAllForTarget(roomId: string, targetId: string): Promise { const range = this._store.IDBKeyRange.bound( encodeKey(roomId, targetId, MIN_UNICODE, MIN_UNICODE), encodeKey(roomId, targetId, MAX_UNICODE, MAX_UNICODE), @@ -47,7 +57,7 @@ export class TimelineRelationStore { return this._store.delete(range); } - async getForTargetAndType(roomId, targetId, relType) { + async getForTargetAndType(roomId: string, targetId: string, relType: string): Promise { // exclude both keys as they are theoretical min and max, // but we should't have a match for just the room id, or room id with max const range = this._store.IDBKeyRange.bound( @@ -60,7 +70,7 @@ export class TimelineRelationStore { return items.map(i => decodeKey(i.key)); } - async getAllForTarget(roomId, targetId) { + async getAllForTarget(roomId: string, targetId: string): Promise { // exclude both keys as they are theoretical min and max, // but we should't have a match for just the room id, or room id with max const range = this._store.IDBKeyRange.bound( From ce20d40ff78ecbc4a3255e289900fcc9123b371c Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 31 Aug 2021 11:31:17 -0700 Subject: [PATCH 08/11] Revert the return-promise change --- src/matrix/storage/idb/stores/RoomMemberStore.ts | 4 ++-- src/matrix/storage/idb/stores/SessionStore.ts | 12 ++++++------ .../storage/idb/stores/TimelineEventStore.ts | 16 ++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/matrix/storage/idb/stores/RoomMemberStore.ts b/src/matrix/storage/idb/stores/RoomMemberStore.ts index 847e8dae..6abd897f 100644 --- a/src/matrix/storage/idb/stores/RoomMemberStore.ts +++ b/src/matrix/storage/idb/stores/RoomMemberStore.ts @@ -78,10 +78,10 @@ export class RoomMemberStore { return userIds; } - removeAllForRoom(roomId: string): Promise { + removeAllForRoom(roomId: string): void { // exclude both keys as they are theoretical min and max, // but we should't have a match for just the room id, or room id with max const range = this._roomMembersStore.IDBKeyRange.bound(roomId, `${roomId}|${MAX_UNICODE}`, true, true); - return this._roomMembersStore.delete(range); + this._roomMembersStore.delete(range); } } diff --git a/src/matrix/storage/idb/stores/SessionStore.ts b/src/matrix/storage/idb/stores/SessionStore.ts index bf3d6d11..4dc467fe 100644 --- a/src/matrix/storage/idb/stores/SessionStore.ts +++ b/src/matrix/storage/idb/stores/SessionStore.ts @@ -34,15 +34,15 @@ export class SessionStore { } } - set(key: string, value: any): Promise { - return this._sessionStore.put({key, value}); + set(key: string, value: any): void { + this._sessionStore.put({key, value}); } - add(key: string, value: any): Promise { - return this._sessionStore.add({key, value}); + add(key: string, value: any): void { + this._sessionStore.add({key, value}); } - remove(key: IDBValidKey): Promise { - return this._sessionStore.delete(key); + remove(key: IDBValidKey): void { + this._sessionStore.delete(key); } } diff --git a/src/matrix/storage/idb/stores/TimelineEventStore.ts b/src/matrix/storage/idb/stores/TimelineEventStore.ts index 764b886b..a479efd7 100644 --- a/src/matrix/storage/idb/stores/TimelineEventStore.ts +++ b/src/matrix/storage/idb/stores/TimelineEventStore.ts @@ -262,23 +262,23 @@ export class TimelineEventStore { /** Inserts a new entry into the store. The combination of roomId and eventKey should not exist yet, or an error is thrown. * @param entry the entry to insert - * @return a promise resolving to undefined if the operation was successful, or a StorageError if not. + * @return nothing. To wait for the operation to finish, await the transaction it's part of. * @throws {StorageError} ... */ - insert(entry: StorageEntry): Promise { + insert(entry: StorageEntry): void { entry.key = encodeKey(entry.roomId, entry.fragmentId, entry.eventIndex); entry.eventIdKey = encodeEventIdKey(entry.roomId, entry.event.event_id); // TODO: map error? or in idb/store? - return this._timelineStore.add(entry); + this._timelineStore.add(entry); } /** Updates the entry into the store with the given [roomId, eventKey] combination. * If not yet present, will insert. Might be slower than add. * @param entry the entry to update. - * @return a promise resolving to undefined if the operation was successful, or a StorageError if not. + * @return nothing. To wait for the operation to finish, await the transaction it's part of. */ - update(entry: StorageEntry): Promise { - return this._timelineStore.put(entry); + update(entry: StorageEntry): void { + this._timelineStore.put(entry); } get(roomId: string, eventKey: EventKey): Promise { @@ -289,10 +289,10 @@ export class TimelineEventStore { return this._timelineStore.index("byEventId").get(encodeEventIdKey(roomId, eventId)); } - removeAllForRoom(roomId: string): Promise { + removeAllForRoom(roomId: string): void { const minKey = encodeKey(roomId, KeyLimits.minStorageKey, KeyLimits.minStorageKey); const maxKey = encodeKey(roomId, KeyLimits.maxStorageKey, KeyLimits.maxStorageKey); const range = this._timelineStore.IDBKeyRange.bound(minKey, maxKey); - return this._timelineStore.delete(range); + this._timelineStore.delete(range); } } From 16d3ed579b0427566f0b704e6c57d851bc9ce8c2 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 31 Aug 2021 11:41:07 -0700 Subject: [PATCH 09/11] Reduce IDBValidKey returns to the public API --- src/matrix/storage/idb/Store.ts | 8 ++++---- src/matrix/storage/idb/schema.js | 2 +- src/matrix/storage/idb/stores/InviteStore.ts | 4 ++-- src/matrix/storage/idb/stores/RoomMemberStore.ts | 4 ++-- src/matrix/storage/idb/stores/RoomSummaryStore.ts | 4 ++-- src/matrix/storage/idb/stores/SessionStore.ts | 4 ++-- src/matrix/storage/idb/stores/TimelineRelationStore.ts | 4 ++-- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/matrix/storage/idb/Store.ts b/src/matrix/storage/idb/Store.ts index 8063a4c8..e2da0707 100644 --- a/src/matrix/storage/idb/Store.ts +++ b/src/matrix/storage/idb/Store.ts @@ -148,7 +148,7 @@ export class Store extends QueryTarget { return new QueryTarget(new QueryTargetWrapper(this._idbStore.index(indexName))); } - put(value: T): Promise { + put(value: T): void { // If this request fails, the error will bubble up to the transaction and abort it, // which is the behaviour we want. Therefore, it is ok to not create a promise for this // request and await it. @@ -159,12 +159,12 @@ export class Store extends QueryTarget { // // Note that this can still throw synchronously, like it does for TransactionInactiveError, // see https://www.w3.org/TR/IndexedDB-2/#transaction-lifetime-concept - return reqAsPromise(this._idbStore.put(value)); + this._idbStore.put(value); } - add(value: T): Promise { + add(value: T): void { // ok to not monitor result of request, see comment in `put`. - return reqAsPromise(this._idbStore.add(value)); + this._idbStore.add(value); } delete(keyOrKeyRange: IDBValidKey | IDBKeyRange): Promise { diff --git a/src/matrix/storage/idb/schema.js b/src/matrix/storage/idb/schema.js index f5472107..50e298d7 100644 --- a/src/matrix/storage/idb/schema.js +++ b/src/matrix/storage/idb/schema.js @@ -4,7 +4,7 @@ import {addRoomToIdentity} from "../../e2ee/DeviceTracker.js"; import {RoomMemberStore} from "./stores/RoomMemberStore"; import {SessionStore} from "./stores/SessionStore"; import {encodeScopeTypeKey} from "./stores/OperationStore.js"; -import {MAX_UNICODE} from "./stores/common.js"; +import {MAX_UNICODE} from "./stores/common"; // FUNCTIONS SHOULD ONLY BE APPENDED!! // the index in the array is the database version diff --git a/src/matrix/storage/idb/stores/InviteStore.ts b/src/matrix/storage/idb/stores/InviteStore.ts index f8841a71..7616ebf7 100644 --- a/src/matrix/storage/idb/stores/InviteStore.ts +++ b/src/matrix/storage/idb/stores/InviteStore.ts @@ -41,8 +41,8 @@ export class InviteStore { return this._inviteStore.selectAll(); } - set(invite: InviteData): Promise { - return this._inviteStore.put(invite); + set(invite: InviteData): void { + this._inviteStore.put(invite); } remove(roomId: string): void { diff --git a/src/matrix/storage/idb/stores/RoomMemberStore.ts b/src/matrix/storage/idb/stores/RoomMemberStore.ts index 6abd897f..e389bb77 100644 --- a/src/matrix/storage/idb/stores/RoomMemberStore.ts +++ b/src/matrix/storage/idb/stores/RoomMemberStore.ts @@ -50,10 +50,10 @@ export class RoomMemberStore { return this._roomMembersStore.get(encodeKey(roomId, userId)); } - async set(member: MemberData): Promise { + set(member: MemberData): void { // Object.assign would be more typesafe, but small objects (member as any).key = encodeKey(member.roomId, member.userId); - return this._roomMembersStore.put(member as MemberStorageEntry); + this._roomMembersStore.put(member as MemberStorageEntry); } getAll(roomId: string): Promise { diff --git a/src/matrix/storage/idb/stores/RoomSummaryStore.ts b/src/matrix/storage/idb/stores/RoomSummaryStore.ts index 19e847b1..bd911572 100644 --- a/src/matrix/storage/idb/stores/RoomSummaryStore.ts +++ b/src/matrix/storage/idb/stores/RoomSummaryStore.ts @@ -42,8 +42,8 @@ export class RoomSummaryStore { return this._summaryStore.selectAll(); } - set(summary: SummaryData): Promise { - return this._summaryStore.put(summary); + set(summary: SummaryData): void { + this._summaryStore.put(summary); } get(roomId: string): Promise { diff --git a/src/matrix/storage/idb/stores/SessionStore.ts b/src/matrix/storage/idb/stores/SessionStore.ts index 4dc467fe..859d3319 100644 --- a/src/matrix/storage/idb/stores/SessionStore.ts +++ b/src/matrix/storage/idb/stores/SessionStore.ts @@ -27,7 +27,7 @@ export class SessionStore { this._sessionStore = sessionStore; } - async get(key: IDBValidKey): Promise { + async get(key: string): Promise { const entry = await this._sessionStore.get(key); if (entry) { return entry.value; @@ -42,7 +42,7 @@ export class SessionStore { this._sessionStore.add({key, value}); } - remove(key: IDBValidKey): void { + remove(key: string): void { this._sessionStore.delete(key); } } diff --git a/src/matrix/storage/idb/stores/TimelineRelationStore.ts b/src/matrix/storage/idb/stores/TimelineRelationStore.ts index 0905eb0c..6772864a 100644 --- a/src/matrix/storage/idb/stores/TimelineRelationStore.ts +++ b/src/matrix/storage/idb/stores/TimelineRelationStore.ts @@ -39,8 +39,8 @@ export class TimelineRelationStore { this._store = store; } - add(roomId: string, targetEventId: string, relType: string, sourceEventId: string): Promise { - return this._store.add({key: encodeKey(roomId, targetEventId, relType, sourceEventId)}); + add(roomId: string, targetEventId: string, relType: string, sourceEventId: string): void { + this._store.add({key: encodeKey(roomId, targetEventId, relType, sourceEventId)}); } remove(roomId: string, targetEventId: string, relType: string, sourceEventId: string): Promise { From 056c7d40eb941696de5e42c0d9d9c137c86931e7 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 31 Aug 2021 12:10:36 -0700 Subject: [PATCH 10/11] Rename RoomEvent to TimelineEvent --- src/matrix/storage/idb/stores/TimelineEventStore.ts | 4 ++-- src/matrix/storage/types.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/matrix/storage/idb/stores/TimelineEventStore.ts b/src/matrix/storage/idb/stores/TimelineEventStore.ts index a479efd7..466f9737 100644 --- a/src/matrix/storage/idb/stores/TimelineEventStore.ts +++ b/src/matrix/storage/idb/stores/TimelineEventStore.ts @@ -19,7 +19,7 @@ import { StorageError } from "../../common"; import { encodeUint32 } from "../utils"; import {KeyLimits} from "../../common"; import {Store} from "../Store"; -import {RoomEvent, StateEvent} from "../../types"; +import {TimelineEvent, StateEvent} from "../../types"; interface Annotation { count: number; @@ -31,7 +31,7 @@ interface StorageEntry { roomId: string; fragmentId: number; eventIndex: number; - event: RoomEvent | StateEvent; + event: TimelineEvent | StateEvent; displayName?: string; avatarUrl?: string; annotations?: { [key : string]: Annotation }; diff --git a/src/matrix/storage/types.ts b/src/matrix/storage/types.ts index bc61e1f8..a03fa1d7 100644 --- a/src/matrix/storage/types.ts +++ b/src/matrix/storage/types.ts @@ -16,7 +16,7 @@ limitations under the License. export type Content = { [key: string]: any } -export interface RoomEvent { +export interface TimelineEvent { content: Content; type: string; event_id: string; @@ -25,4 +25,4 @@ export interface RoomEvent { unsigned?: Content; } -export type StateEvent = RoomEvent & { prev_content?: Content, state_key: string } +export type StateEvent = TimelineEvent & { prev_content?: Content, state_key: string } From 1fcc147da7a02b3847d2d21d217e9ac9e74ac62c Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 31 Aug 2021 12:16:16 -0700 Subject: [PATCH 11/11] Add type to the IDBKeyRange field --- src/matrix/storage/idb/stores/TimelineEventStore.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/matrix/storage/idb/stores/TimelineEventStore.ts b/src/matrix/storage/idb/stores/TimelineEventStore.ts index 466f9737..eb4c70cc 100644 --- a/src/matrix/storage/idb/stores/TimelineEventStore.ts +++ b/src/matrix/storage/idb/stores/TimelineEventStore.ts @@ -53,15 +53,15 @@ function decodeEventIdKey(eventIdKey: string): { roomId: string, eventId: string } class Range { - private _IDBKeyRange: any; // TODO what's the appropriate representation here? + private _IDBKeyRange: typeof IDBKeyRange; private _only?: EventKey; private _lower?: EventKey; private _upper?: EventKey; private _lowerOpen: boolean; private _upperOpen: boolean; - constructor(IDBKeyRange: any, only?: EventKey, lower?: EventKey, upper?: EventKey, lowerOpen: boolean = false, upperOpen: boolean = false) { - this._IDBKeyRange = IDBKeyRange; + constructor(_IDBKeyRange: any, only?: EventKey, lower?: EventKey, upper?: EventKey, lowerOpen: boolean = false, upperOpen: boolean = false) { + this._IDBKeyRange = _IDBKeyRange; this._only = only; this._lower = lower; this._upper = upper;