2023-04-28 17:42:23 +02:00
|
|
|
from typing import List, Union
|
2023-04-21 13:23:10 +02:00
|
|
|
|
2023-04-14 21:17:26 +02:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
|
|
from zerver.lib.exceptions import ResourceNotFoundError
|
2023-12-15 20:21:59 +01:00
|
|
|
from zerver.models import ScheduledMessage, UserProfile
|
|
|
|
from zerver.models.scheduled_jobs import (
|
2023-05-09 14:36:52 +02:00
|
|
|
APIScheduledDirectMessageDict,
|
|
|
|
APIScheduledStreamMessageDict,
|
2023-04-28 17:42:23 +02:00
|
|
|
)
|
2023-04-14 21:17:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def access_scheduled_message(
|
|
|
|
user_profile: UserProfile, scheduled_message_id: int
|
|
|
|
) -> ScheduledMessage:
|
|
|
|
try:
|
|
|
|
return ScheduledMessage.objects.get(id=scheduled_message_id, sender=user_profile)
|
|
|
|
except ScheduledMessage.DoesNotExist:
|
|
|
|
raise ResourceNotFoundError(_("Scheduled message does not exist"))
|
2023-04-21 13:23:10 +02:00
|
|
|
|
|
|
|
|
2023-04-28 17:42:23 +02:00
|
|
|
def get_undelivered_scheduled_messages(
|
|
|
|
user_profile: UserProfile,
|
2023-05-09 14:36:52 +02:00
|
|
|
) -> List[Union[APIScheduledDirectMessageDict, APIScheduledStreamMessageDict]]:
|
2023-04-21 13:23:10 +02:00
|
|
|
scheduled_messages = ScheduledMessage.objects.filter(
|
2023-08-30 21:19:37 +02:00
|
|
|
realm_id=user_profile.realm_id,
|
2023-05-05 01:23:04 +02:00
|
|
|
sender=user_profile,
|
|
|
|
# Notably, we don't require failed=False, since we will want
|
|
|
|
# to display those to users.
|
|
|
|
delivered=False,
|
|
|
|
delivery_type=ScheduledMessage.SEND_LATER,
|
2023-04-21 13:23:10 +02:00
|
|
|
).order_by("scheduled_timestamp")
|
2023-05-09 14:36:52 +02:00
|
|
|
scheduled_message_dicts: List[
|
|
|
|
Union[APIScheduledDirectMessageDict, APIScheduledStreamMessageDict]
|
|
|
|
] = [scheduled_message.to_dict() for scheduled_message in scheduled_messages]
|
2023-04-21 13:23:10 +02:00
|
|
|
return scheduled_message_dicts
|