push_notifs: Add endpoint for sending a test notification.

Fixes #23997
This commit is contained in:
Mateusz Mandera
2023-10-05 13:53:09 +02:00
committed by Tim Abbott
parent f8a74831b0
commit d43be2b7c4
11 changed files with 389 additions and 10 deletions

View File

@@ -1,3 +1,5 @@
from typing import Optional
from django.conf import settings
from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _
@@ -8,6 +10,7 @@ from zerver.lib.push_notifications import (
add_push_device_token,
b64_to_hex,
remove_push_device_token,
send_test_push_notification,
)
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
@@ -66,3 +69,24 @@ def remove_android_reg_id(
validate_token(token, PushDeviceToken.GCM)
remove_push_device_token(user_profile, token, PushDeviceToken.GCM)
return json_success(request)
@human_users_only
@has_request_variables
def send_test_push_notification_api(
request: HttpRequest, user_profile: UserProfile, token: Optional[str] = REQ(default=None)
) -> HttpResponse:
# If a token is specified in the request, the test notification is supposed to be sent
# to that device. If no token is provided, the test notification should be sent to
# all devices registered for the user.
if token is not None:
try:
devices = [PushDeviceToken.objects.get(token=token, user=user_profile)]
except PushDeviceToken.DoesNotExist:
raise JsonableError(_("Token does not exist"))
else:
devices = list(PushDeviceToken.objects.filter(user=user_profile))
send_test_push_notification(user_profile, devices)
return json_success(request)