scheduled_messages: Extract method to get undelivered scheduled messages.

This also changes key for `ID` of scheduled message from `message_id`
to `scheduled_message_id`.
This commit is contained in:
Aman Agrawal 2023-04-21 11:23:10 +00:00 committed by Tim Abbott
parent 4cb238fb6e
commit 4718eaa213
2 changed files with 18 additions and 9 deletions

View File

@ -1,7 +1,9 @@
from typing import List
from django.utils.translation import gettext as _
from zerver.lib.exceptions import ResourceNotFoundError
from zerver.models import ScheduledMessage, UserProfile
from zerver.models import ScheduledMessage, ScheduledMessageDict, UserProfile
def access_scheduled_message(
@ -11,3 +13,13 @@ def access_scheduled_message(
return ScheduledMessage.objects.get(id=scheduled_message_id, sender=user_profile)
except ScheduledMessage.DoesNotExist:
raise ResourceNotFoundError(_("Scheduled message does not exist"))
def get_undelivered_scheduled_messages(user_profile: UserProfile) -> List[ScheduledMessageDict]:
scheduled_messages = ScheduledMessage.objects.filter(
sender=user_profile, delivered=False, delivery_type=ScheduledMessage.SEND_LATER
).order_by("scheduled_timestamp")
scheduled_message_dicts: List[ScheduledMessageDict] = [
scheduled_message.to_dict() for scheduled_message in scheduled_messages
]
return scheduled_message_dicts

View File

@ -3,18 +3,15 @@ from django.http import HttpRequest, HttpResponse
from zerver.actions.scheduled_messages import delete_scheduled_message
from zerver.lib.request import has_request_variables
from zerver.lib.response import json_success
from zerver.models import ScheduledMessage, UserProfile
from zerver.lib.scheduled_messages import get_undelivered_scheduled_messages
from zerver.models import UserProfile
@has_request_variables
def fetch_scheduled_messages(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
scheduled_messages = ScheduledMessage.objects.filter(
sender=user_profile, delivered=False, delivery_type=ScheduledMessage.SEND_LATER
).order_by("scheduled_timestamp")
scheduled_message_dicts = [
scheduled_message.to_dict() for scheduled_message in scheduled_messages
]
return json_success(request, data={"scheduled_messages": scheduled_message_dicts})
return json_success(
request, data={"scheduled_messages": get_undelivered_scheduled_messages(user_profile)}
)
@has_request_variables