From 688f36b7f153b2fffd85bab428297d57a3a53997 Mon Sep 17 00:00:00 2001 From: miruka Date: Mon, 23 Mar 2020 15:39:14 -0400 Subject: [PATCH] Event: add [(text, link)] mentions attributes --- src/backend/html_markdown.py | 19 +++++++++++++++---- src/backend/models/items.py | 9 +++++---- src/backend/nio_callbacks.py | 6 +++++- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/backend/html_markdown.py b/src/backend/html_markdown.py index 8be111b9..fba588ec 100644 --- a/src/backend/html_markdown.py +++ b/src/backend/html_markdown.py @@ -3,7 +3,7 @@ """HTML and Markdown processing tools.""" import re -from typing import DefaultDict, Dict +from typing import DefaultDict, Dict, List, Tuple from urllib.parse import unquote import html_sanitizer.sanitizer as sanitizer @@ -138,6 +138,9 @@ class HTMLProcessor: user_id_regex, room_id_regex, room_alias_regex, ]] + link_is_matrix_to_regex = re.compile( + r"https?://matrix.to/#/.+", re.IGNORECASE, + ) link_is_user_id_regex = re.compile( r"https?://matrix.to/#/@.+", re.IGNORECASE, ) @@ -184,13 +187,21 @@ class HTMLProcessor: ] - def user_id_link_in_html(self, html: str, user_id: str) -> bool: + def mentions_in_html(self, html: str) -> List[Tuple[str, str]]: if not html.strip(): - return False + return [] + return [ + (a_tag.text, href) + for a_tag, _, href, _ in lxml.html.iterlinks(html) + if self.link_is_matrix_to_regex.match(unquote(href.strip())) + ] + + + def user_id_link_in_html(self, html: str, user_id: str) -> bool: regex = re.compile(rf"https?://matrix.to/#/{user_id}", re.IGNORECASE) - for _, _, href, _ in lxml.html.iterlinks(html): + for _, href in self.mentions_in_html(html): if regex.match(unquote(href.strip())): return True diff --git a/src/backend/models/items.py b/src/backend/models/items.py index 7732b267..4157b60a 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -175,10 +175,11 @@ class Event(ModelItem): sender_name: str = field() sender_avatar: str = field() - content: str = "" - inline_content: str = "" - reason: str = "" - links: List[str] = field(default_factory=list) + content: str = "" + inline_content: str = "" + reason: str = "" + links: List[str] = field(default_factory=list) + mentions: List[Tuple[str, str]] = field(default_factory=list) type_specifier: TypeSpecifier = TypeSpecifier.Unset diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index c9c7007f..38fd083d 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -105,7 +105,11 @@ class NioCallbacks: rooms = self.client.models[self.client.user_id, "rooms"] rooms[room.room_id].mentions += 1 - await self.client.register_nio_event(room, ev, content=co) + mention_list = HTML_PROCESSOR.mentions_in_html(co) + + await self.client.register_nio_event( + room, ev, content=co, mentions=mention_list, + ) async def onRoomMessageNotice(self, room, ev) -> None: