2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-01-14 17:51:18 +01:00
|
|
|
from django.conf import settings
|
2013-01-23 22:25:22 +01:00
|
|
|
from zephyr.models import UserActivity
|
2013-01-08 17:44:22 +01:00
|
|
|
|
|
|
|
from zephyr.decorator import asynchronous, authenticated_api_view, \
|
|
|
|
authenticated_json_post_view, internal_notify_view, RespondAsynchronously, \
|
2013-03-22 22:43:49 +01:00
|
|
|
has_request_variables, POST, to_non_negative_int, json_to_bool, json_to_list, \
|
2013-03-22 18:32:20 +01:00
|
|
|
JsonableError, authenticated_rest_api_view, REQ
|
2013-01-23 22:25:22 +01:00
|
|
|
|
2013-03-26 18:06:00 +01:00
|
|
|
from zephyr.lib.response import json_response, json_success, json_error
|
2013-04-23 19:36:50 +02:00
|
|
|
from zephyr.middleware import async_request_restart
|
2013-01-23 23:24:44 +01:00
|
|
|
from zephyr.tornado_callbacks import \
|
|
|
|
get_user_pointer, fetch_stream_messages, fetch_user_messages, \
|
2013-01-23 22:25:22 +01:00
|
|
|
add_stream_receive_callback, add_user_receive_callback, \
|
2013-01-23 23:24:44 +01:00
|
|
|
add_pointer_update_callback, process_notification
|
2013-01-23 22:25:22 +01:00
|
|
|
|
|
|
|
from zephyr.lib.cache_helpers import cache_get_message
|
2013-03-27 22:19:24 +01:00
|
|
|
from zephyr.lib.event_queue import allocate_client_descriptor, get_client_descriptor
|
2013-01-08 17:44:22 +01:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import simplejson
|
|
|
|
import socket
|
|
|
|
import time
|
2013-01-09 20:35:19 +01:00
|
|
|
import sys
|
|
|
|
import logging
|
2013-01-08 17:44:22 +01:00
|
|
|
|
|
|
|
@internal_notify_view
|
2013-01-23 23:24:44 +01:00
|
|
|
def notify(request):
|
|
|
|
process_notification(simplejson.loads(request.POST['data']))
|
2013-01-08 17:44:22 +01:00
|
|
|
return json_success()
|
|
|
|
|
|
|
|
@asynchronous
|
|
|
|
@authenticated_json_post_view
|
|
|
|
def json_get_updates(request, user_profile, handler):
|
|
|
|
client_id = request.session.session_key
|
|
|
|
return get_updates_backend(request, user_profile, handler, client_id,
|
2013-03-26 19:40:28 +01:00
|
|
|
client=request.client, apply_markdown=True)
|
2013-01-08 17:44:22 +01:00
|
|
|
|
|
|
|
@asynchronous
|
|
|
|
@authenticated_api_view
|
2013-03-22 18:32:20 +01:00
|
|
|
def api_get_messages(request, user_profile, handler):
|
|
|
|
return get_messages_backend(request, user_profile, handler)
|
|
|
|
|
2013-01-08 17:44:22 +01:00
|
|
|
@has_request_variables
|
2013-03-22 18:32:20 +01:00
|
|
|
def get_messages_backend(request, user_profile, handler, client_id=REQ(default=None),
|
|
|
|
apply_markdown=REQ(default=False, converter=json_to_bool)):
|
2013-01-08 17:44:22 +01:00
|
|
|
return get_updates_backend(request, user_profile, handler, client_id,
|
|
|
|
apply_markdown=apply_markdown,
|
2013-03-26 19:40:28 +01:00
|
|
|
client=request.client)
|
2013-01-08 17:44:22 +01:00
|
|
|
|
2013-03-22 18:32:20 +01:00
|
|
|
@asynchronous
|
|
|
|
@authenticated_rest_api_view
|
|
|
|
def rest_get_messages(request, user_profile, handler):
|
|
|
|
return get_messages_backend(request, user_profile, handler)
|
|
|
|
|
2013-01-08 17:44:22 +01:00
|
|
|
def format_updates_response(messages=[], apply_markdown=True,
|
|
|
|
user_profile=None, new_pointer=None,
|
|
|
|
client=None, update_types=[],
|
|
|
|
client_server_generation=None):
|
|
|
|
if client is not None and client.name.endswith("_mirror"):
|
|
|
|
messages = [m for m in messages if m.sending_client.name != client.name]
|
|
|
|
ret = {'messages': [message.to_dict(apply_markdown) for message in messages],
|
|
|
|
"result": "success",
|
|
|
|
"msg": "",
|
|
|
|
'update_types': update_types}
|
|
|
|
if client_server_generation is not None:
|
2013-03-26 22:08:25 +01:00
|
|
|
ret['server_generation'] = settings.SERVER_GENERATION
|
2013-01-08 17:44:22 +01:00
|
|
|
if new_pointer is not None:
|
|
|
|
ret['new_pointer'] = new_pointer
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def return_messages_immediately(user_profile, client_id, last,
|
|
|
|
client_server_generation,
|
2013-01-15 20:55:13 +01:00
|
|
|
client_pointer, dont_block,
|
|
|
|
stream_name, **kwargs):
|
2013-01-08 17:44:22 +01:00
|
|
|
update_types = []
|
2012-12-14 19:13:43 +01:00
|
|
|
new_pointer = None
|
2013-01-08 17:44:22 +01:00
|
|
|
if dont_block:
|
|
|
|
update_types.append("nonblocking_request")
|
|
|
|
|
|
|
|
if (client_server_generation is not None and
|
2013-03-26 22:08:25 +01:00
|
|
|
client_server_generation != settings.SERVER_GENERATION):
|
2013-01-08 17:44:22 +01:00
|
|
|
update_types.append("client_reload")
|
|
|
|
|
2013-01-09 16:34:42 +01:00
|
|
|
ptr = get_user_pointer(user_profile.id)
|
2013-01-08 17:44:22 +01:00
|
|
|
if (client_pointer is not None and ptr > client_pointer):
|
|
|
|
new_pointer = ptr
|
|
|
|
update_types.append("pointer_update")
|
|
|
|
|
2012-12-14 19:13:43 +01:00
|
|
|
if last is not None:
|
2013-01-15 20:55:13 +01:00
|
|
|
if stream_name is not None:
|
|
|
|
message_ids = fetch_stream_messages(user_profile.realm.id, stream_name, last)
|
|
|
|
else:
|
|
|
|
message_ids = fetch_user_messages(user_profile.id, last)
|
2013-01-09 20:35:19 +01:00
|
|
|
messages = map(cache_get_message, message_ids)
|
2012-12-14 19:13:43 +01:00
|
|
|
|
|
|
|
# Filter for mirroring before checking whether there are any
|
|
|
|
# messages to pass on. If we don't do this, when the only message
|
|
|
|
# to forward is one that was sent via the mirroring, the API
|
|
|
|
# client will end up in an endless loop requesting more data from
|
|
|
|
# us.
|
|
|
|
if "client" in kwargs and kwargs["client"].name.endswith("_mirror"):
|
|
|
|
messages = [m for m in messages if
|
|
|
|
m.sending_client.name != kwargs["client"].name]
|
|
|
|
else: # last is None, so we're not interested in any old messages
|
|
|
|
messages = []
|
|
|
|
|
|
|
|
if messages:
|
|
|
|
update_types.append("new_messages")
|
|
|
|
|
2013-01-08 17:44:22 +01:00
|
|
|
if update_types:
|
|
|
|
return format_updates_response(messages=messages,
|
|
|
|
user_profile=user_profile,
|
|
|
|
new_pointer=new_pointer,
|
|
|
|
client_server_generation=client_server_generation,
|
|
|
|
update_types=update_types,
|
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2013-01-15 20:55:13 +01:00
|
|
|
# Note: We allow any stream name at all here! Validation and
|
|
|
|
# authorization (is the stream "public") are handled by the caller of
|
2013-01-23 23:24:44 +01:00
|
|
|
# notify new_message. If a user makes a get_updates request for a
|
2013-01-15 20:55:13 +01:00
|
|
|
# nonexistent or non-public stream, they won't get an error -- they'll
|
|
|
|
# just never receive any messages.
|
2013-01-08 17:44:22 +01:00
|
|
|
@has_request_variables
|
|
|
|
def get_updates_backend(request, user_profile, handler, client_id,
|
2013-03-22 18:32:20 +01:00
|
|
|
last = REQ(converter=to_non_negative_int, default=None),
|
|
|
|
client_server_generation = REQ(whence='server_generation', default=None,
|
2013-01-08 17:44:22 +01:00
|
|
|
converter=int),
|
2013-03-22 18:32:20 +01:00
|
|
|
client_pointer = REQ(whence='pointer', converter=int, default=None),
|
|
|
|
dont_block = REQ(converter=json_to_bool, default=False),
|
|
|
|
stream_name = REQ(default=None), apply_markdown=True,
|
2013-01-08 17:44:22 +01:00
|
|
|
**kwargs):
|
|
|
|
resp = return_messages_immediately(user_profile, client_id, last,
|
|
|
|
client_server_generation,
|
|
|
|
client_pointer,
|
2013-03-13 21:02:00 +01:00
|
|
|
dont_block, stream_name,
|
|
|
|
apply_markdown=apply_markdown, **kwargs)
|
2013-01-08 17:44:22 +01:00
|
|
|
if resp is not None:
|
2013-03-15 17:28:03 +01:00
|
|
|
handler.humbug_finish(resp, request, apply_markdown)
|
2013-01-08 17:44:22 +01:00
|
|
|
|
2013-03-15 17:28:03 +01:00
|
|
|
# We have already invoked handler.humbug_finish(), so we bypass the usual view
|
2013-01-08 17:44:22 +01:00
|
|
|
# response path. We are "responding asynchronously" except that it
|
2013-03-13 21:02:00 +01:00
|
|
|
# already happened. This is slightly weird.
|
2013-01-08 17:44:22 +01:00
|
|
|
return RespondAsynchronously
|
|
|
|
|
|
|
|
# Enter long-polling mode.
|
|
|
|
#
|
|
|
|
# Instead of responding to the client right away, leave our connection open
|
|
|
|
# and return to the Tornado main loop. One of the notify_* views will
|
|
|
|
# eventually invoke one of these callbacks, which will send the delayed
|
|
|
|
# response.
|
|
|
|
|
|
|
|
def cb(**cb_kwargs):
|
2013-04-23 19:36:50 +02:00
|
|
|
async_request_restart(request)
|
2013-01-08 17:44:22 +01:00
|
|
|
if handler.request.connection.stream.closed():
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
# It would be nice to be able to do these checks in
|
|
|
|
# UserProfile.receive, but it doesn't know what the value
|
|
|
|
# of "last" was for each callback.
|
|
|
|
if last is not None and "messages" in cb_kwargs:
|
|
|
|
messages = cb_kwargs["messages"]
|
|
|
|
|
|
|
|
# Make sure the client doesn't get a message twice
|
|
|
|
# when messages are processed out of order.
|
|
|
|
if messages[0].id <= last:
|
|
|
|
# We must return a response because we don't have
|
|
|
|
# a way to re-queue a callback and so the client
|
|
|
|
# must do it by making a new request
|
2013-03-15 17:28:03 +01:00
|
|
|
handler.humbug_finish({"result": "success",
|
|
|
|
"msg": "",
|
|
|
|
'update_types': []},
|
|
|
|
request, apply_markdown)
|
2013-01-08 17:44:22 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
kwargs.update(cb_kwargs)
|
|
|
|
res = format_updates_response(user_profile=user_profile,
|
|
|
|
client_server_generation=client_server_generation,
|
2013-03-13 21:02:00 +01:00
|
|
|
apply_markdown=apply_markdown,
|
2013-01-08 17:44:22 +01:00
|
|
|
**kwargs)
|
2013-03-15 17:28:03 +01:00
|
|
|
handler.humbug_finish(res, request, apply_markdown)
|
2013-01-08 17:44:22 +01:00
|
|
|
except socket.error:
|
|
|
|
pass
|
|
|
|
|
2013-01-15 20:55:13 +01:00
|
|
|
if stream_name is not None:
|
|
|
|
add_stream_receive_callback(user_profile.realm.id, stream_name, handler.async_callback(cb))
|
|
|
|
else:
|
|
|
|
add_user_receive_callback(user_profile, handler.async_callback(cb))
|
2013-01-08 17:44:22 +01:00
|
|
|
if client_pointer is not None:
|
2013-01-09 16:18:16 +01:00
|
|
|
add_pointer_update_callback(user_profile, handler.async_callback(cb))
|
2013-01-08 17:44:22 +01:00
|
|
|
|
|
|
|
# runtornado recognizes this special return value.
|
|
|
|
return RespondAsynchronously
|
2013-03-26 18:06:00 +01:00
|
|
|
|
|
|
|
@asynchronous
|
|
|
|
@authenticated_json_post_view
|
|
|
|
def json_get_events(request, user_profile, handler):
|
|
|
|
return get_events_backend(request, user_profile, handler)
|
|
|
|
|
|
|
|
@asynchronous
|
|
|
|
@authenticated_rest_api_view
|
|
|
|
@has_request_variables
|
|
|
|
def rest_get_events(request, user_profile, handler,
|
|
|
|
apply_markdown=REQ(default=False, converter=json_to_bool)):
|
|
|
|
return get_events_backend(request, user_profile, handler,
|
|
|
|
apply_markdown=apply_markdown)
|
|
|
|
|
|
|
|
@has_request_variables
|
|
|
|
def get_events_backend(request, user_profile, handler,
|
2013-03-21 22:43:53 +01:00
|
|
|
last_event_id = REQ(converter=int, default=None),
|
2013-03-14 23:21:53 +01:00
|
|
|
queue_id = REQ(default=None), apply_markdown=True,
|
2013-03-22 22:43:49 +01:00
|
|
|
event_types = REQ(default=None, converter=json_to_list),
|
2013-03-14 23:21:53 +01:00
|
|
|
dont_block = REQ(default=False, converter=json_to_bool)):
|
2013-03-26 18:06:00 +01:00
|
|
|
if queue_id is None:
|
2013-03-21 20:03:48 +01:00
|
|
|
if dont_block:
|
2013-03-22 22:43:49 +01:00
|
|
|
client = allocate_client_descriptor(user_profile.id, event_types,
|
2013-05-03 17:17:08 +02:00
|
|
|
request.client, apply_markdown)
|
2013-03-21 20:03:48 +01:00
|
|
|
queue_id = client.event_queue.id
|
|
|
|
else:
|
|
|
|
return json_error("Missing 'queue_id' argument")
|
2013-03-26 18:06:00 +01:00
|
|
|
else:
|
|
|
|
if last_event_id is None:
|
|
|
|
return json_error("Missing 'last_event_id' argument")
|
2013-03-27 22:19:24 +01:00
|
|
|
client = get_client_descriptor(queue_id)
|
2013-03-26 18:06:00 +01:00
|
|
|
if client is None:
|
|
|
|
return json_error("Bad event queue id: %s" % (queue_id,))
|
|
|
|
if user_profile.id != client.user_profile_id:
|
|
|
|
return json_error("You are not authorized to get events from this queue")
|
|
|
|
client.event_queue.prune(last_event_id)
|
|
|
|
client.disconnect_handler()
|
|
|
|
|
2013-03-14 23:21:53 +01:00
|
|
|
if not client.event_queue.empty() or dont_block:
|
2013-03-26 18:06:00 +01:00
|
|
|
return json_success({'events': client.event_queue.contents(),
|
|
|
|
'queue_id': queue_id})
|
|
|
|
|
|
|
|
handler._request = request
|
|
|
|
client.connect_handler(handler)
|
|
|
|
|
|
|
|
# runtornado recognizes this special return value.
|
|
|
|
return RespondAsynchronously
|