Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Handle multiple notifications with an empty room.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Feb 1, 2021
1 parent 6ea78e4 commit eafcb1d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
22 changes: 19 additions & 3 deletions synapse/push/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,25 @@ async def make_summary_text_from_member_events(
# are from explicitly to avoid, "messages in the Bob room"
sender_ids = {notif_events[n["event_id"]].sender for n in notifs}

member_events = await self.store.get_events(
[room_state_ids[("m.room.member", s)] for s in sender_ids]
)
# Attempt to get some names of the senders.
member_event_ids = [
room_state_ids[("m.room.member", s)]
for s in sender_ids
if ("m.room.member", s) in room_state_ids
]

if not member_event_ids:
# No member events were found! Maybe the room is empty?
# Fallback to the room ID (note that if there was a room name this
# would already have been used previously).
return self.email_subjects.messages_in_room % {
"room": room_id,
"app": self.app_name,
}

# Get the actual member events (in order to calculate a pretty name for
# the room).
member_events = await self.store.get_events(member_event_ids)

# There was a single sender.
if len(sender_ids) == 1:
Expand Down
32 changes: 30 additions & 2 deletions tests/push/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,18 @@ def test_simple_sends_email(self):
)
self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)

# The other user sends some messages
# The other user sends a single message.
self.helper.send(room, body="Hi!", tok=self.others[0].token)
self.helper.send(room, body="There!", tok=self.others[0].token)

# We should get emailed about that message
self._check_for_mail()

# The other user sends multiple messages.
self.helper.send(room, body="Hi!", tok=self.others[0].token)
self.helper.send(room, body="There!", tok=self.others[0].token)

self._check_for_mail()

def test_invite_sends_email(self):
# Create a room and invite the user to it
room = self.helper.create_room_as(self.others[0].id, tok=self.others[0].token)
Expand Down Expand Up @@ -236,6 +241,26 @@ def test_empty_room(self):
# We should get emailed about that message
self._check_for_mail()

def test_empty_room_multiple_messages(self):
"""All users leaving a room shouldn't cause the pusher to break."""
# Create a simple room with two users
room = self.helper.create_room_as(self.user_id, tok=self.access_token)
self.helper.invite(
room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
)
self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)

# The other user sends a single message.
self.helper.send(room, body="Hi!", tok=self.others[0].token)
self.helper.send(room, body="There!", tok=self.others[0].token)

# Leave the room before the message is processed.
self.helper.leave(room, self.user_id, tok=self.access_token)
self.helper.leave(room, self.others[0].id, tok=self.others[0].token)

# We should get emailed about that message
self._check_for_mail()

def test_encrypted_message(self):
room = self.helper.create_room_as(self.user_id, tok=self.access_token)
self.helper.invite(
Expand Down Expand Up @@ -288,3 +313,6 @@ def _check_for_mail(self):
pushers = list(pushers)
self.assertEqual(len(pushers), 1)
self.assertTrue(pushers[0].last_stream_ordering > last_stream_ordering)

# Reset the attempts.
self.email_attempts = []

0 comments on commit eafcb1d

Please sign in to comment.