Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

streams: Add RealmAuditLog entries for description changes.

Eeshan Gargf97093ba327e
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index 80815c5e1b2..928febc98d0 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -5149,9 +5149,25 @@ def do_change_stream_description(
stream: Stream, new_description: str, *, acting_user: UserProfile
) -> None:
old_description = stream.description
- stream.description = new_description
- stream.rendered_description = render_stream_description(new_description)
- stream.save(update_fields=["description", "rendered_description"])
+
+ with transaction.atomic():
+ stream.description = new_description
+ stream.rendered_description = render_stream_description(new_description)
+ 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=orjson.dumps(
+ {
+ RealmAuditLog.OLD_VALUE: old_description,
+ RealmAuditLog.NEW_VALUE: new_description,
+ "property": "description",
+ }
+ ).decode(),
+ )
event = dict(
type="stream",
diff --git a/zerver/models.py b/zerver/models.py
index 306684ddb33..12d0f29b2e4 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -3915,6 +3915,7 @@ class AbstractRealmAuditLog(models.Model):
STREAM_NAME_CHANGED = 603
STREAM_REACTIVATED = 604
STREAM_MESSAGE_RETENTION_DAYS_CHANGED = 605
+ STREAM_PROPERTY_CHANGED = 607
# The following values are only for RemoteZulipServerAuditLog
# Values should be exactly 10000 greater than the corresponding
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index a442a2df77f..c9d62a2a91e 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -1216,6 +1216,20 @@ class StreamAdminTest(ZulipTestCase):
)
self.assertEqual(messages[-1].content, expected_notification)
+ realm_audit_log = RealmAuditLog.objects.filter(
+ event_type=RealmAuditLog.STREAM_PROPERTY_CHANGED,
+ modified_stream=stream,
+ ).last()
+ assert realm_audit_log is not None
+ expected_extra_data = orjson.dumps(
+ {
+ RealmAuditLog.OLD_VALUE: "Test description",
+ RealmAuditLog.NEW_VALUE: "a multi line description",
+ "property": "description",
+ }
+ ).decode()
+ self.assertEqual(realm_audit_log.extra_data, expected_extra_data)
+
# Verify that we don't render inline URL previews in this code path.
with self.settings(INLINE_URL_EMBED_PREVIEW=True):
result = self.client_patch(
@@ -1264,6 +1278,20 @@ class StreamAdminTest(ZulipTestCase):
)
self.assertEqual(messages[-1].content, expected_notification)
+ realm_audit_log = RealmAuditLog.objects.filter(
+ event_type=RealmAuditLog.STREAM_PROPERTY_CHANGED,
+ modified_stream=stream,
+ ).last()
+ assert realm_audit_log is not None
+ expected_extra_data = orjson.dumps(
+ {
+ RealmAuditLog.OLD_VALUE: "See https://zulip.com/team",
+ RealmAuditLog.NEW_VALUE: "Test description",
+ "property": "description",
+ }
+ ).decode()
+ self.assertEqual(realm_audit_log.extra_data, expected_extra_data)
+
def test_change_stream_description_requires_admin(self) -> None:
user_profile = self.example_user("hamlet")
self.login_user(user_profile)