Skip to content

Commit

Permalink
Don't send multiple emails on reply with mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomilov committed Feb 18, 2025
1 parent 121f58a commit 0fe53db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
21 changes: 11 additions & 10 deletions h/subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,30 @@ def publish_annotation_event(event):
@subscriber(AnnotationEvent)
def send_reply_notifications(event):
"""Queue any reply notification emails triggered by an annotation event."""

request = event.request

with request.tm:
annotation = request.find_service(AnnotationReadService).get_annotation_by_id(
event.annotation_id
)
notification = reply.get_notification(request, annotation, event.action)

if notification is None:
reply_notification = reply.get_notification(request, annotation, event.action)
if reply_notification is None:
return

send_params = emails.reply_notification.generate(request, notification)
mention_notifications = mention.get_notifications(
request, annotation, event.action
)
mentioned_users = {mention.mentioned_user for mention in mention_notifications}
if reply_notification.parent_user in mentioned_users:
return

send_params = emails.reply_notification.generate(
request, reply_notification
)
try:
mailer.send.delay(*send_params)
except OperationalError as err: # pragma: no cover
# We could not connect to rabbit! So carry on
report_exception(err)


Expand All @@ -102,14 +108,9 @@ def send_mention_notifications(event):
)
notifications = mention.get_notifications(request, annotation, event.action)

if not notifications:
return

for notification in notifications:
send_params = emails.mention_notification.generate(request, notification)

try:
mailer.send.delay(*send_params)
except OperationalError as err: # pragma: no cover
# We could not connect to rabbit! So carry on
report_exception(err)
14 changes: 14 additions & 0 deletions tests/unit/h/subscribers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ def test_it_fails_gracefully_if_the_task_does_not_queue(self, event, mailer):
# No explosions please
subscribers.send_reply_notifications(event)

def test_it_does_nothing_if_the_reply_user_is_mentioned(
self, event, reply, mailer, mention
):
reply_notification = mock.MagicMock()
reply.get_notification.return_value = reply_notification

mention_notifications = [mock.MagicMock()]
mention.get_notifications.return_value = mention_notifications
mention_notifications[0].mentioned_user = reply_notification.parent_user

subscribers.send_reply_notifications(event)

mailer.send.delay.assert_not_called()

@pytest.fixture
def event(self, pyramid_request):
return AnnotationEvent(pyramid_request, {"id": "any"}, "action")
Expand Down

0 comments on commit 0fe53db

Please sign in to comment.