Files
zulip/zerver/lib/mention.py
Steve Howell 4e7fce60ee Add possible_mentions() to speed up rendering.
We now triage message content for possible mentions before
going to the cache/DB to get name info.  This will create an
extra data hop for messages with mentions, but it will save
a fairly expensive cache lookup for most messages.  (This will
be especially helpful for large realms.)

[Note that we need a subsequent commit to actually make the speedup
happen here, since avatars also cause us to look up all users in
the realm.]
2017-09-15 01:09:08 -07:00

34 lines
876 B
Python

from __future__ import absolute_import
from typing import Optional, Set, Text
import re
# Match multi-word string between @** ** or match any one-word
# sequences after @
find_mentions = r'(?<![^\s\'\"\(,:<])@(\*\*[^\*]+\*\*|all|everyone)'
wildcards = ['all', 'everyone']
def user_mention_matches_wildcard(mention):
# type: (Text) -> bool
return mention in wildcards
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)
names = {extract_name(match) for match in matches}
names = {name for name in names if name}
return names