2017-09-14 19:47:22 +02:00
|
|
|
import re
|
2021-05-15 18:55:34 +02:00
|
|
|
from typing import Match, Optional, Set, Tuple
|
2017-09-14 19:47:22 +02:00
|
|
|
|
2013-06-28 16:02:58 +02:00
|
|
|
# Match multi-word string between @** ** or match any one-word
|
|
|
|
# sequences after @
|
2021-05-15 18:55:34 +02:00
|
|
|
MENTIONS_RE = re.compile(r"(?<![^\s\'\"\(,:<])@(?P<silent>_?)(\*\*(?P<match>[^\*]+)\*\*)")
|
2021-05-16 09:43:47 +02:00
|
|
|
USER_GROUP_MENTIONS_RE = re.compile(r"(?<![^\s\'\"\(,:<])@(?P<silent>_?)(\*(?P<match>[^\*]+)\*)")
|
2013-06-28 16:02:58 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
wildcards = ["all", "everyone", "stream"]
|
2013-06-28 16:02:58 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def user_mention_matches_wildcard(mention: str) -> bool:
|
2013-10-09 20:48:05 +02:00
|
|
|
return mention in wildcards
|
2017-09-14 19:47:22 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-05-15 18:55:34 +02:00
|
|
|
def extract_mention_text(m: Match[str]) -> Tuple[Optional[str], bool]:
|
|
|
|
text = m.group("match")
|
|
|
|
if text in wildcards:
|
|
|
|
return None, True
|
|
|
|
return text, False
|
2017-09-14 19:47:22 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-11-22 10:38:34 +01:00
|
|
|
def possible_mentions(content: str) -> Tuple[Set[str], bool]:
|
2018-11-02 08:22:07 +01:00
|
|
|
# mention texts can either be names, or an extended name|id syntax.
|
2019-11-22 10:38:34 +01:00
|
|
|
texts = set()
|
|
|
|
message_has_wildcards = False
|
2021-05-15 18:55:34 +02:00
|
|
|
for m in MENTIONS_RE.finditer(content):
|
|
|
|
text, is_wildcard = extract_mention_text(m)
|
2019-11-22 10:38:34 +01:00
|
|
|
if text:
|
|
|
|
texts.add(text)
|
|
|
|
if is_wildcard:
|
|
|
|
message_has_wildcards = True
|
|
|
|
return texts, message_has_wildcards
|
2017-09-25 09:47:15 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def possible_user_group_mentions(content: str) -> Set[str]:
|
2021-05-15 19:44:06 +02:00
|
|
|
return {m.group("match") for m in USER_GROUP_MENTIONS_RE.finditer(content)}
|