Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
settings: Add variable to control recipient type for 1:1 DMs.
Mohammad Reza Kianifarb4348bb3f0aa
diff --git a/analytics/tests/test_counts.py b/analytics/tests/test_counts.pyindex f239213ee51..deb3032bac7 100644--- a/analytics/tests/test_counts.py+++ b/analytics/tests/test_counts.py@@ -7,6 +7,7 @@ from unittest import mockimport time_machinefrom django.apps import appsfrom django.db.models import Sum+from django.test import override_settingsfrom django.utils.timezone import now as timezone_nowfrom psycopg2.sql import SQL, Literalfrom typing_extensions import override@@ -722,7 +723,8 @@ class TestCountStats(AnalyticsTestCase):)self.assertTableState(StreamCount, [], [])- def test_1_to_1_and_self_messages_sent_by_message_type_using_direct_group_message(self) -> None:+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)+ def test_1_to_1_and_self_messages_sent_by_message_type_using_direct_message_group(self) -> None:stat = COUNT_STATS["messages_sent:message_type:day"]self.current_property = stat.propertydiff --git a/zerver/lib/recipient_users.py b/zerver/lib/recipient_users.pyindex 13af3203542..2677ac52495 100644--- a/zerver/lib/recipient_users.py+++ b/zerver/lib/recipient_users.py@@ -1,5 +1,6 @@from collections.abc import Sequence+from django.conf import settingsfrom django.core.exceptions import ValidationErrorfrom django.utils.translation import gettext as _@@ -40,24 +41,25 @@ def get_recipient_from_user_profiles(user_ids = list(recipient_profiles_map)# Important note: We are transitioning 1:1 DMs and self DMs to use- # DirectMessageGroup as the Recipient type. If a- # DirectMessageGroup exists for the collection of user IDs, it is- # guaranteed to contain that entire DM conversation. If none+ # DirectMessageGroup as the Recipient type. If PREFER_DIRECT_MESSAGE_GROUP+ # is enabled and a DirectMessageGroup exists for the collection of user IDs,+ # it is guaranteed to contain that entire DM conversation. If none# exists, we use the legacy personal recipient (which may or may# not exist). Once the migration completes, this code path should# just call get_or_create_direct_message_group.if len(recipient_profiles_map) <= 2:- direct_message_group = get_direct_message_group(user_ids)- if direct_message_group:- # Use the existing direct message group as the preferred recipient.- return Recipient(- id=direct_message_group.recipient_id,- type=Recipient.DIRECT_MESSAGE_GROUP,- type_id=direct_message_group.id,- )-- # if no direct message group recipient exists, we need to- # force the direct message to be a personal internally.+ if settings.PREFER_DIRECT_MESSAGE_GROUP:+ direct_message_group = get_direct_message_group(user_ids)+ if direct_message_group:+ # Use the existing direct message group as the preferred recipient.+ return Recipient(+ id=direct_message_group.recipient_id,+ type=Recipient.DIRECT_MESSAGE_GROUP,+ type_id=direct_message_group.id,+ )++ # If no direct message group recipient exists, or if the setting+ # is disabled, we fall back to using personal recipients.del recipient_profiles_map[sender.id]if len(recipient_profiles_map) == 1:[recipient_user_profile] = recipient_profiles_map.values()diff --git a/zerver/management/commands/export_search.py b/zerver/management/commands/export_search.pyindex cbea71f6985..91e6cbf8acd 100644--- a/zerver/management/commands/export_search.py+++ b/zerver/management/commands/export_search.py@@ -11,6 +11,7 @@ from operator import or_from threading import Lock, Threadfrom typing import Any, NoReturn, Union+from django.conf import settingsfrom django.core.management.base import CommandErrorfrom django.db.models import Qfrom typing_extensions import override@@ -215,7 +216,7 @@ This is most often used for legal compliance.elif len(user_profiles) == 2:user_a, user_b = user_profilesdirect_message_group = get_direct_message_group(id_list=[user_a.id, user_b.id])- if direct_message_group:+ if direct_message_group and settings.PREFER_DIRECT_MESSAGE_GROUP:limits &= Q(recipient=direct_message_group.recipient)else:limits &= Q(recipient=user_a.recipient, sender=user_b) | Q(diff --git a/zerver/tests/test_channel_fetch.py b/zerver/tests/test_channel_fetch.pyindex 8953fbc489c..e472772012c 100644--- a/zerver/tests/test_channel_fetch.py+++ b/zerver/tests/test_channel_fetch.py@@ -842,7 +842,7 @@ class GetSubscribersTest(ZulipTestCase):polonius.id,]- with self.assert_database_query_count(52):+ with self.assert_database_query_count(49):self.subscribe_via_post(self.user_profile,stream_names,diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.pyindex a145a774a36..9064808ccb2 100644--- a/zerver/tests/test_email_mirror.py+++ b/zerver/tests/test_email_mirror.py@@ -1304,7 +1304,7 @@ class TestMissedMessageEmailMessages(ZulipTestCase):incoming_valid_message["To"] = mm_addressincoming_valid_message["Reply-to"] = self.example_email("othello")- with self.assert_database_query_count(18):+ with self.assert_database_query_count(17):process_message(incoming_valid_message)# confirm that Hamlet got the messagediff --git a/zerver/tests/test_embedded_bot_system.py b/zerver/tests/test_embedded_bot_system.pyindex d90e53bb752..17de3de861e 100644--- a/zerver/tests/test_embedded_bot_system.py+++ b/zerver/tests/test_embedded_bot_system.py@@ -1,6 +1,7 @@from unittest.mock import patchimport orjson+from django.test.utils import override_settingsfrom typing_extensions import overridefrom zerver.lib.bot_lib import EmbeddedBotQuitError@@ -38,6 +39,7 @@ class TestEmbeddedBotMessaging(ZulipTestCase):self.assert_length(display_recipient, 1)self.assertEqual(display_recipient[0]["email"], self.user_profile.email)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_pm_to_embedded_bot_using_direct_group_message(self) -> None:assert self.bot_profile is not Nonediff --git a/zerver/tests/test_example.py b/zerver/tests/test_example.pyindex b5486e104e7..f74b5d892f4 100644--- a/zerver/tests/test_example.py+++ b/zerver/tests/test_example.py@@ -364,7 +364,7 @@ class TestQueryCounts(ZulipTestCase):hamlet = self.example_user("hamlet")cordelia = self.example_user("cordelia")- with self.assert_database_query_count(17):+ with self.assert_database_query_count(16):self.send_personal_message(from_user=hamlet,to_user=cordelia,diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex 77f43dbc3fb..ef103022441 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -425,6 +425,7 @@ class NarrowBuilderTest(ZulipTestCase):"WHERE NOT ((flags & %(flags_1)s) != %(param_1)s AND realm_id = %(realm_id_1)s AND sender_id = %(sender_id_1)s AND recipient_id = %(recipient_id_1)s)",)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_add_term_using_dm_operator_the_same_user_as_operand_when_direct_message_group_exists(self,) -> None:@@ -447,6 +448,7 @@ class NarrowBuilderTest(ZulipTestCase):"WHERE (flags & %(flags_1)s) != %(param_1)s AND realm_id = %(realm_id_1)s AND (sender_id = %(sender_id_1)s AND recipient_id = %(recipient_id_1)s OR sender_id = %(sender_id_2)s AND recipient_id = %(recipient_id_2)s)",)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_add_term_using_dm_operator_and_self_and_user_as_operand_when_direct_message_group_exists(self,) -> None:@@ -2508,6 +2510,7 @@ class GetOldMessagesTest(ZulipTestCase):result = self.get_and_check_messages(dict(narrow=orjson.dumps(narrow).decode()))self.assertNotEqual(result["messages"], [])+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_get_1_to_1_messages_with_existent_group_dm(self) -> None:me = self.example_user("hamlet")other_user = self.example_user("iago")@@ -2532,6 +2535,7 @@ class GetOldMessagesTest(ZulipTestCase):self.assertIn(message["id"], message_ids)self.assertEqual(message["recipient_id"], direct_message_group.recipient_id)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_get_messages_to_self_with_existent_group_dm(self) -> None:me = self.example_user("hamlet")diff --git a/zerver/tests/test_message_flags.py b/zerver/tests/test_message_flags.pyindex 128dddd4003..66b5accc572 100644--- a/zerver/tests/test_message_flags.py+++ b/zerver/tests/test_message_flags.py@@ -3,6 +3,7 @@ from unittest import mockimport orjsonfrom django.db import connection+from django.test import override_settingsfrom typing_extensions import overridefrom zerver.actions.message_flags import do_update_message_flags@@ -1152,7 +1153,8 @@ class GetUnreadMsgsTest(ZulipTestCase):dict(other_user_id=cordelia.id),)- def test_raw_unread_personal_using_direct_group_message(self) -> None:+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)+ def test_raw_unread_personal_using_direct_message_group(self) -> None:cordelia = self.example_user("cordelia")othello = self.example_user("othello")hamlet = self.example_user("hamlet")@@ -1277,6 +1279,7 @@ class GetUnreadMsgsTest(ZulipTestCase):dict(other_user_id=hamlet.id),)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_raw_unread_personal_from_self_using_direct_message_group(self) -> None:hamlet = self.example_user("hamlet")diff --git a/zerver/tests/test_message_notification_emails.py b/zerver/tests/test_message_notification_emails.pyindex e8b498c8049..6d0dff0e3a0 100644--- a/zerver/tests/test_message_notification_emails.py+++ b/zerver/tests/test_message_notification_emails.py@@ -1297,6 +1297,7 @@ class TestMessageNotificationEmails(ZulipTestCase):email_subject = "Group DMs with iago and Iago"self._test_cases(msg_id, verify_body_include, email_subject)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_pm_link_in_missed_message_header_using_direct_message_group(self) -> None:cordelia = self.example_user("cordelia")hamlet = self.example_user("hamlet")@@ -1360,6 +1361,7 @@ class TestMessageNotificationEmails(ZulipTestCase):mail.outbox[2].alternatives[0][0],)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_sender_name_in_missed_pm_using_direct_message_group(self) -> None:hamlet = self.example_user("hamlet")iago = self.example_user("iago")@@ -1382,6 +1384,7 @@ class TestMessageNotificationEmails(ZulipTestCase):mail.outbox[0].alternatives[0][0],)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_your_name_in_missed_pm_to_self_using_direct_message_group(self) -> None:hamlet = self.example_user("hamlet")diff --git a/zerver/tests/test_message_report.py b/zerver/tests/test_message_report.pyindex 9cd4007bd0d..f7048eba0a2 100644--- a/zerver/tests/test_message_report.py+++ b/zerver/tests/test_message_report.py@@ -1,4 +1,5 @@from django.conf import settings+from django.test import override_settingsfrom typing_extensions import Any, overridefrom zerver.actions.realm_settings import do_set_realm_moderation_request_channel@@ -345,6 +346,7 @@ class ReportMessageTest(ZulipTestCase):assert len(reports) == 1self.assertEqual(reports[0]["content"], expected_message.strip())+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_personal_message_report_using_direct_message_group(self) -> None:direct_message_group = get_or_create_direct_message_group(id_list=[self.hamlet.id, self.reported_user.id],diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex 9041120cf83..7b700b4c6a9 100644--- a/zerver/tests/test_message_send.py+++ b/zerver/tests/test_message_send.py@@ -2498,6 +2498,7 @@ class PersonalMessageSendTest(ZulipTestCase):recipient = Recipient.objects.get(type_id=user_profile.id, type=Recipient.PERSONAL)self.assertEqual(most_recent_message(user_profile).recipient, recipient)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_personal_to_self_using_direct_message_group(self) -> None:"""If you send a personal to yourself using direct_message_group, only you see it.@@ -2543,7 +2544,7 @@ class PersonalMessageSendTest(ZulipTestCase):self.assertEqual(message_stream_count(receiver), receiver_messages + 1)direct_message_group = get_direct_message_group([sender.id, receiver.id])- if direct_message_group:+ if direct_message_group and settings.PREFER_DIRECT_MESSAGE_GROUP:recipient = Recipient.objects.get(type_id=direct_message_group.id, type=Recipient.DIRECT_MESSAGE_GROUP)@@ -2565,6 +2566,7 @@ class PersonalMessageSendTest(ZulipTestCase):receiver=self.example_user("othello"),)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_personal_using_direct_message_group(self) -> None:"""If you send a personal using direct_message_group, only you and the recipient see it.@@ -2620,7 +2622,7 @@ class PersonalMessageSendTest(ZulipTestCase):# Have the administrator send a message, and verify that allows the user to reply.self.send_personal_message(admin, user_profile)- with self.assert_database_query_count(17):+ with self.assert_database_query_count(16):self.send_personal_message(user_profile, admin)# Tests that user cannot initiate direct message thread in groups.@@ -2656,7 +2658,7 @@ class PersonalMessageSendTest(ZulipTestCase):user_group,acting_user=None,)- with self.assert_database_query_count(17):+ with self.assert_database_query_count(16):self.send_personal_message(user_profile, cordelia)# Test that query count decreases if setting is set to a system group.@@ -2670,7 +2672,7 @@ class PersonalMessageSendTest(ZulipTestCase):acting_user=None,)othello = self.example_user("othello")- with self.assert_database_query_count(16):+ with self.assert_database_query_count(15):self.send_personal_message(user_profile, othello)def test_direct_message_permission_group_setting(self) -> None:@@ -2698,7 +2700,7 @@ class PersonalMessageSendTest(ZulipTestCase):acting_user=None,)# Tests if the user is allowed to send to administrators.- with self.assert_database_query_count(17):+ with self.assert_database_query_count(16):self.send_personal_message(user_profile, admin)self.send_personal_message(admin, user_profile)# Tests if we can send messages to self irrespective of the value of the setting.@@ -2748,7 +2750,7 @@ class PersonalMessageSendTest(ZulipTestCase):with self.assertRaises(DirectMessagePermissionError):self.send_personal_message(cordelia, polonius)- with self.assert_database_query_count(17):+ with self.assert_database_query_count(16):self.send_personal_message(user_profile, cordelia)# Test that query count decreases if setting is set to a system group.@@ -2761,7 +2763,7 @@ class PersonalMessageSendTest(ZulipTestCase):members_group,acting_user=None,)- with self.assert_database_query_count(16):+ with self.assert_database_query_count(15):self.send_personal_message(user_profile, cordelia)do_change_realm_permission_group_setting(diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.pyindex f6b32cb2152..b532d77af94 100644--- a/zerver/tests/test_push_notifications.py+++ b/zerver/tests/test_push_notifications.py@@ -1510,6 +1510,7 @@ class TestGetAPNsPayload(PushNotificationTestCase):}self.assertDictEqual(payload, expected)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_get_message_payload_apns_personal_message_using_direct_message_group(self) -> None:user_profile = self.example_user("othello")diff --git a/zerver/tests/test_reminders.py b/zerver/tests/test_reminders.pyindex fbb4cd4b9d8..421777e2a48 100644--- a/zerver/tests/test_reminders.py+++ b/zerver/tests/test_reminders.py@@ -3,6 +3,7 @@ import timefrom typing import TYPE_CHECKINGimport time_machine+from django.test.utils import override_settingsfrom zerver.actions.scheduled_messages import try_deliver_one_scheduled_messagefrom zerver.lib.test_classes import ZulipTestCase@@ -117,6 +118,7 @@ class RemindersTest(ZulipTestCase):)self.assertEqual(scheduled_message.topic_name(), Message.DM_TOPIC)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_schedule_reminder_using_direct_message_group(self) -> None:hamlet = self.example_user("hamlet")othello = self.example_user("othello")diff --git a/zerver/tests/test_scheduled_messages.py b/zerver/tests/test_scheduled_messages.pyindex 9560851895d..9f52b8d1999 100644--- a/zerver/tests/test_scheduled_messages.py+++ b/zerver/tests/test_scheduled_messages.py@@ -7,6 +7,7 @@ from unittest import mockimport orjsonimport time_machine+from django.test import override_settingsfrom django.utils.timezone import now as timezone_nowfrom zerver.actions.scheduled_messages import (@@ -193,6 +194,7 @@ class ScheduledMessageTest(ZulipTestCase):)self.assert_json_error(updated_response, "Scheduled message was already sent")+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_successful_deliver_personal_scheduled_message_using_direct_message_group(self) -> None:# No scheduled messageself.assertFalse(try_deliver_one_scheduled_message())@@ -257,6 +259,7 @@ class ScheduledMessageTest(ZulipTestCase):self.assert_scheduled_message_delivered(scheduled_message, recipient=sender.recipient)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_successful_deliver_direct_scheduled_message_to_self_using_direct_message_group(self,) -> None:diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex 524ac98d3ce..200c1bc114a 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -1068,7 +1068,7 @@ class LoginTest(ZulipTestCase):# to sending messages, such as getting the welcome bot, looking up# the alert words for a realm, etc.with (- self.assert_database_query_count(97),+ self.assert_database_query_count(96),self.assert_memcached_count(18),self.captureOnCommitCallbacks(execute=True),):diff --git a/zerver/tests/test_tutorial.py b/zerver/tests/test_tutorial.pyindex 8e927e165a3..b8659ff1012 100644--- a/zerver/tests/test_tutorial.py+++ b/zerver/tests/test_tutorial.py@@ -1,4 +1,5 @@from django.conf import settings+from django.test import override_settingsfrom typing_extensions import overridefrom zerver.actions.message_send import internal_send_private_message@@ -146,6 +147,7 @@ class TutorialTests(ZulipTestCase):)self.assertEqual(most_recent_message(user).content, expected_response)+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)def test_response_to_pm_for_help_using_direct_message_group(self) -> None:user = self.example_user("hamlet")bot = get_system_bot(settings.WELCOME_BOT, user.realm_id)@@ -208,7 +210,8 @@ class TutorialTests(ZulipTestCase):)self.assertEqual(most_recent_message(user).content, expected_response)- def test_response_to_pm_for_undefined_using_direct_group_message(self) -> None:+ @override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)+ def test_response_to_pm_for_undefined_using_direct_message_group(self) -> None:user = self.example_user("hamlet")bot = get_system_bot(settings.WELCOME_BOT, user.realm_id)diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 8981d5fe318..94835d4f9c6 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1000,7 +1000,7 @@ class QueryCountTest(ZulipTestCase):prereg_user = PreregistrationUser.objects.get(email="[email protected]")with (- self.assert_database_query_count(88),+ self.assert_database_query_count(86),self.assert_memcached_count(23),self.capture_send_event_calls(expected_num_events=11) as events,):diff --git a/zproject/default_settings.py b/zproject/default_settings.pyindex c27893271fa..0b04beeee17 100644--- a/zproject/default_settings.py+++ b/zproject/default_settings.py@@ -748,3 +748,7 @@ SCIM_CONFIG: dict[str, SCIMConfigDict] = {}# Minimum number of subscribers in a channel for us to no longer# send full subscriber data to the client.MIN_PARTIAL_SUBSCRIBERS_CHANNEL_SIZE = 1000++# Whether to prefer direct message group over personal recipient+# for 1:1 or self messages.+PREFER_DIRECT_MESSAGE_GROUP = False