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: Add 'has_topic_wildcards' to 'MentionData'.

Prakhar Pratyush1df63ed4484b
diff --git a/zerver/lib/mention.py b/zerver/lib/mention.py
index 0d8463caf0e..55f5ff77e06 100644
--- a/zerver/lib/mention.py
+++ b/zerver/lib/mention.py
@@ -18,6 +18,7 @@ USER_GROUP_MENTIONS_RE = re.compile(
rf"{BEFORE_MENTION_ALLOWED_REGEX}@(?P<silent>_?)(\*(?P<match>[^\*]+)\*)"
)
+topic_wildcards = frozenset(["topic"])
stream_wildcards = frozenset(["all", "everyone", "stream"])
@@ -46,12 +47,14 @@ class UserFilter:
@dataclass
class MentionText:
text: Optional[str]
+ is_topic_wildcard: bool
is_stream_wildcard: bool
@dataclass
class PossibleMentions:
mention_texts: Set[str]
+ message_has_topic_wildcards: bool
message_has_stream_wildcards: bool
@@ -153,24 +156,31 @@ def user_mention_matches_stream_wildcard(mention: str) -> bool:
def extract_mention_text(m: Match[str]) -> MentionText:
text = m.group("match")
+ if text in topic_wildcards:
+ return MentionText(text=None, is_topic_wildcard=True, is_stream_wildcard=False)
if text in stream_wildcards:
- return MentionText(text=None, is_stream_wildcard=True)
- return MentionText(text=text, is_stream_wildcard=False)
+ return MentionText(text=None, is_topic_wildcard=False, is_stream_wildcard=True)
+ return MentionText(text=text, is_topic_wildcard=False, is_stream_wildcard=False)
def possible_mentions(content: str) -> PossibleMentions:
# mention texts can either be names, or an extended name|id syntax.
texts = set()
+ message_has_topic_wildcards = False
message_has_stream_wildcards = False
for m in MENTIONS_RE.finditer(content):
mention_text = extract_mention_text(m)
text = mention_text.text
if text:
texts.add(text)
+ if mention_text.is_topic_wildcard:
+ message_has_topic_wildcards = True
if mention_text.is_stream_wildcard:
message_has_stream_wildcards = True
return PossibleMentions(
- mention_texts=texts, message_has_stream_wildcards=message_has_stream_wildcards
+ mention_texts=texts,
+ message_has_topic_wildcards=message_has_topic_wildcards,
+ message_has_stream_wildcards=message_has_stream_wildcards,
)
@@ -216,10 +226,14 @@ class MentionData:
self.user_id_info = {row.id: row for row in possible_mentions_info}
self.init_user_group_data(realm_id=realm_id, content=content)
self.has_stream_wildcards = mentions.message_has_stream_wildcards
+ self.has_topic_wildcards = mentions.message_has_topic_wildcards
def message_has_stream_wildcards(self) -> bool:
return self.has_stream_wildcards
+ def message_has_topic_wildcards(self) -> bool:
+ return self.has_topic_wildcards
+
def init_user_group_data(self, realm_id: int, content: str) -> None:
self.user_group_name_info: Dict[str, UserGroup] = {}
self.user_group_members: Dict[int, List[int]] = {}
diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py
index c0c7bed781a..5316cca3ba6 100644
--- a/zerver/tests/test_markdown.py
+++ b/zerver/tests/test_markdown.py
@@ -283,6 +283,11 @@ class MarkdownMiscTest(ZulipTestCase):
mention_data = MentionData(mention_backend, content)
self.assertTrue(mention_data.message_has_stream_wildcards())
+ self.assertFalse(mention_data.message_has_topic_wildcards())
+ content = "@**King Hamlet** @**Cordelia, lear's daughter** @**topic**"
+ mention_data = MentionData(mention_backend, content)
+ self.assertTrue(mention_data.message_has_topic_wildcards())
+
def test_invalid_katex_path(self) -> None:
with self.settings(DEPLOY_ROOT="/nonexistent"):
with self.assertLogs(level="ERROR") as m:
@@ -2044,12 +2049,17 @@ class MarkdownTest(ZulipTestCase):
def test_possible_mentions(self) -> None:
def assert_mentions(
- content: str, names: Set[str], has_stream_wildcards: bool = False
+ content: str,
+ names: Set[str],
+ has_topic_wildcards: bool = False,
+ has_stream_wildcards: bool = False,
) -> None:
self.assertEqual(
possible_mentions(content),
PossibleMentions(
- mention_texts=names, message_has_stream_wildcards=has_stream_wildcards
+ mention_texts=names,
+ message_has_topic_wildcards=has_topic_wildcards,
+ message_has_stream_wildcards=has_stream_wildcards,
),
)
@@ -2057,12 +2067,14 @@ class MarkdownTest(ZulipTestCase):
assert_mentions("", set())
assert_mentions("boring", set())
- assert_mentions("@**all**", set(), True)
+ assert_mentions("@**topic**", set(), True)
+ assert_mentions("@**all**", set(), False, True)
assert_mentions("smush@**steve**smush", set())
assert_mentions(
f"Hello @**King Hamlet**, @**|{aaron.id}** and @**Cordelia, Lear's daughter**\n@**Foo van Barson|1234** @**all**",
{"King Hamlet", f"|{aaron.id}", "Cordelia, Lear's daughter", "Foo van Barson|1234"},
+ False,
True,
)