Skip to content

Commit

Permalink
remove polls from room state on redaction (#3475)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry authored Jun 15, 2023
1 parent 22f0b78 commit f938d10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions spec/unit/room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import * as threadUtils from "../test-utils/thread";
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../test-utils/client";
import { logger } from "../../src/logger";
import { IMessageOpts } from "../test-utils/test-utils";
import { flushPromises } from "../test-utils/flushPromises";

describe("Room", function () {
const roomId = "!foo:bar";
Expand Down Expand Up @@ -3388,6 +3389,23 @@ describe("Room", function () {
const poll = room.polls.get(pollStartEventId);
expect(poll?.pollId).toBe(pollStartEventId);
});

it("removes poll from state when redacted", async () => {
const pollStartEvent = makePollStart("1");
const events = [pollStartEvent];

await room.processPollEvents(events);

expect(room.polls.get(pollStartEvent.getId()!)).toBeTruthy();

const redactedEvent = new MatrixEvent({ type: "m.room.redaction" });
pollStartEvent.makeRedacted(redactedEvent);

await flushPromises();

// removed from poll state
expect(room.polls.get(pollStartEvent.getId()!)).toBeFalsy();
});
});

describe("findPredecessorRoomId", () => {
Expand Down
5 changes: 5 additions & 0 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,11 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
const poll = new Poll(event, this.client, this);
this.polls.set(event.getId()!, poll);
this.emit(PollEvent.New, poll);

// remove the poll when redacted
event.once(MatrixEventEvent.BeforeRedaction, (redactedEvent: MatrixEvent) => {
this.polls.delete(redactedEvent.getId()!);
});
} catch {}
// poll creation can fail for malformed poll start events
return;
Expand Down

0 comments on commit f938d10

Please sign in to comment.