2020-12-24 21:00:20 +01: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
|
2021-02-05 19:32:46 +01:00
|
|
|
from zerver.lib.validator import check_int, check_list, check_string_in
|
2016-10-12 20:57:59 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2021-02-05 19:32:46 +01:00
|
|
|
VALID_OPERATOR_TYPES = ["start", "stop"]
|
2020-12-24 21:00:20 +01:00
|
|
|
VALID_MESSAGE_TYPES = ["private", "stream"]
|
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,
|
2021-04-04 12:11:19 +02:00
|
|
|
message_type: str = REQ(
|
|
|
|
"type", str_validator=check_string_in(VALID_MESSAGE_TYPES), default="private"
|
|
|
|
),
|
2021-02-05 19:32:46 +01:00
|
|
|
operator: str = REQ("op", str_validator=check_string_in(VALID_OPERATOR_TYPES)),
|
2020-12-24 21:00:20 +01:00
|
|
|
notification_to: List[int] = REQ("to", json_validator=check_list(check_int)),
|
|
|
|
topic: Optional[str] = REQ("topic", default=None),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2020-12-24 21:00:20 +01:00
|
|
|
to_length = len(notification_to)
|
|
|
|
|
|
|
|
if to_length == 0:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Empty 'to' list"))
|
2020-12-24 21:00:20 +01:00
|
|
|
|
|
|
|
if message_type == "stream":
|
|
|
|
if to_length > 1:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Cannot send to multiple streams"))
|
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:
|
|
|
|
raise JsonableError(_("User has disabled typing notifications for stream messages"))
|
|
|
|
|
2020-12-24 21:00:20 +01:00
|
|
|
stream_id = notification_to[0]
|
|
|
|
# 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:
|
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
|
|
|
user_ids = notification_to
|
|
|
|
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)
|