Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
narrow: Fix mentions operator to query target user's UserMessage.
Aman Agrawal2a90b29f2fc4
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.pyindex e1d9e52f58e..94cc1c99fcd 100644--- a/zerver/lib/narrow.py+++ b/zerver/lib/narrow.py@@ -651,8 +651,22 @@ class NarrowBuilder:# topic_wildcard_mentioned) or silent mentions, since this# operator is defined to only match explicit @-mentions# directed at notifying this user individually.- cond = (column("user_profile_id", Integer) == target_user.id) & (- column("flags", Integer).op("&")(literal(UserMessage.flags.mentioned.mask)) != 0+ #+ # Use a subquery on the target user's UserMessage rows,+ # since the base query's UserMessage join is constrained to+ # the current user, who may differ from the mentioned user.+ # We correlate via zerver_message.id (present in both base+ # query variants) to avoid ambiguity with the outer query's+ # zerver_usermessage table.+ cond = (+ select(1)+ .select_from(table("zerver_usermessage"))+ .where(+ column("message_id", Integer) == literal_column("zerver_message.id", Integer),+ column("user_profile_id", Integer) == literal(target_user.id),+ column("flags", Integer).op("&")(literal(UserMessage.flags.mentioned.mask)) != 0,+ )+ .exists())return query.where(maybe_negate(cond))diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex 2b276b7bd56..a3b2d5a85f8 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -939,7 +939,7 @@ class NarrowBuilderTest(ZulipTestCase):term = NarrowParameter(operator="mentions", operand=self.user_profile.id)self._do_add_term_test(term,- "WHERE user_profile_id = %(user_profile_id_1)s AND (flags & %(param_1)s) != %(param_2)s",+ "WHERE EXISTS (SELECT 1 \nFROM zerver_usermessage \nWHERE message_id = zerver_message.id AND user_profile_id = %(param_1)s AND (flags & %(param_2)s) != %(param_3)s)",)def test_add_term_using_mentions_operator_with_different_user_email(self) -> None:@@ -948,7 +948,7 @@ class NarrowBuilderTest(ZulipTestCase):self._do_add_term_test(term,- "WHERE user_profile_id = %(user_profile_id_1)s AND (flags & %(param_1)s) != %(param_2)s",+ "WHERE EXISTS (SELECT 1 \nFROM zerver_usermessage \nWHERE message_id = zerver_message.id AND user_profile_id = %(param_1)s AND (flags & %(param_2)s) != %(param_3)s)",)self.send_stream_message(@@ -959,7 +959,7 @@ class NarrowBuilderTest(ZulipTestCase):self._do_add_term_test(term,- "WHERE user_profile_id = %(user_profile_id_1)s AND (flags & %(param_1)s) != %(param_2)s",+ "WHERE EXISTS (SELECT 1 \nFROM zerver_usermessage \nWHERE message_id = zerver_message.id AND user_profile_id = %(param_1)s AND (flags & %(param_2)s) != %(param_3)s)",)@@ -5070,11 +5070,7 @@ class GetOldMessagesTest(ZulipTestCase):self.assert_json_success(payload)result = orjson.loads(payload.content)- # BUG: The by_mention query adds user_profile_id = target_user.id- # directly, which conflicts with the base query's- # user_profile_id = current_user.id when they differ.- # This should return [mention_message_id] once fixed.- self.assertEqual([m["id"] for m in result["messages"]], [])+ self.assertEqual([m["id"] for m in result["messages"]], [mention_message_id])def test_exclude_muting_conditions(self) -> None:realm = get_realm("zulip")