2016-07-26 23:43:38 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-10-27 23:55:31 +02:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
|
|
|
|
from typing import Optional, Text
|
2016-07-26 23:43:38 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2017-03-06 04:11:13 +01:00
|
|
|
from zerver.lib.push_notifications import add_push_device_token, \
|
|
|
|
remove_push_device_token
|
2017-03-06 04:04:23 +01:00
|
|
|
from zerver.lib.request import has_request_variables, REQ, JsonableError
|
2016-07-26 23:43:38 +02:00
|
|
|
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
|
|
|
|
|
2017-03-06 04:04:23 +01:00
|
|
|
def validate_token(token_str):
|
|
|
|
# type: (str) -> None
|
|
|
|
if token_str == '' or len(token_str) > 4096:
|
|
|
|
raise JsonableError(_('Empty or invalid length token'))
|
|
|
|
|
2016-07-26 23:43:38 +02:00
|
|
|
@has_request_variables
|
2017-03-06 04:04:23 +01:00
|
|
|
def add_apns_device_token(request, user_profile, token=REQ(),
|
|
|
|
appid=REQ(default=settings.ZULIP_IOS_APP_ID)):
|
2016-07-26 23:43:38 +02:00
|
|
|
# type: (HttpRequest, UserProfile, str, str) -> HttpResponse
|
2017-03-06 04:04:23 +01:00
|
|
|
validate_token(token)
|
2017-03-06 03:57:31 +01:00
|
|
|
add_push_device_token(user_profile, token, PushDeviceToken.APNS, ios_app_id=appid)
|
|
|
|
return json_success()
|
2016-07-26 23:43:38 +02:00
|
|
|
|
|
|
|
@has_request_variables
|
2017-03-06 04:04:23 +01:00
|
|
|
def add_android_reg_id(request, user_profile, token=REQ()):
|
2016-07-26 23:43:38 +02:00
|
|
|
# type: (HttpRequest, UserProfile, str) -> HttpResponse
|
2017-03-06 04:04:23 +01:00
|
|
|
validate_token(token)
|
|
|
|
add_push_device_token(user_profile, token, PushDeviceToken.GCM)
|
2017-03-06 03:57:31 +01:00
|
|
|
return json_success()
|
2016-07-26 23:43:38 +02:00
|
|
|
|
|
|
|
@has_request_variables
|
|
|
|
def remove_apns_device_token(request, user_profile, token=REQ()):
|
|
|
|
# type: (HttpRequest, UserProfile, str) -> HttpResponse
|
2017-03-06 04:04:23 +01:00
|
|
|
validate_token(token)
|
2017-03-06 07:01:28 +01:00
|
|
|
remove_push_device_token(user_profile, token, PushDeviceToken.APNS)
|
2017-03-06 04:11:13 +01:00
|
|
|
return json_success()
|
2016-07-26 23:43:38 +02:00
|
|
|
|
|
|
|
@has_request_variables
|
|
|
|
def remove_android_reg_id(request, user_profile, token=REQ()):
|
|
|
|
# type: (HttpRequest, UserProfile, str) -> HttpResponse
|
2017-03-06 04:04:23 +01:00
|
|
|
validate_token(token)
|
2017-03-06 07:01:28 +01:00
|
|
|
remove_push_device_token(user_profile, token, PushDeviceToken.GCM)
|
2017-03-06 04:11:13 +01:00
|
|
|
return json_success()
|