Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
mention: Refactor `USER_GROUP_MENTIONS_RE` and simplify its related code path.
akshatdaltonf56fca308a23
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex 347ff58ef4f..c128996618f 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -52,7 +52,7 @@ from zerver.lib.emoji import EMOTICON_RE, codepoint_to_name, name_to_codepoint,from zerver.lib.exceptions import MarkdownRenderingExceptionfrom zerver.lib.markdown import fenced_codefrom zerver.lib.markdown.fenced_code import FENCE_RE-from zerver.lib.mention import extract_user_group, possible_mentions, possible_user_group_mentions+from zerver.lib.mention import possible_mentions, possible_user_group_mentionsfrom zerver.lib.subdomains import is_static_or_current_realm_urlfrom zerver.lib.tex import render_texfrom zerver.lib.thumbnail import user_uploads_or_external@@ -1869,14 +1869,20 @@ class UserMentionPattern(markdown.inlinepatterns.InlineProcessor):class UserGroupMentionPattern(markdown.inlinepatterns.InlineProcessor):+ def __init__(self, compiled_re: Pattern[str], md: markdown.Markdown) -> None:+ # This is similar to the superclass's small __init__ function,+ # but we skip the compilation step and let the caller give us+ # a compiled regex.+ self.compiled_re = compiled_re+ self.md = md+def handleMatch( # type: ignore[override] # supertype incompatible with supersupertypeself, m: Match[str], data: str) -> Union[Tuple[None, None, None], Tuple[Element, int, int]]:- match = m.group(1)+ name = m.group("match")db_data = self.md.zulip_db_dataif self.md.zulip_message and db_data is not None:- name = extract_user_group(match)user_group = db_data["mention_data"].get_user_group(name)if user_group:self.md.zulip_message.mentions_user_group_ids.add(user_group.id)diff --git a/zerver/lib/mention.py b/zerver/lib/mention.pyindex 98cfbad5524..df68ccf8a13 100644--- a/zerver/lib/mention.py+++ b/zerver/lib/mention.py@@ -4,7 +4,7 @@ from typing import Match, Optional, Set, Tuple# Match multi-word string between @** ** or match any one-word# sequences after @MENTIONS_RE = re.compile(r"(?<![^\s\'\"\(,:<])@(?P<silent>_?)(\*\*(?P<match>[^\*]+)\*\*)")-USER_GROUP_MENTIONS_RE = r"(?<![^\s\'\"\(,:<])@(\*[^\*]+\*)"+USER_GROUP_MENTIONS_RE = re.compile(r"(?<![^\s\'\"\(,:<])@(\*(?P<match>[^\*]+)\*)")wildcards = ["all", "everyone", "stream"]@@ -33,10 +33,5 @@ def possible_mentions(content: str) -> Tuple[Set[str], bool]:return texts, message_has_wildcards-def extract_user_group(matched_text: str) -> str:- return matched_text[1:-1]--def possible_user_group_mentions(content: str) -> Set[str]:- matches = re.findall(USER_GROUP_MENTIONS_RE, content)- return {extract_user_group(match) for match in matches}+ return {m.group("match") for m in USER_GROUP_MENTIONS_RE.finditer(content)}