Make alerts and notification push rules-aware

This commit is contained in:
miruka 2020-09-16 10:53:29 -04:00
parent 69e414b31c
commit 4dacc3a12d
2 changed files with 35 additions and 30 deletions

View File

@ -3,7 +3,7 @@
"""HTML and Markdown processing tools."""
import re
from typing import DefaultDict, Dict, List, Optional, Tuple
from typing import Dict, List, Optional, Tuple
from urllib.parse import unquote
import html_sanitizer.sanitizer as sanitizer
@ -210,18 +210,6 @@ class HTMLProcessor:
]
def user_id_link_in_html(self, html: str, user_id: str) -> bool:
"""Return whether html contains a mention link for `user_id`."""
regex = re.compile(rf"https?://matrix.to/#/{user_id}", re.IGNORECASE)
for _, href in self.mentions_in_html(html):
if regex.match(unquote(href.strip())):
return True
return False
def from_markdown(
self,
text: str,

View File

@ -211,6 +211,9 @@ class MatrixClient(nio.AsyncClient):
self.unassigned_event_last_read_by: DefaultDict[str, Dict[str, int]] =\
DefaultDict(dict)
self.previous_server_unreads: Dict[str, int] = {}
self.previous_server_highlights: Dict[str, int] = {}
# {room_id: event}
self.power_level_events: Dict[str, nio.PowerLevelsEvent] = {}
@ -2101,6 +2104,37 @@ class MatrixClient(nio.AsyncClient):
item.id = f"echo-{tx_id}"
self.event_to_echo_ids[ev.event_id] = item.id
model[item.id] = item
await self.set_room_last_event(room.room_id, item)
if from_us or await self.event_is_past(ev):
return item
# Alerts & notifications
room_item = self.models[self.user_id, "rooms"][room.room_id]
unread = \
room_item.unreads and \
room_item.unreads != \
self.previous_server_unreads.get(room.room_id, 0)
highlight = \
room_item.highlights and \
room_item.highlights != \
self.previous_server_highlights.get(room.room_id, 0)
self.previous_server_unreads[room.room_id] = room_item.unreads
self.previous_server_highlights[room.room_id] = room_item.highlights
if highlight:
room_item.set_fields(local_unreads=True, local_highlights=True)
else:
room_item.local_unreads = True
if unread or highlight:
AlertRequested(high_importance=highlight)
notif_room = room.display_name
notif_sender = item.sender_name or item.sender_id
body_start = f"{notif_sender}: "
@ -2118,22 +2152,5 @@ class MatrixClient(nio.AsyncClient):
# ),
)
model[item.id] = item
await self.set_room_last_event(room.room_id, item)
# Alerts
if from_us or await self.event_is_past(ev):
return item
mentions_us = HTML.user_id_link_in_html(item.content, self.user_id)
AlertRequested(high_importance=mentions_us)
room_item = self.models[self.user_id, "rooms"][room.room_id]
room_item.local_unreads = True
if mentions_us:
room_item.local_highlights = True
await self.update_account_unread_counts()
return item