2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-10-25 20:13:17 +02:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.models import get_client
|
2013-01-08 17:44:22 +01:00
|
|
|
|
2013-11-05 17:02:34 +01:00
|
|
|
from zerver.decorator import asynchronous, \
|
2013-01-08 17:44:22 +01:00
|
|
|
authenticated_json_post_view, internal_notify_view, RespondAsynchronously, \
|
2013-12-10 16:28:16 +01:00
|
|
|
has_request_variables, json_to_bool, json_to_list, json_to_dict, REQ
|
2013-01-23 22:25:22 +01:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2013-11-05 17:02:34 +01:00
|
|
|
from zerver.tornado_callbacks import process_notification
|
2013-01-23 22:25:22 +01:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.event_queue import allocate_client_descriptor, get_client_descriptor
|
2013-12-10 16:28:16 +01:00
|
|
|
from zerver.lib.narrow import check_supported_events_narrow_filter
|
2013-01-08 17:44:22 +01:00
|
|
|
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2013-11-07 21:14:58 +01:00
|
|
|
import logging
|
2013-01-08 17:44:22 +01:00
|
|
|
|
2013-10-25 20:13:17 +02:00
|
|
|
from zerver.lib.rest import rest_dispatch as _rest_dispatch
|
|
|
|
rest_dispatch = csrf_exempt((lambda request, *args, **kwargs: _rest_dispatch(request, globals(), *args, **kwargs)))
|
|
|
|
|
2013-01-08 17:44:22 +01:00
|
|
|
@internal_notify_view
|
2013-01-23 23:24:44 +01:00
|
|
|
def notify(request):
|
2013-06-18 23:55:55 +02:00
|
|
|
process_notification(ujson.loads(request.POST['data']))
|
2013-01-08 17:44:22 +01:00
|
|
|
return json_success()
|
|
|
|
|
2013-11-19 23:11:30 +01:00
|
|
|
@has_request_variables
|
|
|
|
def cleanup_event_queue(request, user_profile, queue_id=REQ()):
|
|
|
|
client = get_client_descriptor(queue_id)
|
|
|
|
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 access this queue")
|
|
|
|
request._log_data['extra'] = "[%s]" % (queue_id,)
|
|
|
|
client.cleanup()
|
|
|
|
return json_success()
|
|
|
|
|
2013-03-26 18:06:00 +01:00
|
|
|
@authenticated_json_post_view
|
2013-07-26 22:30:55 +02:00
|
|
|
def json_get_events(request, user_profile):
|
|
|
|
return get_events_backend(request, user_profile, apply_markdown=True)
|
2013-03-26 18:06:00 +01:00
|
|
|
|
|
|
|
@asynchronous
|
|
|
|
@has_request_variables
|
2013-07-26 22:30:55 +02:00
|
|
|
def get_events_backend(request, user_profile, handler = None,
|
2013-05-08 17:14:52 +02:00
|
|
|
user_client = REQ(converter=get_client, default=None),
|
2013-03-21 22:43:53 +01:00
|
|
|
last_event_id = REQ(converter=int, default=None),
|
2013-07-30 20:36:16 +02:00
|
|
|
queue_id = REQ(default=None),
|
2013-07-26 22:30:55 +02:00
|
|
|
apply_markdown = REQ(default=False, converter=json_to_bool),
|
2013-10-17 23:51:25 +02:00
|
|
|
all_public_streams = REQ(default=False, converter=json_to_bool),
|
2013-03-22 22:43:49 +01:00
|
|
|
event_types = REQ(default=None, converter=json_to_list),
|
2013-08-05 22:09:12 +02:00
|
|
|
dont_block = REQ(default=False, converter=json_to_bool),
|
2013-12-10 16:28:16 +01:00
|
|
|
narrow = REQ(default=[], converter=json_to_list),
|
2013-08-05 22:09:12 +02:00
|
|
|
lifespan_secs = REQ(default=0, converter=int)):
|
2013-05-07 17:25:25 +02:00
|
|
|
if user_client is None:
|
|
|
|
user_client = request.client
|
|
|
|
|
2013-12-11 23:07:43 +01:00
|
|
|
was_connected = False
|
2013-08-08 19:44:12 +02:00
|
|
|
orig_queue_id = queue_id
|
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-10-17 23:51:25 +02:00
|
|
|
client = allocate_client_descriptor(user_profile.id, user_profile.realm.id,
|
|
|
|
event_types, user_client, apply_markdown,
|
2013-12-10 16:28:16 +01:00
|
|
|
all_public_streams, lifespan_secs,
|
|
|
|
narrow=narrow)
|
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)
|
2013-12-12 18:59:02 +01:00
|
|
|
was_connected = client.finish_current_handler()
|
2013-03-26 18:06:00 +01:00
|
|
|
|
2013-03-14 23:21:53 +01:00
|
|
|
if not client.event_queue.empty() or dont_block:
|
2013-08-08 19:44:12 +02:00
|
|
|
ret = {'events': client.event_queue.contents()}
|
|
|
|
if orig_queue_id is None:
|
|
|
|
ret['queue_id'] = queue_id
|
2013-11-08 20:18:02 +01:00
|
|
|
request._log_data['extra'] = "[%s/%s]" % (queue_id, len(ret["events"]))
|
2013-12-11 23:07:43 +01:00
|
|
|
if was_connected:
|
|
|
|
request._log_data['extra'] += " [was connected]"
|
2013-08-08 19:44:12 +02:00
|
|
|
return json_success(ret)
|
2013-03-26 18:06:00 +01:00
|
|
|
|
|
|
|
handler._request = request
|
2013-12-11 23:07:43 +01:00
|
|
|
if was_connected:
|
|
|
|
logging.info("Disconnected handler for queue %s (%s/%s)" % (queue_id, user_profile.email,
|
|
|
|
user_client.name))
|
2013-03-26 18:06:00 +01:00
|
|
|
client.connect_handler(handler)
|
|
|
|
|
|
|
|
# runtornado recognizes this special return value.
|
|
|
|
return RespondAsynchronously
|