Event: add [(text, link)] mentions attributes

This commit is contained in:
miruka 2020-03-23 15:39:14 -04:00
parent 8e7cd7bde9
commit 688f36b7f1
3 changed files with 25 additions and 9 deletions

View File

@ -3,7 +3,7 @@
"""HTML and Markdown processing tools.""" """HTML and Markdown processing tools."""
import re import re
from typing import DefaultDict, Dict from typing import DefaultDict, Dict, List, Tuple
from urllib.parse import unquote from urllib.parse import unquote
import html_sanitizer.sanitizer as sanitizer import html_sanitizer.sanitizer as sanitizer
@ -138,6 +138,9 @@ class HTMLProcessor:
user_id_regex, room_id_regex, room_alias_regex, 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( link_is_user_id_regex = re.compile(
r"https?://matrix.to/#/@.+", re.IGNORECASE, 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(): 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) 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())): if regex.match(unquote(href.strip())):
return True return True

View File

@ -179,6 +179,7 @@ class Event(ModelItem):
inline_content: str = "" inline_content: str = ""
reason: str = "" reason: str = ""
links: List[str] = field(default_factory=list) links: List[str] = field(default_factory=list)
mentions: List[Tuple[str, str]] = field(default_factory=list)
type_specifier: TypeSpecifier = TypeSpecifier.Unset type_specifier: TypeSpecifier = TypeSpecifier.Unset

View File

@ -105,7 +105,11 @@ class NioCallbacks:
rooms = self.client.models[self.client.user_id, "rooms"] rooms = self.client.models[self.client.user_id, "rooms"]
rooms[room.room_id].mentions += 1 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: async def onRoomMessageNotice(self, room, ev) -> None: