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: remove polls from room state on redaction #3475

Merged
merged 2 commits into from
Jun 15, 2023
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
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