realm_settings: Update do_set...default_setting to send event on commit.

Earlier, we were using 'send_event' in do_set_realm_user_default_setting
which can lead to a situation, if any db operation is added after
the 'send_event' in future, where we enqueue events but the action
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-22 14:09:11 +05:30 committed by Tim Abbott
parent 8ddaa949fc
commit 63064e926b
1 changed files with 15 additions and 15 deletions

View File

@ -452,6 +452,7 @@ def do_set_realm_zulip_update_announcements_stream(
) )
@transaction.atomic(durable=True)
def do_set_realm_user_default_setting( def do_set_realm_user_default_setting(
realm_user_default: RealmUserDefault, realm_user_default: RealmUserDefault,
name: str, name: str,
@ -463,21 +464,20 @@ def do_set_realm_user_default_setting(
realm = realm_user_default.realm realm = realm_user_default.realm
event_time = timezone_now() event_time = timezone_now()
with transaction.atomic(savepoint=False): setattr(realm_user_default, name, value)
setattr(realm_user_default, name, value) realm_user_default.save(update_fields=[name])
realm_user_default.save(update_fields=[name])
RealmAuditLog.objects.create( RealmAuditLog.objects.create(
realm=realm, realm=realm,
event_type=RealmAuditLog.REALM_DEFAULT_USER_SETTINGS_CHANGED, event_type=RealmAuditLog.REALM_DEFAULT_USER_SETTINGS_CHANGED,
event_time=event_time, event_time=event_time,
acting_user=acting_user, acting_user=acting_user,
extra_data={ extra_data={
RealmAuditLog.OLD_VALUE: old_value, RealmAuditLog.OLD_VALUE: old_value,
RealmAuditLog.NEW_VALUE: value, RealmAuditLog.NEW_VALUE: value,
"property": name, "property": name,
}, },
) )
event = dict( event = dict(
type="realm_user_settings_defaults", type="realm_user_settings_defaults",
@ -485,7 +485,7 @@ def do_set_realm_user_default_setting(
property=name, property=name,
value=value, value=value,
) )
send_event(realm, event, active_user_ids(realm.id)) send_event_on_commit(realm, event, active_user_ids(realm.id))
RealmDeactivationReasonType = Literal[ RealmDeactivationReasonType = Literal[