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

Commit

Permalink
Do not assume a member is still in the room when generating email not…
Browse files Browse the repository at this point in the history
…ifications.
  • Loading branch information
clokep committed Feb 1, 2021
1 parent 8fee6a3 commit 6ea78e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/9257.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix long-standing KeyError in synapse.push.mailer in get_message_vars.
13 changes: 9 additions & 4 deletions synapse/push/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,15 @@ async def get_message_vars(
if event.type != EventTypes.Message and event.type != EventTypes.Encrypted:
return None

sender_state_event_id = room_state_ids[("m.room.member", event.sender)]
sender_state_event = await self.store.get_event(sender_state_event_id)
sender_name = name_from_member_event(sender_state_event)
sender_avatar_url = sender_state_event.content.get("avatar_url")
sender_state_event_id = room_state_ids.get(("m.room.member", event.sender))
if sender_state_event_id:
sender_state_event = await self.store.get_event(sender_state_event_id)
sender_name = name_from_member_event(sender_state_event)
sender_avatar_url = sender_state_event.content.get("avatar_url")
else:
# The sender is no longer in the room for some reason.
sender_name = event.sender
sender_avatar_url = None

# 'hash' for deterministically picking default images: use
# sender_hash % the number of default images to choose from
Expand Down
19 changes: 19 additions & 0 deletions tests/push/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,25 @@ def test_multiple_rooms(self):
# We should get emailed about those messages
self._check_for_mail()

def test_empty_room(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)

# 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

0 comments on commit 6ea78e4

Please sign in to comment.