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.

message_send: Use stream object for name in "no subscribers" message.

Alex Vandiverf78c2eab2cc4
diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.py
index 6b62928dbd7..3c8f25ea648 100644
--- a/zerver/actions/message_send.py
+++ b/zerver/actions/message_send.py
@@ -1240,22 +1240,28 @@ def send_pm_if_empty_stream(
if not sender.is_bot or sender.bot_owner is None:
return
- arg_dict = {
- "bot_identity": f"`{sender.delivery_email}`",
- "stream_id": stream_id,
- "stream_name": f"#**{stream_name}**",
- "new_stream_link": "#streams/new",
- }
if sender.bot_owner is not None:
with override_language(sender.bot_owner.default_language):
+ arg_dict: Dict[str, Any] = {
+ "bot_identity": f"`{sender.delivery_email}`",
+ }
if stream is None:
if stream_id is not None:
+ arg_dict = {
+ **arg_dict,
+ "stream_id": stream_id,
+ }
content = _(
"Your bot {bot_identity} tried to send a message to stream ID "
"{stream_id}, but there is no stream with that ID."
).format(**arg_dict)
else:
assert stream_name is not None
+ arg_dict = {
+ **arg_dict,
+ "stream_name": f"#**{stream_name}**",
+ "new_stream_link": "#streams/new",
+ }
content = _(
"Your bot {bot_identity} tried to send a message to stream "
"{stream_name}, but that stream does not exist. "
@@ -1264,6 +1270,10 @@ def send_pm_if_empty_stream(
else:
if num_subscribers_for_stream_id(stream.id) > 0:
return
+ arg_dict = {
+ **arg_dict,
+ "stream_name": f"#**{stream.name}**",
+ }
content = _(
"Your bot {bot_identity} tried to send a message to "
"stream {stream_name}. The stream exists but "
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py
index 2a918b944a9..2eac9c8c4d2 100644
--- a/zerver/tests/test_message_send.py
+++ b/zerver/tests/test_message_send.py
@@ -35,6 +35,7 @@ from zerver.lib.addressee import Addressee
from zerver.lib.cache import cache_delete, get_stream_cache_key
from zerver.lib.exceptions import JsonableError
from zerver.lib.message import MessageDict, get_raw_unread_data, get_recent_private_conversations
+from zerver.lib.streams import create_stream_if_needed
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import (
get_user_messages,
@@ -145,6 +146,76 @@ class MessagePOSTTest(ZulipTestCase):
)
self.assertEqual(msg.content, expected)
+ def test_message_to_stream_with_no_subscribers(self) -> None:
+ """
+ Sending a message to an empty stream succeeds, but sends a warning
+ to the owner.
+ """
+ realm = get_realm("zulip")
+ cordelia = self.example_user("cordelia")
+ bot = self.create_test_bot(
+ short_name="whatever",
+ user_profile=cordelia,
+ )
+ stream = create_stream_if_needed(realm, "Acropolis")[0]
+ result = self.api_post(
+ bot,
+ "/api/v1/messages",
+ {
+ "type": "stream",
+ "to": orjson.dumps(stream.name).decode(),
+ "content": "Stream message to an empty stream by name.",
+ "topic": "Test topic for empty stream name message",
+ },
+ )
+ self.assert_json_success(result)
+
+ msg = self.get_last_message()
+ expected = "Stream message to an empty stream by name."
+ self.assertEqual(msg.content, expected)
+
+ msg = self.get_second_to_last_message()
+ expected = (
+ "Your bot `[email protected]` tried to send a message to "
+ "stream #**Acropolis**. The stream exists but does not have any subscribers."
+ )
+ self.assertEqual(msg.content, expected)
+
+ def test_message_to_stream_with_no_subscribers_by_id(self) -> None:
+ """
+ Sending a message to an empty stream succeeds, but sends a warning
+ to the owner.
+ """
+ realm = get_realm("zulip")
+ cordelia = self.example_user("cordelia")
+ bot = self.create_test_bot(
+ short_name="whatever",
+ user_profile=cordelia,
+ )
+ stream = create_stream_if_needed(realm, "Acropolis")[0]
+ result = self.api_post(
+ bot,
+ "/api/v1/messages",
+ {
+ "type": "stream",
+ "to": orjson.dumps([stream.id]).decode(),
+ "content": "Stream message to an empty stream by id.",
+ "topic": "Test topic for empty stream id message",
+ },
+ )
+ self.assert_json_success(result)
+
+ msg = self.get_last_message()
+ expected = "Stream message to an empty stream by id."
+ self.assertEqual(msg.content, expected)
+
+ msg = self.get_second_to_last_message()
+ expected = (
+ "Your bot `[email protected]` tried to send a message to "
+ "stream #**Acropolis**. The stream exists but does not have any subscribers."
+ )
+ self.assertEqual(msg.content, expected)
+
def test_message_to_stream_by_id(self) -> None:
"""
Sending a message to a stream (by stream ID) to which you are