From 8ae9f7d79a91cf57fc8704004e6c1260fd4e4175 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 16 May 2022 09:11:50 +0100 Subject: [PATCH] Fix edge case thread summaries around events without a msgtype (#8576) * Fix edge case thread summaries around events without a msgtype * Remove incomprehensible comment --- src/stores/room-list/MessagePreviewStore.ts | 11 ++++++----- src/stores/room-list/previews/IPreview.ts | 3 ++- .../room-list/previews/MessageEventPreview.ts | 14 ++++++++------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/stores/room-list/MessagePreviewStore.ts b/src/stores/room-list/MessagePreviewStore.ts index 4ab7f96ff6..2fa0abc70d 100644 --- a/src/stores/room-list/MessagePreviewStore.ts +++ b/src/stores/room-list/MessagePreviewStore.ts @@ -31,12 +31,16 @@ import { CallHangupEvent } from "./previews/CallHangupEvent"; import { StickerEventPreview } from "./previews/StickerEventPreview"; import { ReactionEventPreview } from "./previews/ReactionEventPreview"; import { UPDATE_EVENT } from "../AsyncStore"; +import { IPreview } from "./previews/IPreview"; // Emitted event for when a room's preview has changed. First argument will the room for which // the change happened. const ROOM_PREVIEW_CHANGED = "room_preview_changed"; -const PREVIEWS = { +const PREVIEWS: Record = { 'm.room.message': { isState: false, previewer: new MessageEventPreview(), @@ -122,10 +126,7 @@ export class MessagePreviewStore extends AsyncStoreWithClient { public generatePreviewForEvent(event: MatrixEvent): string { const previewDef = PREVIEWS[event.getType()]; - // TODO: Handle case where we don't have - if (!previewDef) return ''; - const previewText = previewDef.previewer.getTextFor(event, null, true); - return previewText ?? ''; + return previewDef?.previewer.getTextFor(event, null, true) ?? ""; } private async generatePreview(room: Room, tagId?: TagID) { diff --git a/src/stores/room-list/previews/IPreview.ts b/src/stores/room-list/previews/IPreview.ts index 6049d9f8a2..f45476dd2f 100644 --- a/src/stores/room-list/previews/IPreview.ts +++ b/src/stores/room-list/previews/IPreview.ts @@ -26,7 +26,8 @@ export interface IPreview { * Gets the text which represents the event as a preview. * @param event The event to preview. * @param tagId Optional. The tag where the room the event was sent in resides. + * @param isThread Optional. Whether the preview being generated is for a thread summary. * @returns The preview. */ - getTextFor(event: MatrixEvent, tagId?: TagID): string | null; + getTextFor(event: MatrixEvent, tagId?: TagID, isThread?: boolean): string | null; } diff --git a/src/stores/room-list/previews/MessageEventPreview.ts b/src/stores/room-list/previews/MessageEventPreview.ts index 4212f421e0..72b513be5f 100644 --- a/src/stores/room-list/previews/MessageEventPreview.ts +++ b/src/stores/room-list/previews/MessageEventPreview.ts @@ -15,6 +15,7 @@ limitations under the License. */ import { MatrixEvent } from "matrix-js-sdk/src/models/event"; +import { MsgType, RelationType } from "matrix-js-sdk/src/@types/event"; import { IPreview } from "./IPreview"; import { TagID } from "../models"; @@ -27,16 +28,17 @@ export class MessageEventPreview implements IPreview { public getTextFor(event: MatrixEvent, tagId?: TagID, isThread?: boolean): string { let eventContent = event.getContent(); - if (event.isRelation("m.replace")) { + if (event.isRelation(RelationType.Replace)) { // It's an edit, generate the preview on the new text eventContent = event.getContent()['m.new_content']; } - if (!eventContent || !eventContent['body']) return null; // invalid for our purposes + if (!eventContent?.['body']) return null; // invalid for our purposes - let body = (eventContent['body'] || '').trim(); - const msgtype = eventContent['msgtype']; - if (!body || !msgtype) return null; // invalid event, no preview + let body = eventContent['body'].trim(); + if (!body) return null; // invalid event, no preview + // A msgtype is actually required in the spec but the app is a bit softer on this requirement + const msgtype = eventContent['msgtype'] ?? MsgType.Text; const hasHtml = eventContent.format === "org.matrix.custom.html" && eventContent.formatted_body; if (hasHtml) { @@ -62,7 +64,7 @@ export class MessageEventPreview implements IPreview { body = sanitizeForTranslation(body); - if (msgtype === 'm.emote') { + if (msgtype === MsgType.Emote) { return _t("* %(senderName)s %(emote)s", { senderName: getSenderName(event), emote: body }); }