streams: Update do_change...retention_days to send event on commit.

Earlier, we were using 'send_event' in
do_change_stream_message_retention_days which can lead to a situation
where we enqueue events but the function fails at a later stage.

Events should not be sent until we know we're not rolling back.

Fixes part of #30489.
This commit is contained in:
Prakhar Pratyush 2024-08-19 14:56:53 +05:30 committed by Tim Abbott
parent a9af1c0ce2
commit 2406a3feea
1 changed files with 16 additions and 17 deletions

View File

@ -69,7 +69,7 @@ from zerver.models import (
)
from zerver.models.groups import SystemGroups
from zerver.models.users import active_non_guest_user_ids, active_user_ids, get_system_bot
from zerver.tornado.django_api import send_event, send_event_on_commit
from zerver.tornado.django_api import send_event_on_commit
def send_user_remove_events_on_stream_deactivation(
@ -1592,12 +1592,11 @@ def send_change_stream_message_retention_days_notification(
)
@transaction.atomic(durable=True)
def do_change_stream_message_retention_days(
stream: Stream, acting_user: UserProfile, message_retention_days: int | None = None
) -> None:
old_message_retention_days_value = stream.message_retention_days
with transaction.atomic():
stream.message_retention_days = message_retention_days
stream.save(update_fields=["message_retention_days"])
RealmAuditLog.objects.create(
@ -1620,7 +1619,7 @@ def do_change_stream_message_retention_days(
stream_id=stream.id,
name=stream.name,
)
send_event(stream.realm, event, can_access_stream_user_ids(stream))
send_event_on_commit(stream.realm, event, can_access_stream_user_ids(stream))
send_change_stream_message_retention_days_notification(
user_profile=acting_user,
stream=stream,