From 75e47fe1b4a66ed2d1e71a6a06d28db21b02d77e Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Mon, 27 Nov 2023 17:55:43 +0100 Subject: [PATCH 1/4] Add config to change the delay before sending a notification email --- changelog.d/16696.feature | 1 + docs/usage/configuration/config_documentation.md | 2 ++ synapse/config/emailconfig.py | 3 +++ synapse/push/emailpusher.py | 10 ++++------ 4 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 changelog.d/16696.feature diff --git a/changelog.d/16696.feature b/changelog.d/16696.feature new file mode 100644 index 000000000000..53d7b40f36ea --- /dev/null +++ b/changelog.d/16696.feature @@ -0,0 +1 @@ +Add a setting to be able to tweak the delay without interaction before an email is sent following a notification. diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index 7c4e742cd5d5..3c74984d9206 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -663,6 +663,8 @@ This setting has the following sub-options: has missed. Disabled by default. * `notif_for_new_users`: Set to false to disable automatic subscription to email notifications for new users. Enabled by default. +* `delay_before_mail`: Time we always wait before ever emailing about a notification + (to give the user a chance to respond to other push or notice the window). Defaults to 10 minutes. * `client_base_url`: Custom URL for client links within the email notifications. By default links will be based on "https://matrix.to". (This setting used to be called `riot_base_url`; the old name is still supported for backwards-compatibility but is now deprecated.) diff --git a/synapse/config/emailconfig.py b/synapse/config/emailconfig.py index a3af35b7c47b..c085b63cfe10 100644 --- a/synapse/config/emailconfig.py +++ b/synapse/config/emailconfig.py @@ -294,6 +294,9 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: self.email_riot_base_url = email_config.get( "client_base_url", email_config.get("riot_base_url", None) ) + self.delay_before_mail_ms = Config.parse_duration( + email_config.get("delay_before_mail", "10m") + ) if self.root.account_validity.account_validity_renew_by_email_enabled: expiry_template_html = email_config.get( diff --git a/synapse/push/emailpusher.py b/synapse/push/emailpusher.py index cf45fd09a84d..7c65e4634ef8 100644 --- a/synapse/push/emailpusher.py +++ b/synapse/push/emailpusher.py @@ -30,10 +30,6 @@ logger = logging.getLogger(__name__) -# The amount of time we always wait before ever emailing about a notification -# (to give the user a chance to respond to other push or notice the window) -DELAY_BEFORE_MAIL_MS = 10 * 60 * 1000 - # THROTTLE is the minimum time between mail notifications sent for a given room. # Each room maintains its own throttle counter, but each new mail notification # sends the pending notifications for all rooms. @@ -80,6 +76,8 @@ def __init__(self, hs: "HomeServer", pusher_config: PusherConfig, mailer: Mailer except ValueError: raise PusherConfigException("Invalid email") + self._delay_before_mail_ms = self.hs.config.email.delay_before_mail_ms + def on_started(self, should_check_for_notifs: bool) -> None: """Called when this pusher has been started. @@ -180,7 +178,7 @@ async def _unsafe_process(self) -> None: received_at = push_action.received_ts if received_at is None: received_at = 0 - notif_ready_at = received_at + DELAY_BEFORE_MAIL_MS + notif_ready_at = received_at + self._delay_before_mail_ms room_ready_at = self.room_ready_to_notify_at(push_action.room_id) @@ -196,7 +194,7 @@ async def _unsafe_process(self) -> None: "room_id": push_action.room_id, "now": self.clock.time_msec(), "received_at": received_at, - "delay_before_mail_ms": DELAY_BEFORE_MAIL_MS, + "delay_before_mail_ms": self._delay_before_mail_ms, "last_sent_ts": self.get_room_last_sent_ts(push_action.room_id), "throttle_ms": self.get_room_throttle_ms(push_action.room_id), } From abd2bf371ec0ec7d76453ed75006817bb3fd8c84 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Thu, 7 Dec 2023 10:47:46 +0100 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Patrick Cloke --- docs/usage/configuration/config_documentation.md | 7 +++++-- synapse/config/emailconfig.py | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index 3c74984d9206..491abef0a75c 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -663,8 +663,11 @@ This setting has the following sub-options: has missed. Disabled by default. * `notif_for_new_users`: Set to false to disable automatic subscription to email notifications for new users. Enabled by default. -* `delay_before_mail`: Time we always wait before ever emailing about a notification - (to give the user a chance to respond to other push or notice the window). Defaults to 10 minutes. +* `delay_before_mail`: The time to wait before emailing about a notification. + This gives the user a chance to view the message via push or an open client. + Defaults to 10 minutes. + + *New in Synapse 1.98.0._ * `client_base_url`: Custom URL for client links within the email notifications. By default links will be based on "https://matrix.to". (This setting used to be called `riot_base_url`; the old name is still supported for backwards-compatibility but is now deprecated.) diff --git a/synapse/config/emailconfig.py b/synapse/config/emailconfig.py index c085b63cfe10..5f2723c22f3f 100644 --- a/synapse/config/emailconfig.py +++ b/synapse/config/emailconfig.py @@ -294,6 +294,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: self.email_riot_base_url = email_config.get( "client_base_url", email_config.get("riot_base_url", None) ) + # The amount of time we always wait before ever emailing about a notification + # (to give the user a chance to respond to other push or notice the window) self.delay_before_mail_ms = Config.parse_duration( email_config.get("delay_before_mail", "10m") ) From 651b2ab7efd745c32f33ef253c4635511cf63db2 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Thu, 7 Dec 2023 11:14:10 +0100 Subject: [PATCH 3/4] Change to notif_delay_before_mail --- docs/usage/configuration/config_documentation.md | 4 ++-- synapse/config/emailconfig.py | 4 ++-- synapse/push/emailpusher.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index 71d911c42ef7..55e2a30fab41 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -680,11 +680,11 @@ This setting has the following sub-options: has missed. Disabled by default. * `notif_for_new_users`: Set to false to disable automatic subscription to email notifications for new users. Enabled by default. -* `delay_before_mail`: The time to wait before emailing about a notification. +* `notif_delay_before_mail`: The time to wait before emailing about a notification. This gives the user a chance to view the message via push or an open client. Defaults to 10 minutes. - *New in Synapse 1.98.0._ + _New in Synapse 1.99.0._ * `client_base_url`: Custom URL for client links within the email notifications. By default links will be based on "https://matrix.to". (This setting used to be called `riot_base_url`; the old name is still supported for backwards-compatibility but is now deprecated.) diff --git a/synapse/config/emailconfig.py b/synapse/config/emailconfig.py index 5f2723c22f3f..e33791fab98d 100644 --- a/synapse/config/emailconfig.py +++ b/synapse/config/emailconfig.py @@ -296,8 +296,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: ) # The amount of time we always wait before ever emailing about a notification # (to give the user a chance to respond to other push or notice the window) - self.delay_before_mail_ms = Config.parse_duration( - email_config.get("delay_before_mail", "10m") + self.notif_delay_before_mail_ms = Config.parse_duration( + email_config.get("notif_delay_before_mail", "10m") ) if self.root.account_validity.account_validity_renew_by_email_enabled: diff --git a/synapse/push/emailpusher.py b/synapse/push/emailpusher.py index 7c65e4634ef8..f72284d09edf 100644 --- a/synapse/push/emailpusher.py +++ b/synapse/push/emailpusher.py @@ -76,7 +76,7 @@ def __init__(self, hs: "HomeServer", pusher_config: PusherConfig, mailer: Mailer except ValueError: raise PusherConfigException("Invalid email") - self._delay_before_mail_ms = self.hs.config.email.delay_before_mail_ms + self._delay_before_mail_ms = self.hs.config.email.notif_delay_before_mail_ms def on_started(self, should_check_for_notifs: bool) -> None: """Called when this pusher has been started. From 77015366111f2b8ed8f259ebb487fd92c430a490 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Thu, 7 Dec 2023 11:16:43 +0100 Subject: [PATCH 4/4] Change THROTTLE_START_MS to just be the delay --- synapse/push/emailpusher.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/synapse/push/emailpusher.py b/synapse/push/emailpusher.py index f72284d09edf..be7631e8d0a2 100644 --- a/synapse/push/emailpusher.py +++ b/synapse/push/emailpusher.py @@ -33,7 +33,6 @@ # THROTTLE is the minimum time between mail notifications sent for a given room. # Each room maintains its own throttle counter, but each new mail notification # sends the pending notifications for all rooms. -THROTTLE_START_MS = 10 * 60 * 1000 THROTTLE_MAX_MS = 24 * 60 * 60 * 1000 # 24h # THROTTLE_MULTIPLIER = 6 # 10 mins, 1 hour, 6 hours, 24 hours THROTTLE_MULTIPLIER = 144 # 10 mins, 24 hours - i.e. jump straight to 1 day @@ -298,10 +297,10 @@ async def sent_notif_update_throttle( current_throttle_ms = self.get_room_throttle_ms(room_id) if gap > THROTTLE_RESET_AFTER_MS: - new_throttle_ms = THROTTLE_START_MS + new_throttle_ms = self._delay_before_mail_ms else: if current_throttle_ms == 0: - new_throttle_ms = THROTTLE_START_MS + new_throttle_ms = self._delay_before_mail_ms else: new_throttle_ms = min( current_throttle_ms * THROTTLE_MULTIPLIER, THROTTLE_MAX_MS