diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 790aa405529a3..726357dd3f3c0 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -738,22 +738,37 @@ def do_set_realm_authentication_methods(realm: Realm, def do_set_realm_message_editing(realm: Realm, allow_message_editing: bool, message_content_edit_limit_seconds: int, - allow_community_topic_editing: bool) -> None: + allow_community_topic_editing: bool, + acting_user: Optional[UserProfile]=None) -> None: + old_values = dict(allow_message_editing=realm.allow_message_editing, + message_content_edit_limit_seconds=realm.message_content_edit_limit_seconds, + allow_community_topic_editing=realm.allow_community_topic_editing) + realm.allow_message_editing = allow_message_editing realm.message_content_edit_limit_seconds = message_content_edit_limit_seconds realm.allow_community_topic_editing = allow_community_topic_editing - realm.save(update_fields=['allow_message_editing', - 'allow_community_topic_editing', - 'message_content_edit_limit_seconds', - ], - ) + + event_time = timezone_now() + updated_properties = dict(allow_message_editing=allow_message_editing, + message_content_edit_limit_seconds=message_content_edit_limit_seconds, + allow_community_topic_editing=allow_community_topic_editing) + + for updated_property, updated_value in updated_properties.items(): + if updated_value == old_values[updated_property]: + continue + RealmAuditLog.objects.create( + realm=realm, event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, event_time=event_time, + acting_user=acting_user, extra_data=ujson.dumps({ + RealmAuditLog.OLD_VALUE: {'property': updated_property, 'value': old_values[updated_property]}, + RealmAuditLog.NEW_VALUE: {'property': updated_property, 'value': updated_value} + })) + + realm.save(update_fields=list(updated_properties.keys())) event = dict( type="realm", op="update_dict", property="default", - data=dict(allow_message_editing=allow_message_editing, - message_content_edit_limit_seconds=message_content_edit_limit_seconds, - allow_community_topic_editing=allow_community_topic_editing), + data=updated_properties, ) send_event(realm, event, active_user_ids(realm.id)) diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py index 14774e8d565cb..4e36cea69ff09 100644 --- a/zerver/tests/test_audit_log.py +++ b/zerver/tests/test_audit_log.py @@ -24,6 +24,7 @@ do_reactivate_user, do_regenerate_api_key, do_set_realm_authentication_methods, + do_set_realm_message_editing, get_last_message_id, get_streams_traffic, ) @@ -264,3 +265,28 @@ def test_get_last_message_id(self) -> None: Message.objects.all().delete() self.assertEqual(get_last_message_id(), -1) + + def test_set_realm_message_editing(self) -> None: + now = timezone_now() + realm = get_realm('zulip') + user = self.example_user('hamlet') + old_values_expected = [{'property': 'message_content_edit_limit_seconds', 'value': realm.message_content_edit_limit_seconds}, + {'property': 'allow_community_topic_editing', 'value': realm.allow_community_topic_editing}] + + do_set_realm_message_editing(realm, True, 1000, False, acting_user=user) + realm_audit_logs = RealmAuditLog.objects.filter(realm=realm, event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_time__gte=now, acting_user=user) + self.assertEqual(realm_audit_logs.count(), 2) + + # allow_message_editing was already True. + new_values_expected = [{'property': 'message_content_edit_limit_seconds', 'value': 1000}, + {'property': 'allow_community_topic_editing', 'value': False}] + new_values_seen = [] + old_values_seen = [] + for realm_audit_log in realm_audit_logs: + extra_data = ujson.loads(realm_audit_log.extra_data) + new_values_seen.append(extra_data[RealmAuditLog.NEW_VALUE]) + old_values_seen.append(extra_data[RealmAuditLog.OLD_VALUE]) + + self.assertEqual(new_values_seen, new_values_expected) + self.assertEqual(old_values_seen, old_values_expected) diff --git a/zerver/views/realm.py b/zerver/views/realm.py index 807c77ccd02e0..4d9d39c793f07 100644 --- a/zerver/views/realm.py +++ b/zerver/views/realm.py @@ -154,7 +154,8 @@ def update_realm( allow_community_topic_editing = realm.allow_community_topic_editing do_set_realm_message_editing(realm, allow_message_editing, message_content_edit_limit_seconds, - allow_community_topic_editing) + allow_community_topic_editing, + acting_user=user_profile) data['allow_message_editing'] = allow_message_editing data['message_content_edit_limit_seconds'] = message_content_edit_limit_seconds data['allow_community_topic_editing'] = allow_community_topic_editing