2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-01-23 23:24:44 +01:00
|
|
|
from django.conf import settings
|
2013-09-13 23:33:11 +02:00
|
|
|
from django.utils.timezone import now
|
|
|
|
|
2013-11-05 17:02:34 +01:00
|
|
|
from zerver.models import Message, UserProfile, \
|
|
|
|
Recipient, get_user_profile_by_id
|
2013-01-23 22:25:22 +01:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.decorator import JsonableError
|
2013-09-13 22:35:27 +02:00
|
|
|
from zerver.lib.cache import cache_get_many, message_cache_key, \
|
2013-09-17 20:29:43 +02:00
|
|
|
user_profile_by_id_cache_key, cache_save_user_profile
|
2013-09-13 22:35:27 +02:00
|
|
|
from zerver.lib.cache_helpers import cache_save_message
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.queue import queue_json_publish
|
2013-10-17 23:51:25 +02:00
|
|
|
from zerver.lib.event_queue import get_client_descriptors_for_user,\
|
|
|
|
get_client_descriptors_for_realm_all_streams
|
2013-09-13 23:33:11 +02:00
|
|
|
from zerver.lib.timestamp import timestamp_to_datetime
|
2013-01-23 22:25:22 +01:00
|
|
|
|
2013-05-07 23:19:52 +02:00
|
|
|
import time
|
2013-01-23 22:25:22 +01:00
|
|
|
import logging
|
2013-01-23 23:24:44 +01:00
|
|
|
import requests
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2013-09-13 23:33:11 +02:00
|
|
|
import datetime
|
2013-01-23 22:25:22 +01:00
|
|
|
|
2013-09-13 23:33:11 +02:00
|
|
|
# Send email notifications to idle users
|
|
|
|
# after they are idle for 1 hour
|
|
|
|
NOTIFY_AFTER_IDLE_HOURS = 1
|
|
|
|
|
2013-01-23 22:25:22 +01:00
|
|
|
def update_pointer(user_profile_id, new_pointer):
|
2013-03-22 22:43:49 +01:00
|
|
|
event = dict(type='pointer', pointer=new_pointer)
|
2013-03-27 22:19:24 +01:00
|
|
|
for client in get_client_descriptors_for_user(user_profile_id):
|
2013-03-22 22:43:49 +01:00
|
|
|
if client.accepts_event_type(event['type']):
|
|
|
|
client.add_event(event.copy())
|
2013-03-26 18:06:00 +01:00
|
|
|
|
2013-05-22 23:49:02 +02:00
|
|
|
|
2013-09-13 22:35:27 +02:00
|
|
|
def receives_offline_notifications(user_profile):
|
2013-10-23 20:29:07 +02:00
|
|
|
return ((user_profile.enable_offline_email_notifications or
|
|
|
|
user_profile.enable_offline_push_notifications) and
|
2013-05-22 23:49:02 +02:00
|
|
|
not user_profile.is_bot)
|
|
|
|
|
2013-09-13 22:35:27 +02:00
|
|
|
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)
|
|
|
|
|
2013-05-22 23:49:02 +02:00
|
|
|
def build_offline_notification_event(user_profile_id, message_id):
|
|
|
|
return {"user_profile_id": user_profile_id,
|
|
|
|
"message_id": message_id,
|
|
|
|
"timestamp": time.time()}
|
|
|
|
|
|
|
|
def missedmessage_hook(user_profile_id, queue, last_for_client):
|
|
|
|
# Only process missedmessage hook when the last queue for a
|
|
|
|
# client has been garbage collected
|
|
|
|
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
|
2013-09-13 22:35:27 +02:00
|
|
|
if not receives_offline_notifications_by_id(user_profile_id):
|
2013-05-22 23:49:02 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
message_ids = []
|
|
|
|
for event in queue.event_queue.contents():
|
|
|
|
if not event['type'] == 'message' or not event['flags']:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if 'mentioned' in event['flags'] and not 'read' in event['flags']:
|
|
|
|
message_ids.append(event['message']['id'])
|
|
|
|
|
|
|
|
for msg_id in message_ids:
|
|
|
|
event = build_offline_notification_event(user_profile_id, msg_id)
|
|
|
|
queue_json_publish("missedmessage_emails", event, lambda event: None)
|
2013-11-19 00:55:24 +01:00
|
|
|
queue_json_publish("missedmessage_mobile_notifications", event, lambda event: None)
|
2013-05-22 23:49:02 +02:00
|
|
|
|
2013-09-15 19:10:16 +02:00
|
|
|
def cache_load_message_data(message_id, users):
|
2013-09-13 22:35:27 +02:00
|
|
|
# Get everything that we'll need out of memcached in one fetch, to save round-trip times:
|
|
|
|
# * The message itself
|
|
|
|
# * Every recipient's UserProfile
|
|
|
|
user_profile_keys = [user_profile_by_id_cache_key(user_data['id']) for user_data in users]
|
|
|
|
|
|
|
|
cache_keys = [message_cache_key(message_id)]
|
|
|
|
cache_keys.extend(user_profile_keys)
|
|
|
|
|
|
|
|
# Single memcached fetch
|
|
|
|
result = cache_get_many(cache_keys)
|
|
|
|
|
|
|
|
cache_extractor = lambda result: result[0] if result is not None else None
|
|
|
|
|
|
|
|
message = cache_extractor(result.get(cache_keys[0], None))
|
|
|
|
|
|
|
|
user_profiles = dict((user_data['id'], cache_extractor(result.get(user_profile_by_id_cache_key(user_data['id']), None)))
|
|
|
|
for user_data in users)
|
|
|
|
|
|
|
|
# Any data that was not found in memcached, we have to load from the database
|
|
|
|
# and save back. This should never happen---we take steps to keep recent messages,
|
|
|
|
# all user profile & presence objects in memcached.
|
|
|
|
if message is None:
|
|
|
|
if not settings.TEST_SUITE:
|
|
|
|
logging.warning("Tornado failed to load message from memcached when delivering!")
|
|
|
|
|
|
|
|
message = Message.objects.select_related().get(id=message_id)
|
|
|
|
cache_save_message(message)
|
2013-09-13 23:33:11 +02:00
|
|
|
|
2013-09-13 22:35:27 +02:00
|
|
|
for user_profile_id, user_profile in user_profiles.iteritems():
|
|
|
|
if user_profile:
|
|
|
|
continue
|
|
|
|
if not settings.TEST_SUITE:
|
|
|
|
logging.warning("Tornado failed to load user profile from memcached when delivering message!")
|
|
|
|
|
|
|
|
user_profile = UserProfile.objects.select_related().get(id=user_profile_id)
|
|
|
|
user_profiles[user_profile_id] = user_profile
|
|
|
|
cache_save_user_profile(user_profile)
|
|
|
|
|
2013-09-15 19:10:16 +02:00
|
|
|
return message, user_profiles
|
2013-09-13 22:35:27 +02:00
|
|
|
|
2013-09-13 23:33:11 +02:00
|
|
|
def receiver_is_idle(user_profile, realm_presences):
|
2013-09-13 22:35:27 +02:00
|
|
|
# If a user has no message-receiving event queues, they've got no open zulip
|
|
|
|
# session so we notify them
|
|
|
|
all_client_descriptors = get_client_descriptors_for_user(user_profile.id)
|
|
|
|
message_event_queues = [client for client in all_client_descriptors if client.accepts_event_type('message')]
|
|
|
|
off_zulip = len(message_event_queues) == 0
|
|
|
|
|
2013-09-13 23:33:11 +02:00
|
|
|
# It's possible a recipient is not in the realm of a sender. We don't have
|
|
|
|
# presence information in this case (and it's hard to get without an additional
|
|
|
|
# db query) so we simply don't try to guess if this cross-realm recipient
|
|
|
|
# has been idle for too long
|
|
|
|
if realm_presences is None or not user_profile.email in realm_presences:
|
|
|
|
return off_zulip
|
|
|
|
|
|
|
|
# If the most recent online status from a user is >1hr in the past, we notify
|
|
|
|
# them regardless of whether or not they have an open window
|
|
|
|
user_presence = realm_presences[user_profile.email]
|
|
|
|
idle_too_long = False
|
|
|
|
newest = None
|
|
|
|
for client, status in user_presence.iteritems():
|
|
|
|
if newest is None or status['timestamp'] > newest['timestamp']:
|
|
|
|
newest = status
|
|
|
|
|
|
|
|
update_time = timestamp_to_datetime(newest['timestamp'])
|
|
|
|
if now() - update_time > datetime.timedelta(hours=NOTIFY_AFTER_IDLE_HOURS):
|
|
|
|
idle_too_long = True
|
|
|
|
|
|
|
|
return off_zulip or idle_too_long
|
2013-09-13 22:35:27 +02:00
|
|
|
|
2013-01-23 23:24:44 +01:00
|
|
|
def process_new_message(data):
|
2013-09-15 19:10:16 +02:00
|
|
|
message, user_profiles = cache_load_message_data(data['message'],
|
|
|
|
data['users'])
|
|
|
|
|
|
|
|
realm_presences = data['presences']
|
2013-01-23 23:24:44 +01:00
|
|
|
|
2013-04-12 18:07:34 +02:00
|
|
|
message_dict_markdown = message.to_dict(True)
|
|
|
|
message_dict_no_markdown = message.to_dict(False)
|
2013-05-24 16:55:05 +02:00
|
|
|
|
2013-10-17 23:51:25 +02:00
|
|
|
# To remove duplicate clients: Maps queue ID to (Client, flags)
|
|
|
|
send_to_clients = dict()
|
|
|
|
|
|
|
|
if 'stream_name' in data and not data.get("invite_only"):
|
|
|
|
for client in get_client_descriptors_for_realm_all_streams(data['realm_id']):
|
|
|
|
send_to_clients[client.event_queue.id] = (client, None)
|
|
|
|
|
2013-06-18 18:01:41 +02:00
|
|
|
for user_data in data['users']:
|
|
|
|
user_profile_id = user_data['id']
|
2013-09-13 22:35:27 +02:00
|
|
|
user_profile = user_profiles[user_data['id']]
|
2013-06-18 18:01:41 +02:00
|
|
|
flags = user_data.get('flags', [])
|
|
|
|
|
2013-03-27 22:19:24 +01:00
|
|
|
for client in get_client_descriptors_for_user(user_profile_id):
|
2013-10-17 23:51:25 +02:00
|
|
|
send_to_clients[client.event_queue.id] = (client, flags)
|
2013-03-26 18:06:00 +01:00
|
|
|
|
2013-05-22 23:52:17 +02:00
|
|
|
# If the recipient was offline and the message was a single or group PM to him
|
|
|
|
# or she was @-notified potentially notify more immediately
|
|
|
|
received_pm = message.recipient.type in (Recipient.PERSONAL, Recipient.HUDDLE) and \
|
|
|
|
user_profile_id != message.sender.id
|
|
|
|
mentioned = 'mentioned' in flags
|
2013-09-15 22:06:22 +02:00
|
|
|
if (received_pm or mentioned) and receiver_is_idle(user_profile, realm_presences):
|
2013-09-13 22:35:27 +02:00
|
|
|
if receives_offline_notifications(user_profile):
|
2013-05-22 23:49:02 +02:00
|
|
|
event = build_offline_notification_event(user_profile_id, message.id)
|
2013-05-07 23:19:52 +02:00
|
|
|
|
|
|
|
# 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)
|
2013-11-19 00:55:24 +01:00
|
|
|
queue_json_publish("missedmessage_mobile_notifications", event, lambda event: None)
|
2013-05-07 23:19:52 +02:00
|
|
|
|
2013-10-17 23:51:25 +02:00
|
|
|
for client, flags in send_to_clients.itervalues():
|
|
|
|
if not client.accepts_event_type('message'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# The below prevents (Zephyr) mirroring loops.
|
|
|
|
if ('mirror' in message.sending_client.name and
|
|
|
|
message.sending_client == client.client_type):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if client.apply_markdown:
|
|
|
|
message_dict = message_dict_markdown
|
|
|
|
else:
|
|
|
|
message_dict = message_dict_no_markdown
|
|
|
|
|
|
|
|
# Make sure Zephyr mirroring bots know whether stream is invite-only
|
|
|
|
if "mirror" in client.client_type.name and data.get("invite_only"):
|
|
|
|
message_dict = message_dict.copy()
|
|
|
|
message_dict["invite_only_stream"] = True
|
|
|
|
|
|
|
|
event = dict(type='message', message=message_dict, flags=flags)
|
|
|
|
client.add_event(event)
|
|
|
|
|
2013-03-29 15:35:37 +01:00
|
|
|
def process_event(data):
|
|
|
|
event = data['event']
|
|
|
|
for user_profile_id in data['users']:
|
|
|
|
for client in get_client_descriptors_for_user(user_profile_id):
|
|
|
|
if client.accepts_event_type(event['type']):
|
|
|
|
client.add_event(event.copy())
|
|
|
|
|
2013-01-23 23:24:44 +01:00
|
|
|
def process_notification(data):
|
2013-03-29 15:35:37 +01:00
|
|
|
if 'type' not in data:
|
|
|
|
# Generic event that doesn't need special handling
|
|
|
|
process_event(data)
|
|
|
|
elif data['type'] == 'new_message':
|
2013-01-23 23:24:44 +01:00
|
|
|
process_new_message(data)
|
|
|
|
elif data['type'] == 'pointer_update':
|
|
|
|
update_pointer(data['user'], data['new_pointer'])
|
|
|
|
else:
|
|
|
|
raise JsonableError('bad notification type ' + data['type'])
|
|
|
|
|
|
|
|
# Runs in the Django process to send a notification to Tornado.
|
|
|
|
#
|
|
|
|
# We use JSON rather than bare form parameters, so that we can represent
|
|
|
|
# different types and for compatibility with non-HTTP transports.
|
2013-03-22 20:55:40 +01:00
|
|
|
|
|
|
|
def send_notification_http(data):
|
2013-10-11 21:31:20 +02:00
|
|
|
if settings.TORNADO_SERVER and not settings.RUNNING_INSIDE_TORNADO:
|
2013-03-29 21:04:49 +01:00
|
|
|
requests.post(settings.TORNADO_SERVER + '/notify_tornado', data=dict(
|
2013-06-18 23:55:55 +02:00
|
|
|
data = ujson.dumps(data),
|
2013-03-29 21:04:49 +01:00
|
|
|
secret = settings.SHARED_SECRET))
|
2013-07-02 00:11:59 +02:00
|
|
|
else:
|
|
|
|
process_notification(data)
|
2013-03-22 20:55:40 +01:00
|
|
|
|
2013-03-25 20:37:00 +01:00
|
|
|
def send_notification(data):
|
|
|
|
return queue_json_publish("notify_tornado", data, send_notification_http)
|