Skip to content

Commit

Permalink
Merge pull request #13 from cloudrac3r/no-spoilers-in-previews-2
Browse files Browse the repository at this point in the history
Do not display spoilers in message previews
  • Loading branch information
su-ex authored Apr 5, 2023
2 parents becb561 + 8876877 commit def6646
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/Notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import ToastStore from "./stores/ToastStore";
import { ElementCall } from "./models/Call";
import { VoiceBroadcastChunkEventType, VoiceBroadcastInfoEventType } from "./voice-broadcast";
import { getSenderName } from "./utils/event/getSenderName";
import { MessageEventPreview } from "./stores/room-list/previews/MessageEventPreview";

/*
* Dispatches:
Expand Down Expand Up @@ -113,6 +114,15 @@ class NotifierClass {
return TextForEvent.textForEvent(ev);
}

private getEventTextRepresentation(ev: MatrixEvent): string {
const previewer = new MessageEventPreview();
const msg = previewer.getTextFor(ev);
if (msg == null) {
return "";
}
return msg;
}

// XXX: exported for tests
public displayPopupNotification(ev: MatrixEvent, room: Room): void {
const plaf = PlatformPeg.get();
Expand All @@ -137,7 +147,7 @@ class NotifierClass {
// notificationMessageForEvent includes sender,
// but we already have the sender here
if (ev.getContent().body && !msgTypeHandlers.hasOwnProperty(ev.getContent().msgtype)) {
msg = ev.getContent().body;
msg = this.getEventTextRepresentation(ev);
}
} else if (ev.getType() === "m.room.member") {
// context is all in the message here, we don't need
Expand All @@ -148,7 +158,7 @@ class NotifierClass {
// notificationMessageForEvent includes sender,
// but we've just out sender in the title
if (ev.getContent().body && !msgTypeHandlers.hasOwnProperty(ev.getContent().msgtype)) {
msg = ev.getContent().body;
msg = this.getEventTextRepresentation(ev);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,7 @@
"View chat timeline": "View chat timeline",
"Room options": "Room options",
"Join Room": "Join Room",
"[spoiler]": "[spoiler]",
"(~%(count)s results)|other": "(~%(count)s results)",
"(~%(count)s results)|one": "(~%(count)s result)",
"Video rooms are a beta feature": "Video rooms are a beta feature",
Expand Down
7 changes: 4 additions & 3 deletions src/stores/room-list/previews/MessageEventPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { IPreview } from "./IPreview";
import { TagID } from "../models";
import { _t, sanitizeForTranslation } from "../../../languageHandler";
import { getSenderName, isSelf, shouldPrefixMessagesIn } from "./utils";
import { getHtmlText } from "../../../HtmlUtils";
import { stripHTMLReply, stripPlainReply } from "../../../utils/Reply";
import { VoiceBroadcastChunkEventType } from "../../../voice-broadcast/types";

Expand Down Expand Up @@ -61,9 +60,11 @@ export class MessageEventPreview implements IPreview {
}

if (hasHtml) {
const sanitised = getHtmlText(body.replace(/<br\/?>/gi, "\n")); // replace line breaks before removing them
const cleanedLines = body.replace(/<br\/?>/gi, "\n"); // replace line breaks before removing them
// run it through DOMParser to fixup encoded html entities
body = new DOMParser().parseFromString(sanitised, "text/html").documentElement.textContent;
const document = new DOMParser().parseFromString(cleanedLines, "text/html").documentElement;
document.querySelectorAll("[data-mx-spoiler]").forEach(spoiler => spoiler.textContent = _t("[spoiler]"));
body = document.textContent;
}

body = sanitizeForTranslation(body);
Expand Down

0 comments on commit def6646

Please sign in to comment.