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.

narrow: Do not create huddles when fetching messages.

Alex Vandiver16a08a8b6b35
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.py
index b5178fb6b6c..d4eb34a8ddf 100644
--- a/zerver/lib/narrow.py
+++ b/zerver/lib/narrow.py
@@ -28,6 +28,7 @@ from sqlalchemy.sql import (
Select,
and_,
column,
+ false,
func,
join,
literal,
@@ -70,7 +71,7 @@ from zerver.lib.validator import (
check_string_or_int,
check_string_or_int_list,
)
-from zerver.models import Realm, Recipient, Stream, Subscription, UserMessage, UserProfile
+from zerver.models import Huddle, Realm, Recipient, Stream, Subscription, UserMessage, UserProfile
from zerver.models.streams import get_active_streams
from zerver.models.users import (
get_user_by_id_in_realm_including_cross_realm,
@@ -522,9 +523,13 @@ class NarrowBuilder:
forwarder_user_profile=None,
sender=self.user_profile,
allow_deactivated=True,
+ create=False,
)
except (JsonableError, ValidationError):
raise BadNarrowOperatorError("unknown user in " + str(operand))
+ except Huddle.DoesNotExist:
+ # Group DM where huddle doesn't exist
+ return query.where(maybe_negate(false()))
# Group direct message
if recipient.type == Recipient.DIRECT_MESSAGE_GROUP:
diff --git a/zerver/lib/recipient_users.py b/zerver/lib/recipient_users.py
index cd97acad6ef..0529d958513 100644
--- a/zerver/lib/recipient_users.py
+++ b/zerver/lib/recipient_users.py
@@ -3,8 +3,8 @@ from typing import Dict, Optional, Sequence
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _
-from zerver.models import Recipient, UserProfile
-from zerver.models.recipients import get_or_create_huddle
+from zerver.models import Huddle, Recipient, UserProfile
+from zerver.models.recipients import get_huddle_hash, get_or_create_huddle
from zerver.models.users import is_cross_realm_bot_email
@@ -13,6 +13,7 @@ def get_recipient_from_user_profiles(
forwarded_mirror_message: bool,
forwarder_user_profile: Optional[UserProfile],
sender: UserProfile,
+ create: bool = True,
) -> Recipient:
# Avoid mutating the passed in list of recipient_profiles.
recipient_profiles_map = {user_profile.id: user_profile for user_profile in recipient_profiles}
@@ -48,7 +49,13 @@ def get_recipient_from_user_profiles(
recipient_profiles_map[sender.id] = sender
user_ids = list(recipient_profiles_map)
- huddle = get_or_create_huddle(user_ids)
+ if create:
+ huddle = get_or_create_huddle(user_ids)
+ else:
+ # We intentionally let the Huddle.DoesNotExist escape, in the
+ # case that there is no such huddle, and the user passed
+ # create=False
+ huddle = Huddle.objects.get(huddle_hash=get_huddle_hash(user_ids))
return Recipient(
id=huddle.recipient_id,
type=Recipient.DIRECT_MESSAGE_GROUP,
@@ -91,12 +98,14 @@ def recipient_for_user_profiles(
forwarded_mirror_message: bool,
forwarder_user_profile: Optional[UserProfile],
sender: UserProfile,
+ *,
allow_deactivated: bool = False,
+ create: bool = True,
) -> Recipient:
recipient_profiles = validate_recipient_user_profiles(
user_profiles, sender, allow_deactivated=allow_deactivated
)
return get_recipient_from_user_profiles(
- recipient_profiles, forwarded_mirror_message, forwarder_user_profile, sender
+ recipient_profiles, forwarded_mirror_message, forwarder_user_profile, sender, create=create
)
diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.py
index f7ddc501de8..64450c66607 100644
--- a/zerver/tests/test_message_fetch.py
+++ b/zerver/tests/test_message_fetch.py
@@ -61,6 +61,7 @@ from zerver.models import (
UserTopic,
)
from zerver.models.realms import get_realm
+from zerver.models.recipients import get_or_create_huddle
from zerver.models.streams import get_stream
from zerver.views.message_fetch import get_messages_backend
@@ -362,7 +363,21 @@ 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)",
)
+ def test_add_term_using_dm_operator_more_than_one_user_as_operand_no_huddle(self) -> None:
+ # If the group doesn't exist, it's a flat false
+ two_others = f"{self.example_user('cordelia').email},{self.example_user('othello').email}"
+ term = dict(operator="dm", operand=two_others)
+ self._do_add_term_test(term, "WHERE false")
+
def test_add_term_using_dm_operator_more_than_one_user_as_operand(self) -> None:
+ # Make the huddle first
+ get_or_create_huddle(
+ [
+ self.example_user("hamlet").id,
+ self.example_user("cordelia").id,
+ self.example_user("othello").id,
+ ]
+ )
two_others = f"{self.example_user('cordelia').email},{self.example_user('othello').email}"
term = dict(operator="dm", operand=two_others)
self._do_add_term_test(term, "WHERE recipient_id = %(recipient_id_1)s")
@@ -379,9 +394,25 @@ 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 OR sender_id = %(sender_id_2)s AND recipient_id = %(recipient_id_2)s))",
)
+ def test_add_term_using_dm_operator_more_than_one_user_as_operand_no_huddle_and_negated(
+ self,
+ ) -> None: # NEGATED
+ # If the group doesn't exist, it's a flat true
+ two_others = f"{self.example_user('cordelia').email},{self.example_user('othello').email}"
+ term = dict(operator="dm", operand=two_others, negated=True)
+ self._do_add_term_test(term, "WHERE true")
+
def test_add_term_using_dm_operator_more_than_one_user_as_operand_and_negated(
self,
) -> None: # NEGATED
+ # Make the huddle first
+ get_or_create_huddle(
+ [
+ self.example_user("hamlet").id,
+ self.example_user("cordelia").id,
+ self.example_user("othello").id,
+ ]
+ )
two_others = f"{self.example_user('cordelia').email},{self.example_user('othello').email}"
term = dict(operator="dm", operand=two_others, negated=True)
self._do_add_term_test(term, "WHERE recipient_id != %(recipient_id_1)s")
@@ -2171,6 +2202,23 @@ class GetOldMessagesTest(ZulipTestCase):
for message in result["messages"]:
self.assertEqual(dr_emails(message["display_recipient"]), emails)
+ def test_get_messages_with_nonexistant_group_dm(self) -> None:
+ me = self.example_user("hamlet")
+ # Huddle which doesn't match anything gets no results
+ non_existant_huddle = [
+ me.id,
+ self.example_user("iago").id,
+ self.example_user("othello").id,
+ ]
+ self.login_user(me)
+ narrow: List[Dict[str, Any]] = [dict(operator="dm", operand=non_existant_huddle)]
+ result = self.get_and_check_messages(dict(narrow=orjson.dumps(narrow).decode()))
+ self.assertEqual(result["messages"], [])
+
+ narrow = [dict(operator="dm", operand=non_existant_huddle, negated=True)]
+ result = self.get_and_check_messages(dict(narrow=orjson.dumps(narrow).decode()))
+ self.assertEqual([m["id"] for m in result["messages"]], [1, 3])
+
def test_get_visible_messages_with_narrow_dm(self) -> None:
me = self.example_user("hamlet")
self.login_user(me)