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-06-19 22:09:35 +02:00
|
|
|
from zerver.decorator import human_users_only
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.push_notifications import (
|
|
|
|
add_push_device_token,
|
|
|
|
b64_to_hex,
|
|
|
|
remove_push_device_token,
|
|
|
|
)
|
|
|
|
from zerver.lib.request import REQ, JsonableError, has_request_variables
|
2019-02-02 23:53:22 +01:00
|
|
|
from zerver.lib.response import json_success
|
2016-07-26 23:43:38 +02:00
|
|
|
from zerver.models import PushDeviceToken, UserProfile
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2019-11-13 06:54:30 +01:00
|
|
|
def validate_token(token_str: str, kind: int) -> None:
|
2017-03-06 04:04:23 +01:00
|
|
|
if token_str == '' or len(token_str) > 4096:
|
|
|
|
raise JsonableError(_('Empty or invalid length token'))
|
2017-07-07 18:18:37 +02:00
|
|
|
if kind == PushDeviceToken.APNS:
|
|
|
|
# Validate that we can actually decode the token.
|
|
|
|
try:
|
|
|
|
b64_to_hex(token_str)
|
|
|
|
except Exception:
|
|
|
|
raise JsonableError(_('Invalid APNS token'))
|
2017-03-06 04:04:23 +01:00
|
|
|
|
2017-06-19 22:09:35 +02:00
|
|
|
@human_users_only
|
2016-07-26 23:43:38 +02:00
|
|
|
@has_request_variables
|
2017-12-24 05:04:09 +01:00
|
|
|
def add_apns_device_token(request: HttpRequest, user_profile: UserProfile,
|
2019-11-13 06:54:30 +01:00
|
|
|
token: str=REQ(),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
appid: str=REQ(default=settings.ZULIP_IOS_APP_ID),
|
2017-12-24 05:04:09 +01:00
|
|
|
) -> HttpResponse:
|
2017-07-07 18:18:37 +02:00
|
|
|
validate_token(token, PushDeviceToken.APNS)
|
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
|
|
|
|
2017-06-19 22:09:35 +02:00
|
|
|
@human_users_only
|
2016-07-26 23:43:38 +02:00
|
|
|
@has_request_variables
|
2017-10-27 02:18:49 +02:00
|
|
|
def add_android_reg_id(request: HttpRequest, user_profile: UserProfile,
|
2019-11-13 06:54:30 +01:00
|
|
|
token: str=REQ()) -> HttpResponse:
|
2017-07-07 18:18:37 +02:00
|
|
|
validate_token(token, PushDeviceToken.GCM)
|
2017-03-06 04:04:23 +01:00
|
|
|
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
|
|
|
|
2017-06-19 22:09:35 +02:00
|
|
|
@human_users_only
|
2016-07-26 23:43:38 +02:00
|
|
|
@has_request_variables
|
2017-10-27 02:18:49 +02:00
|
|
|
def remove_apns_device_token(request: HttpRequest, user_profile: UserProfile,
|
2019-11-13 06:54:30 +01:00
|
|
|
token: str=REQ()) -> HttpResponse:
|
2017-07-07 18:18:37 +02:00
|
|
|
validate_token(token, PushDeviceToken.APNS)
|
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
|
|
|
|
2017-06-19 22:09:35 +02:00
|
|
|
@human_users_only
|
2016-07-26 23:43:38 +02:00
|
|
|
@has_request_variables
|
2017-10-27 02:18:49 +02:00
|
|
|
def remove_android_reg_id(request: HttpRequest, user_profile: UserProfile,
|
2019-11-13 06:54:30 +01:00
|
|
|
token: str=REQ()) -> HttpResponse:
|
2017-07-07 18:18:37 +02:00
|
|
|
validate_token(token, PushDeviceToken.GCM)
|
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()
|