2017-09-14 19:47:22 +02:00
|
|
|
import re
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import 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 @
|
2019-02-20 10:15:33 +01:00
|
|
|
find_mentions = r'(?<![^\s\'\"\(,:<])@(?P<silent>_?)(?P<match>\*\*[^\*]+\*\*|all|everyone|stream)'
|
2017-09-25 09:47:15 +02:00
|
|
|
user_group_mentions = r'(?<![^\s\'\"\(,:<])@(\*[^\*]+\*)'
|
2013-06-28 16:02:58 +02:00
|
|
|
|
2018-04-03 17:55:57 +02:00
|
|
|
wildcards = ['all', 'everyone', 'stream']
|
2013-06-28 16:02:58 +02: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
|
|
|
|
2019-11-22 10:38:34 +01:00
|
|
|
def extract_mention_text(m: Tuple[str, str]) -> Tuple[Optional[str], bool]:
|
2019-01-08 09:30:19 +01:00
|
|
|
# re.findall provides tuples of match elements; we want the second
|
|
|
|
# to get the main mention content.
|
|
|
|
s = m[1]
|
2017-09-14 19:47:22 +02:00
|
|
|
if s.startswith("**") and s.endswith("**"):
|
2018-11-02 08:22:07 +01:00
|
|
|
text = s[2:-2]
|
|
|
|
if text in wildcards:
|
2019-11-22 10:38:34 +01:00
|
|
|
return None, True
|
|
|
|
return text, False
|
|
|
|
return None, False
|
2017-09-14 19:47:22 +02:00
|
|
|
|
2019-11-22 10:38:34 +01:00
|
|
|
def possible_mentions(content: str) -> Tuple[Set[str], bool]:
|
2017-09-14 19:47:22 +02:00
|
|
|
matches = re.findall(find_mentions, content)
|
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
|
|
|
|
for match in matches:
|
|
|
|
text, is_wildcard = extract_mention_text(match)
|
|
|
|
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
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def extract_user_group(matched_text: str) -> str:
|
2017-09-25 09:47:15 +02:00
|
|
|
return matched_text[1:-1]
|
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def possible_user_group_mentions(content: str) -> Set[str]:
|
2017-09-25 09:47:15 +02:00
|
|
|
matches = re.findall(user_group_mentions, content)
|
|
|
|
return {extract_user_group(match) for match in matches}
|