diff --git a/zerver/tornado/event_queue.py b/zerver/tornado/event_queue.py index 913739fea2..3fc0a72a93 100644 --- a/zerver/tornado/event_queue.py +++ b/zerver/tornado/event_queue.py @@ -654,6 +654,37 @@ def receiver_is_off_zulip(user_profile_id): off_zulip = len(message_event_queues) == 0 return off_zulip +def maybe_enqueue_notifications(user_profile_id, message_id, private_message, + mentioned, stream_push_notify, stream_name, + always_push_notify, idle): + # type: (int, int, bool, bool, bool, Optional[str], bool, bool) -> Optional[Dict[str, bool]] + notified = dict() # type: Dict[str, bool] + + if (idle or always_push_notify) and (private_message or mentioned or stream_push_notify): + notice = build_offline_notification(user_profile_id, message_id) + notice['triggers'] = { + 'private_message': private_message, + 'mentioned': mentioned, + 'stream_push_notify': stream_push_notify, + } + notice['stream_name'] = stream_name + queue_json_publish("missedmessage_mobile_notifications", notice, lambda notice: None) + notified['push_notified'] = True + + # Send missed_message emails if a private message or a + # mention. Eventually, we'll add settings to allow email + # notifications to match the model of push notifications + # above. + if idle and (private_message or mentioned): + # We require RabbitMQ to do this, as we can't call the email handler + # from the Tornado process. So if there's no rabbitmq support do nothing + queue_json_publish("missedmessage_emails", notice, lambda notice: None) + notified['email_notified'] = True + + if len(notified) > 0: + return notified + return None + def process_message_event(event_template, users): # type: (Mapping[str, Any], Iterable[Mapping[str, Any]]) -> None missed_message_userids = set(event_template.get('missed_message_userids', [])) @@ -697,31 +728,12 @@ def process_message_event(event_template, users): if private_message or mentioned or stream_push_notify: idle = receiver_is_off_zulip(user_profile_id) or (user_profile_id in missed_message_userids) always_push_notify = user_data.get('always_push_notify', False) - notified = dict() # type: Dict[str, bool] - - if (idle or always_push_notify) and (private_message or mentioned or stream_push_notify): - notice = build_offline_notification(user_profile_id, message_id) - notice['triggers'] = { - 'private_message': private_message, - 'mentioned': mentioned, - 'stream_push_notify': stream_push_notify, - } - notice['stream_name'] = event_template.get('stream_name') - queue_json_publish("missedmessage_mobile_notifications", notice, lambda notice: None) - notified['push_notified'] = True - - # Send missed_message emails if a private message or a - # mention. Eventually, we'll add settings to allow email - # notifications to match the model of push notifications - # above. - if idle and (private_message or mentioned): - # We require RabbitMQ to do this, as we can't call the email handler - # from the Tornado process. So if there's no rabbitmq support do nothing - queue_json_publish("missedmessage_emails", notice, lambda notice: None) - notified['email_notified'] = True - - if len(notified) > 0: - extra_user_data[user_profile_id] = notified + stream_name = event_template.get('stream_name') + result = maybe_enqueue_notifications(user_profile_id, message_id, private_message, + mentioned, stream_push_notify, stream_name, + always_push_notify, idle) + if result is not None: + extra_user_data[user_profile_id] = result for client_data in six.itervalues(send_to_clients): client = client_data['client']