From b5f16468ce7550d049be8bc6cd5d68136012b522 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 4 Aug 2021 15:31:25 -0700 Subject: [PATCH] Add a flag to strip replies --- .../session/room/timeline/deserialize.js | 19 ++++++++++++++----- .../session/room/timeline/tiles/TextTile.js | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/domain/session/room/timeline/deserialize.js b/src/domain/session/room/timeline/deserialize.js index d7f81316..12d3d80d 100644 --- a/src/domain/session/room/timeline/deserialize.js +++ b/src/domain/session/room/timeline/deserialize.js @@ -34,7 +34,8 @@ const baseUrl = 'https://matrix.to'; const linkPrefix = `${baseUrl}/#/`; class Deserializer { - constructor(result, mediaRepository) { + constructor(result, mediaRepository, allowReplies) { + this.allowReplies = allowReplies; this.result = result; this.mediaRepository = mediaRepository; } @@ -287,6 +288,10 @@ class Deserializer { return true; } + _isAllowedNode(node) { + return this.allowReplies || !this._ensureElement(node, "MX-REPLY"); + } + _parseInlineNodes(nodes, into) { for (const htmlNode of nodes) { if (this._parseTextParts(htmlNode, into)) { @@ -301,7 +306,9 @@ class Deserializer { } // Node is either block or unrecognized. In // both cases, just move on to its children. - this._parseInlineNodes(this.result.getChildNodes(htmlNode), into); + if (this._isAllowedNode(htmlNode)) { + this._parseInlineNodes(this.result.getChildNodes(htmlNode), into); + } } } @@ -325,7 +332,9 @@ class Deserializer { continue; } // Node is unrecognized. Just move on to its children. - this._parseAnyNodes(this.result.getChildNodes(htmlNode), into); + if (this._isAllowedNode(htmlNode)) { + this._parseAnyNodes(this.result.getChildNodes(htmlNode), into); + } } } @@ -336,9 +345,9 @@ class Deserializer { } } -export function parseHTMLBody(platform, mediaRepository, html) { +export function parseHTMLBody(platform, mediaRepository, allowReplies, html) { const parseResult = platform.parseHTML(html); - const deserializer = new Deserializer(parseResult, mediaRepository); + const deserializer = new Deserializer(parseResult, mediaRepository, allowReplies); const parts = deserializer.parseAnyNodes(parseResult.rootNodes); return new MessageBody(html, parts); } diff --git a/src/domain/session/room/timeline/tiles/TextTile.js b/src/domain/session/room/timeline/tiles/TextTile.js index ffd06c1a..3dc830a2 100644 --- a/src/domain/session/room/timeline/tiles/TextTile.js +++ b/src/domain/session/room/timeline/tiles/TextTile.js @@ -54,7 +54,7 @@ export class TextTile extends BaseTextTile { _parseBody(body, format) { if (format === BodyFormat.Html) { - return parseHTMLBody(this.platform, this._mediaRepository, body); + return parseHTMLBody(this.platform, this._mediaRepository, this._entry.isReply, body); } else { return parsePlainBody(body); }