2013-10-10 21:37:26 +02:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
from typing import Optional, Set
|
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 @
|
2018-04-03 17:55:57 +02:00
|
|
|
find_mentions = r'(?<![^\s\'\"\(,:<])@(\*\*[^\*]+\*\*|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
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def extract_name(s: str) -> Optional[str]:
|
2017-09-14 19:47:22 +02:00
|
|
|
if s.startswith("**") and s.endswith("**"):
|
|
|
|
name = s[2:-2]
|
|
|
|
if name in wildcards:
|
|
|
|
return None
|
|
|
|
return name
|
|
|
|
|
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)
|
2017-09-26 00:28:18 +02:00
|
|
|
names_with_none = (extract_name(match) for match in matches)
|
2017-09-25 01:16:07 +02:00
|
|
|
names = {name for name in names_with_none if name}
|
2017-09-14 19:47:22 +02:00
|
|
|
return names
|
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}
|