From 271bd5a28232c844c0b0aee5dd2da7ada4a4e740 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Sun, 5 Mar 2017 18:57:31 -0800 Subject: [PATCH] push: Move add_push_device_token to library. --- zerver/lib/push_notifications.py | 22 ++++++++++++++++++++++ zerver/views/push_notifications.py | 29 +++++------------------------ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py index 0007228922..af00ffb039 100644 --- a/zerver/lib/push_notifications.py +++ b/zerver/lib/push_notifications.py @@ -8,6 +8,7 @@ from zerver.models import PushDeviceToken, Message, Recipient, UserProfile, \ receives_online_notifications from zerver.models import get_user_profile_by_id from zerver.lib.avatar import avatar_url +from zerver.lib.request import JsonableError from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime from zerver.decorator import statsd_increment from zerver.lib.utils import generate_random_token @@ -17,6 +18,8 @@ from apns import APNs, Frame, Payload, SENT_BUFFER_QTY from gcm import GCM from django.conf import settings +from django.utils import timezone +from django.utils.translation import ugettext as _ import base64 import binascii @@ -336,3 +339,22 @@ def handle_push_notification(user_profile_id, missed_message): except UserMessage.DoesNotExist: logging.error("Could not find UserMessage with message_id %s" % (missed_message['message_id'],)) + +def add_push_device_token(user_profile, token_str, kind, ios_app_id=None): + # type: (UserProfile, str, int, Optional[str]) -> None + if token_str == '' or len(token_str) > 4096: + raise JsonableError(_('Empty or invalid length token')) + + # If another user was previously logged in on the same device and didn't + # properly log out, the token will still be registered to the wrong account + PushDeviceToken.objects.filter(token=token_str).exclude(user=user_profile).delete() + + # Overwrite with the latest value + token, created = PushDeviceToken.objects.get_or_create(user=user_profile, + token=token_str, + defaults=dict( + kind=kind, + ios_app_id=ios_app_id)) + if not created: + token.last_updated = timezone.now() + token.save(update_fields=['last_updated']) diff --git a/zerver/views/push_notifications.py b/zerver/views/push_notifications.py index c852ed78b6..fd7a88a05c 100644 --- a/zerver/views/push_notifications.py +++ b/zerver/views/push_notifications.py @@ -4,44 +4,25 @@ from typing import Optional from django.conf import settings from django.http import HttpRequest, HttpResponse -from django.utils import timezone from django.utils.translation import ugettext as _ +from zerver.lib.push_notifications import add_push_device_token from zerver.lib.request import has_request_variables, REQ from zerver.lib.response import json_success, json_error from zerver.lib.validator import check_string, check_list, check_bool from zerver.models import PushDeviceToken, UserProfile -def add_push_device_token(request, user_profile, token_str, kind, ios_app_id=None): - # type: (HttpRequest, UserProfile, str, int, Optional[str]) -> HttpResponse - if token_str == '' or len(token_str) > 4096: - return json_error(_('Empty or invalid length token')) - - # If another user was previously logged in on the same device and didn't - # properly log out, the token will still be registered to the wrong account - PushDeviceToken.objects.filter(token=token_str).exclude(user=user_profile).delete() - - # Overwrite with the latest value - token, created = PushDeviceToken.objects.get_or_create(user=user_profile, - token=token_str, - defaults=dict( - kind=kind, - ios_app_id=ios_app_id)) - if not created: - token.last_updated = timezone.now() - token.save(update_fields=['last_updated']) - - return json_success() - @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 - return add_push_device_token(request, user_profile, token, PushDeviceToken.APNS, ios_app_id=appid) + 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_str=REQ("token")): # type: (HttpRequest, UserProfile, str) -> HttpResponse - return add_push_device_token(request, user_profile, token_str, PushDeviceToken.GCM) + add_push_device_token(user_profile, token_str, PushDeviceToken.GCM) + return json_success() def remove_push_device_token(request, user_profile, token_str, kind): # type: (HttpRequest, UserProfile, str, int) -> HttpResponse