schedules_messages: Move logic out of management command.

This commit is contained in:
Tim Abbott 2023-05-04 16:05:35 -07:00
parent 611cee177d
commit c8f3443fb6
2 changed files with 44 additions and 30 deletions

View File

@ -2,16 +2,25 @@ import datetime
from typing import List, Optional, Sequence, Tuple, Union from typing import List, Optional, Sequence, Tuple, Union
import orjson import orjson
from django.conf import settings
from django.db import transaction from django.db import transaction
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from zerver.actions.message_send import check_message from zerver.actions.message_send import build_message_send_dict, check_message, do_send_messages
from zerver.actions.uploads import check_attachment_reference_change, do_claim_attachments from zerver.actions.uploads import check_attachment_reference_change, do_claim_attachments
from zerver.lib.addressee import Addressee from zerver.lib.addressee import Addressee
from zerver.lib.exceptions import JsonableError from zerver.lib.exceptions import JsonableError
from zerver.lib.message import SendMessageRequest, render_markdown from zerver.lib.message import SendMessageRequest, render_markdown
from zerver.lib.scheduled_messages import access_scheduled_message from zerver.lib.scheduled_messages import access_scheduled_message
from zerver.models import Client, Realm, ScheduledMessage, UserProfile from zerver.models import (
Client,
Message,
Realm,
ScheduledMessage,
UserProfile,
get_user_by_delivery_email,
)
from zerver.tornado.django_api import send_event from zerver.tornado.django_api import send_event
@ -166,3 +175,33 @@ def delete_scheduled_message(user_profile: UserProfile, scheduled_message_id: in
"scheduled_message_id": scheduled_message_id, "scheduled_message_id": scheduled_message_id,
} }
send_event(user_profile.realm, event, [user_profile.id]) send_event(user_profile.realm, event, [user_profile.id])
def construct_send_request(scheduled_message: ScheduledMessage) -> SendMessageRequest:
message = Message()
original_sender = scheduled_message.sender
message.content = scheduled_message.content
message.recipient = scheduled_message.recipient
message.realm = scheduled_message.realm
message.subject = scheduled_message.subject
message.date_sent = timezone_now()
message.sending_client = scheduled_message.sending_client
delivery_type = scheduled_message.delivery_type
if delivery_type == ScheduledMessage.SEND_LATER:
message.sender = original_sender
elif delivery_type == ScheduledMessage.REMIND:
message.sender = get_user_by_delivery_email(
settings.NOTIFICATION_BOT, original_sender.realm
)
return build_message_send_dict(
message=message, stream=scheduled_message.stream, realm=scheduled_message.realm
)
def send_scheduled_message(scheduled_message: ScheduledMessage) -> None:
message_send_request = construct_send_request(scheduled_message)
do_send_messages([message_send_request])
scheduled_message.delivered = True
scheduled_message.save(update_fields=["delivered"])

View File

@ -8,10 +8,9 @@ from django.core.management.base import BaseCommand
from django.db import transaction from django.db import transaction
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from zerver.actions.message_send import build_message_send_dict, do_send_messages from zerver.actions.scheduled_messages import send_scheduled_message
from zerver.lib.logging_util import log_to_file from zerver.lib.logging_util import log_to_file
from zerver.lib.message import SendMessageRequest from zerver.models import ScheduledMessage
from zerver.models import Message, ScheduledMessage, get_user_by_delivery_email
## Setup ## ## Setup ##
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -27,28 +26,6 @@ This management command is run via supervisor.
Usage: ./manage.py deliver_scheduled_messages Usage: ./manage.py deliver_scheduled_messages
""" """
def construct_message(self, scheduled_message: ScheduledMessage) -> SendMessageRequest:
message = Message()
original_sender = scheduled_message.sender
message.content = scheduled_message.content
message.recipient = scheduled_message.recipient
message.realm = scheduled_message.realm
message.subject = scheduled_message.subject
message.date_sent = timezone_now()
message.sending_client = scheduled_message.sending_client
delivery_type = scheduled_message.delivery_type
if delivery_type == ScheduledMessage.SEND_LATER:
message.sender = original_sender
elif delivery_type == ScheduledMessage.REMIND:
message.sender = get_user_by_delivery_email(
settings.NOTIFICATION_BOT, original_sender.realm
)
return build_message_send_dict(
message=message, stream=scheduled_message.stream, realm=scheduled_message.realm
)
def handle(self, *args: Any, **options: Any) -> None: def handle(self, *args: Any, **options: Any) -> None:
try: try:
while True: while True:
@ -57,9 +34,7 @@ Usage: ./manage.py deliver_scheduled_messages
scheduled_timestamp__lte=timezone_now(), delivered=False scheduled_timestamp__lte=timezone_now(), delivered=False
).select_for_update() ).select_for_update()
for scheduled_message in messages_to_deliver: for scheduled_message in messages_to_deliver:
do_send_messages([self.construct_message(scheduled_message)]) send_scheduled_message(scheduled_message)
scheduled_message.delivered = True
scheduled_message.save(update_fields=["delivered"])
cur_time = timezone_now() cur_time = timezone_now()
time_next_min = (cur_time + timedelta(minutes=1)).replace(second=0, microsecond=0) time_next_min = (cur_time + timedelta(minutes=1)).replace(second=0, microsecond=0)