streams: Update do_change_stream_description to send event on commit.

Earlier, we were using 'send_event' in do_change_stream_description
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:45:07 +05:30 committed by Tim Abbott
parent e474ab7ee0
commit a9af1c0ce2
1 changed files with 17 additions and 18 deletions

View File

@ -1504,27 +1504,26 @@ def send_change_stream_description_notification(
)
@transaction.atomic(durable=True)
def do_change_stream_description(
stream: Stream, new_description: str, *, acting_user: UserProfile
) -> None:
old_description = stream.description
with transaction.atomic():
stream.description = new_description
stream.rendered_description = render_stream_description(new_description, stream.realm)
stream.save(update_fields=["description", "rendered_description"])
RealmAuditLog.objects.create(
realm=stream.realm,
acting_user=acting_user,
modified_stream=stream,
event_type=RealmAuditLog.STREAM_PROPERTY_CHANGED,
event_time=timezone_now(),
extra_data={
RealmAuditLog.OLD_VALUE: old_description,
RealmAuditLog.NEW_VALUE: new_description,
"property": "description",
},
)
stream.description = new_description
stream.rendered_description = render_stream_description(new_description, stream.realm)
stream.save(update_fields=["description", "rendered_description"])
RealmAuditLog.objects.create(
realm=stream.realm,
acting_user=acting_user,
modified_stream=stream,
event_type=RealmAuditLog.STREAM_PROPERTY_CHANGED,
event_time=timezone_now(),
extra_data={
RealmAuditLog.OLD_VALUE: old_description,
RealmAuditLog.NEW_VALUE: new_description,
"property": "description",
},
)
event = dict(
type="stream",
@ -1535,7 +1534,7 @@ def do_change_stream_description(
value=new_description,
rendered_description=stream.rendered_description,
)
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_description_notification(
stream,