2023-04-17 15:02:07 +02:00
|
|
|
import datetime
|
2023-05-09 03:41:17 +02:00
|
|
|
import logging
|
2023-05-18 13:56:10 +02:00
|
|
|
from typing import List, Optional, Sequence, Tuple
|
2023-04-17 15:02:07 +02:00
|
|
|
|
2023-05-09 20:45:42 +02:00
|
|
|
import orjson
|
2023-05-11 18:12:03 +02:00
|
|
|
from django.conf import settings
|
2023-04-17 15:02:07 +02:00
|
|
|
from django.db import transaction
|
2023-05-09 03:26:52 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2023-04-17 15:02:07 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2023-05-11 18:12:03 +02:00
|
|
|
from django.utils.translation import override as override_language
|
2023-04-17 15:02:07 +02:00
|
|
|
|
2023-05-11 18:12:03 +02:00
|
|
|
from zerver.actions.message_send import (
|
|
|
|
check_message,
|
|
|
|
do_send_messages,
|
|
|
|
internal_send_private_message,
|
|
|
|
)
|
2023-05-07 20:04:37 +02:00
|
|
|
from zerver.actions.uploads import check_attachment_reference_change, do_claim_attachments
|
2023-04-17 15:02:07 +02:00
|
|
|
from zerver.lib.addressee import Addressee
|
2023-05-05 01:23:04 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError, RealmDeactivatedError, UserDeactivatedError
|
2023-04-17 15:02:07 +02:00
|
|
|
from zerver.lib.message import SendMessageRequest, render_markdown
|
|
|
|
from zerver.lib.scheduled_messages import access_scheduled_message
|
2023-05-05 01:05:35 +02:00
|
|
|
from zerver.models import (
|
|
|
|
Client,
|
|
|
|
Realm,
|
|
|
|
ScheduledMessage,
|
2023-05-05 21:50:51 +02:00
|
|
|
Subscription,
|
2023-05-05 01:05:35 +02:00
|
|
|
UserProfile,
|
2023-05-11 18:12:03 +02:00
|
|
|
get_system_bot,
|
2023-05-05 01:05:35 +02:00
|
|
|
)
|
2023-04-17 15:02:07 +02:00
|
|
|
from zerver.tornado.django_api import send_event
|
|
|
|
|
2023-05-09 03:26:52 +02:00
|
|
|
SCHEDULED_MESSAGE_LATE_CUTOFF_MINUTES = 10
|
|
|
|
|
2023-04-17 15:02:07 +02:00
|
|
|
|
2023-05-09 20:45:42 +02:00
|
|
|
def extract_stream_id(req_to: str) -> List[int]:
|
|
|
|
# Recipient should only be a single stream ID.
|
|
|
|
try:
|
|
|
|
stream_id = int(req_to)
|
|
|
|
except ValueError:
|
|
|
|
raise JsonableError(_("Invalid data type for stream ID"))
|
|
|
|
return [stream_id]
|
|
|
|
|
|
|
|
|
|
|
|
def extract_direct_message_recipient_ids(req_to: str) -> List[int]:
|
|
|
|
try:
|
|
|
|
user_ids = orjson.loads(req_to)
|
|
|
|
except orjson.JSONDecodeError:
|
|
|
|
user_ids = req_to
|
|
|
|
|
|
|
|
if not isinstance(user_ids, list):
|
|
|
|
raise JsonableError(_("Invalid data type for recipients"))
|
|
|
|
|
|
|
|
for user_id in user_ids:
|
|
|
|
if not isinstance(user_id, int):
|
|
|
|
raise JsonableError(_("Recipient list may only contain user IDs"))
|
|
|
|
|
|
|
|
return list(set(user_ids))
|
|
|
|
|
|
|
|
|
2023-04-17 15:02:07 +02:00
|
|
|
def check_schedule_message(
|
|
|
|
sender: UserProfile,
|
|
|
|
client: Client,
|
|
|
|
recipient_type_name: str,
|
2023-05-18 13:56:10 +02:00
|
|
|
message_to: List[int],
|
2023-04-17 15:02:07 +02:00
|
|
|
topic_name: Optional[str],
|
|
|
|
message_content: str,
|
|
|
|
scheduled_message_id: Optional[int],
|
|
|
|
deliver_at: datetime.datetime,
|
|
|
|
realm: Optional[Realm] = None,
|
|
|
|
forwarder_user_profile: Optional[UserProfile] = None,
|
|
|
|
) -> int:
|
|
|
|
addressee = Addressee.legacy_build(sender, recipient_type_name, message_to, topic_name)
|
|
|
|
send_request = check_message(
|
|
|
|
sender,
|
|
|
|
client,
|
|
|
|
addressee,
|
|
|
|
message_content,
|
|
|
|
realm=realm,
|
|
|
|
forwarder_user_profile=forwarder_user_profile,
|
|
|
|
)
|
|
|
|
send_request.deliver_at = deliver_at
|
|
|
|
|
|
|
|
if scheduled_message_id is not None:
|
|
|
|
return edit_scheduled_message(scheduled_message_id, send_request, sender)
|
|
|
|
|
2023-04-20 04:40:41 +02:00
|
|
|
return do_schedule_messages([send_request], sender)[0]
|
2023-04-17 15:02:07 +02:00
|
|
|
|
|
|
|
|
2023-04-20 04:40:41 +02:00
|
|
|
def do_schedule_messages(
|
|
|
|
send_message_requests: Sequence[SendMessageRequest], sender: UserProfile
|
|
|
|
) -> List[int]:
|
2023-05-07 20:04:37 +02:00
|
|
|
scheduled_messages: List[Tuple[ScheduledMessage, SendMessageRequest]] = []
|
2023-04-17 15:02:07 +02:00
|
|
|
|
|
|
|
for send_request in send_message_requests:
|
|
|
|
scheduled_message = ScheduledMessage()
|
|
|
|
scheduled_message.sender = send_request.message.sender
|
|
|
|
scheduled_message.recipient = send_request.message.recipient
|
|
|
|
topic_name = send_request.message.topic_name()
|
|
|
|
scheduled_message.set_topic_name(topic_name=topic_name)
|
|
|
|
rendering_result = render_markdown(
|
|
|
|
send_request.message, send_request.message.content, send_request.realm
|
|
|
|
)
|
|
|
|
scheduled_message.content = send_request.message.content
|
|
|
|
scheduled_message.rendered_content = rendering_result.rendered_content
|
|
|
|
scheduled_message.sending_client = send_request.message.sending_client
|
|
|
|
scheduled_message.stream = send_request.stream
|
|
|
|
scheduled_message.realm = send_request.realm
|
|
|
|
assert send_request.deliver_at is not None
|
|
|
|
scheduled_message.scheduled_timestamp = send_request.deliver_at
|
2023-04-20 04:38:38 +02:00
|
|
|
scheduled_message.delivery_type = ScheduledMessage.SEND_LATER
|
2023-04-17 15:02:07 +02:00
|
|
|
|
2023-05-07 20:04:37 +02:00
|
|
|
scheduled_messages.append((scheduled_message, send_request))
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
ScheduledMessage.objects.bulk_create(
|
|
|
|
[scheduled_message for scheduled_message, ignored in scheduled_messages]
|
|
|
|
)
|
|
|
|
for scheduled_message, send_request in scheduled_messages:
|
|
|
|
if do_claim_attachments(
|
|
|
|
scheduled_message, send_request.rendering_result.potential_attachment_path_ids
|
|
|
|
):
|
|
|
|
scheduled_message.has_attachment = True
|
|
|
|
scheduled_message.save(update_fields=["has_attachment"])
|
2023-04-17 15:02:07 +02:00
|
|
|
|
2023-04-20 04:40:41 +02:00
|
|
|
event = {
|
|
|
|
"type": "scheduled_messages",
|
|
|
|
"op": "add",
|
|
|
|
"scheduled_messages": [
|
2023-05-07 20:04:37 +02:00
|
|
|
scheduled_message.to_dict() for scheduled_message, ignored in scheduled_messages
|
2023-04-20 04:40:41 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
send_event(sender.realm, event, [sender.id])
|
2023-05-07 20:04:37 +02:00
|
|
|
return [scheduled_message.id for scheduled_message, ignored in scheduled_messages]
|
2023-04-17 15:02:07 +02:00
|
|
|
|
|
|
|
|
2023-05-13 10:48:07 +02:00
|
|
|
def notify_update_scheduled_message(
|
|
|
|
user_profile: UserProfile, scheduled_message: ScheduledMessage
|
|
|
|
) -> None:
|
|
|
|
event = {
|
|
|
|
"type": "scheduled_messages",
|
|
|
|
"op": "update",
|
|
|
|
"scheduled_message": scheduled_message.to_dict(),
|
|
|
|
}
|
|
|
|
send_event(user_profile.realm, event, [user_profile.id])
|
|
|
|
|
|
|
|
|
2023-04-17 15:02:07 +02:00
|
|
|
def edit_scheduled_message(
|
|
|
|
scheduled_message_id: int, send_request: SendMessageRequest, sender: UserProfile
|
|
|
|
) -> int:
|
|
|
|
with transaction.atomic():
|
|
|
|
scheduled_message_object = access_scheduled_message(sender, scheduled_message_id)
|
|
|
|
|
|
|
|
# Handles the race between us initiating this transaction and user sending us the edit request.
|
|
|
|
if scheduled_message_object.delivered is True:
|
|
|
|
raise JsonableError(_("Scheduled message was already sent"))
|
|
|
|
|
|
|
|
# Only override fields that user can change.
|
|
|
|
scheduled_message_object.recipient = send_request.message.recipient
|
|
|
|
topic_name = send_request.message.topic_name()
|
|
|
|
scheduled_message_object.set_topic_name(topic_name=topic_name)
|
|
|
|
rendering_result = render_markdown(
|
|
|
|
send_request.message, send_request.message.content, send_request.realm
|
|
|
|
)
|
|
|
|
scheduled_message_object.content = send_request.message.content
|
|
|
|
scheduled_message_object.rendered_content = rendering_result.rendered_content
|
|
|
|
scheduled_message_object.sending_client = send_request.message.sending_client
|
|
|
|
scheduled_message_object.stream = send_request.stream
|
|
|
|
assert send_request.deliver_at is not None
|
|
|
|
scheduled_message_object.scheduled_timestamp = send_request.deliver_at
|
2023-05-07 20:04:37 +02:00
|
|
|
|
|
|
|
scheduled_message_object.has_attachment = check_attachment_reference_change(
|
|
|
|
scheduled_message_object, rendering_result
|
|
|
|
)
|
|
|
|
|
2023-05-12 20:14:17 +02:00
|
|
|
# If the user is editing a scheduled message that the server tried
|
|
|
|
# and failed to send, we need to update the `failed` boolean field
|
|
|
|
# as well as the associated `failure_message` field.
|
|
|
|
if scheduled_message_object.failed:
|
|
|
|
scheduled_message_object.failed = False
|
|
|
|
scheduled_message_object.failure_message = None
|
|
|
|
|
2023-04-17 15:02:07 +02:00
|
|
|
scheduled_message_object.save()
|
2023-04-20 04:40:41 +02:00
|
|
|
|
2023-05-13 10:48:07 +02:00
|
|
|
notify_update_scheduled_message(sender, scheduled_message_object)
|
2023-04-17 15:02:07 +02:00
|
|
|
return scheduled_message_id
|
|
|
|
|
|
|
|
|
2023-05-05 00:54:17 +02:00
|
|
|
def notify_remove_scheduled_message(user_profile: UserProfile, scheduled_message_id: int) -> None:
|
2023-04-17 15:02:07 +02:00
|
|
|
event = {
|
2023-04-20 04:40:41 +02:00
|
|
|
"type": "scheduled_messages",
|
2023-04-17 15:02:07 +02:00
|
|
|
"op": "remove",
|
|
|
|
"scheduled_message_id": scheduled_message_id,
|
|
|
|
}
|
|
|
|
send_event(user_profile.realm, event, [user_profile.id])
|
2023-05-05 01:05:35 +02:00
|
|
|
|
|
|
|
|
2023-05-05 00:54:17 +02:00
|
|
|
def delete_scheduled_message(user_profile: UserProfile, scheduled_message_id: int) -> None:
|
|
|
|
scheduled_message_object = access_scheduled_message(user_profile, scheduled_message_id)
|
|
|
|
scheduled_message_id = scheduled_message_object.id
|
|
|
|
scheduled_message_object.delete()
|
|
|
|
|
|
|
|
notify_remove_scheduled_message(user_profile, scheduled_message_id)
|
|
|
|
|
|
|
|
|
2023-05-05 01:05:35 +02:00
|
|
|
def send_scheduled_message(scheduled_message: ScheduledMessage) -> None:
|
2023-05-05 01:17:12 +02:00
|
|
|
assert not scheduled_message.delivered
|
2023-05-05 01:23:04 +02:00
|
|
|
assert not scheduled_message.failed
|
|
|
|
|
2023-05-05 21:50:51 +02:00
|
|
|
# It's currently not possible to use the reminder feature.
|
|
|
|
assert scheduled_message.delivery_type == ScheduledMessage.SEND_LATER
|
|
|
|
|
2023-05-05 01:23:04 +02:00
|
|
|
# Repeat the checks from validate_account_and_subdomain, in case
|
|
|
|
# the state changed since the message as scheduled.
|
|
|
|
if scheduled_message.realm.deactivated:
|
|
|
|
raise RealmDeactivatedError
|
|
|
|
|
|
|
|
if not scheduled_message.sender.is_active:
|
|
|
|
raise UserDeactivatedError
|
|
|
|
|
2023-05-09 03:26:52 +02:00
|
|
|
# Limit how late we're willing to send a scheduled message.
|
|
|
|
latest_send_time = scheduled_message.scheduled_timestamp + datetime.timedelta(
|
|
|
|
minutes=SCHEDULED_MESSAGE_LATE_CUTOFF_MINUTES
|
|
|
|
)
|
|
|
|
if timezone_now() > latest_send_time:
|
2023-05-11 18:12:03 +02:00
|
|
|
raise JsonableError(_("Message could not be sent at the scheduled time."))
|
2023-05-09 03:26:52 +02:00
|
|
|
|
2023-05-05 21:50:51 +02:00
|
|
|
# Recheck that we have permission to send this message, in case
|
|
|
|
# permissions have changed since the message was scheduled.
|
|
|
|
if scheduled_message.stream is not None:
|
|
|
|
addressee = Addressee.for_stream(scheduled_message.stream, scheduled_message.topic_name())
|
|
|
|
else:
|
|
|
|
subscriber_ids = list(
|
|
|
|
Subscription.objects.filter(recipient=scheduled_message.recipient).values_list(
|
|
|
|
"user_profile_id", flat=True
|
|
|
|
)
|
|
|
|
)
|
|
|
|
addressee = Addressee.for_user_ids(subscriber_ids, scheduled_message.realm)
|
|
|
|
|
|
|
|
# Calling check_message again is important because permissions may
|
|
|
|
# have changed since the message was originally scheduled. This
|
|
|
|
# means that Markdown syntax referencing mutable organization data
|
|
|
|
# (for example, mentioning a user by name) will work (or not) as
|
|
|
|
# if the message was sent at the delviery time, not the sending
|
|
|
|
# time.
|
|
|
|
send_request = check_message(
|
|
|
|
scheduled_message.sender,
|
|
|
|
scheduled_message.sending_client,
|
|
|
|
addressee,
|
|
|
|
scheduled_message.content,
|
|
|
|
scheduled_message.realm,
|
|
|
|
)
|
|
|
|
|
2023-05-13 01:15:08 +02:00
|
|
|
scheduled_message_to_self = scheduled_message.recipient == scheduled_message.sender.recipient
|
|
|
|
message_id = do_send_messages(
|
|
|
|
[send_request], scheduled_message_to_self=scheduled_message_to_self
|
|
|
|
)[0]
|
2023-05-09 03:47:07 +02:00
|
|
|
scheduled_message.delivered_message_id = message_id
|
2023-05-05 01:05:35 +02:00
|
|
|
scheduled_message.delivered = True
|
2023-05-09 03:47:07 +02:00
|
|
|
scheduled_message.save(update_fields=["delivered", "delivered_message_id"])
|
2023-05-05 00:54:17 +02:00
|
|
|
notify_remove_scheduled_message(scheduled_message.sender, scheduled_message.id)
|
2023-05-09 03:41:17 +02:00
|
|
|
|
|
|
|
|
2023-05-11 18:12:03 +02:00
|
|
|
def send_failed_scheduled_message_notification(
|
|
|
|
user_profile: UserProfile, scheduled_message_id: int
|
|
|
|
) -> None:
|
|
|
|
scheduled_message = access_scheduled_message(user_profile, scheduled_message_id)
|
|
|
|
delivery_datetime_string = str(scheduled_message.scheduled_timestamp)
|
|
|
|
|
|
|
|
with override_language(user_profile.default_language):
|
|
|
|
error_string = scheduled_message.failure_message
|
|
|
|
delivery_time_markdown = f"<time:{delivery_datetime_string}> "
|
|
|
|
|
|
|
|
content = "".join(
|
|
|
|
[
|
|
|
|
_(
|
|
|
|
"The message you scheduled for {delivery_datetime} was not sent because of the following error:"
|
|
|
|
),
|
|
|
|
"\n\n",
|
|
|
|
"> {error_message}",
|
|
|
|
"\n\n",
|
|
|
|
_("[View scheduled messages](#scheduled)"),
|
|
|
|
"\n\n",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
content = content.format(
|
|
|
|
delivery_datetime=delivery_time_markdown,
|
|
|
|
error_message=error_string,
|
|
|
|
)
|
|
|
|
|
|
|
|
internal_send_private_message(
|
|
|
|
get_system_bot(settings.NOTIFICATION_BOT, user_profile.realm_id),
|
|
|
|
user_profile,
|
|
|
|
content,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-05-09 03:41:17 +02:00
|
|
|
@transaction.atomic
|
|
|
|
def try_deliver_one_scheduled_message(logger: logging.Logger) -> bool:
|
|
|
|
# Returns whether there was a scheduled message to attempt
|
|
|
|
# delivery on, regardless of whether delivery succeeded.
|
|
|
|
scheduled_message = (
|
|
|
|
ScheduledMessage.objects.filter(
|
|
|
|
scheduled_timestamp__lte=timezone_now(),
|
|
|
|
delivered=False,
|
|
|
|
failed=False,
|
|
|
|
)
|
|
|
|
.select_for_update()
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
if scheduled_message is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Sending scheduled message %s with date %s (sender: %s)",
|
|
|
|
scheduled_message.id,
|
|
|
|
scheduled_message.scheduled_timestamp,
|
|
|
|
scheduled_message.sender_id,
|
|
|
|
)
|
|
|
|
|
2023-05-11 18:12:03 +02:00
|
|
|
with override_language(scheduled_message.sender.default_language):
|
|
|
|
try:
|
|
|
|
send_scheduled_message(scheduled_message)
|
|
|
|
except Exception as e:
|
|
|
|
scheduled_message.refresh_from_db()
|
|
|
|
was_delivered = scheduled_message.delivered
|
|
|
|
scheduled_message.failed = True
|
|
|
|
|
|
|
|
if isinstance(e, JsonableError):
|
|
|
|
scheduled_message.failure_message = e.msg
|
|
|
|
logger.info("Failed with message: %s", e.msg)
|
|
|
|
else:
|
|
|
|
# An unexpected failure; store and send user a generic
|
|
|
|
# internal server error in notification message.
|
|
|
|
scheduled_message.failure_message = _("Internal server error")
|
|
|
|
logger.exception(
|
|
|
|
"Unexpected error sending scheduled message %s (sent: %s)",
|
|
|
|
scheduled_message.id,
|
|
|
|
was_delivered,
|
|
|
|
stack_info=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
scheduled_message.save(update_fields=["failed", "failure_message"])
|
|
|
|
|
|
|
|
if (
|
|
|
|
not was_delivered
|
|
|
|
# Do not send notification if either the realm or
|
|
|
|
# the sending user account has been deactivated.
|
|
|
|
and not isinstance(e, RealmDeactivatedError)
|
|
|
|
and not isinstance(e, UserDeactivatedError)
|
|
|
|
):
|
2023-05-13 10:49:01 +02:00
|
|
|
notify_update_scheduled_message(scheduled_message.sender, scheduled_message)
|
2023-05-11 18:12:03 +02:00
|
|
|
send_failed_scheduled_message_notification(
|
|
|
|
scheduled_message.sender, scheduled_message.id
|
|
|
|
)
|
2023-05-09 03:41:17 +02:00
|
|
|
|
|
|
|
return True
|