diff --git a/src/stores/room-list/MessagePreviewStore.ts b/src/stores/room-list/MessagePreviewStore.ts index 62dfa6f0f1..647300f99c 100644 --- a/src/stores/room-list/MessagePreviewStore.ts +++ b/src/stores/room-list/MessagePreviewStore.ts @@ -32,6 +32,7 @@ import { UPDATE_EVENT } from "../AsyncStore"; import { IPreview } from "./previews/IPreview"; import { VoiceBroadcastInfoEventType } from "../../voice-broadcast"; import { VoiceBroadcastPreview } from "./previews/VoiceBroadcastPreview"; +import shouldHideEvent from "../../shouldHideEvent"; // Emitted event for when a room's preview has changed. First argument will the room for which // the change happened. @@ -184,21 +185,6 @@ export class MessagePreviewStore extends AsyncStoreWithClient { return previewDef?.previewer.getTextFor(event, undefined, true) ?? ""; } - private shouldSkipPreview(event: MatrixEvent, previousEvent?: MatrixEvent): boolean { - if (event.isRelation(RelationType.Replace)) { - if (previousEvent !== undefined) { - // Ignore edits if they don't apply to the latest event in the room to keep the preview on the latest event - const room = this.matrixClient?.getRoom(event.getRoomId()!); - const relatedEvent = room?.findEventById(event.relationEventId!); - if (relatedEvent !== previousEvent) { - return true; - } - } - } - - return false; - } - private async generatePreview(room: Room, tagId?: TagID): Promise { const events = [...room.getLiveTimeline().getEvents()]; @@ -221,8 +207,6 @@ export class MessagePreviewStore extends AsyncStoreWithClient { this.previews.set(room.roomId, map); } - const previousEventInAny = map.get(TAG_ANY)?.event; - // Set the tags so we know what to generate if (!map.has(TAG_ANY)) map.set(TAG_ANY, null); if (tagId && !map.has(tagId)) map.set(tagId, null); @@ -237,7 +221,8 @@ export class MessagePreviewStore extends AsyncStoreWithClient { const event = events[i]; await this.matrixClient?.decryptEventIfNeeded(event); - + const shouldHide = shouldHideEvent(event); + if (shouldHide) continue; const previewDef = PREVIEWS[event.getType()]; if (!previewDef) continue; if (previewDef.isState && isNullOrUndefined(event.getStateKey())) continue; @@ -245,16 +230,11 @@ export class MessagePreviewStore extends AsyncStoreWithClient { const anyPreviewText = previewDef.previewer.getTextFor(event); if (!anyPreviewText) continue; // not previewable for some reason - if (!this.shouldSkipPreview(event, previousEventInAny)) { - changed = changed || anyPreviewText !== map.get(TAG_ANY)?.text; - map.set(TAG_ANY, mkMessagePreview(anyPreviewText, event)); - } + changed = changed || anyPreviewText !== map.get(TAG_ANY)?.text; + map.set(TAG_ANY, mkMessagePreview(anyPreviewText, event)); const tagsToGenerate = Array.from(map.keys()).filter((t) => t !== TAG_ANY); // we did the any tag above for (const genTagId of tagsToGenerate) { - const previousEventInTag = map.get(genTagId)?.event; - if (this.shouldSkipPreview(event, previousEventInTag)) continue; - const realTagId = genTagId === TAG_ANY ? undefined : genTagId; const preview = previewDef.previewer.getTextFor(event, realTagId); diff --git a/test/stores/room-list/MessagePreviewStore-test.ts b/test/stores/room-list/MessagePreviewStore-test.ts index fb6207ed52..cbb045e6e9 100644 --- a/test/stores/room-list/MessagePreviewStore-test.ts +++ b/test/stores/room-list/MessagePreviewStore-test.ts @@ -126,6 +126,62 @@ describe("MessagePreviewStore", () => { ); }); + it("should not display a redacted edit", async () => { + const firstMessage = mkMessage({ + user: "@sender:server", + event: true, + room: room.roomId, + msg: "First message", + }); + await addEvent(store, room, firstMessage, false); + + expect((await store.getPreviewForRoom(room, DefaultTagID.Untagged))?.text).toMatchInlineSnapshot( + `"@sender:server: First message"`, + ); + + const secondMessage = mkMessage({ + user: "@sender:server", + event: true, + room: room.roomId, + msg: "Second message", + }); + await addEvent(store, room, secondMessage); + + expect((await store.getPreviewForRoom(room, DefaultTagID.Untagged))?.text).toMatchInlineSnapshot( + `"@sender:server: Second message"`, + ); + + const secondMessageEdit = mkEvent({ + event: true, + type: EventType.RoomMessage, + user: "@sender:server", + room: room.roomId, + content: { + "body": "* Second Message Edit", + "m.new_content": { + body: "Second Message Edit", + }, + "m.relates_to": { + rel_type: RelationType.Replace, + event_id: secondMessage.getId()!, + }, + }, + }); + await addEvent(store, room, secondMessageEdit); + + expect((await store.getPreviewForRoom(room, DefaultTagID.Untagged))?.text).toMatchInlineSnapshot( + `"@sender:server: Second Message Edit"`, + ); + + secondMessage.makeRedacted(secondMessage, room); + + await addEvent(store, room, secondMessage); + + expect((await store.getPreviewForRoom(room, DefaultTagID.Untagged))?.text).toMatchInlineSnapshot( + `"@sender:server: First message"`, + ); + }); + it("should ignore edits to unknown events", async () => { await expect(store.getPreviewForRoom(room, DefaultTagID.DM)).resolves.toBeNull();