Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect prevEv being sent in ClientEvent.AccountData events #2794

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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