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.

get_messages: Add mentions operator for filtering user mentions.

kumbhar-sarthak27cb57a60471
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index 32e130633ed..3c09bdfe630 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -20,6 +20,16 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 12.0
+**Feature level 446**
+
+* [`GET /messages`](/api/get-messages),
+ [`GET /messages/matches_narrow`](/api/check-messages-match-narrow),
+ [`POST /messages/flags/narrow`](/api/update-message-flags-for-narrow),
+ [`POST /register`](/api/register-queue):
+ Added support for a new [search/narrow filter](/api/construct-narrow#changes),
+ `mentions`. This operator filters messages that contain a direct,
+ visible personal mention of the specified user.
+
**Feature level 445**
* [`GET /messages`](/api/get-messages): Added a new `date` value for
diff --git a/api_docs/construct-narrow.md b/api_docs/construct-narrow.md
index 8b00d1479d5..2efda56289b 100644
--- a/api_docs/construct-narrow.md
+++ b/api_docs/construct-narrow.md
@@ -58,6 +58,10 @@ as an empty string.
## Changes
+* In Zulip 12.0 (feature level 446), add the `mentions` operator,
+ matching messages that contain a direct personal mention of the
+ specified user.
+
* In Zulip 10.0 (feature level 366), support was added for a new
`is:muted` operator combination, matching messages in topics and
channels that the user has [muted](/help/mute-a-topic).
diff --git a/version.py b/version.py
index 8881a53af5a..b52b7acc216 100644
--- a/version.py
+++ b/version.py
@@ -35,7 +35,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# new level means in api_docs/changelog.md, as well as "**Changes**"
# entries in the endpoint's documentation in `zulip.yaml`.
-API_FEATURE_LEVEL = 445
+API_FEATURE_LEVEL = 446
# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.py
index ec560dd4e3b..b09fa520ad7 100644
--- a/zerver/lib/narrow.py
+++ b/zerver/lib/narrow.py
@@ -121,6 +121,7 @@ class NarrowParameter(BaseModel):
"sender",
"group-pm-with",
"dm-including",
+ "mentions",
"with",
]
operators_supporting_ids = ["pm-with", "dm"]
@@ -302,6 +303,7 @@ class NarrowBuilder:
# "pm-with:" is a legacy alias for "dm:"
"pm-with": self.by_dm,
"dm-including": self.by_dm_including,
+ "mentions": self.by_mention,
# "group-pm-with:" was deprecated by the addition of "dm-including:"
"group-pm-with": self.by_group_pm_with,
# TODO/compatibility: Prior to commit a9b3a9c, the server implementation
@@ -692,6 +694,31 @@ class NarrowBuilder:
cond = and_(private_flag, realm_match, direct_message_group_cond)
return query.where(maybe_negate(cond))
+ def by_mention(
+ self, query: Select, operand: str | int, maybe_negate: ConditionTransform
+ ) -> Select:
+ assert self.user_profile is not None
+
+ try:
+ if isinstance(operand, str):
+ target_user = get_user_including_cross_realm(operand, self.realm)
+ else:
+ target_user = get_user_by_id_in_realm_including_cross_realm(operand, self.realm)
+ except (JsonableError, UserProfile.DoesNotExist):
+ raise BadNarrowOperatorError("unknown user " + str(operand))
+
+ # Only check for direct (visible) personal mentions here. We
+ # intentionally do not consider other mention-related flags
+ # (group_mentioned, stream_wildcard_mentioned,
+ # 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
+ )
+
+ return query.where(maybe_negate(cond))
+
def by_group_pm_with(
self, query: Select, operand: str | int, maybe_negate: ConditionTransform
) -> Select:
diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.py
index 701e659ef75..038a453b4b2 100644
--- a/zerver/tests/test_message_fetch.py
+++ b/zerver/tests/test_message_fetch.py
@@ -760,6 +760,10 @@ class NarrowBuilderTest(ZulipTestCase):
term = NarrowParameter(operator="group-pm-with", operand="[email protected]")
self.assertRaises(BadNarrowOperatorError, self._build_query, term)
+ def test_add_term_using_mentions_operator_with_non_existing_user(self) -> None:
+ term = NarrowParameter(operator="mentions", operand="[email protected]")
+ self.assertRaises(BadNarrowOperatorError, self._build_query, term)
+
# Test that the underscore version of "group-pm-with" works.
def test_add_term_using_underscore_version_of_group_pm_with_operator(self) -> None:
term = NarrowParameter(operator="group_pm_with", operand=self.othello_email)
@@ -816,6 +820,33 @@ class NarrowBuilderTest(ZulipTestCase):
def _build_query(self, term: NarrowParameter) -> Select:
return self.builder.add_term(self.raw_query, term)
+ def test_add_term_using_mentions_operator_with_logged_in_user_email(self) -> None:
+ 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",
+ )
+
+ def test_add_term_using_mentions_operator_with_different_user_email(self) -> None:
+ othello = self.example_user("othello")
+ term = NarrowParameter(operator="mentions", operand=othello.id)
+
+ self._do_add_term_test(
+ term,
+ "WHERE user_profile_id = %(user_profile_id_1)s AND (flags & %(param_1)s) != %(param_2)s",
+ )
+
+ self.send_stream_message(
+ self.user_profile,
+ "Denmark",
+ content=f"Hello @**{othello.full_name}**",
+ )
+
+ self._do_add_term_test(
+ term,
+ "WHERE user_profile_id = %(user_profile_id_1)s AND (flags & %(param_1)s) != %(param_2)s",
+ )
+
class NarrowLibraryTest(ZulipTestCase):
def test_build_narrow_predicate(self) -> None:
@@ -1140,6 +1171,11 @@ class NarrowLibraryTest(ZulipTestCase):
]
)
)
+ self.assertFalse(
+ is_spectator_compatible(
+ [NarrowParameter(operator="mentions", operand="[email protected]")]
+ )
+ )
self.assertFalse(
is_spectator_compatible([NarrowParameter(operator="is", operand="starred")])
)
@@ -4814,6 +4850,44 @@ class GetOldMessagesTest(ZulipTestCase):
self.assert_length(queries, 1)
self.assertIn(f"AND zerver_message.id = {LARGER_THAN_MAX_MESSAGE_ID}", queries[0].sql)
+ def test_get_visible_messages_with_mentions_narrow(self) -> None:
+ iago = self.example_user("iago")
+ self.login_user(iago)
+
+ hamlet = self.example_user("hamlet")
+ stream = self.make_stream("design")
+ self.subscribe(iago, stream.name)
+ self.subscribe(hamlet, stream.name)
+
+ content = f"Hello @**{iago.full_name}**!"
+ mention_message_id = self.send_stream_message(
+ hamlet,
+ stream.name,
+ content=content,
+ )
+
+ silent_mention_content = f"Hello @_**{iago.full_name}**!"
+ self.send_stream_message(
+ hamlet,
+ stream.name,
+ content=silent_mention_content,
+ )
+
+ narrow = [dict(operator="mentions", operand=iago.email)]
+
+ # This should check just the messages that mentioned this user.
+ post_params = dict(
+ narrow=orjson.dumps(narrow).decode(),
+ num_before=10,
+ num_after=0,
+ anchor=LARGER_THAN_MAX_MESSAGE_ID,
+ )
+ payload = self.client_get("/json/messages", dict(post_params))
+ self.assert_json_success(payload)
+ result = orjson.loads(payload.content)
+
+ self.assertEqual([m["id"] for m in result["messages"]], [mention_message_id])
+
def test_exclude_muting_conditions(self) -> None:
realm = get_realm("zulip")
self.make_stream("web stuff")