diff options
Diffstat (limited to 'synapse/push')
-rw-r--r-- | synapse/push/emailpusher.py | 10 | ||||
-rw-r--r-- | synapse/push/httppusher.py | 3 | ||||
-rw-r--r-- | synapse/push/mailer.py | 72 | ||||
-rw-r--r-- | synapse/push/push_types.py | 136 |
4 files changed, 34 insertions, 187 deletions
diff --git a/synapse/push/emailpusher.py b/synapse/push/emailpusher.py index 4f13c0418a..cf5abdfbda 100644 --- a/synapse/push/emailpusher.py +++ b/synapse/push/emailpusher.py @@ -21,8 +21,6 @@ from twisted.internet.interfaces import IDelayedCall from synapse.metrics.background_process_metrics import run_as_background_process from synapse.push import Pusher, PusherConfig, PusherConfigException, ThrottleParams from synapse.push.mailer import Mailer -from synapse.push.push_types import EmailReason -from synapse.storage.databases.main.event_push_actions import EmailPushAction from synapse.util.threepids import validate_email if TYPE_CHECKING: @@ -192,7 +190,7 @@ class EmailPusher(Pusher): # we then consider all previously outstanding notifications # to be delivered. - reason: EmailReason = { + reason = { "room_id": push_action["room_id"], "now": self.clock.time_msec(), "received_at": received_at, @@ -277,7 +275,7 @@ class EmailPusher(Pusher): return may_send_at async def sent_notif_update_throttle( - self, room_id: str, notified_push_action: EmailPushAction + self, room_id: str, notified_push_action: dict ) -> None: # We have sent a notification, so update the throttle accordingly. # If the event that triggered the notif happened more than @@ -317,9 +315,7 @@ class EmailPusher(Pusher): self.pusher_id, room_id, self.throttle_params[room_id] ) - async def send_notification( - self, push_actions: List[EmailPushAction], reason: EmailReason - ) -> None: + async def send_notification(self, push_actions: List[dict], reason: dict) -> None: logger.info("Sending notif email for user %r", self.user_id) await self.mailer.send_notification_mail( diff --git a/synapse/push/httppusher.py b/synapse/push/httppusher.py index 3fa603ccb7..dbf4ad7f97 100644 --- a/synapse/push/httppusher.py +++ b/synapse/push/httppusher.py @@ -26,7 +26,6 @@ from synapse.events import EventBase from synapse.logging import opentracing from synapse.metrics.background_process_metrics import run_as_background_process from synapse.push import Pusher, PusherConfig, PusherConfigException -from synapse.storage.databases.main.event_push_actions import HttpPushAction from . import push_rule_evaluator, push_tools @@ -274,7 +273,7 @@ class HttpPusher(Pusher): ) break - async def _process_one(self, push_action: HttpPushAction) -> bool: + async def _process_one(self, push_action: dict) -> bool: if "notify" not in push_action["actions"]: return True diff --git a/synapse/push/mailer.py b/synapse/push/mailer.py index ba4f866487..ce299ba3da 100644 --- a/synapse/push/mailer.py +++ b/synapse/push/mailer.py @@ -14,7 +14,7 @@ import logging import urllib.parse -from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, TypeVar +from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, TypeVar import bleach import jinja2 @@ -28,14 +28,6 @@ from synapse.push.presentable_names import ( descriptor_from_member_events, name_from_member_event, ) -from synapse.push.push_types import ( - EmailReason, - MessageVars, - NotifVars, - RoomVars, - TemplateVars, -) -from synapse.storage.databases.main.event_push_actions import EmailPushAction from synapse.storage.state import StateFilter from synapse.types import StateMap, UserID from synapse.util.async_helpers import concurrently_execute @@ -143,7 +135,7 @@ class Mailer: % urllib.parse.urlencode(params) ) - template_vars: TemplateVars = {"link": link} + template_vars = {"link": link} await self.send_email( email_address, @@ -173,7 +165,7 @@ class Mailer: % urllib.parse.urlencode(params) ) - template_vars: TemplateVars = {"link": link} + template_vars = {"link": link} await self.send_email( email_address, @@ -204,7 +196,7 @@ class Mailer: % urllib.parse.urlencode(params) ) - template_vars: TemplateVars = {"link": link} + template_vars = {"link": link} await self.send_email( email_address, @@ -218,8 +210,8 @@ class Mailer: app_id: str, user_id: str, email_address: str, - push_actions: Iterable[EmailPushAction], - reason: EmailReason, + push_actions: Iterable[Dict[str, Any]], + reason: Dict[str, Any], ) -> None: """ Send email regarding a user's room notifications @@ -238,7 +230,7 @@ class Mailer: [pa["event_id"] for pa in push_actions] ) - notifs_by_room: Dict[str, List[EmailPushAction]] = {} + notifs_by_room: Dict[str, List[Dict[str, Any]]] = {} for pa in push_actions: notifs_by_room.setdefault(pa["room_id"], []).append(pa) @@ -266,7 +258,7 @@ class Mailer: # actually sort our so-called rooms_in_order list, most recent room first rooms_in_order.sort(key=lambda r: -(notifs_by_room[r][-1]["received_ts"] or 0)) - rooms: List[RoomVars] = [] + rooms: List[Dict[str, Any]] = [] for r in rooms_in_order: roomvars = await self._get_room_vars( @@ -297,7 +289,7 @@ class Mailer: notifs_by_room, state_by_room, notif_events, reason ) - template_vars: TemplateVars = { + template_vars = { "user_display_name": user_display_name, "unsubscribe_link": self._make_unsubscribe_link( user_id, app_id, email_address @@ -310,10 +302,10 @@ class Mailer: await self.send_email(email_address, summary_text, template_vars) async def send_email( - self, email_address: str, subject: str, extra_template_vars: TemplateVars + self, email_address: str, subject: str, extra_template_vars: Dict[str, Any] ) -> None: """Send an email with the given information and template text""" - template_vars: TemplateVars = { + template_vars = { "app_name": self.app_name, "server_name": self.hs.config.server.server_name, } @@ -335,10 +327,10 @@ class Mailer: self, room_id: str, user_id: str, - notifs: Iterable[EmailPushAction], + notifs: Iterable[Dict[str, Any]], notif_events: Dict[str, EventBase], room_state_ids: StateMap[str], - ) -> RoomVars: + ) -> Dict[str, Any]: """ Generate the variables for notifications on a per-room basis. @@ -364,7 +356,7 @@ class Mailer: room_name = await calculate_room_name(self.store, room_state_ids, user_id) - room_vars: RoomVars = { + room_vars: Dict[str, Any] = { "title": room_name, "hash": string_ordinal_total(room_id), # See sender avatar hash "notifs": [], @@ -425,11 +417,11 @@ class Mailer: async def _get_notif_vars( self, - notif: EmailPushAction, + notif: Dict[str, Any], user_id: str, notif_event: EventBase, room_state_ids: StateMap[str], - ) -> NotifVars: + ) -> Dict[str, Any]: """ Generate the variables for a single notification. @@ -450,7 +442,7 @@ class Mailer: after_limit=CONTEXT_AFTER, ) - ret: NotifVars = { + ret = { "link": self._make_notif_link(notif), "ts": notif["received_ts"], "messages": [], @@ -469,8 +461,8 @@ class Mailer: return ret async def _get_message_vars( - self, notif: EmailPushAction, event: EventBase, room_state_ids: StateMap[str] - ) -> Optional[MessageVars]: + self, notif: Dict[str, Any], event: EventBase, room_state_ids: StateMap[str] + ) -> Optional[Dict[str, Any]]: """ Generate the variables for a single event, if possible. @@ -502,9 +494,7 @@ class Mailer: if sender_state_event: sender_name = name_from_member_event(sender_state_event) - sender_avatar_url: Optional[str] = sender_state_event.content.get( - "avatar_url" - ) + sender_avatar_url = sender_state_event.content.get("avatar_url") else: # No state could be found, fallback to the MXID. sender_name = event.sender @@ -514,7 +504,7 @@ class Mailer: # sender_hash % the number of default images to choose from sender_hash = string_ordinal_total(event.sender) - ret: MessageVars = { + ret = { "event_type": event.type, "is_historical": event.event_id != notif["event_id"], "id": event.event_id, @@ -529,8 +519,6 @@ class Mailer: return ret msgtype = event.content.get("msgtype") - if not isinstance(msgtype, str): - msgtype = None ret["msgtype"] = msgtype @@ -545,7 +533,7 @@ class Mailer: return ret def _add_text_message_vars( - self, messagevars: MessageVars, event: EventBase + self, messagevars: Dict[str, Any], event: EventBase ) -> None: """ Potentially add a sanitised message body to the message variables. @@ -555,8 +543,8 @@ class Mailer: event: The event under consideration. """ msgformat = event.content.get("format") - if not isinstance(msgformat, str): - msgformat = None + + messagevars["format"] = msgformat formatted_body = event.content.get("formatted_body") body = event.content.get("body") @@ -567,7 +555,7 @@ class Mailer: messagevars["body_text_html"] = safe_text(body) def _add_image_message_vars( - self, messagevars: MessageVars, event: EventBase + self, messagevars: Dict[str, Any], event: EventBase ) -> None: """ Potentially add an image URL to the message variables. @@ -582,7 +570,7 @@ class Mailer: async def _make_summary_text_single_room( self, room_id: str, - notifs: List[EmailPushAction], + notifs: List[Dict[str, Any]], room_state_ids: StateMap[str], notif_events: Dict[str, EventBase], user_id: str, @@ -697,10 +685,10 @@ class Mailer: async def _make_summary_text( self, - notifs_by_room: Dict[str, List[EmailPushAction]], + notifs_by_room: Dict[str, List[Dict[str, Any]]], room_state_ids: Dict[str, StateMap[str]], notif_events: Dict[str, EventBase], - reason: EmailReason, + reason: Dict[str, Any], ) -> str: """ Make a summary text for the email when multiple rooms have notifications. @@ -730,7 +718,7 @@ class Mailer: async def _make_summary_text_from_member_events( self, room_id: str, - notifs: List[EmailPushAction], + notifs: List[Dict[str, Any]], room_state_ids: StateMap[str], notif_events: Dict[str, EventBase], ) -> str: @@ -817,7 +805,7 @@ class Mailer: base_url = "https://matrix.to/#" return "%s/%s" % (base_url, room_id) - def _make_notif_link(self, notif: EmailPushAction) -> str: + def _make_notif_link(self, notif: Dict[str, str]) -> str: """ Generate a link to open an event in the web client. diff --git a/synapse/push/push_types.py b/synapse/push/push_types.py deleted file mode 100644 index 8d16ab62ce..0000000000 --- a/synapse/push/push_types.py +++ /dev/null @@ -1,136 +0,0 @@ -# Copyright 2021 The Matrix.org Foundation C.I.C. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -from typing import List, Optional - -from typing_extensions import TypedDict - - -class EmailReason(TypedDict, total=False): - """ - Information on the event that triggered the email to be sent - - room_id: the ID of the room the event was sent in - now: timestamp in ms when the email is being sent out - room_name: a human-readable name for the room the event was sent in - received_at: the time in milliseconds at which the event was received - delay_before_mail_ms: the amount of time in milliseconds Synapse always waits - before ever emailing about a notification (to give the user a chance to respond - to other push or notice the window) - last_sent_ts: the time in milliseconds at which a notification was last sent - for an event in this room - throttle_ms: the minimum amount of time in milliseconds between two - notifications can be sent for this room - """ - - room_id: str - now: int - room_name: Optional[str] - received_at: int - delay_before_mail_ms: int - last_sent_ts: int - throttle_ms: int - - -class MessageVars(TypedDict, total=False): - """ - Details about a specific message to include in a notification - - event_type: the type of the event - is_historical: a boolean, which is `False` if the message is the one - that triggered the notification, `True` otherwise - id: the ID of the event - ts: the time in milliseconds at which the event was sent - sender_name: the display name for the event's sender - sender_avatar_url: the avatar URL (as a `mxc://` URL) for the event's - sender - sender_hash: a hash of the user ID of the sender - msgtype: the type of the message - body_text_html: html representation of the message - body_text_plain: plaintext representation of the message - image_url: mxc url of an image, when "msgtype" is "m.image" - """ - - event_type: str - is_historical: bool - id: str - ts: int - sender_name: str - sender_avatar_url: Optional[str] - sender_hash: int - msgtype: Optional[str] - body_text_html: str - body_text_plain: str - image_url: str - - -class NotifVars(TypedDict): - """ - Details about an event we are about to include in a notification - - link: a `matrix.to` link to the event - ts: the time in milliseconds at which the event was received - messages: a list of messages containing one message before the event, the - message in the event, and one message after the event. - """ - - link: str - ts: Optional[int] - messages: List[MessageVars] - - -class RoomVars(TypedDict): - """ - Represents a room containing events to include in the email. - - title: a human-readable name for the room - hash: a hash of the ID of the room - invite: a boolean, which is `True` if the room is an invite the user hasn't - accepted yet, `False` otherwise - notifs: a list of events, or an empty list if `invite` is `True`. - link: a `matrix.to` link to the room - avator_url: url to the room's avator - """ - - title: Optional[str] - hash: int - invite: bool - notifs: List[NotifVars] - link: str - avatar_url: Optional[str] - - -class TemplateVars(TypedDict, total=False): - """ - Generic structure for passing to the email sender, can hold all the fields used in email templates. - - app_name: name of the app/service this homeserver is associated with - server_name: name of our own homeserver - link: a link to include into the email to be sent - user_display_name: the display name for the user receiving the notification - unsubscribe_link: the link users can click to unsubscribe from email notifications - summary_text: a summary of the notification(s). The text used can be customised - by configuring the various settings in the `email.subjects` section of the - configuration file. - rooms: a list of rooms containing events to include in the email - reason: information on the event that triggered the email to be sent - """ - - app_name: str - server_name: str - link: str - user_display_name: str - unsubscribe_link: str - summary_text: str - rooms: List[RoomVars] - reason: EmailReason |