2013-10-10 21:37:26 +02:00
|
|
|
|
2017-09-14 19:47:22 +02:00
|
|
|
from typing import Optional, Set, Text
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2013-06-28 16:02:58 +02:00
|
|
|
# Match multi-word string between @** ** or match any one-word
|
|
|
|
# sequences after @
|
2017-09-14 22:55:22 +02:00
|
|
|
find_mentions = r'(?<![^\s\'\"\(,:<])@(\*\*[^\*]+\*\*|all|everyone)'
|
2017-09-25 09:47:15 +02:00
|
|
|
user_group_mentions = r'(?<![^\s\'\"\(,:<])@(\*[^\*]+\*)'
|
2013-06-28 16:02:58 +02:00
|
|
|
|
|
|
|
wildcards = ['all', 'everyone']
|
|
|
|
|
2013-10-09 20:48:05 +02:00
|
|
|
def user_mention_matches_wildcard(mention):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> bool
|
2013-10-09 20:48:05 +02:00
|
|
|
return mention in wildcards
|
2017-09-14 19:47:22 +02:00
|
|
|
|
|
|
|
def extract_name(s):
|
|
|
|
# type: (Text) -> Optional[Text]
|
|
|
|
if s.startswith("**") and s.endswith("**"):
|
|
|
|
name = s[2:-2]
|
|
|
|
if name in wildcards:
|
|
|
|
return None
|
|
|
|
return name
|
|
|
|
|
|
|
|
# We don't care about @all or @everyone
|
|
|
|
return None
|
|
|
|
|
|
|
|
def possible_mentions(content):
|
|
|
|
# type: (Text) -> Set[Text]
|
|
|
|
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
|
|
|
|
|
|
|
def extract_user_group(matched_text):
|
|
|
|
# type: (Text) -> Text
|
|
|
|
return matched_text[1:-1]
|
|
|
|
|
|
|
|
def possible_user_group_mentions(content):
|
|
|
|
# type: (Text) -> Set[Text]
|
|
|
|
matches = re.findall(user_group_mentions, content)
|
|
|
|
return {extract_user_group(match) for match in matches}
|