From b7ff56a7264fe9adad08e27e560574ba7731a6b7 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 25 Oct 2022 19:57:36 +0100 Subject: [PATCH] Add type assertions where we know an event is a room event (has room_id) (#9498) --- src/stores/widgets/StopGapWidget.ts | 5 +++-- src/stores/widgets/StopGapWidgetDriver.ts | 12 ++++++------ .../__snapshots__/StopGapWidgetDriver-test.ts.snap | 6 +++--- test/utils/location/isSelfLocation-test.ts | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/stores/widgets/StopGapWidget.ts b/src/stores/widgets/StopGapWidget.ts index aa1ad2c393..59653bf384 100644 --- a/src/stores/widgets/StopGapWidget.ts +++ b/src/stores/widgets/StopGapWidget.ts @@ -18,6 +18,7 @@ import { Room } from "matrix-js-sdk/src/models/room"; import { ClientWidgetApi, IModalWidgetOpenRequest, + IRoomEvent, IStickerActionRequest, IStickyActionRequest, ITemplateParams, @@ -465,7 +466,7 @@ export class StopGapWidget extends EventEmitter { private onToDeviceEvent = async (ev: MatrixEvent) => { await this.client.decryptEventIfNeeded(ev); if (ev.isDecryptionFailure()) return; - await this.messaging.feedToDevice(ev.getEffectiveEvent(), ev.isEncrypted()); + await this.messaging.feedToDevice(ev.getEffectiveEvent() as IRoomEvent, ev.isEncrypted()); }; private feedEvent(ev: MatrixEvent) { @@ -509,7 +510,7 @@ export class StopGapWidget extends EventEmitter { this.readUpToMap[ev.getRoomId()] = ev.getId(); const raw = ev.getEffectiveEvent(); - this.messaging.feedEvent(raw, this.eventListenerRoomId).catch(e => { + this.messaging.feedEvent(raw as IRoomEvent, this.eventListenerRoomId).catch(e => { logger.error("Error sending event to widget: ", e); }); } diff --git a/src/stores/widgets/StopGapWidgetDriver.ts b/src/stores/widgets/StopGapWidgetDriver.ts index e1fb1d6729..3a72193c3f 100644 --- a/src/stores/widgets/StopGapWidgetDriver.ts +++ b/src/stores/widgets/StopGapWidgetDriver.ts @@ -34,7 +34,7 @@ import { } from "matrix-widget-api"; import { ClientEvent, ITurnServer as IClientTurnServer } from "matrix-js-sdk/src/client"; import { EventType } from "matrix-js-sdk/src/@types/event"; -import { IContent, IEvent, MatrixEvent } from "matrix-js-sdk/src/models/event"; +import { IContent, MatrixEvent } from "matrix-js-sdk/src/models/event"; import { Room } from "matrix-js-sdk/src/models/room"; import { logger } from "matrix-js-sdk/src/logger"; import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread"; @@ -310,7 +310,7 @@ export class StopGapWidgetDriver extends WidgetDriver { limitPerRoom = limitPerRoom > 0 ? Math.min(limitPerRoom, Number.MAX_SAFE_INTEGER) : Number.MAX_SAFE_INTEGER; // relatively arbitrary const rooms = this.pickRooms(roomIds); - const allResults: IEvent[] = []; + const allResults: IRoomEvent[] = []; for (const room of rooms) { const results: MatrixEvent[] = []; const events = room.getLiveTimeline().getEvents(); // timelines are most recent last @@ -323,7 +323,7 @@ export class StopGapWidgetDriver extends WidgetDriver { results.push(ev); } - results.forEach(e => allResults.push(e.getEffectiveEvent())); + results.forEach(e => allResults.push(e.getEffectiveEvent() as IRoomEvent)); } return allResults; } @@ -337,7 +337,7 @@ export class StopGapWidgetDriver extends WidgetDriver { limitPerRoom = limitPerRoom > 0 ? Math.min(limitPerRoom, Number.MAX_SAFE_INTEGER) : Number.MAX_SAFE_INTEGER; // relatively arbitrary const rooms = this.pickRooms(roomIds); - const allResults: IEvent[] = []; + const allResults: IRoomEvent[] = []; for (const room of rooms) { const results: MatrixEvent[] = []; const state: Map = room.currentState.events.get(eventType); @@ -350,7 +350,7 @@ export class StopGapWidgetDriver extends WidgetDriver { } } - results.slice(0, limitPerRoom).forEach(e => allResults.push(e.getEffectiveEvent())); + results.slice(0, limitPerRoom).forEach(e => allResults.push(e.getEffectiveEvent() as IRoomEvent)); } return allResults; } @@ -459,7 +459,7 @@ export class StopGapWidgetDriver extends WidgetDriver { }); return { - chunk: events.map(e => e.getEffectiveEvent()), + chunk: events.map(e => e.getEffectiveEvent() as IRoomEvent), nextBatch, prevBatch, }; diff --git a/test/stores/widgets/__snapshots__/StopGapWidgetDriver-test.ts.snap b/test/stores/widgets/__snapshots__/StopGapWidgetDriver-test.ts.snap index 5f19dbb793..4c67524015 100644 --- a/test/stores/widgets/__snapshots__/StopGapWidgetDriver-test.ts.snap +++ b/test/stores/widgets/__snapshots__/StopGapWidgetDriver-test.ts.snap @@ -6,7 +6,7 @@ Array [ Array [ Object { "deviceInfo": DeviceInfo { - "algorithms": undefined, + "algorithms": Array [], "deviceId": "aliceWeb", "keys": Object {}, "known": false, @@ -18,7 +18,7 @@ Array [ }, Object { "deviceInfo": DeviceInfo { - "algorithms": undefined, + "algorithms": Array [], "deviceId": "aliceMobile", "keys": Object {}, "known": false, @@ -37,7 +37,7 @@ Array [ Array [ Object { "deviceInfo": DeviceInfo { - "algorithms": undefined, + "algorithms": Array [], "deviceId": "bobDesktop", "keys": Object {}, "known": false, diff --git a/test/utils/location/isSelfLocation-test.ts b/test/utils/location/isSelfLocation-test.ts index 6fafc5e467..cd1b3452a9 100644 --- a/test/utils/location/isSelfLocation-test.ts +++ b/test/utils/location/isSelfLocation-test.ts @@ -28,7 +28,7 @@ import { isSelfLocation } from "../../../src/utils/location"; describe("isSelfLocation", () => { it("Returns true for a full m.asset event", () => { - const content = makeLocationContent("", '0'); + const content = makeLocationContent("", '0', Date.now()); expect(isSelfLocation(content)).toBe(true); });