From 9bdeea0a8d0b1c95c3f9baf45e5d890dc78b58cc Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 24 Oct 2022 15:30:55 +0100 Subject: [PATCH] Fix incorrect prevEv being sent in ClientEvent.AccountData events (#2794) --- spec/integ/matrix-client-syncing.spec.ts | 40 ++++++++++++++++++++++++ src/sliding-sync-sdk.ts | 4 +-- src/sync.ts | 4 +-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/spec/integ/matrix-client-syncing.spec.ts b/spec/integ/matrix-client-syncing.spec.ts index 635c6fc94ed..3609e8a0f3d 100644 --- a/spec/integ/matrix-client-syncing.spec.ts +++ b/spec/integ/matrix-client-syncing.spec.ts @@ -1602,6 +1602,46 @@ describe("MatrixClient syncing", () => { }); }); + describe("user account data", () => { + it("should include correct prevEv in the ClientEvent.AccountData emit", async () => { + const eventA1 = new MatrixEvent({ type: "a", content: { body: "1" } }); + const eventA2 = new MatrixEvent({ type: "a", content: { body: "2" } }); + const eventB1 = new MatrixEvent({ type: "b", content: { body: "1" } }); + const eventB2 = new MatrixEvent({ type: "b", content: { body: "2" } }); + + client!.store.storeAccountDataEvents([eventA1, eventB1]); + const fn = jest.fn(); + client!.on(ClientEvent.AccountData, fn); + + httpBackend!.when("GET", "/sync").respond(200, { + next_batch: "batch_token", + rooms: {}, + presence: {}, + account_data: { + events: [eventA2.event, eventB2.event], + }, + }); + + await Promise.all([ + client!.startClient(), + httpBackend!.flushAllExpected(), + ]); + + const eventA = client?.getAccountData("a"); + expect(eventA).not.toBe(eventA1); + const eventB = client?.getAccountData("b"); + expect(eventB).not.toBe(eventB1); + + expect(fn).toHaveBeenCalledWith(eventA, eventA1); + expect(fn).toHaveBeenCalledWith(eventB, eventB1); + + expect(eventA?.getContent().body).toBe("2"); + expect(eventB?.getContent().body).toBe("2"); + + client!.off(ClientEvent.AccountData, fn); + }); + }); + /** * waits for the MatrixClient to emit one or more 'sync' events. * diff --git a/src/sliding-sync-sdk.ts b/src/sliding-sync-sdk.ts index b5810601076..1c0bd63401e 100644 --- a/src/sliding-sync-sdk.ts +++ b/src/sliding-sync-sdk.ts @@ -208,7 +208,7 @@ class ExtensionAccountData implements Extension { private processGlobalAccountData(globalAccountData: object[]): void { const events = mapEvents(this.client, undefined, globalAccountData); const prevEventsMap = events.reduce((m, c) => { - m[c.getId()] = this.client.store.getAccountData(c.getType()); + m[c.getType()] = this.client.store.getAccountData(c.getType()); return m; }, {}); this.client.store.storeAccountDataEvents(events); @@ -222,7 +222,7 @@ class ExtensionAccountData implements Extension { const rules = accountDataEvent.getContent(); this.client.pushRules = PushProcessor.rewriteDefaultRules(rules); } - const prevEvent = prevEventsMap[accountDataEvent.getId()]; + const prevEvent = prevEventsMap[accountDataEvent.getType()]; this.client.emit(ClientEvent.AccountData, accountDataEvent, prevEvent); return accountDataEvent; }, diff --git a/src/sync.ts b/src/sync.ts index 1669f2d54dc..f655d247646 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -1097,7 +1097,7 @@ export class SyncApi { if (Array.isArray(data.account_data?.events)) { const events = data.account_data.events.map(client.getEventMapper()); const prevEventsMap = events.reduce((m, c) => { - m[c.getId()] = client.store.getAccountData(c.getType()); + m[c.getType()] = client.store.getAccountData(c.getType()); return m; }, {}); client.store.storeAccountDataEvents(events); @@ -1111,7 +1111,7 @@ export class SyncApi { const rules = accountDataEvent.getContent(); client.pushRules = PushProcessor.rewriteDefaultRules(rules); } - const prevEvent = prevEventsMap[accountDataEvent.getId()]; + const prevEvent = prevEventsMap[accountDataEvent.getType()]; client.emit(ClientEvent.AccountData, accountDataEvent, prevEvent); return accountDataEvent; },