Skip to content

Commit

Permalink
decrypt poll relations before processing (#3148)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry authored Feb 14, 2023
1 parent 108f157 commit cdd7dbb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spec/unit/models/poll.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import { M_POLL_END, M_POLL_KIND_DISCLOSED, M_POLL_RESPONSE } from "../../../src
import { PollStartEvent } from "../../../src/extensible_events_v1/PollStartEvent";
import { Poll } from "../../../src/models/poll";
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../test-utils/client";
import { flushPromises } from "../../test-utils/flushPromises";

jest.useFakeTimers();

describe("Poll", () => {
const userId = "@alice:server.org";
const mockClient = getMockClientWithEventEmitter({
...mockClientMethodsUser(userId),
decryptEventIfNeeded: jest.fn().mockResolvedValue(true),
relations: jest.fn(),
});
const roomId = "!room:server";
Expand Down Expand Up @@ -172,6 +174,8 @@ describe("Poll", () => {
jest.spyOn(poll, "emit");
const responses = await poll.getResponses();

await flushPromises();

expect(mockClient.relations.mock.calls).toEqual([
[roomId, basePollStartEvent.getId(), "m.reference", undefined, { from: undefined }],
[roomId, basePollStartEvent.getId(), "m.reference", undefined, { from: "test-next-1" }],
Expand Down
3 changes: 3 additions & 0 deletions src/models/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@ export class Poll extends TypedEventEmitter<Exclude<PollEvent, PollEvent.New>, P
},
);

await Promise.all(allRelations.events.map((event) => this.matrixClient.decryptEventIfNeeded(event)));

const responses =
this.responses ||
new Relations("m.reference", M_POLL_RESPONSE.name, this.matrixClient, [M_POLL_RESPONSE.altName!]);

const pollEndEvent = allRelations.events.find((event) => M_POLL_END.matches(event.getType()));

if (this.validateEndEvent(pollEndEvent)) {
this.endEvent = pollEndEvent;
this.refilterResponsesOnEnd();
Expand Down

0 comments on commit cdd7dbb

Please sign in to comment.