diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index e7c3473401..0fa3dd8d02 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -43,14 +43,14 @@ from django_auth_ldap.backend import LDAPBackend, _LDAPUser from zerver.lib import bugdown from zerver.lib.validator import check_string, check_list, check_bool from zerver.decorator import require_post, authenticated_json_post_view, \ - has_request_variables, to_non_negative_int, \ + has_request_variables, \ JsonableError, get_user_profile_by_email, REQ, \ zulip_login_required from zerver.lib.avatar import avatar_url from zerver.lib.i18n import get_language_list, get_language_name, \ get_language_list_for_templates from zerver.lib.response import json_success, json_error -from zerver.lib.utils import statsd, generate_random_token +from zerver.lib.utils import statsd from version import ZULIP_VERSION from zproject.backends import password_auth_enabled, dev_auth_enabled, google_auth_enabled @@ -1023,47 +1023,6 @@ def is_buggy_ua(agent): return ("Humbug Desktop/" in agent or "Zulip Desktop/" in agent or "ZulipDesktop/" in agent) and \ "Mac" not in agent -def get_pointer_backend(request, user_profile): - # type: (HttpRequest, UserProfile) -> HttpResponse - return json_success({'pointer': user_profile.pointer}) - -@has_request_variables -def update_pointer_backend(request, user_profile, - pointer=REQ(converter=to_non_negative_int)): - # type: (HttpRequest, UserProfile, int) -> HttpResponse - if pointer <= user_profile.pointer: - return json_success() - - try: - UserMessage.objects.get( - user_profile=user_profile, - message__id=pointer - ) - except UserMessage.DoesNotExist: - raise JsonableError(_("Invalid message ID")) - - request._log_data["extra"] = "[%s]" % (pointer,) - update_flags = (request.client.name.lower() in ['android', "zulipandroid"]) - do_update_pointer(user_profile, pointer, update_flags=update_flags) - - return json_success() - -def generate_client_id(): - # type: () -> text_type - return generate_random_token(32) - -def get_profile_backend(request, user_profile): - # type: (HttpRequest, UserProfile) -> HttpResponse - result = dict(pointer = user_profile.pointer, - client_id = generate_client_id(), - max_message_id = -1) - - messages = Message.objects.filter(usermessage__user_profile=user_profile).order_by('-id')[:1] - if messages: - result['max_message_id'] = messages[0].id - - return json_success(result) - @csrf_exempt @require_post @has_request_variables diff --git a/zerver/views/pointer.py b/zerver/views/pointer.py new file mode 100644 index 0000000000..a52590287d --- /dev/null +++ b/zerver/views/pointer.py @@ -0,0 +1,55 @@ +from __future__ import absolute_import + +from django.core.exceptions import ValidationError +from django.http import HttpRequest, HttpResponse +from django.utils.translation import ugettext as _ +from six import text_type + +from zerver.decorator import to_non_negative_int +from zerver.lib.actions import do_update_pointer +from zerver.lib.request import has_request_variables, JsonableError, REQ +from zerver.lib.response import json_success +from zerver.lib.utils import statsd, generate_random_token +from zerver.models import UserProfile, Message, UserMessage + +def get_pointer_backend(request, user_profile): + # type: (HttpRequest, UserProfile) -> HttpResponse + return json_success({'pointer': user_profile.pointer}) + +@has_request_variables +def update_pointer_backend(request, user_profile, + pointer=REQ(converter=to_non_negative_int)): + # type: (HttpRequest, UserProfile, int) -> HttpResponse + if pointer <= user_profile.pointer: + return json_success() + + try: + UserMessage.objects.get( + user_profile=user_profile, + message__id=pointer + ) + except UserMessage.DoesNotExist: + raise JsonableError(_("Invalid message ID")) + + request._log_data["extra"] = "[%s]" % (pointer,) + update_flags = (request.client.name.lower() in ['android', "zulipandroid"]) + do_update_pointer(user_profile, pointer, update_flags=update_flags) + + return json_success() + +def generate_client_id(): + # type: () -> text_type + return generate_random_token(32) + +def get_profile_backend(request, user_profile): + # type: (HttpRequest, UserProfile) -> HttpResponse + result = dict(pointer = user_profile.pointer, + client_id = generate_client_id(), + max_message_id = -1) + + messages = Message.objects.filter(usermessage__user_profile=user_profile).order_by('-id')[:1] + if messages: + result['max_message_id'] = messages[0].id + + return json_success(result) + diff --git a/zproject/urls.py b/zproject/urls.py index 71ebb55d27..784db319f2 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -174,10 +174,10 @@ v1_api_and_json_patterns = [ # users/me -> zerver.views url(r'^users/me$', 'zerver.lib.rest.rest_dispatch', - {'GET': 'zerver.views.get_profile_backend'}), + {'GET': 'zerver.views.pointer.get_profile_backend'}), url(r'^users/me/pointer$', 'zerver.lib.rest.rest_dispatch', - {'GET': 'zerver.views.get_pointer_backend', - 'PUT': 'zerver.views.update_pointer_backend'}), + {'GET': 'zerver.views.pointer.get_pointer_backend', + 'PUT': 'zerver.views.pointer.update_pointer_backend'}), url(r'^users/me/presence$', 'zerver.lib.rest.rest_dispatch', {'POST': 'zerver.views.presence.update_active_status_backend'}), # Endpoint used by mobile devices to register their push