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.

mention: Skip fetching group membership for silent-mentioned groups.

bedo215cc632bc6b
diff --git a/zerver/lib/mention.py b/zerver/lib/mention.py
index 54e2c1d28f6..4142a2c1fa3 100644
--- a/zerver/lib/mention.py
+++ b/zerver/lib/mention.py
@@ -3,6 +3,7 @@ import re
from collections import defaultdict
from dataclasses import dataclass
from re import Match
+from typing import Literal
from django.conf import settings
from django.db.models import Q
@@ -304,8 +305,22 @@ def possible_mentions(content: str) -> PossibleMentions:
)
-def possible_user_group_mentions(content: str) -> set[str]:
- return {m.group("match") for m in USER_GROUP_MENTIONS_RE.finditer(content)}
+def possible_user_group_mentions(content: str) -> dict[str, Literal["silent", "non-silent"]]:
+ # maps each group name to its mention type, silent or non-silent.
+ mentions: dict[str, Literal["silent", "non-silent"]] = {}
+
+ for mention in USER_GROUP_MENTIONS_RE.finditer(content):
+ group_mention = mention.group("match")
+
+ # non-silent mention can override silent.
+ if not mention.group("silent"):
+ mentions[group_mention] = "non-silent"
+
+ # silent mention should NOT override non-silent.
+ if mention.group("silent") and group_mention not in mentions:
+ mentions[group_mention] = "silent"
+
+ return mentions
def get_possible_mentions_info(
@@ -362,40 +377,41 @@ class MentionData:
def init_user_group_data(self, realm_id: int, content: str) -> None:
self.user_group_name_info: dict[str, NamedUserGroup] = {}
self.user_group_members: dict[int, set[int]] = defaultdict(set)
- user_group_names = possible_user_group_mentions(content)
- if user_group_names:
+ user_group_names_mentions = possible_user_group_mentions(content)
+ if user_group_names_mentions:
named_user_groups = NamedUserGroup.objects.filter(
- realm_id=realm_id, name__in=user_group_names
+ realm_id=realm_id, name__in=user_group_names_mentions
)
- # we need user_group_name_info for both groups, deactivated and non-deactivated.
+
+ # No filter here as we need user_group_name_info for all groups mentions.
self.user_group_name_info = {group.name.lower(): group for group in named_user_groups}
- non_deactivated_group_ids = [
- group.id for group in named_user_groups if not group.deactivated
+ # We are interested only in fetching group membersship for
+ # non-deactivated groups and non-silent mentioned groups.
+ filtered_group_ids = [
+ group.id
+ for group in named_user_groups
+ if not group.deactivated
+ and user_group_names_mentions.get(group.name) == "non-silent"
]
- # If there are no matching non_deactivated_group_ids
- # (e.g non-existent user_group_names or only deactivated-groups were mentioned)
+ # If no filtered_group_ids exist,
# then no need to execute the relatively expensive recursive CTE query
# triggered by get_root_id_annotated_recursive_subgroups_for_groups.
# Also the base case for the recursive CTE requires non-empty matching rows.
- if len(non_deactivated_group_ids) == 0:
+ if len(filtered_group_ids) == 0:
return
# Here we fetch membership for non-deactivated groups in a
# single, efficient bulk query. Deactivated group mentions
# are non-operative, so we don't need to waste resources
- # collecting membership data for them. Further possible
- # optimizations include, in order of value:
+ # collecting membership data for them. Further possible optimization:
#
- # - Skipping fetching membership data for silent mention syntax.
# - Filtering groups the current user doesn't have permission to mention
# from what we fetch membership for. This requires care to avoid race bugs
# if the user's permissions change while the message is being sent.
for group_root_id, member_id in (
- get_root_id_annotated_recursive_subgroups_for_groups(
- non_deactivated_group_ids, realm_id
- )
+ get_root_id_annotated_recursive_subgroups_for_groups(filtered_group_ids, realm_id)
.filter(direct_members__is_active=True)
.values_list("root_id", "direct_members") # type: ignore[misc] # root_id is an annotated field.
):
diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py
index 276dc1d682a..7cb0f58195c 100644
--- a/zerver/tests/test_markdown.py
+++ b/zerver/tests/test_markdown.py
@@ -359,6 +359,43 @@ class MarkdownMiscTest(ZulipTestCase):
mention_data.get_group_members(class_B.id), {cordelia.id, aaron.id, hamlet.id}
)
+ def test_silent_mention_user_groups(self) -> None:
+ # silent VS non-silent group mentions, in regard to fetching group membership.
+
+ realm = get_realm("zulip")
+ aaron = self.example_user("aaron")
+ hamlet = self.example_user("hamlet")
+ cordelia = self.example_user("cordelia")
+ iago = self.example_user("iago")
+ othello = self.example_user("othello")
+
+ hamlet_group = NamedUserGroup.objects.get(realm=realm, name="hamletcharacters")
+ zulip_group = check_add_user_group(realm, "zulip_group", [iago, aaron], acting_user=othello)
+ mention_backend = MentionBackend(realm.id)
+
+ # mention zulip_group, but silent mention hamlet_group.
+ content = "@*zulip_group*, @_*hamletcharacters*"
+ mention_data = MentionData(mention_backend, content, message_sender=None)
+
+ # non-silent mention should fetch group membership.
+ self.assertEqual(mention_data.get_group_members(zulip_group.id), {iago.id, aaron.id})
+
+ # silent mention should NOT fetch group membership.
+ self.assertEqual(mention_data.get_group_members(hamlet_group.id), set())
+
+ # We should make sure non-silent mention always results in fetching group membership,
+ # whether it precedes or follows a silent mention for the SAME group.
+
+ # non-silent before silent.
+ content = "@*hamletcharacters*, @_*hamletcharacters*"
+ mention_data = MentionData(mention_backend, content, message_sender=None)
+ self.assertEqual(mention_data.get_group_members(hamlet_group.id), {hamlet.id, cordelia.id})
+
+ # non-silent after silent.
+ content = "@_*hamletcharacters*, @*hamletcharacters*"
+ mention_data = MentionData(mention_backend, content, message_sender=None)
+ self.assertEqual(mention_data.get_group_members(hamlet_group.id), {hamlet.id, cordelia.id})
+
def test_invalid_katex_path(self) -> None:
with self.settings(DEPLOY_ROOT="/nonexistent"):
with self.assertLogs(level="ERROR") as m:
@@ -2840,23 +2877,49 @@ class MarkdownMentionTest(ZulipTestCase):
self.assertEqual(rendering_result.mentions_user_group_ids, {user_group.id})
def test_possible_user_group_mentions(self) -> None:
- def assert_mentions(content: str, names: set[str]) -> None:
+ def assert_mentions(content: str, names: dict[str, str]) -> None:
self.assertEqual(possible_user_group_mentions(content), names)
- assert_mentions("", set())
- assert_mentions("boring", set())
- assert_mentions("@**all**", set())
- assert_mentions("smush@*steve*smush", set())
+ assert_mentions("", dict())
+ assert_mentions("boring", dict())
+ assert_mentions("@**all**", dict())
+ assert_mentions("smush@*steve*smush", dict())
+ # capture only the group mention among other mentions.
assert_mentions(
"@*support* Hello @**King Hamlet** and @**Cordelia, Lear's daughter**\n"
"@**Foo van Barson** @**all**",
- {"support"},
+ {"support": "non-silent"},
)
-
+ # non-silent mentions
assert_mentions(
"Attention @*support*, @*frontend* and @*backend*\ngroups.",
- {"support", "frontend", "backend"},
+ {"support": "non-silent", "frontend": "non-silent", "backend": "non-silent"},
+ )
+ # silent mentions
+ assert_mentions(
+ "I prefer @_*backend* over @_*frontend*,",
+ {"backend": "silent", "frontend": "silent"},
+ )
+ # silent and non-silent
+ assert_mentions(
+ "Attention @*frontend* please refer to @_*support*,",
+ {"frontend": "non-silent", "support": "silent"},
+ )
+
+ # Make sure regular (non-silent) mention always takes precedence,
+ # whether it precedes or follows a silent mention for the SAME group.
+
+ # non-silent before silent.
+ assert_mentions(
+ "@*help* the building is on fire!, folks should refer to @_*help*",
+ {"help": "non-silent"},
+ )
+
+ # non-silent after silent.
+ assert_mentions(
+ "folks should refer to @_*help*, @*help* the building is on fire! ",
+ {"help": "non-silent"},
)
def test_user_group_mention_multiple(self) -> None: