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_messages: Use DM groups for 1:1 or self DMs if present.

Mohammad Reza Kianifara0488715f306
diff --git a/zerver/lib/recipient_users.py b/zerver/lib/recipient_users.py
index f6a94fcdfe3..13af3203542 100644
--- a/zerver/lib/recipient_users.py
+++ b/zerver/lib/recipient_users.py
@@ -5,6 +5,7 @@ from django.utils.translation import gettext as _
from zerver.models import DirectMessageGroup, Recipient, UserProfile
from zerver.models.recipients import (
+ get_direct_message_group,
get_direct_message_group_hash,
get_or_create_direct_message_group,
)
@@ -34,25 +35,40 @@ def get_recipient_from_user_profiles(
if forwarder_user_profile.id not in recipient_profiles_map:
raise ValidationError(_("User not authorized for this query"))
- # If the direct message is just between the sender and
- # another person, force it to be a personal internally
- if len(recipient_profiles_map) == 2 and sender.id in recipient_profiles_map:
- del recipient_profiles_map[sender.id]
+ # Make sure the sender is included in the group direct messages.
+ recipient_profiles_map[sender.id] = sender
+ 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
+ # 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,
+ )
- assert recipient_profiles_map
- if len(recipient_profiles_map) == 1:
- [user_profile] = recipient_profiles_map.values()
+ # if no direct message group recipient exists, we need to
+ # force the direct message to be a personal internally.
+ del recipient_profiles_map[sender.id]
+ if len(recipient_profiles_map) == 1:
+ [recipient_user_profile] = recipient_profiles_map.values()
+ else:
+ recipient_user_profile = sender
return Recipient(
- id=user_profile.recipient_id,
+ id=recipient_user_profile.recipient_id,
type=Recipient.PERSONAL,
- type_id=user_profile.id,
+ type_id=recipient_user_profile.id,
)
- # Otherwise, we need a direct message group. Make sure the sender
- # is included in the group direct messages
- recipient_profiles_map[sender.id] = sender
-
- user_ids = list(recipient_profiles_map)
if create:
direct_message_group = get_or_create_direct_message_group(user_ids)
else:
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py
index 80e5bd9e8ec..6f7855674cf 100644
--- a/zerver/lib/test_classes.py
+++ b/zerver/lib/test_classes.py
@@ -1176,7 +1176,7 @@ Output:
read_by_sender: bool = True,
) -> int:
to_user_ids = [u.id for u in to_users]
- assert len(to_user_ids) >= 2
+ assert len(to_user_ids) >= 1
(sending_client, _) = Client.objects.get_or_create(name="test suite")
diff --git a/zerver/models/recipients.py b/zerver/models/recipients.py
index 01aad00b486..0dbcaafed91 100644
--- a/zerver/models/recipients.py
+++ b/zerver/models/recipients.py
@@ -191,3 +191,21 @@ def get_or_create_direct_message_group(id_list: list[int]) -> DirectMessageGroup
]
Subscription.objects.bulk_create(subs_to_create)
return direct_message_group
+
+
+def get_direct_message_group(id_list: list[int]) -> DirectMessageGroup | None:
+ """
+ Takes a list of user IDs and returns the DirectMessageGroup
+ object for the group consisting of these users if exists. If
+ the DirectMessageGroup object does not yet exist, it will
+ return None.
+ """
+
+ try:
+ direct_message_group_hash = get_direct_message_group_hash(id_list)
+ return DirectMessageGroup.objects.get(
+ huddle_hash=direct_message_group_hash,
+ group_size=len(id_list),
+ )
+ except DirectMessageGroup.DoesNotExist:
+ return None
diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.py
index 70cd4c4b5f3..38cbb56953c 100644
--- a/zerver/tests/test_email_mirror.py
+++ b/zerver/tests/test_email_mirror.py
@@ -1267,7 +1267,7 @@ class TestMissedMessageEmailMessages(ZulipTestCase):
incoming_valid_message["To"] = mm_address
incoming_valid_message["Reply-to"] = self.example_email("othello")
- with self.assert_database_query_count(17):
+ with self.assert_database_query_count(18):
process_message(incoming_valid_message)
# confirm that Hamlet got the message
diff --git a/zerver/tests/test_example.py b/zerver/tests/test_example.py
index f3e163396b1..9e2877f8016 100644
--- a/zerver/tests/test_example.py
+++ b/zerver/tests/test_example.py
@@ -363,7 +363,7 @@ class TestQueryCounts(ZulipTestCase):
hamlet = self.example_user("hamlet")
cordelia = self.example_user("cordelia")
- with self.assert_database_query_count(16):
+ with self.assert_database_query_count(17):
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.py
index dc08d92dc11..1262d134d10 100644
--- a/zerver/tests/test_message_fetch.py
+++ b/zerver/tests/test_message_fetch.py
@@ -2471,6 +2471,52 @@ class GetOldMessagesTest(ZulipTestCase):
result = self.get_and_check_messages(dict(narrow=orjson.dumps(narrow).decode()))
self.assertNotEqual(result["messages"], [])
+ def test_get_1_to_1_messages_with_existent_group_dm(self) -> None:
+ me = self.example_user("hamlet")
+ other_user = self.example_user("iago")
+
+ user_ids = [me.id, other_user.id]
+ direct_message_group = get_or_create_direct_message_group(user_ids)
+
+ self.login_user(me)
+ narrow = [dict(operator="dm", operand=user_ids)]
+ result = self.get_and_check_messages(dict(narrow=orjson.dumps(narrow).decode()))
+ self.assertEqual(result["messages"], [])
+
+ message_ids = [
+ self.send_group_direct_message(me, [other_user]),
+ self.send_group_direct_message(other_user, [me]),
+ self.send_personal_message(me, other_user),
+ self.send_personal_message(other_user, me),
+ ]
+
+ result = self.get_and_check_messages(dict(narrow=orjson.dumps(narrow).decode()))
+ for message in result["messages"]:
+ self.assertIn(message["id"], message_ids)
+ self.assertEqual(message["recipient_id"], direct_message_group.recipient_id)
+
+ def test_get_messages_to_self_with_existent_group_dm(self) -> None:
+ me = self.example_user("hamlet")
+
+ user_ids = [me.id]
+ direct_message_group = get_or_create_direct_message_group(user_ids)
+
+ self.login_user(me)
+ narrow = [dict(operator="dm", operand=user_ids)]
+ result = self.get_and_check_messages(dict(narrow=orjson.dumps(narrow).decode()))
+ self.assertEqual(result["messages"], [])
+
+ message_ids = [
+ self.send_group_direct_message(me, [me]),
+ self.send_personal_message(me, me),
+ ]
+
+ result = self.get_and_check_messages(dict(narrow=orjson.dumps(narrow).decode()))
+ for message in result["messages"]:
+ self.assertIn(message["id"], message_ids)
+ self.assertEqual(message["sender_id"], me.id)
+ self.assertEqual(message["recipient_id"], direct_message_group.recipient_id)
+
def test_get_visible_messages_with_narrow_dm(self) -> None:
me = self.example_user("hamlet")
self.login_user(me)
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py
index a938bba4a5f..917600f8ee9 100644
--- a/zerver/tests/test_message_send.py
+++ b/zerver/tests/test_message_send.py
@@ -75,7 +75,7 @@ from zerver.models import (
from zerver.models.constants import MAX_TOPIC_NAME_LENGTH
from zerver.models.groups import SystemGroups
from zerver.models.realms import get_realm
-from zerver.models.recipients import get_or_create_direct_message_group
+from zerver.models.recipients import get_direct_message_group, get_or_create_direct_message_group
from zerver.models.streams import get_stream
from zerver.models.users import get_system_bot, get_user
from zerver.views.message_send import InvalidMirrorInputError
@@ -2554,6 +2554,27 @@ class PersonalMessageSendTest(ZulipTestCase):
recipient = Recipient.objects.get(type_id=user_profile.id, type=Recipient.PERSONAL)
self.assertEqual(most_recent_message(user_profile).recipient, recipient)
+ 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.
+ """
+ user_profile = self.example_user("hamlet")
+
+ direct_message_group = get_or_create_direct_message_group([user_profile.id])
+
+ other_user_profiles = UserProfile.objects.filter(~Q(id=user_profile.id))
+ old_messages = list(map(message_stream_count, other_user_profiles))
+
+ self.login("hamlet")
+ self.send_personal_message(user_profile, user_profile)
+
+ new_messages = list(map(message_stream_count, other_user_profiles))
+ self.assertEqual(old_messages, new_messages)
+
+ self.assertEqual(
+ most_recent_message(user_profile).recipient, direct_message_group.recipient
+ )
+
def assert_personal(
self, sender: UserProfile, receiver: UserProfile, content: str = "testcontent"
) -> None:
@@ -2577,7 +2598,14 @@ class PersonalMessageSendTest(ZulipTestCase):
self.assertEqual(message_stream_count(sender), sender_messages + 1)
self.assertEqual(message_stream_count(receiver), receiver_messages + 1)
- recipient = Recipient.objects.get(type_id=receiver.id, type=Recipient.PERSONAL)
+ direct_message_group = get_direct_message_group([sender.id, receiver.id])
+ if direct_message_group:
+ recipient = Recipient.objects.get(
+ type_id=direct_message_group.id, type=Recipient.DIRECT_MESSAGE_GROUP
+ )
+ else:
+ recipient = Recipient.objects.get(type_id=receiver.id, type=Recipient.PERSONAL)
+
self.assertEqual(most_recent_message(sender).recipient, recipient)
self.assertEqual(most_recent_message(receiver).recipient, recipient)
@@ -2591,6 +2619,21 @@ class PersonalMessageSendTest(ZulipTestCase):
receiver=self.example_user("othello"),
)
+ 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.
+ """
+ sender = self.example_user("hamlet")
+ receiver = self.example_user("othello")
+
+ get_or_create_direct_message_group([sender.id, receiver.id])
+
+ self.login("hamlet")
+ self.assert_personal(
+ sender=sender,
+ receiver=receiver,
+ )
+
def test_direct_message_initiator_group_setting(self) -> None:
"""
Tests that direct_message_initiator_group_setting works correctly.
@@ -2631,7 +2674,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(16):
+ with self.assert_database_query_count(17):
self.send_personal_message(user_profile, admin)
# Tests that user cannot initiate direct message thread in groups.
@@ -2667,7 +2710,7 @@ class PersonalMessageSendTest(ZulipTestCase):
user_group,
acting_user=None,
)
- with self.assert_database_query_count(16):
+ with self.assert_database_query_count(17):
self.send_personal_message(user_profile, cordelia)
# Test that query count decreases if setting is set to a system group.
@@ -2681,7 +2724,7 @@ class PersonalMessageSendTest(ZulipTestCase):
acting_user=None,
)
othello = self.example_user("othello")
- with self.assert_database_query_count(15):
+ with self.assert_database_query_count(16):
self.send_personal_message(user_profile, othello)
def test_direct_message_permission_group_setting(self) -> None:
@@ -2709,7 +2752,7 @@ class PersonalMessageSendTest(ZulipTestCase):
acting_user=None,
)
# Tests if the user is allowed to send to administrators.
- with self.assert_database_query_count(16):
+ with self.assert_database_query_count(17):
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.
@@ -2759,7 +2802,7 @@ class PersonalMessageSendTest(ZulipTestCase):
with self.assertRaises(DirectMessagePermissionError):
self.send_personal_message(cordelia, polonius)
- with self.assert_database_query_count(16):
+ with self.assert_database_query_count(17):
self.send_personal_message(user_profile, cordelia)
# Test that query count decreases if setting is set to a system group.
@@ -2772,7 +2815,7 @@ class PersonalMessageSendTest(ZulipTestCase):
members_group,
acting_user=None,
)
- with self.assert_database_query_count(15):
+ with self.assert_database_query_count(16):
self.send_personal_message(user_profile, cordelia)
do_change_realm_permission_group_setting(
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py
index ef48ebca0a9..d1a9644445e 100644
--- a/zerver/tests/test_signup.py
+++ b/zerver/tests/test_signup.py
@@ -1032,7 +1032,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(96),
+ self.assert_database_query_count(97),
self.assert_memcached_count(18),
self.captureOnCommitCallbacks(execute=True),
):
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index 027584eeca0..624593b2fd5 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -8320,7 +8320,7 @@ class GetSubscribersTest(ZulipTestCase):
polonius.id,
]
- with self.assert_database_query_count(52):
+ with self.assert_database_query_count(55):
self.subscribe_via_post(
self.user_profile,
stream_names,
diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py
index f52d59b80ec..35b0b587b58 100644
--- a/zerver/tests/test_users.py
+++ b/zerver/tests/test_users.py
@@ -998,7 +998,7 @@ class QueryCountTest(ZulipTestCase):
prereg_user = PreregistrationUser.objects.get(email="[email protected]")
with (
- self.assert_database_query_count(86),
+ self.assert_database_query_count(88),
self.assert_memcached_count(23),
self.capture_send_event_calls(expected_num_events=11) as events,
):