From d6e0960ca22e8c4f376a9a02f106485291f3312e Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Fri, 7 Jul 2017 10:54:37 -0700 Subject: [PATCH] push_notifications: Fix mypy annotation logic around push tokens. I'm not 100% confident this is long-term correct, but at least it's consistent. --- zerver/lib/push_notifications.py | 4 ++-- zerver/views/push_notifications.py | 10 +++++----- zilencer/views.py | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py index 797c089e53..eba0834f05 100644 --- a/zerver/lib/push_notifications.py +++ b/zerver/lib/push_notifications.py @@ -393,7 +393,7 @@ def send_notifications_to_bouncer(user_profile_id, apns_payload, gcm_payload): send_json_to_push_bouncer('POST', 'notify', post_data) def add_push_device_token(user_profile, token_str, kind, ios_app_id=None): - # type: (UserProfile, str, int, Optional[str]) -> None + # type: (UserProfile, bytes, int, Optional[str]) -> None # If we're sending things to the push notification bouncer # register this user with them here @@ -426,7 +426,7 @@ def add_push_device_token(user_profile, token_str, kind, ios_app_id=None): token.save(update_fields=['last_updated']) def remove_push_device_token(user_profile, token_str, kind): - # type: (UserProfile, str, int) -> None + # type: (UserProfile, bytes, int) -> None # If we're sending things to the push notification bouncer # register this user with them here diff --git a/zerver/views/push_notifications.py b/zerver/views/push_notifications.py index d122bd7606..35dc12fd75 100644 --- a/zerver/views/push_notifications.py +++ b/zerver/views/push_notifications.py @@ -17,7 +17,7 @@ from zerver.lib.validator import check_string, check_list, check_bool from zerver.models import PushDeviceToken, UserProfile def validate_token(token_str, kind): - # type: (str, int) -> None + # type: (bytes, int) -> None if token_str == '' or len(token_str) > 4096: raise JsonableError(_('Empty or invalid length token')) if kind == PushDeviceToken.APNS: @@ -30,28 +30,28 @@ def validate_token(token_str, kind): @has_request_variables def add_apns_device_token(request, user_profile, token=REQ(), appid=REQ(default=settings.ZULIP_IOS_APP_ID)): - # type: (HttpRequest, UserProfile, str, str) -> HttpResponse + # type: (HttpRequest, UserProfile, bytes, str) -> HttpResponse validate_token(token, PushDeviceToken.APNS) add_push_device_token(user_profile, token, PushDeviceToken.APNS, ios_app_id=appid) return json_success() @has_request_variables def add_android_reg_id(request, user_profile, token=REQ()): - # type: (HttpRequest, UserProfile, str) -> HttpResponse + # type: (HttpRequest, UserProfile, bytes) -> HttpResponse validate_token(token, PushDeviceToken.GCM) add_push_device_token(user_profile, token, PushDeviceToken.GCM) return json_success() @has_request_variables def remove_apns_device_token(request, user_profile, token=REQ()): - # type: (HttpRequest, UserProfile, str) -> HttpResponse + # type: (HttpRequest, UserProfile, bytes) -> HttpResponse validate_token(token, PushDeviceToken.APNS) remove_push_device_token(user_profile, token, PushDeviceToken.APNS) return json_success() @has_request_variables def remove_android_reg_id(request, user_profile, token=REQ()): - # type: (HttpRequest, UserProfile, str) -> HttpResponse + # type: (HttpRequest, UserProfile, bytes) -> HttpResponse validate_token(token, PushDeviceToken.GCM) remove_push_device_token(user_profile, token, PushDeviceToken.GCM) return json_success() diff --git a/zilencer/views.py b/zilencer/views.py index 590fe66ea2..bb1a1b4000 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -24,7 +24,7 @@ def validate_entity(entity): raise JsonableError(_("Must validate with valid Zulip server API key")) def validate_bouncer_token_request(entity, token, kind): - # type: (Union[UserProfile, RemoteZulipServer], str, int) -> None + # type: (Union[UserProfile, RemoteZulipServer], bytes, int) -> None if kind not in [RemotePushDeviceToken.APNS, RemotePushDeviceToken.GCM]: raise JsonableError(_("Invalid token type")) validate_entity(entity) @@ -38,7 +38,7 @@ def report_error(request, deployment, type=REQ(), report=REQ(validator=check_dic @has_request_variables def remote_server_register_push(request, entity, user_id=REQ(), token=REQ(), token_kind=REQ(validator=check_int), ios_app_id=None): - # type: (HttpRequest, Union[UserProfile, RemoteZulipServer], int, str, int, Optional[Text]) -> HttpResponse + # type: (HttpRequest, Union[UserProfile, RemoteZulipServer], int, bytes, int, Optional[Text]) -> HttpResponse validate_bouncer_token_request(entity, token, token_kind) server = cast(RemoteZulipServer, entity) @@ -63,7 +63,7 @@ def remote_server_register_push(request, entity, user_id=REQ(), @has_request_variables def remote_server_unregister_push(request, entity, token=REQ(), token_kind=REQ(validator=check_int), ios_app_id=None): - # type: (HttpRequest, Union[UserProfile, RemoteZulipServer], str, int, Optional[Text]) -> HttpResponse + # type: (HttpRequest, Union[UserProfile, RemoteZulipServer], bytes, int, Optional[Text]) -> HttpResponse validate_bouncer_token_request(entity, token, token_kind) server = cast(RemoteZulipServer, entity) deleted = RemotePushDeviceToken.objects.filter(token=token,