Fix edge case thread summaries around events without a msgtype (#8576)

* Fix edge case thread summaries around events without a msgtype

* Remove incomprehensible comment
pull/28217/head
Michael Telatynski 2022-05-16 09:11:50 +01:00 committed by GitHub
parent fc2d7b67d1
commit 8ae9f7d79a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 12 deletions

View File

@ -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(),
@ -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) {

View File

@ -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;
}

View File

@ -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 });
}