streams: Don't glue translated strings in new channel notification.

We now translate the whole notification message instead of substituting
an already translated policy_name. This avoids scenarios where only part
of the notification message is translated.

Fixes #30212.

Co-authored-by: Tanmay Kumar <tnmdotkr@gmail.com>
This commit is contained in:
Ritwik
2025-03-29 01:25:26 +05:30
committed by Tim Abbott
parent 187a008f13
commit 2429157498
2 changed files with 71 additions and 4 deletions

View File

@@ -894,6 +894,54 @@ class TestCreateStreams(ZulipTestCase):
created_stream = new_streams[0]
self.assertEqual(created_stream.creator_id, hamlet.id)
def test_channel_create_message_exists_for_all_policy_types(self) -> None:
"""
Create a channel for each policy type to ensure they all have a "new channel" message.
"""
# this is to check if the appropriate channel name is present in the "new channel" message
policy_key_map: dict[str, str] = {
"web_public": "**Web-public**",
"public": "**Public**",
"private_shared_history": "**Private, shared history**",
"private_protected_history": "**Private, protected history**",
}
for policy_key, policy_dict in Stream.PERMISSION_POLICIES.items():
channel_creator = self.example_user("desdemona")
subdomain = "zulip"
if policy_key == "public_protected_history":
# This is a special channel policy only available in Zephyr realms.
channel_creator = self.mit_user("starnine")
subdomain = "zephyr"
self.login_user(channel_creator)
new_channel_name = f"New {policy_key} channel"
result = self.api_post(
channel_creator,
"/json/users/me/subscriptions",
{
"subscriptions": orjson.dumps([{"name": new_channel_name}]).decode(),
"is_web_public": orjson.dumps(policy_dict["is_web_public"]).decode(),
"invite_only": orjson.dumps(policy_dict["invite_only"]).decode(),
"history_public_to_subscribers": orjson.dumps(
policy_dict["history_public_to_subscribers"]
).decode(),
},
subdomain=subdomain,
)
self.assert_json_success(result)
new_channel = get_stream(new_channel_name, channel_creator.realm)
channel_events_messages = get_topic_messages(
channel_creator, new_channel, "channel events"
)
if policy_key == "public_protected_history":
# These do not get channel creation notification.
self.assert_length(channel_events_messages, 0)
continue
self.assert_length(channel_events_messages, 1)
self.assertIn(policy_key_map[policy_key], channel_events_messages[0].content)
class RecipientTest(ZulipTestCase):
def test_recipient(self) -> None:

View File

@@ -885,16 +885,35 @@ def send_messages_for_new_subscribers(
history_public_to_subscribers=stream.history_public_to_subscribers,
is_web_public=stream.is_web_public,
)
new_channel_message = None
# Policy `public_protected_history` is missing here as those channels don't get
# channel creation notification.
if policy_key == "web_public":
new_channel_message = _(
"**Web-public** channel created by {user_name}. **Description:**"
)
elif policy_key == "public":
new_channel_message = _(
"**Public** channel created by {user_name}. **Description:**"
)
elif policy_key == "private_shared_history":
new_channel_message = _(
"**Private, shared history** channel created by {user_name}. **Description:**"
)
elif policy_key == "private_protected_history":
new_channel_message = _(
"**Private, protected history** channel created by {user_name}. **Description:**"
)
assert new_channel_message is not None
notifications.append(
internal_prep_stream_message(
sender=sender,
stream=stream,
topic_name=str(Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME),
content=_(
"**{policy}** channel created by {user_name}. **Description:**"
).format(
content=new_channel_message.format(
user_name=silent_mention_syntax_for_user(user_profile),
policy=Stream.PERMISSION_POLICIES[policy_key]["policy_name"],
)
+ f"\n```` quote\n{stream_description}\n````",
),