2023-10-12 09:13:34 +02:00
|
|
|
from typing import List, Optional
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2022-04-14 23:31:02 +02:00
|
|
|
from zerver.actions.typing import check_send_typing_notification, do_send_stream_typing_notification
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2020-12-24 21:00:20 +01:00
|
|
|
from zerver.lib.streams import access_stream_by_id, access_stream_for_send_message
|
2023-10-12 09:13:34 +02:00
|
|
|
from zerver.lib.validator import check_int, check_list, check_string_in
|
2023-10-01 20:59:21 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2021-02-05 19:32:46 +01:00
|
|
|
VALID_OPERATOR_TYPES = ["start", "stop"]
|
2024-04-10 20:48:10 +02:00
|
|
|
VALID_RECIPIENT_TYPES = ["direct", "stream", "channel"]
|
2021-02-05 19:32:46 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2016-10-12 20:57:59 +02:00
|
|
|
@has_request_variables
|
2017-12-18 15:07:43 +01:00
|
|
|
def send_notification_backend(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-04-17 17:02:07 +02:00
|
|
|
req_type: str = REQ(
|
2023-10-01 20:59:21 +02:00
|
|
|
"type", str_validator=check_string_in(VALID_RECIPIENT_TYPES), default="direct"
|
2021-04-04 12:11:19 +02:00
|
|
|
),
|
2021-02-05 19:32:46 +01:00
|
|
|
operator: str = REQ("op", str_validator=check_string_in(VALID_OPERATOR_TYPES)),
|
2023-10-12 09:13:34 +02:00
|
|
|
notification_to: Optional[List[int]] = REQ(
|
|
|
|
"to", json_validator=check_list(check_int), default=None
|
|
|
|
),
|
|
|
|
stream_id: Optional[int] = REQ(json_validator=check_int, default=None),
|
2020-12-24 21:00:20 +01:00
|
|
|
topic: Optional[str] = REQ("topic", default=None),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2023-04-17 17:02:07 +02:00
|
|
|
recipient_type_name = req_type
|
2024-04-10 20:48:10 +02:00
|
|
|
if recipient_type_name == "channel":
|
|
|
|
# For now, use "stream" from Message.API_RECIPIENT_TYPES.
|
|
|
|
# TODO: Use "channel" here, as well as in events and
|
|
|
|
# message (created, schdeduled, drafts) objects/dicts.
|
|
|
|
recipient_type_name = "stream"
|
|
|
|
|
2023-04-17 17:02:07 +02:00
|
|
|
if recipient_type_name == "stream":
|
2023-10-12 09:13:34 +02:00
|
|
|
if stream_id is None:
|
2024-04-16 15:52:21 +02:00
|
|
|
raise JsonableError(_("Missing '{var_name}' argument").format(var_name="stream_id"))
|
2023-10-12 09:13:34 +02:00
|
|
|
|
2020-12-24 21:00:20 +01:00
|
|
|
if topic is None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Missing topic"))
|
2020-12-24 21:00:20 +01:00
|
|
|
|
2021-07-25 21:30:44 +02:00
|
|
|
if not user_profile.send_stream_typing_notifications:
|
2024-04-16 15:52:21 +02:00
|
|
|
raise JsonableError(_("User has disabled typing notifications for channel messages"))
|
2021-07-25 21:30:44 +02:00
|
|
|
|
2020-12-24 21:00:20 +01:00
|
|
|
# Verify that the user has access to the stream and has
|
|
|
|
# permission to send messages to it.
|
|
|
|
stream = access_stream_by_id(user_profile, stream_id)[0]
|
|
|
|
access_stream_for_send_message(user_profile, stream, forwarder_user_profile=None)
|
|
|
|
do_send_stream_typing_notification(user_profile, operator, stream, topic)
|
|
|
|
else:
|
2023-10-12 09:13:34 +02:00
|
|
|
if notification_to is None:
|
|
|
|
raise JsonableError(_("Missing 'to' argument"))
|
|
|
|
|
|
|
|
user_ids = notification_to
|
2023-10-07 09:22:12 +02:00
|
|
|
to_length = len(user_ids)
|
|
|
|
if to_length == 0:
|
|
|
|
raise JsonableError(_("Empty 'to' list"))
|
|
|
|
|
2021-07-25 21:30:44 +02:00
|
|
|
if not user_profile.send_private_typing_notifications:
|
2023-01-24 15:36:03 +01:00
|
|
|
raise JsonableError(_("User has disabled typing notifications for direct messages"))
|
2021-07-25 21:30:44 +02:00
|
|
|
|
2020-12-24 21:00:20 +01:00
|
|
|
check_send_typing_notification(user_profile, user_ids, operator)
|
2021-04-01 17:16:52 +02:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|