zulip/zerver/views/pointer.py

27 lines
1.1 KiB
Python
Raw Normal View History

2016-09-14 02:20:36 +02:00
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _
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.validator import to_non_negative_int
from zerver.models import UserProfile, get_usermessage_by_message_id
2016-09-14 02:20:36 +02:00
def get_pointer_backend(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
2016-09-14 02:20:36 +02:00
return json_success({'pointer': user_profile.pointer})
@has_request_variables
def update_pointer_backend(request: HttpRequest, user_profile: UserProfile,
pointer: int=REQ(converter=to_non_negative_int)) -> HttpResponse:
2016-09-14 02:20:36 +02:00
if pointer <= user_profile.pointer:
return json_success()
if get_usermessage_by_message_id(user_profile, pointer) is None:
2016-09-14 02:20:36 +02:00
raise JsonableError(_("Invalid message ID"))
request._log_data["extra"] = f"[{pointer}]"
2016-09-14 02:20:36 +02:00
update_flags = (request.client.name.lower() in ['android', "zulipandroid"])
do_update_pointer(user_profile, request.client, pointer, update_flags=update_flags)
2016-09-14 02:20:36 +02:00
return json_success()