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 notifications for description changes.

Eeshan Garg80f30f187e58
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index 9309868c7c6..80815c5e1b2 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -5116,7 +5116,39 @@ def do_rename_stream(stream: Stream, new_name: str, user_profile: UserProfile) -
return {"email_address": new_email}
-def do_change_stream_description(stream: Stream, new_description: str) -> None:
+def send_change_stream_description_notification(
+ stream: Stream, *, old_description: str, new_description: str, acting_user: UserProfile
+) -> None:
+ sender = get_system_bot(settings.NOTIFICATION_BOT, acting_user.realm_id)
+ user_mention = silent_mention_syntax_for_user(acting_user)
+
+ with override_language(stream.realm.default_language):
+ notification_string = _(
+ "{user} changed the description for this stream.\n"
+ "Old description:\n"
+ "``` quote\n"
+ "{old_description}\n"
+ "```\n"
+ "New description:\n"
+ "``` quote\n"
+ "{new_description}\n"
+ "```"
+ )
+ notification_string = notification_string.format(
+ user=user_mention,
+ old_description=old_description,
+ new_description=new_description,
+ )
+
+ internal_send_stream_message(
+ sender, stream, Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, notification_string
+ )
+
+
+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"])
@@ -5132,6 +5164,13 @@ def do_change_stream_description(stream: Stream, new_description: str) -> None:
)
send_event(stream.realm, event, can_access_stream_user_ids(stream))
+ send_change_stream_description_notification(
+ stream,
+ old_description=old_description,
+ new_description=new_description,
+ acting_user=acting_user,
+ )
+
def send_change_stream_message_retention_days_notification(
user_profile: UserProfile, stream: Stream, old_value: Optional[int], new_value: Optional[int]
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index e749a221b35..d5bdb599566 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -2532,9 +2532,12 @@ class SubscribeActionTest(BaseAction):
)
check_subscription_add("events[0]", events[0])
- action = lambda: do_change_stream_description(stream, "new description")
- events = self.verify_action(action, include_subscribers=include_subscribers)
+ action = lambda: do_change_stream_description(
+ stream, "new description", acting_user=self.example_user("hamlet")
+ )
+ events = self.verify_action(action, include_subscribers=include_subscribers, num_events=2)
check_stream_update("events[0]", events[0])
+ check_message("events[1]", events[1])
# Update stream privacy - make stream web public
action = lambda: do_change_stream_permission(
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index 611351905f6..a442a2df77f 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -879,7 +879,7 @@ class StreamAdminTest(ZulipTestCase):
self.subscribe(self.example_user("cordelia"), "private_stream")
events: List[Mapping[str, Any]] = []
- with self.tornado_redirected_to_list(events, expected_num_events=1):
+ with self.tornado_redirected_to_list(events, expected_num_events=2):
stream_id = get_stream("private_stream", user_profile.realm).id
result = self.client_patch(
f"/json/streams/{stream_id}",
@@ -890,7 +890,7 @@ class StreamAdminTest(ZulipTestCase):
cordelia = self.example_user("cordelia")
prospero = self.example_user("prospero")
- notified_user_ids = set(events[-1]["users"])
+ notified_user_ids = set(events[0]["users"])
self.assertIn(user_profile.id, notified_user_ids)
self.assertIn(cordelia.id, notified_user_ids)
self.assertNotIn(prospero.id, notified_user_ids)
@@ -1158,7 +1158,7 @@ class StreamAdminTest(ZulipTestCase):
self.subscribe(user_profile, "stream_name1")
events: List[Mapping[str, Any]] = []
- with self.tornado_redirected_to_list(events, expected_num_events=1):
+ with self.tornado_redirected_to_list(events, expected_num_events=2):
stream_id = get_stream("stream_name1", realm).id
result = self.client_patch(
f"/json/streams/{stream_id}",
@@ -1186,7 +1186,6 @@ class StreamAdminTest(ZulipTestCase):
self.assertIn(user_profile.id, notified_user_ids)
self.assertIn(self.example_user("prospero").id, notified_user_ids)
self.assertNotIn(self.example_user("polonius").id, notified_user_ids)
-
self.assertEqual("Test description", stream.description)
result = self.client_patch(f"/json/streams/{stream_id}", {"description": "a" * 1025})
@@ -1203,6 +1202,20 @@ class StreamAdminTest(ZulipTestCase):
stream = get_stream("stream_name1", realm)
self.assertEqual(stream.description, "a multi line description")
+ messages = get_topic_messages(user_profile, stream, "stream events")
+ expected_notification = (
+ f"@_**{user_profile.full_name}|{user_profile.id}** changed the description for this stream.\n"
+ "Old description:\n"
+ "``` quote\n"
+ "Test description\n"
+ "```\n"
+ "New description:\n"
+ "``` quote\n"
+ "a multi line description\n"
+ "```"
+ )
+ self.assertEqual(messages[-1].content, expected_notification)
+
# 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(
@@ -1228,16 +1241,29 @@ class StreamAdminTest(ZulipTestCase):
acting_user=None,
)
- with self.tornado_redirected_to_list(events, expected_num_events=1):
- stream_id = get_stream("stream_name1", realm).id
- result = self.client_patch(
- f"/json/streams/{stream_id}",
- {"description": "Test description"},
- )
+ stream_id = get_stream("stream_name1", realm).id
+ result = self.client_patch(
+ f"/json/streams/{stream_id}",
+ {"description": "Test description"},
+ )
self.assert_json_success(result)
stream = get_stream("stream_name1", realm)
self.assertEqual(stream.description, "Test description")
+ messages = get_topic_messages(user_profile, stream, "stream events")
+ expected_notification = (
+ f"@_**{user_profile.full_name}|{user_profile.id}** changed the description for this stream.\n"
+ "Old description:\n"
+ "``` quote\n"
+ "See https://zulip.com/team\n"
+ "```\n"
+ "New description:\n"
+ "``` quote\n"
+ "Test description\n"
+ "```"
+ )
+ self.assertEqual(messages[-1].content, expected_notification)
+
def test_change_stream_description_requires_admin(self) -> None:
user_profile = self.example_user("hamlet")
self.login_user(user_profile)
diff --git a/zerver/views/streams.py b/zerver/views/streams.py
index d5437f6cc9d..7ce854a17b7 100644
--- a/zerver/views/streams.py
+++ b/zerver/views/streams.py
@@ -284,7 +284,7 @@ def update_stream_backend(
if "\n" in description:
# We don't allow newline characters in stream descriptions.
description = description.replace("\n", " ")
- do_change_stream_description(stream, description)
+ do_change_stream_description(stream, description, acting_user=user_profile)
if new_name is not None:
new_name = new_name.strip()
if stream.name == new_name: