Skip to content

Commit

Permalink
Fix incorrect prevEv being sent in ClientEvent.AccountData events (#2794
Browse files Browse the repository at this point in the history
)
  • Loading branch information
t3chguy authored Oct 24, 2022
1 parent ade2e81 commit 9bdeea0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
40 changes: 40 additions & 0 deletions spec/integ/matrix-client-syncing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions src/sliding-sync-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -222,7 +222,7 @@ class ExtensionAccountData implements Extension {
const rules = accountDataEvent.getContent<IPushRules>();
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;
},
Expand Down
4 changes: 2 additions & 2 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -1111,7 +1111,7 @@ export class SyncApi {
const rules = accountDataEvent.getContent<IPushRules>();
client.pushRules = PushProcessor.rewriteDefaultRules(rules);
}
const prevEvent = prevEventsMap[accountDataEvent.getId()];
const prevEvent = prevEventsMap[accountDataEvent.getType()];
client.emit(ClientEvent.AccountData, accountDataEvent, prevEvent);
return accountDataEvent;
},
Expand Down

0 comments on commit 9bdeea0

Please sign in to comment.