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-04-28 17:42:23 +02:00
|
|
|
from zerver.models import (
|
|
|
|
DirectScheduledMessageAPI,
|
|
|
|
ScheduledMessage,
|
|
|
|
StreamScheduledMessageAPI,
|
|
|
|
UserProfile,
|
|
|
|
)
|
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,
|
|
|
|
) -> List[Union[DirectScheduledMessageAPI, StreamScheduledMessageAPI]]:
|
2023-04-21 13:23:10 +02:00
|
|
|
scheduled_messages = ScheduledMessage.objects.filter(
|
|
|
|
sender=user_profile, delivered=False, delivery_type=ScheduledMessage.SEND_LATER
|
|
|
|
).order_by("scheduled_timestamp")
|
2023-04-28 17:42:23 +02:00
|
|
|
scheduled_message_dicts: List[Union[DirectScheduledMessageAPI, StreamScheduledMessageAPI]] = [
|
2023-04-21 13:23:10 +02:00
|
|
|
scheduled_message.to_dict() for scheduled_message in scheduled_messages
|
|
|
|
]
|
|
|
|
return scheduled_message_dicts
|