2013-10-10 21:37:26 +02:00
|
|
|
|
2019-01-08 09:30:19 +01:00
|
|
|
from typing import Optional, Set, Tuple
|
2017-09-14 19:47:22 +02:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
2013-06-28 16:02:58 +02:00
|
|
|
# Match multi-word string between @** ** or match any one-word
|
|
|
|
# sequences after @
|
2019-01-08 09:30:19 +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-01-08 09:30:19 +01:00
|
|
|
def extract_mention_text(m: Tuple[str, str]) -> Optional[str]:
|
|
|
|
# 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:
|
2017-09-14 19:47:22 +02:00
|
|
|
return None
|
2018-11-02 08:22:07 +01:00
|
|
|
return text
|
2017-09-14 19:47:22 +02:00
|
|
|
|
2018-04-03 17:55:57 +02:00
|
|
|
# We don't care about @all, @everyone or @stream
|
2017-09-14 19:47:22 +02:00
|
|
|
return None
|
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def possible_mentions(content: str) -> Set[str]:
|
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.
|
|
|
|
texts_with_none = (extract_mention_text(match) for match in matches)
|
|
|
|
texts = {text for text in texts_with_none if text}
|
|
|
|
return texts
|
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}
|