2013-04-23 18:51:17 +02:00
|
|
|
|
2017-11-16 00:53:11 +01:00
|
|
|
import time
|
|
|
|
from typing import Iterable, List, Optional, Sequence, Text, Union
|
2013-01-08 17:44:22 +01:00
|
|
|
|
2017-11-16 00:53:11 +01:00
|
|
|
import ujson
|
|
|
|
from django.core.handlers.base import BaseHandler
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
2013-01-23 22:25:22 +01:00
|
|
|
|
2017-11-16 00:53:11 +01:00
|
|
|
from zerver.decorator import REQ, RespondAsynchronously, \
|
|
|
|
_RespondAsynchronously, asynchronous, \
|
|
|
|
has_request_variables, internal_notify_view
|
|
|
|
from zerver.lib.response import json_error, json_success
|
2014-02-14 16:25:31 +01:00
|
|
|
from zerver.lib.validator import check_bool, check_list, check_string
|
2017-11-16 00:53:11 +01:00
|
|
|
from zerver.models import Client, UserProfile, get_client
|
|
|
|
from zerver.tornado.event_queue import fetch_events, \
|
|
|
|
get_client_descriptor, process_notification
|
2017-07-21 02:20:31 +02:00
|
|
|
from zerver.tornado.exceptions import BadEventQueueIdError
|
2013-01-08 17:44:22 +01:00
|
|
|
|
2017-04-18 18:56:19 +02:00
|
|
|
@internal_notify_view(True)
|
2017-10-26 11:38:28 +02:00
|
|
|
def notify(request: HttpRequest) -> HttpResponse:
|
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
|
2017-10-26 11:38:28 +02:00
|
|
|
def cleanup_event_queue(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
queue_id: Text=REQ()) -> HttpResponse:
|
2016-07-03 18:09:53 +02:00
|
|
|
client = get_client_descriptor(str(queue_id))
|
2013-11-19 23:11:30 +01:00
|
|
|
if client is None:
|
2017-07-25 22:17:55 +02:00
|
|
|
raise BadEventQueueIdError(queue_id)
|
2013-11-19 23:11:30 +01:00
|
|
|
if user_profile.id != client.user_profile_id:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("You are not authorized to access this queue"))
|
2013-11-19 23:11:30 +01:00
|
|
|
request._log_data['extra'] = "[%s]" % (queue_id,)
|
|
|
|
client.cleanup()
|
|
|
|
return json_success()
|
|
|
|
|
2013-03-26 18:06:00 +01:00
|
|
|
@asynchronous
|
|
|
|
@has_request_variables
|
2017-12-30 08:52:28 +01:00
|
|
|
def get_events_backend(request: HttpRequest, user_profile: UserProfile, handler: BaseHandler,
|
|
|
|
user_client: Optional[Client]=REQ(converter=get_client, default=None),
|
|
|
|
last_event_id: Optional[int]=REQ(converter=int, default=None),
|
|
|
|
queue_id: Optional[List[Text]]=REQ(default=None),
|
|
|
|
apply_markdown: bool=REQ(default=False, validator=check_bool),
|
|
|
|
client_gravatar: bool=REQ(default=False, validator=check_bool),
|
|
|
|
all_public_streams: bool=REQ(default=False, validator=check_bool),
|
|
|
|
event_types: Optional[Text]=REQ(default=None, validator=check_list(check_string)),
|
|
|
|
dont_block: bool=REQ(default=False, validator=check_bool),
|
|
|
|
narrow: Iterable[Sequence[Text]]=REQ(default=[], validator=check_list(None)),
|
|
|
|
lifespan_secs: int=REQ(default=0, converter=int)
|
|
|
|
) -> Union[HttpResponse, _RespondAsynchronously]:
|
2013-05-07 17:25:25 +02:00
|
|
|
if user_client is None:
|
2018-01-08 19:30:23 +01:00
|
|
|
valid_user_client = request.client
|
|
|
|
else:
|
|
|
|
valid_user_client = user_client
|
2013-05-07 17:25:25 +02:00
|
|
|
|
2014-01-28 18:11:08 +01:00
|
|
|
events_query = dict(
|
|
|
|
user_profile_id = user_profile.id,
|
|
|
|
user_profile_email = user_profile.email,
|
|
|
|
queue_id = queue_id,
|
|
|
|
last_event_id = last_event_id,
|
|
|
|
event_types = event_types,
|
2018-01-08 19:30:23 +01:00
|
|
|
client_type_name = valid_user_client.name,
|
2014-01-28 18:11:08 +01:00
|
|
|
all_public_streams = all_public_streams,
|
|
|
|
lifespan_secs = lifespan_secs,
|
|
|
|
narrow = narrow,
|
|
|
|
dont_block = dont_block,
|
|
|
|
handler_id = handler.handler_id)
|
|
|
|
|
|
|
|
if queue_id is None:
|
|
|
|
events_query['new_queue_data'] = dict(
|
|
|
|
user_profile_id = user_profile.id,
|
2017-01-03 21:04:55 +01:00
|
|
|
realm_id = user_profile.realm_id,
|
2014-01-28 18:11:08 +01:00
|
|
|
user_profile_email = user_profile.email,
|
|
|
|
event_types = event_types,
|
2018-01-08 19:30:23 +01:00
|
|
|
client_type_name = valid_user_client.name,
|
2014-01-28 18:11:08 +01:00
|
|
|
apply_markdown = apply_markdown,
|
2017-10-31 18:36:18 +01:00
|
|
|
client_gravatar = client_gravatar,
|
2014-01-28 18:11:08 +01:00
|
|
|
all_public_streams = all_public_streams,
|
|
|
|
queue_timeout = lifespan_secs,
|
|
|
|
last_connection_time = time.time(),
|
|
|
|
narrow = narrow)
|
|
|
|
|
2014-01-28 20:03:05 +01:00
|
|
|
result = fetch_events(events_query)
|
|
|
|
if "extra_log_data" in result:
|
|
|
|
request._log_data['extra'] = result["extra_log_data"]
|
|
|
|
|
|
|
|
if result["type"] == "async":
|
2014-01-27 23:21:39 +01:00
|
|
|
handler._request = request
|
2014-01-28 20:03:05 +01:00
|
|
|
return RespondAsynchronously
|
|
|
|
if result["type"] == "error":
|
2017-07-25 22:17:55 +02:00
|
|
|
raise result["exception"]
|
2014-01-28 20:03:05 +01:00
|
|
|
return json_success(result["response"])
|