mirror of https://github.com/vector-im/riot-web
Fix edge case thread summaries around events without a msgtype (#8576)
* Fix edge case thread summaries around events without a msgtype * Remove incomprehensible commentpull/28217/head
parent
fc2d7b67d1
commit
8ae9f7d79a
|
@ -31,12 +31,16 @@ import { CallHangupEvent } from "./previews/CallHangupEvent";
|
||||||
import { StickerEventPreview } from "./previews/StickerEventPreview";
|
import { StickerEventPreview } from "./previews/StickerEventPreview";
|
||||||
import { ReactionEventPreview } from "./previews/ReactionEventPreview";
|
import { ReactionEventPreview } from "./previews/ReactionEventPreview";
|
||||||
import { UPDATE_EVENT } from "../AsyncStore";
|
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
|
// Emitted event for when a room's preview has changed. First argument will the room for which
|
||||||
// the change happened.
|
// the change happened.
|
||||||
const ROOM_PREVIEW_CHANGED = "room_preview_changed";
|
const ROOM_PREVIEW_CHANGED = "room_preview_changed";
|
||||||
|
|
||||||
const PREVIEWS = {
|
const PREVIEWS: Record<string, {
|
||||||
|
isState: boolean;
|
||||||
|
previewer: IPreview;
|
||||||
|
}> = {
|
||||||
'm.room.message': {
|
'm.room.message': {
|
||||||
isState: false,
|
isState: false,
|
||||||
previewer: new MessageEventPreview(),
|
previewer: new MessageEventPreview(),
|
||||||
|
@ -122,10 +126,7 @@ export class MessagePreviewStore extends AsyncStoreWithClient<IState> {
|
||||||
|
|
||||||
public generatePreviewForEvent(event: MatrixEvent): string {
|
public generatePreviewForEvent(event: MatrixEvent): string {
|
||||||
const previewDef = PREVIEWS[event.getType()];
|
const previewDef = PREVIEWS[event.getType()];
|
||||||
// TODO: Handle case where we don't have
|
return previewDef?.previewer.getTextFor(event, null, true) ?? "";
|
||||||
if (!previewDef) return '';
|
|
||||||
const previewText = previewDef.previewer.getTextFor(event, null, true);
|
|
||||||
return previewText ?? '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async generatePreview(room: Room, tagId?: TagID) {
|
private async generatePreview(room: Room, tagId?: TagID) {
|
||||||
|
|
|
@ -26,7 +26,8 @@ export interface IPreview {
|
||||||
* Gets the text which represents the event as a preview.
|
* Gets the text which represents the event as a preview.
|
||||||
* @param event The event to preview.
|
* @param event The event to preview.
|
||||||
* @param tagId Optional. The tag where the room the event was sent in resides.
|
* @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.
|
* @returns The preview.
|
||||||
*/
|
*/
|
||||||
getTextFor(event: MatrixEvent, tagId?: TagID): string | null;
|
getTextFor(event: MatrixEvent, tagId?: TagID, isThread?: boolean): string | null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
||||||
|
import { MsgType, RelationType } from "matrix-js-sdk/src/@types/event";
|
||||||
|
|
||||||
import { IPreview } from "./IPreview";
|
import { IPreview } from "./IPreview";
|
||||||
import { TagID } from "../models";
|
import { TagID } from "../models";
|
||||||
|
@ -27,16 +28,17 @@ export class MessageEventPreview implements IPreview {
|
||||||
public getTextFor(event: MatrixEvent, tagId?: TagID, isThread?: boolean): string {
|
public getTextFor(event: MatrixEvent, tagId?: TagID, isThread?: boolean): string {
|
||||||
let eventContent = event.getContent();
|
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
|
// It's an edit, generate the preview on the new text
|
||||||
eventContent = event.getContent()['m.new_content'];
|
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();
|
let body = eventContent['body'].trim();
|
||||||
const msgtype = eventContent['msgtype'];
|
if (!body) return null; // invalid event, no preview
|
||||||
if (!body || !msgtype) 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;
|
const hasHtml = eventContent.format === "org.matrix.custom.html" && eventContent.formatted_body;
|
||||||
if (hasHtml) {
|
if (hasHtml) {
|
||||||
|
@ -62,7 +64,7 @@ export class MessageEventPreview implements IPreview {
|
||||||
|
|
||||||
body = sanitizeForTranslation(body);
|
body = sanitizeForTranslation(body);
|
||||||
|
|
||||||
if (msgtype === 'm.emote') {
|
if (msgtype === MsgType.Emote) {
|
||||||
return _t("* %(senderName)s %(emote)s", { senderName: getSenderName(event), emote: body });
|
return _t("* %(senderName)s %(emote)s", { senderName: getSenderName(event), emote: body });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue