diff options
author | Mark Haines <mark.haines@matrix.org> | 2016-08-26 14:35:31 +0100 |
---|---|---|
committer | Mark Haines <mark.haines@matrix.org> | 2016-08-26 14:35:31 +0100 |
commit | 4bbef62124f0fb249e314e94a1b9c15204c8daa9 (patch) | |
tree | cc2e7458da160915f7224c0487f185e2433eaadb /synapse/push/mailer.py | |
parent | More 0_0 in tests (diff) | |
parent | Merge pull request #1048 from matrix-org/erikj/fix_mail_name (diff) | |
download | synapse-4bbef62124f0fb249e314e94a1b9c15204c8daa9.tar.xz |
Merge remote-tracking branch 'origin/develop' into markjh/direct_to_device
Diffstat (limited to 'synapse/push/mailer.py')
-rw-r--r-- | synapse/push/mailer.py | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/synapse/push/mailer.py b/synapse/push/mailer.py index 1028731bc9..81b1af4a19 100644 --- a/synapse/push/mailer.py +++ b/synapse/push/mailer.py @@ -22,7 +22,7 @@ from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from synapse.util.async import concurrently_execute -from synapse.util.presentable_names import ( +from synapse.push.presentable_names import ( calculate_room_name, name_from_member_event, descriptor_from_member_events ) from synapse.types import UserID @@ -139,7 +139,7 @@ class Mailer(object): @defer.inlineCallbacks def _fetch_room_state(room_id): - room_state = yield self.state_handler.get_current_state(room_id) + room_state = yield self.state_handler.get_current_state_ids(room_id) state_by_room[room_id] = room_state # Run at most 3 of these at once: sync does 10 at a time but email @@ -159,11 +159,12 @@ class Mailer(object): ) rooms.append(roomvars) - reason['room_name'] = calculate_room_name( - state_by_room[reason['room_id']], user_id, fallback_to_members=True + reason['room_name'] = yield calculate_room_name( + self.store, state_by_room[reason['room_id']], user_id, + fallback_to_members=True ) - summary_text = self.make_summary_text( + summary_text = yield self.make_summary_text( notifs_by_room, state_by_room, notif_events, user_id, reason ) @@ -203,12 +204,15 @@ class Mailer(object): ) @defer.inlineCallbacks - def get_room_vars(self, room_id, user_id, notifs, notif_events, room_state): - my_member_event = room_state[("m.room.member", user_id)] + def get_room_vars(self, room_id, user_id, notifs, notif_events, room_state_ids): + my_member_event_id = room_state_ids[("m.room.member", user_id)] + my_member_event = yield self.store.get_event(my_member_event_id) is_invite = my_member_event.content["membership"] == "invite" + room_name = yield calculate_room_name(self.store, room_state_ids, user_id) + room_vars = { - "title": calculate_room_name(room_state, user_id), + "title": room_name, "hash": string_ordinal_total(room_id), # See sender avatar hash "notifs": [], "invite": is_invite, @@ -218,7 +222,7 @@ class Mailer(object): if not is_invite: for n in notifs: notifvars = yield self.get_notif_vars( - n, user_id, notif_events[n['event_id']], room_state + n, user_id, notif_events[n['event_id']], room_state_ids ) # merge overlapping notifs together. @@ -243,7 +247,7 @@ class Mailer(object): defer.returnValue(room_vars) @defer.inlineCallbacks - def get_notif_vars(self, notif, user_id, notif_event, room_state): + def get_notif_vars(self, notif, user_id, notif_event, room_state_ids): results = yield self.store.get_events_around( notif['room_id'], notif['event_id'], before_limit=CONTEXT_BEFORE, after_limit=CONTEXT_AFTER @@ -261,17 +265,19 @@ class Mailer(object): the_events.append(notif_event) for event in the_events: - messagevars = self.get_message_vars(notif, event, room_state) + messagevars = yield self.get_message_vars(notif, event, room_state_ids) if messagevars is not None: ret['messages'].append(messagevars) defer.returnValue(ret) - def get_message_vars(self, notif, event, room_state): + @defer.inlineCallbacks + def get_message_vars(self, notif, event, room_state_ids): if event.type != EventTypes.Message: - return None + return - sender_state_event = room_state[("m.room.member", event.sender)] + sender_state_event_id = room_state_ids[("m.room.member", event.sender)] + sender_state_event = yield 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") @@ -299,7 +305,7 @@ class Mailer(object): if "body" in event.content: ret["body_text_plain"] = event.content["body"] - return ret + defer.returnValue(ret) def add_text_message_vars(self, messagevars, event): msgformat = event.content.get("format") @@ -321,6 +327,7 @@ class Mailer(object): return messagevars + @defer.inlineCallbacks def make_summary_text(self, notifs_by_room, state_by_room, notif_events, user_id, reason): if len(notifs_by_room) == 1: @@ -330,7 +337,7 @@ class Mailer(object): # If the room has some kind of name, use it, but we don't # want the generated-from-names one here otherwise we'll # end up with, "new message from Bob in the Bob room" - room_name = calculate_room_name( + room_name = yield calculate_room_name( state_by_room[room_id], user_id, fallback_to_members=False ) @@ -342,16 +349,16 @@ class Mailer(object): inviter_name = name_from_member_event(inviter_member_event) if room_name is None: - return INVITE_FROM_PERSON % { + defer.returnValue(INVITE_FROM_PERSON % { "person": inviter_name, "app": self.app_name - } + }) else: - return INVITE_FROM_PERSON_TO_ROOM % { + defer.returnValue(INVITE_FROM_PERSON_TO_ROOM % { "person": inviter_name, "room": room_name, "app": self.app_name, - } + }) sender_name = None if len(notifs_by_room[room_id]) == 1: @@ -362,24 +369,24 @@ class Mailer(object): sender_name = name_from_member_event(state_event) if sender_name is not None and room_name is not None: - return MESSAGE_FROM_PERSON_IN_ROOM % { + defer.returnValue(MESSAGE_FROM_PERSON_IN_ROOM % { "person": sender_name, "room": room_name, "app": self.app_name, - } + }) elif sender_name is not None: - return MESSAGE_FROM_PERSON % { + defer.returnValue(MESSAGE_FROM_PERSON % { "person": sender_name, "app": self.app_name, - } + }) else: # There's more than one notification for this room, so just # say there are several if room_name is not None: - return MESSAGES_IN_ROOM % { + defer.returnValue(MESSAGES_IN_ROOM % { "room": room_name, "app": self.app_name, - } + }) else: # If the room doesn't have a name, say who the messages # are from explicitly to avoid, "messages in the Bob room" @@ -388,22 +395,22 @@ class Mailer(object): for n in notifs_by_room[room_id] ])) - return MESSAGES_FROM_PERSON % { + defer.returnValue(MESSAGES_FROM_PERSON % { "person": descriptor_from_member_events([ state_by_room[room_id][("m.room.member", s)] for s in sender_ids ]), "app": self.app_name, - } + }) else: # Stuff's happened in multiple different rooms # ...but we still refer to the 'reason' room which triggered the mail if reason['room_name'] is not None: - return MESSAGES_IN_ROOM_AND_OTHERS % { + defer.returnValue(MESSAGES_IN_ROOM_AND_OTHERS % { "room": reason['room_name'], "app": self.app_name, - } + }) else: # If the reason room doesn't have a name, say who the messages # are from explicitly to avoid, "messages in the Bob room" @@ -412,13 +419,13 @@ class Mailer(object): for n in notifs_by_room[reason['room_id']] ])) - return MESSAGES_FROM_PERSON_AND_OTHERS % { + defer.returnValue(MESSAGES_FROM_PERSON_AND_OTHERS % { "person": descriptor_from_member_events([ state_by_room[reason['room_id']][("m.room.member", s)] for s in sender_ids ]), "app": self.app_name, - } + }) def make_room_link(self, room_id): # need /beta for Universal Links to work on iOS |