From 334065895fcbc078345f5fac3859604f6ff8766b Mon Sep 17 00:00:00 2001 From: miruka Date: Sat, 10 Apr 2021 04:37:29 -0400 Subject: [PATCH] Open links keybinds: ignore message mentions When using the open_links_files(_externally) keybinds on a message, ignore the URLs that mentions (user ID, username, room ID, room alias) point to. --- src/backend/models/items.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/backend/models/items.py b/src/backend/models/items.py index 29807d78..38b89964 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -398,15 +398,24 @@ class Event(ModelItem): @staticmethod def parse_links(text: str) -> List[str]: - """Return list of URLs (`` tags) present in the text.""" + """Return list of URLs (`` tags) present in the content.""" ignore = [] - if "" in text: + if "" in text or "mention" in text: parser = lxml.html.etree.HTMLParser() - tree = lxml.etree.fromstring(text, parser) # nosec - xpath = "//mx-reply/blockquote/a[count(preceding-sibling::*)<=1]" - ignore = [lxml.etree.tostring(el) for el in tree.xpath(xpath)] + tree = lxml.etree.fromstring(text, parser) + ignore = [ + lxml.etree.tostring(matching_element) + for ugly_disgusting_xpath in [ + # Match mx-reply > blockquote > second a (user ID link) + "//mx-reply/blockquote/a[count(preceding-sibling::*)<=1]", + # Match tags with a mention class + '//a[contains(concat(" ",normalize-space(@class)," ")' + '," mention ")]', + ] + for matching_element in tree.xpath(ugly_disgusting_xpath) + ] if not text.strip(): return []