Move missedmessage hook checks on UserProfile settings to workers.

This does result in a few more rabbitmq events to be processed (though
a negligible number compared to what we already do), but it saves a
database query from inside Tornado whenever we occasionally have a
cache miss looking up the UserProfile, which is far more important.

(imported from commit a553a00a3004ba27bfb54ffbc3e9c9b170ebae4d)
This commit is contained in:
Tim Abbott
2014-01-09 15:48:19 -05:00
parent a05e24667e
commit e890f06c1c
2 changed files with 17 additions and 23 deletions

View File

@@ -2187,12 +2187,20 @@ def do_send_missedmessage_events(user_profile, missed_messages):
return
def receives_offline_notifications(user_profile):
return ((user_profile.enable_offline_email_notifications or
user_profile.enable_offline_push_notifications) and
not user_profile.is_bot)
@statsd_increment("push_notifications")
def handle_push_notification(user_profile_id, missed_message):
try:
user_profile = get_user_profile_by_id(user_profile_id)
if not receives_offline_notifications(user_profile):
return
umessage = UserMessage.objects.get(user_profile=user_profile,
message__id=missed_message['message_id'])
message__id=missed_message['message_id'])
message = umessage.message
if umessage.flags.read:
return
@@ -2252,6 +2260,9 @@ def handle_missedmessage_emails(user_profile_id, missed_email_events):
message_ids = [event.get('message_id') for event in missed_email_events]
user_profile = get_user_profile_by_id(user_profile_id)
if not receives_offline_notifications(user_profile):
return
messages = [um.message for um in UserMessage.objects.filter(user_profile=user_profile,
message__id__in=message_ids,
flags=~UserMessage.flags.read)]

View File

@@ -31,16 +31,6 @@ def update_pointer(user_profile_id, new_pointer):
if client.accepts_event(event):
client.add_event(event.copy())
def receives_offline_notifications(user_profile):
return ((user_profile.enable_offline_email_notifications or
user_profile.enable_offline_push_notifications) and
not user_profile.is_bot)
def receives_offline_notifications_by_id(user_profile_id):
user_profile = get_user_profile_by_id(user_profile_id)
return receives_offline_notifications(user_profile)
def build_offline_notification_event(user_profile_id, message_id):
return {"user_profile_id": user_profile_id,
"message_id": message_id,
@@ -52,12 +42,6 @@ def missedmessage_hook(user_profile_id, queue, last_for_client):
if not last_for_client:
return
# If a user has gone offline but has unread messages
# received in the idle time, send them a missed
# message email
if not receives_offline_notifications_by_id(user_profile_id):
return
message_ids = []
for event in queue.event_queue.contents():
if not event['type'] == 'message' or not event['flags']:
@@ -182,13 +166,12 @@ def process_new_message(data):
user_profile_id != message.sender.id
mentioned = 'mentioned' in flags
if (received_pm or mentioned) and receiver_is_idle(user_profile, realm_presences):
if receives_offline_notifications(user_profile):
event = build_offline_notification_event(user_profile_id, message.id)
event = build_offline_notification_event(user_profile_id, message.id)
# 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", event, lambda event: None)
queue_json_publish("missedmessage_mobile_notifications", event, lambda event: None)
# 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", event, lambda event: None)
queue_json_publish("missedmessage_mobile_notifications", event, lambda event: None)
for client_data in send_to_clients.itervalues():
client = client_data['client']