diff --git a/zerver/actions/scheduled_messages.py b/zerver/actions/scheduled_messages.py index 5668882606..bca56a7801 100644 --- a/zerver/actions/scheduled_messages.py +++ b/zerver/actions/scheduled_messages.py @@ -230,10 +230,10 @@ def send_scheduled_message(scheduled_message: ScheduledMessage) -> None: scheduled_message.realm, ) - # TODO: Store the resulting message ID on the scheduled_message object. - do_send_messages([send_request]) + message_id = do_send_messages([send_request])[0] + scheduled_message.delivered_message_id = message_id scheduled_message.delivered = True - scheduled_message.save(update_fields=["delivered"]) + scheduled_message.save(update_fields=["delivered", "delivered_message_id"]) notify_remove_scheduled_message(scheduled_message.sender, scheduled_message.id) diff --git a/zerver/migrations/0448_scheduledmessage_new_fields.py b/zerver/migrations/0448_scheduledmessage_new_fields.py index e173892953..e84b2503c9 100644 --- a/zerver/migrations/0448_scheduledmessage_new_fields.py +++ b/zerver/migrations/0448_scheduledmessage_new_fields.py @@ -1,5 +1,6 @@ # Generated by Django 4.2 on 2023-05-05 00:18 +import django.db.models.deletion from django.db import migrations, models @@ -19,4 +20,11 @@ class Migration(migrations.Migration): name="failure_message", field=models.TextField(null=True), ), + migrations.AddField( + model_name="scheduledmessage", + name="delivered_message", + field=models.ForeignKey( + null=True, on_delete=django.db.models.deletion.CASCADE, to="zerver.message" + ), + ), ] diff --git a/zerver/models.py b/zerver/models.py index c40242f2d7..fe0bda1f52 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -4325,6 +4325,7 @@ class ScheduledMessage(models.Model): realm = models.ForeignKey(Realm, on_delete=CASCADE) scheduled_timestamp = models.DateTimeField(db_index=True) delivered = models.BooleanField(default=False) + delivered_message = models.ForeignKey(Message, null=True, on_delete=CASCADE) has_attachment = models.BooleanField(default=False, db_index=True) # Metadata for messages that failed to send when their scheduled diff --git a/zerver/tests/test_retention.py b/zerver/tests/test_retention.py index 0d7335f2ba..c2ebea6ddd 100644 --- a/zerver/tests/test_retention.py +++ b/zerver/tests/test_retention.py @@ -1073,7 +1073,7 @@ class TestDoDeleteMessages(ZulipTestCase): message_ids = [self.send_stream_message(cordelia, "Verona", str(i)) for i in range(0, 10)] messages = Message.objects.filter(id__in=message_ids) - with self.assert_database_query_count(19): + with self.assert_database_query_count(20): do_delete_messages(realm, messages) self.assertFalse(Message.objects.filter(id__in=message_ids).exists())