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.

direct_message: Allow DMs to bots and self regardless of policy.

sayyedarib5c993f047993
diff --git a/web/src/compose_recipient.js b/web/src/compose_recipient.js
index f69e4cb0bd0..45bd2809bf1 100644
--- a/web/src/compose_recipient.js
+++ b/web/src/compose_recipient.js
@@ -95,6 +95,7 @@ function update_fade() {
export function update_on_recipient_change() {
update_fade();
update_narrow_to_recipient_visibility();
+ check_posting_policy_for_compose_box();
}
export function get_posting_policy_error_message() {
@@ -212,7 +213,6 @@ export function on_compose_select_recipient_update() {
stream_bar.decorate(stream_id, $stream_header_colorblock);
}
- check_posting_policy_for_compose_box();
update_on_recipient_change();
}
diff --git a/web/src/compose_validate.js b/web/src/compose_validate.js
index f2011a753e5..aea1050bf2e 100644
--- a/web/src/compose_validate.js
+++ b/web/src/compose_validate.js
@@ -679,12 +679,9 @@ function validate_private_message() {
const user_ids = compose_pm_pill.get_user_ids();
const $banner_container = $("#compose_banners");
- if (
- realm.realm_private_message_policy ===
- settings_config.private_message_policy_values.disabled.code &&
- (user_ids.length !== 1 || !people.get_by_user_id(user_ids[0]).is_bot)
- ) {
- // Unless we're composing to a bot
+ const user_ids_string = user_ids.join(",");
+
+ if (!people.user_can_direct_message(user_ids_string)) {
compose_banner.show_error_message(
$t({defaultMessage: "Direct messages are disabled in this organization."}),
compose_banner.CLASSNAMES.private_messages_disabled,
diff --git a/web/src/people.ts b/web/src/people.ts
index 0a214c72443..8062e73fc5a 100644
--- a/web/src/people.ts
+++ b/web/src/people.ts
@@ -805,9 +805,12 @@ export function user_can_direct_message(recipient_ids_string: string): boolean {
// message to the target user (or group of users) represented by a
// user ids string.
- // Regardless of policy, we allow sending direct messages to bots.
+ // Regardless of policy, we allow sending direct messages to bots and to self.
const recipient_ids = user_ids_string_to_ids_array(recipient_ids_string);
- if (recipient_ids.length === 1 && is_valid_bot_user(recipient_ids[0])) {
+ if (
+ recipient_ids.length === 1 &&
+ (is_valid_bot_user(recipient_ids[0]) || is_my_user_id(recipient_ids[0]))
+ ) {
return true;
}
diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.py
index a6a680c4623..97e13a131b1 100644
--- a/zerver/actions/message_send.py
+++ b/zerver/actions/message_send.py
@@ -1530,8 +1530,10 @@ def check_private_message_policy(
realm: Realm, sender: UserProfile, user_profiles: Sequence[UserProfile]
) -> None:
if realm.private_message_policy == Realm.PRIVATE_MESSAGE_POLICY_DISABLED:
- if sender.is_bot or (len(user_profiles) == 1 and user_profiles[0].is_bot):
- # We allow direct messages only between users and bots,
+ if sender.is_bot or (
+ len(user_profiles) == 1 and (user_profiles[0].is_bot or user_profiles[0] == sender)
+ ):
+ # We allow direct messages only between users and bots or to oneself,
# to avoid breaking the tutorial as well as automated
# notifications from system bots to users.
return
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py
index 639bccf94f8..d2ba430c5a0 100644
--- a/zerver/tests/test_message_send.py
+++ b/zerver/tests/test_message_send.py
@@ -2613,6 +2613,73 @@ class InternalPrepTest(ZulipTestCase):
# wasn't automatically created.
Stream.objects.get(name=stream_name, realm_id=realm.id)
+ def test_direct_message_to_self_and_bot_in_dm_disabled_org(self) -> None:
+ """
+ Test that a user can send a direct message to themselves and to a bot in a DM disabled organization
+ """
+ sender = self.example_user("hamlet")
+ sender.realm.private_message_policy = Realm.PRIVATE_MESSAGE_POLICY_DISABLED
+ sender.realm.save()
+
+ # Create a non-bot user
+ recipient_user = self.example_user("othello")
+ recipient_user.realm = sender.realm
+
+ # Create a new bot user
+ bot = do_create_user(
+ password="",
+ realm=sender.realm,
+ full_name="Test Bot",
+ bot_type=UserProfile.DEFAULT_BOT,
+ bot_owner=sender,
+ acting_user=None,
+ )
+
+ # Test sending a message to self
+ result = self.api_post(
+ sender,
+ "/api/v1/messages",
+ {
+ "type": "private",
+ "to": orjson.dumps([sender.id]).decode(),
+ "content": "Test message to self",
+ },
+ )
+ self.assert_json_success(result)
+
+ msg = self.get_last_message()
+ expected = "Test message to self"
+ self.assertEqual(msg.content, expected)
+
+ # Test sending a message to non-bot user
+ result = self.api_post(
+ sender,
+ "/api/v1/messages",
+ {
+ "type": "private",
+ "to": orjson.dumps([recipient_user.id]).decode(),
+ "content": "Test message",
+ },
+ )
+ self.assert_json_error(result, "Direct messages are disabled in this organization.")
+
+ # Test sending a message to the bot
+ result = self.api_post(
+ sender,
+ "/api/v1/messages",
+ {
+ "type": "private",
+ "to": orjson.dumps([bot.id]).decode(),
+ "content": "Test message to bot",
+ },
+ )
+ self.assert_json_success(result)
+
+ msg = self.get_last_message()
+ expected = "Test message to bot"
+ self.assertEqual(msg.content, expected)
+
class TestCrossRealmPMs(ZulipTestCase):
def make_realm(self, domain: str) -> Realm: