Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Fix edge case thread summaries around events without a msgtype (#8576)
Browse files Browse the repository at this point in the history
* Fix edge case thread summaries around events without a msgtype

* Remove incomprehensible comment
  • Loading branch information
t3chguy committed May 16, 2022
1 parent fc2d7b6 commit 8ae9f7d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/stores/room-list/MessagePreviewStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, {
isState: boolean;
previewer: IPreview;
}> = {
'm.room.message': {
isState: false,
previewer: new MessageEventPreview(),
Expand Down Expand Up @@ -122,10 +126,7 @@ export class MessagePreviewStore extends AsyncStoreWithClient<IState> {

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) {
Expand Down
3 changes: 2 additions & 1 deletion src/stores/room-list/previews/IPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
14 changes: 8 additions & 6 deletions src/stores/room-list/previews/MessageEventPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) {
Expand All @@ -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 });
}

Expand Down

0 comments on commit 8ae9f7d

Please sign in to comment.