From 6592b2c2051bd6413e8ffcf1882f626e65b2628d Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 28 Nov 2022 10:58:38 +0000 Subject: [PATCH] sonarcloud --- spec/integ/sliding-sync-sdk.spec.ts | 59 +++++++++++++---------------- src/sliding-sync-sdk.ts | 53 +++++++++++++------------- 2 files changed, 53 insertions(+), 59 deletions(-) diff --git a/spec/integ/sliding-sync-sdk.spec.ts b/spec/integ/sliding-sync-sdk.spec.ts index 6945404dd..72a7eeaa2 100644 --- a/spec/integ/sliding-sync-sdk.spec.ts +++ b/spec/integ/sliding-sync-sdk.spec.ts @@ -932,6 +932,27 @@ describe("SlidingSyncSdk", () => { describe("ExtensionReceipts", () => { let ext: Extension; + const generateReceiptResponse = ( + userId: string, roomId: string, eventId: string, recType: string, ts: number, + ) => { + return { + rooms: { + [roomId]: { + type: EventType.Receipt, + content: { + [eventId]: { + [recType]: { + [userId]: { + ts: ts, + }, + }, + }, + }, + }, + }, + }; + }; + beforeAll(async () => { await setupClient(); const hasSynced = sdk!.sync(); @@ -973,22 +994,9 @@ describe("SlidingSyncSdk", () => { const room = client!.getRoom(roomId)!; expect(room).toBeDefined(); expect(room.getReadReceiptForUserId(alice, true)).toBeNull(); - ext.onResponse({ - rooms: { - [roomId]: { - type: EventType.Receipt, - content: { - [lastEvent.event_id]: { - "m.read": { - [alice]: { - ts: 1234567, - }, - }, - }, - }, - }, - }, - }); + ext.onResponse( + generateReceiptResponse(alice, roomId, lastEvent.event_id, "m.read", 1234567), + ); const receipt = room.getReadReceiptForUserId(alice); expect(receipt).toBeDefined(); expect(receipt?.eventId).toEqual(lastEvent.event_id); @@ -1000,22 +1008,9 @@ describe("SlidingSyncSdk", () => { const roomId = "!room:id"; const alice = "@alice:alice"; const eventId = "$something"; - ext.onResponse({ - rooms: { - [roomId]: { - type: EventType.Receipt, - content: { - [eventId]: { - "m.read": { - [alice]: { - ts: 1234567, - }, - }, - }, - }, - }, - }, - }); + ext.onResponse( + generateReceiptResponse(alice, roomId, eventId, "m.read", 1234567), + ); // we expect it not to crash }); }); diff --git a/src/sliding-sync-sdk.ts b/src/sliding-sync-sdk.ts index 5dc10bb6f..ae6000667 100644 --- a/src/sliding-sync-sdk.ts +++ b/src/sliding-sync-sdk.ts @@ -253,22 +253,15 @@ class ExtensionTyping implements Extension { }; } - public onResponse(data: {rooms: Record}): void { + public onResponse(data: {rooms: Record}): void { if (!data || !data.rooms) { return; } for (const roomId in data.rooms) { - const ephemeralEvents = mapEvents(this.client, roomId, [data.rooms[roomId]]); - const room = this.client.getRoom(roomId); - if (!room) { - logger.warn("got typing events for room but room doesn't exist on client:", roomId); - continue; - } - room.addEphemeralEvents(ephemeralEvents); - ephemeralEvents.forEach((e) => { - this.client.emit(ClientEvent.Event, e); - }); + processEphemeralEvents( + this.client, roomId, [data.rooms[roomId]], + ); } } } @@ -285,30 +278,23 @@ class ExtensionReceipts implements Extension { } public onRequest(isInitial: boolean): object | undefined { - if (!isInitial) { - return undefined; + if (isInitial) { + return { + enabled: true, + }; } - return { - enabled: true, - }; + return undefined; } - public onResponse(data: {rooms: Record}): void { + public onResponse(data: {rooms: Record}): void { if (!data || !data.rooms) { return; } for (const roomId in data.rooms) { - const ephemeralEvents = mapEvents(this.client, roomId, [data.rooms[roomId]]); - const room = this.client.getRoom(roomId); - if (!room) { - logger.warn("got receipts for room but room doesn't exist on client:", roomId); - continue; - } - room.addEphemeralEvents(ephemeralEvents); - ephemeralEvents.forEach((e) => { - this.client.emit(ClientEvent.Event, e); - }); + processEphemeralEvents( + this.client, roomId, [data.rooms[roomId]], + ); } } } @@ -970,3 +956,16 @@ function mapEvents(client: MatrixClient, roomId: string | undefined, events: obj return mapper(e); }); } + +function processEphemeralEvents(client: MatrixClient, roomId: string, ephEvents: IMinimalEvent[]) { + const ephemeralEvents = mapEvents(client, roomId, ephEvents); + const room = client.getRoom(roomId); + if (!room) { + logger.warn("got ephemeral events for room but room doesn't exist on client:", roomId); + return; + } + room.addEphemeralEvents(ephemeralEvents); + ephemeralEvents.forEach((e) => { + client.emit(ClientEvent.Event, e); + }); +}