Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
topic: Extract the UserTopic correlation into a helper.
Aman Agrawalcc2ac44c8d32
diff --git a/zerver/lib/topic.py b/zerver/lib/topic.pyindex bef8ab0c28b..954fb4f7253 100644--- a/zerver/lib/topic.py+++ b/zerver/lib/topic.py@@ -414,14 +414,31 @@ def get_resolved_topic_condition_q() -> Q:return Q(subject__startswith=RESOLVED_TOPIC_PREFIX, is_channel_message=True)-def get_followed_topic_condition_q(user_id: int) -> Q:+def matching_user_topic_exists_q(topics: QuerySet[UserTopic]) -> Q:+ """Matches the messages that one of the given UserTopic rows is for.++ Correlating against zerver_usertopic, rather than inlining a+ condition per row, keeps the query a fixed size however many rows+ there are.++ Matching a UserTopic's recipient already implies the message is a+ channel message, since a UserTopic's recipient is always its+ channel's, so is_channel_message is not tested here.+ """return Q(Exists(- UserTopic.objects.filter(- user_profile_id=user_id,- visibility_policy=UserTopic.VisibilityPolicy.FOLLOWED,- topic_name__iexact=OuterRef("subject"),- recipient=OuterRef("recipient"),+ topics.filter(+ recipient_id=OuterRef("recipient_id"),+ topic_name__iexact=OuterRef(DB_TOPIC_NAME),)))+++def get_followed_topic_condition_q(user_id: int) -> Q:+ return matching_user_topic_exists_q(+ UserTopic.objects.filter(+ user_profile_id=user_id,+ visibility_policy=UserTopic.VisibilityPolicy.FOLLOWED,+ )+ )diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex bbfaeb5d568..1a16763b82a 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -300,14 +300,14 @@ class NarrowBuilderTest(ZulipTestCase):term = NarrowParameter(operator="is", operand="followed", negated=False)self._do_add_term_test(term,- 'WHERE EXISTS(SELECT %s AS "a" FROM "zerver_usertopic" U0 WHERE (U0."recipient_id" = ("zerver_message"."recipient_id") AND UPPER(U0."topic_name"::text) = UPPER(("zerver_message"."subject")) AND U0."user_profile_id" = %s AND U0."visibility_policy" = %s) LIMIT 1)',+ 'WHERE EXISTS(SELECT %s AS "a" FROM "zerver_usertopic" U0 WHERE (U0."user_profile_id" = %s AND U0."visibility_policy" = %s AND U0."recipient_id" = ("zerver_message"."recipient_id") AND UPPER(U0."topic_name"::text) = UPPER(("zerver_message"."subject"))) LIMIT 1)',)def test_add_term_using_is_operator_for_negated_followed_topics(self) -> None:term = NarrowParameter(operator="is", operand="followed", negated=True)self._do_add_term_test(term,- 'WHERE NOT (EXISTS(SELECT %s AS "a" FROM "zerver_usertopic" U0 WHERE (U0."recipient_id" = ("zerver_message"."recipient_id") AND UPPER(U0."topic_name"::text) = UPPER(("zerver_message"."subject")) AND U0."user_profile_id" = %s AND U0."visibility_policy" = %s) LIMIT 1))',+ 'WHERE NOT (EXISTS(SELECT %s AS "a" FROM "zerver_usertopic" U0 WHERE (U0."user_profile_id" = %s AND U0."visibility_policy" = %s AND U0."recipient_id" = ("zerver_message"."recipient_id") AND UPPER(U0."topic_name"::text) = UPPER(("zerver_message"."subject"))) LIMIT 1))',)def test_add_term_using_is_operator_for_muted_topics(self) -> None: