2021-06-13 00:51:30 +02:00
|
|
|
import functools
|
2017-09-14 19:47:22 +02:00
|
|
|
import re
|
2021-12-28 10:02:27 +01:00
|
|
|
from dataclasses import dataclass
|
2021-06-13 00:51:30 +02:00
|
|
|
from typing import Dict, List, Match, Optional, Set, Tuple
|
|
|
|
|
|
|
|
from django.db.models import Q
|
|
|
|
|
2021-08-05 03:26:56 +02:00
|
|
|
from zerver.models import Realm, UserGroup, UserProfile, get_active_streams
|
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
|
|
|
|
2021-12-28 10:02:27 +01:00
|
|
|
@dataclass
|
|
|
|
class FullNameInfo:
|
2021-12-27 19:17:49 +01:00
|
|
|
id: int
|
|
|
|
full_name: str
|
|
|
|
|
|
|
|
|
2021-12-28 14:06:14 +01:00
|
|
|
@dataclass
|
|
|
|
class UserFilter:
|
|
|
|
id: Optional[int]
|
|
|
|
full_name: Optional[str]
|
|
|
|
|
|
|
|
def Q(self) -> Q:
|
|
|
|
if self.full_name is not None and self.id is not None:
|
|
|
|
return Q(full_name__iexact=self.full_name, id=self.id)
|
|
|
|
elif self.id is not None:
|
|
|
|
return Q(id=self.id)
|
|
|
|
elif self.full_name is not None:
|
|
|
|
return Q(full_name__iexact=self.full_name)
|
|
|
|
else:
|
|
|
|
raise AssertionError("totally empty filter makes no sense")
|
|
|
|
|
|
|
|
|
2021-12-29 13:52:27 +01:00
|
|
|
@dataclass
|
|
|
|
class MentionBackend:
|
|
|
|
realm_id: int
|
|
|
|
|
|
|
|
def get_full_name_info_list(self, user_filters: List[UserFilter]) -> List[FullNameInfo]:
|
|
|
|
q_list = [user_filter.Q() for user_filter in user_filters]
|
|
|
|
|
|
|
|
rows = (
|
|
|
|
UserProfile.objects.filter(
|
|
|
|
realm_id=self.realm_id,
|
|
|
|
is_active=True,
|
|
|
|
)
|
|
|
|
.filter(
|
|
|
|
functools.reduce(lambda a, b: a | b, q_list),
|
|
|
|
)
|
|
|
|
.only(
|
|
|
|
"id",
|
|
|
|
"full_name",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return [FullNameInfo(id=row.id, full_name=row.full_name) for row in rows]
|
|
|
|
|
|
|
|
|
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)}
|
2021-06-13 00:51:30 +02:00
|
|
|
|
|
|
|
|
2021-12-29 13:52:27 +01:00
|
|
|
def get_possible_mentions_info(
|
|
|
|
mention_backend: MentionBackend, mention_texts: Set[str]
|
|
|
|
) -> List[FullNameInfo]:
|
2021-06-13 00:51:30 +02:00
|
|
|
if not mention_texts:
|
|
|
|
return []
|
|
|
|
|
2021-12-28 14:06:14 +01:00
|
|
|
user_filters = list()
|
2021-06-13 00:51:30 +02:00
|
|
|
|
|
|
|
name_re = r"(?P<full_name>.+)?\|(?P<mention_id>\d+)$"
|
|
|
|
for mention_text in mention_texts:
|
|
|
|
name_syntax_match = re.match(name_re, mention_text)
|
|
|
|
if name_syntax_match:
|
|
|
|
full_name = name_syntax_match.group("full_name")
|
|
|
|
mention_id = name_syntax_match.group("mention_id")
|
|
|
|
if full_name:
|
|
|
|
# For **name|id** mentions as mention_id
|
|
|
|
# cannot be null inside this block.
|
2021-12-28 14:06:14 +01:00
|
|
|
user_filters.append(UserFilter(full_name=full_name, id=int(mention_id)))
|
2021-06-13 00:51:30 +02:00
|
|
|
else:
|
|
|
|
# For **|id** syntax.
|
2021-12-28 14:06:14 +01:00
|
|
|
user_filters.append(UserFilter(full_name=None, id=int(mention_id)))
|
2021-06-13 00:51:30 +02:00
|
|
|
else:
|
|
|
|
# For **name** syntax.
|
2021-12-28 14:06:14 +01:00
|
|
|
user_filters.append(UserFilter(full_name=mention_text, id=None))
|
|
|
|
|
2021-12-29 13:52:27 +01:00
|
|
|
return mention_backend.get_full_name_info_list(user_filters)
|
2021-06-13 00:51:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MentionData:
|
2021-12-29 13:52:27 +01:00
|
|
|
def __init__(self, mention_backend: MentionBackend, content: str) -> None:
|
|
|
|
realm_id = mention_backend.realm_id
|
2021-06-13 00:51:30 +02:00
|
|
|
mention_texts, has_wildcards = possible_mentions(content)
|
2021-12-29 13:52:27 +01:00
|
|
|
possible_mentions_info = get_possible_mentions_info(mention_backend, mention_texts)
|
2021-12-28 10:02:27 +01:00
|
|
|
self.full_name_info = {row.full_name.lower(): row for row in possible_mentions_info}
|
|
|
|
self.user_id_info = {row.id: row for row in possible_mentions_info}
|
2021-06-13 00:51:30 +02:00
|
|
|
self.init_user_group_data(realm_id=realm_id, content=content)
|
|
|
|
self.has_wildcards = has_wildcards
|
|
|
|
|
|
|
|
def message_has_wildcards(self) -> bool:
|
|
|
|
return self.has_wildcards
|
|
|
|
|
|
|
|
def init_user_group_data(self, realm_id: int, content: str) -> None:
|
2021-08-05 03:26:56 +02:00
|
|
|
self.user_group_name_info: Dict[str, UserGroup] = {}
|
|
|
|
self.user_group_members: Dict[int, List[int]] = {}
|
2021-06-13 00:51:30 +02:00
|
|
|
user_group_names = possible_user_group_mentions(content)
|
2021-08-05 03:26:56 +02:00
|
|
|
if user_group_names:
|
|
|
|
for group in UserGroup.objects.filter(
|
2021-08-21 16:25:05 +02:00
|
|
|
realm_id=realm_id, name__in=user_group_names, is_system_group=False
|
2021-10-11 08:37:15 +02:00
|
|
|
).prefetch_related("direct_members"):
|
2021-08-05 03:26:56 +02:00
|
|
|
self.user_group_name_info[group.name.lower()] = group
|
2021-10-11 08:37:15 +02:00
|
|
|
self.user_group_members[group.id] = [m.id for m in group.direct_members.all()]
|
2021-06-13 00:51:30 +02:00
|
|
|
|
|
|
|
def get_user_by_name(self, name: str) -> Optional[FullNameInfo]:
|
|
|
|
# warning: get_user_by_name is not dependable if two
|
|
|
|
# users of the same full name are mentioned. Use
|
|
|
|
# get_user_by_id where possible.
|
|
|
|
return self.full_name_info.get(name.lower(), None)
|
|
|
|
|
|
|
|
def get_user_by_id(self, id: int) -> Optional[FullNameInfo]:
|
|
|
|
return self.user_id_info.get(id, None)
|
|
|
|
|
|
|
|
def get_user_ids(self) -> Set[int]:
|
|
|
|
"""
|
|
|
|
Returns the user IDs that might have been mentioned by this
|
|
|
|
content. Note that because this data structure has not parsed
|
|
|
|
the message and does not know about escaping/code blocks, this
|
|
|
|
will overestimate the list of user ids.
|
|
|
|
"""
|
|
|
|
return set(self.user_id_info.keys())
|
|
|
|
|
|
|
|
def get_user_group(self, name: str) -> Optional[UserGroup]:
|
|
|
|
return self.user_group_name_info.get(name.lower(), None)
|
|
|
|
|
|
|
|
def get_group_members(self, user_group_id: int) -> List[int]:
|
|
|
|
return self.user_group_members.get(user_group_id, [])
|
|
|
|
|
|
|
|
|
2021-12-27 19:41:00 +01:00
|
|
|
def get_stream_name_map(realm: Realm, stream_names: Set[str]) -> Dict[str, int]:
|
2021-06-13 00:51:30 +02:00
|
|
|
if not stream_names:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
q_list = {Q(name=name) for name in stream_names}
|
|
|
|
|
|
|
|
rows = (
|
|
|
|
get_active_streams(
|
|
|
|
realm=realm,
|
|
|
|
)
|
|
|
|
.filter(
|
|
|
|
functools.reduce(lambda a, b: a | b, q_list),
|
|
|
|
)
|
|
|
|
.values(
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-12-27 19:41:00 +01:00
|
|
|
dct = {row["name"]: row["id"] for row in rows}
|
2021-06-13 00:51:30 +02:00
|
|
|
return dct
|
2021-12-07 21:41:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
def silent_mention_syntax_for_user(user_profile: UserProfile) -> str:
|
|
|
|
return f"@_**{user_profile.full_name}|{user_profile.id}**"
|