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 message retention policy updates.

Eeshan Gargd2901892e2ad
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index 7ab0aef64b4..911c0aa30db 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -5016,9 +5016,49 @@ def do_change_stream_description(stream: Stream, new_description: str) -> None:
send_event(stream.realm, event, can_access_stream_user_ids(stream))
+def send_change_stream_message_retention_days_notification(
+ user_profile: UserProfile, stream: Stream, old_value: Optional[int], new_value: Optional[int]
+) -> None:
+ sender = get_system_bot(settings.NOTIFICATION_BOT, user_profile.realm_id)
+ user_mention = f"@_**{user_profile.full_name}|{user_profile.id}**"
+
+ # If switching from or to the organization's default retention policy,
+ # we want to take the realm's default into account.
+ if old_value is None:
+ old_value = stream.realm.message_retention_days
+ if new_value is None:
+ new_value = stream.realm.message_retention_days
+
+ with override_language(stream.realm.default_language):
+ if old_value == Stream.MESSAGE_RETENTION_SPECIAL_VALUES_MAP["unlimited"]:
+ notification_string = _(
+ "{user} has changed the [message retention period](/help/message-retention-policy) for this stream from "
+ "**Forever** to **{new_value} days**. Messages will be automatically deleted after {new_value} days."
+ )
+ notification_string = notification_string.format(user=user_mention, new_value=new_value)
+ elif new_value == Stream.MESSAGE_RETENTION_SPECIAL_VALUES_MAP["unlimited"]:
+ notification_string = _(
+ "{user} has changed the [message retention period](/help/message-retention-policy) for this stream from "
+ "**{old_value} days** to **Forever**."
+ )
+ notification_string = notification_string.format(user=user_mention, old_value=old_value)
+ else:
+ notification_string = _(
+ "{user} has changed the [message retention period](/help/message-retention-policy) for this stream from "
+ "**{old_value} days** to **{new_value} days**. Messages will be automatically deleted after {new_value} days."
+ )
+ notification_string = notification_string.format(
+ user=user_mention, old_value=old_value, new_value=new_value
+ )
+ internal_send_stream_message(
+ sender, stream, Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, notification_string
+ )
+
+
def do_change_stream_message_retention_days(
- stream: Stream, message_retention_days: Optional[int] = None
+ stream: Stream, acting_user: UserProfile, message_retention_days: Optional[int] = None
) -> None:
+ old_message_retention_days_value = stream.message_retention_days
stream.message_retention_days = message_retention_days
stream.save(update_fields=["message_retention_days"])
@@ -5031,6 +5071,12 @@ def do_change_stream_message_retention_days(
name=stream.name,
)
send_event(stream.realm, event, can_access_stream_user_ids(stream))
+ send_change_stream_message_retention_days_notification(
+ user_profile=acting_user,
+ stream=stream,
+ old_value=old_message_retention_days_value,
+ new_value=message_retention_days,
+ )
def set_realm_permissions_based_on_org_type(realm: Realm) -> None:
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index c7c59f745ed..6a78ff90397 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -2492,8 +2492,10 @@ class SubscribeActionTest(BaseAction):
events = self.verify_action(action, include_subscribers=include_subscribers, num_events=2)
check_stream_update("events[0]", events[0])
- action = lambda: do_change_stream_message_retention_days(stream, -1)
- events = self.verify_action(action, include_subscribers=include_subscribers, num_events=1)
+ action = lambda: do_change_stream_message_retention_days(
+ stream, self.example_user("hamlet"), -1
+ )
+ events = self.verify_action(action, include_subscribers=include_subscribers, num_events=2)
check_stream_update("events[0]", events[0])
# Subscribe to a totally new invite-only stream, so it's just Hamlet on it
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index d6f3755300a..9775f996750 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -36,6 +36,7 @@ from zerver.lib.actions import (
gather_subscriptions_helper,
get_average_weekly_stream_traffic,
get_default_streams_for_realm,
+ get_topic_messages,
lookup_default_stream_groups,
round_to_2_significant_digits,
validate_user_access_to_subscribers_helper,
@@ -1272,6 +1273,55 @@ class StreamAdminTest(ZulipTestCase):
stream = get_stream("stream_name1", user_profile.realm)
self.assertEqual(stream.stream_post_policy, policy)
+ def test_change_stream_message_retention_days_notifications(self) -> None:
+ user_profile = self.example_user("desdemona")
+ self.login_user(user_profile)
+ realm = user_profile.realm
+ do_change_realm_plan_type(realm, Realm.PLAN_TYPE_SELF_HOSTED, acting_user=None)
+ stream = self.subscribe(user_profile, "stream_name1")
+
+ # Go from realm default (forever) to 2 days
+ result = self.client_patch(
+ f"/json/streams/{stream.id}", {"message_retention_days": orjson.dumps(2).decode()}
+ )
+ self.assert_json_success(result)
+ messages = get_topic_messages(user_profile, stream, "stream events")
+ self.assert_length(messages, 1)
+ expected_notification = (
+ f"@_**Desdemona|{user_profile.id}** has changed the [message retention period](/help/message-retention-policy) "
+ "for this stream from **Forever** to **2 days**. Messages will be automatically "
+ "deleted after 2 days."
+ )
+ self.assertEqual(messages[0].content, expected_notification)
+
+ # Go from 2 days to 8 days
+ result = self.client_patch(
+ f"/json/streams/{stream.id}", {"message_retention_days": orjson.dumps(8).decode()}
+ )
+ self.assert_json_success(result)
+ messages = get_topic_messages(user_profile, stream, "stream events")
+ self.assert_length(messages, 2)
+ expected_notification = (
+ f"@_**Desdemona|{user_profile.id}** has changed the [message retention period](/help/message-retention-policy) "
+ "for this stream from **2 days** to **8 days**. Messages will be automatically "
+ "deleted after 8 days."
+ )
+ self.assertEqual(messages[1].content, expected_notification)
+
+ # Go from 2 days to realm default (None on stream, forever/-1 on realm)
+ result = self.client_patch(
+ f"/json/streams/{stream.id}",
+ {"message_retention_days": orjson.dumps("realm_default").decode()},
+ )
+ self.assert_json_success(result)
+ messages = get_topic_messages(user_profile, stream, "stream events")
+ self.assert_length(messages, 3)
+ expected_notification = (
+ f"@_**Desdemona|{user_profile.id}** has changed the [message retention period](/help/message-retention-policy) "
+ "for this stream from **8 days** to **Forever**."
+ )
+ self.assertEqual(messages[2].content, expected_notification)
+
def test_change_stream_message_retention_days(self) -> None:
user_profile = self.example_user("desdemona")
self.login_user(user_profile)
@@ -1286,7 +1336,7 @@ class StreamAdminTest(ZulipTestCase):
do_change_realm_plan_type(realm, Realm.PLAN_TYPE_SELF_HOSTED, acting_user=None)
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):
result = self.client_patch(
f"/json/streams/{stream.id}", {"message_retention_days": orjson.dumps(2).decode()}
)
@@ -1313,7 +1363,7 @@ class StreamAdminTest(ZulipTestCase):
self.assertNotIn(self.example_user("polonius").id, notified_user_ids)
self.assertEqual(stream.message_retention_days, 2)
- with self.tornado_redirected_to_list(events, expected_num_events=1):
+ with self.tornado_redirected_to_list(events, expected_num_events=2):
result = self.client_patch(
f"/json/streams/{stream.id}",
{"message_retention_days": orjson.dumps("unlimited").decode()},
@@ -1335,7 +1385,7 @@ class StreamAdminTest(ZulipTestCase):
stream = get_stream("stream_name1", realm)
self.assertEqual(stream.message_retention_days, -1)
- with self.tornado_redirected_to_list(events, expected_num_events=1):
+ with self.tornado_redirected_to_list(events, expected_num_events=2):
result = self.client_patch(
f"/json/streams/{stream.id}",
{"message_retention_days": orjson.dumps("realm_default").decode()},
diff --git a/zerver/views/streams.py b/zerver/views/streams.py
index 8d000dd36e9..aa6fa73f94a 100644
--- a/zerver/views/streams.py
+++ b/zerver/views/streams.py
@@ -272,10 +272,12 @@ def update_stream_backend(
if not user_profile.is_realm_owner:
raise OrganizationOwnerRequired()
user_profile.realm.ensure_not_on_limited_plan()
- message_retention_days_value = parse_message_retention_days(
+ new_message_retention_days_value = parse_message_retention_days(
message_retention_days, Stream.MESSAGE_RETENTION_SPECIAL_VALUES_MAP
)
- do_change_stream_message_retention_days(stream, message_retention_days_value)
+ do_change_stream_message_retention_days(
+ stream, user_profile, new_message_retention_days_value
+ )
if description is not None:
if "\n" in description: