streams: Fix error while sending notice in deactivated stream.

Sending messages to a deactivated stream is not allowed with
the exception of notices sent in "channel events" topic.

Earlier, notice sent to a deactivated stream when it is
deactivated was working correctly but it was resulting in
an error in the following cases:
* Renaming stream
* Changing stream description
* Changing message retention period
* Changing posting permission
* Changing access permission

This commit makes sure to send notice successfully in those cases.
This commit is contained in:
Prakhar Pratyush
2025-04-08 16:05:56 +05:30
committed by Tim Abbott
parent 4515c29d44
commit 7970e1fa45
2 changed files with 57 additions and 4 deletions

View File

@@ -1237,7 +1237,11 @@ def send_change_stream_permission_notification(
new_policy=new_policy_name,
)
internal_send_stream_message(
sender, stream, str(Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME), notification_string
sender,
stream,
str(Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME),
notification_string,
archived_channel_notice=stream.deactivated,
)
@@ -1467,7 +1471,11 @@ def send_stream_posting_permission_update_notification(
new_setting_description=new_setting_description,
)
internal_send_stream_message(
sender, stream, str(Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME), notification_string
sender,
stream,
str(Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME),
notification_string,
archived_channel_notice=stream.deactivated,
)
@@ -1526,6 +1534,7 @@ def do_rename_stream(stream: Stream, new_name: str, user_profile: UserProfile) -
old_channel_name=f"**{old_name}**",
new_channel_name=f"**{new_name}**",
),
archived_channel_notice=stream.deactivated,
)
@@ -1554,7 +1563,11 @@ def send_change_stream_description_notification(
)
internal_send_stream_message(
sender, stream, str(Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME), notification_string
sender,
stream,
str(Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME),
notification_string,
archived_channel_notice=stream.deactivated,
)
@@ -1644,7 +1657,11 @@ def send_change_stream_message_retention_days_notification(
summary_line=summary_line,
)
internal_send_stream_message(
sender, stream, str(Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME), notification_string
sender,
stream,
str(Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME),
notification_string,
archived_channel_notice=stream.deactivated,
)

View File

@@ -1154,6 +1154,42 @@ class StreamAdminTest(ZulipTestCase):
old_style.save()
self.assertEqual(set(deactivated_streams_by_old_name(realm, "old_style")), {old_style})
def test_archived_channel_notice(self) -> None:
desdemona = self.example_user("desdemona")
channel = get_stream("Denmark", desdemona.realm)
moderators_group = NamedUserGroup.objects.get(
name=SystemGroups.MODERATORS, realm=channel.realm, is_system_group=True
)
self.login_user(desdemona)
do_deactivate_stream(channel, acting_user=desdemona)
self.assertTrue(channel.deactivated)
param_to_notice_list = [
({"new_name": "New Denmark"}, f"@_**Desdemona|{desdemona.id}** renamed channel"),
(
{"description": "New description"},
f"@_**Desdemona|{desdemona.id}** changed the description",
),
(
{"message_retention_days": orjson.dumps(2).decode()},
f"@_**Desdemona|{desdemona.id}** has changed the [message retention period]",
),
(
{"can_send_message_group": orjson.dumps({"new": moderators_group.id}).decode()},
f"@_**Desdemona|{desdemona.id}** changed the [posting permissions]",
),
(
{"is_private": orjson.dumps(False).decode()},
f"@_**Desdemona|{desdemona.id}** changed the [access permissions]",
),
]
for param, notice in param_to_notice_list:
result = self.client_patch(f"/json/streams/{channel.id}", param)
self.assert_json_success(result)
message = self.get_last_message()
self.assertIn(notice, message.content)
def test_unarchive_stream_active_stream(self) -> None:
stream = self.make_stream("new_stream")
with self.assertRaisesRegex(JsonableError, "Channel is not currently deactivated"):