From de7e35609445d067f2e9dfe6f9dd37b56e8f8e66 Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Thu, 15 Jun 2023 13:47:11 +1200 Subject: [PATCH] remove polls from room state on redaction --- spec/unit/room.spec.ts | 18 ++++++++++++++++++ src/models/room.ts | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/spec/unit/room.spec.ts b/spec/unit/room.spec.ts index 2ed44dffd7b..150f2c30cb7 100644 --- a/spec/unit/room.spec.ts +++ b/spec/unit/room.spec.ts @@ -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"; @@ -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", () => { diff --git a/src/models/room.ts b/src/models/room.ts index 48c8b00ff1b..a73c750bad3 100644 --- a/src/models/room.ts +++ b/src/models/room.ts @@ -2008,6 +2008,11 @@ export class Room extends ReadReceipt { 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;