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 _
|
2024-06-06 14:05:03 +02:00
|
|
|
from pydantic import Json
|
|
|
|
from typing_extensions import Annotated, Literal
|
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
|
|
|
|
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
|
2024-06-06 14:05:03 +02:00
|
|
|
from zerver.lib.typed_endpoint import ApiParamConfig, OptionalTopic, typed_endpoint
|
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
|
|
|
|
2024-06-06 14:05:03 +02:00
|
|
|
@typed_endpoint
|
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,
|
2024-06-06 14:05:03 +02:00
|
|
|
*,
|
|
|
|
req_type: Annotated[Literal["direct", "stream", "channel"], ApiParamConfig("type")] = "direct",
|
|
|
|
operator: Annotated[Literal["start", "stop"], ApiParamConfig("op")],
|
2024-07-12 02:30:23 +02:00
|
|
|
notification_to: Annotated[Json[list[int] | None], ApiParamConfig("to")] = None,
|
|
|
|
stream_id: Json[int | None] = None,
|
2024-06-06 14:05:03 +02:00
|
|
|
topic: OptionalTopic = 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)
|