Skip to content
Snippets Groups Projects
Commit 4894f005 authored by roadscape's avatar roadscape
Browse files

test basic mentions match

parent 9e17ecb0
No related branches found
No related tags found
No related merge requests found
...@@ -560,7 +560,11 @@ class CachedPost: ...@@ -560,7 +560,11 @@ class CachedPost:
Notify(notif_type, src_id=author_id, dst_id=parent_id, Notify(notif_type, src_id=author_id, dst_id=parent_id,
post_id=pid, when=post['last_update'], score=score).write() post_id=pid, when=post['last_update'], score=score).write()
if level in ('insert', 'update'): if level in ('insert', 'update'):
mentions(post) accts = mentions(post['body'])
for acct in accts:
if not Accounts.exists(acct):
url = '@' + post['author'] + '/' + post['permlink']
log.warning("bad mention [%s] in %s", acct, url)
@classmethod @classmethod
......
...@@ -11,39 +11,17 @@ from hive.utils.normalize import sbd_amount, rep_log10, safe_img_url, parse_time ...@@ -11,39 +11,17 @@ from hive.utils.normalize import sbd_amount, rep_log10, safe_img_url, parse_time
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def mentions(post): def mentions(body):
"""Given a post, return proper @-mentioned account names."""
# pylint: disable=invalid-name
detected = text_mentions(post['body'])
provided = _post_users(post)
d1 = detected - provided
d2 = provided - detected
url = '@' + post['author'] + '/' + post['permlink']
if d1: log.warning("%s detected - provided: %s", url, d1)
if d2: log.warning("%s provided - detected: %s", url, d2)
return detected & provided
def text_mentions(body):
"""Given a post body, return proper @-mentioned account names.""" """Given a post body, return proper @-mentioned account names."""
matches = re.findall('(?:^|[^a-zA-Z0-9_!#$%&*@])(:?@)([a-z\\d\\-.]+)', body) # condenser:
return {grp[1] for grp in matches} # /(^|[^a-zA-Z0-9_!#$%&*@@\/]|(^|[^a-zA-Z0-9_+~.-\/#]))[@@]([a-z][-\.a-z\d]+[a-z\d])/gi,
def _post_users(post): matches = re.findall(
"""Retrieve `users` key from json_metadata.""" '(?:^|[^a-zA-Z0-9_!#$%&*@\\/])'
md = {} '(:?@)'
try: '([a-zA-Z0-9][a-zA-Z0-9\\-.]{1,14}[a-zA-Z0-9])'
md = json.loads(post['json_metadata']) '(?![a-z])', body)
if not isinstance(md, dict): return {grp[1].lower() for grp in matches}
md = {}
except Exception:
pass
if 'users' in md and isinstance(md['users'], list):
return {user.strip('.@') for user in md['users'] if user and isinstance(user, str)}
return set()
def post_basic(post): def post_basic(post):
"""Basic post normalization: json-md, tags, and flags.""" """Basic post normalization: json-md, tags, and flags."""
......
...@@ -3,7 +3,6 @@ from decimal import Decimal ...@@ -3,7 +3,6 @@ from decimal import Decimal
from hive.utils.post import ( from hive.utils.post import (
mentions, mentions,
text_mentions,
post_basic, post_basic,
post_legacy, post_legacy,
post_payout, post_payout,
...@@ -140,19 +139,17 @@ POST_2 = { ...@@ -140,19 +139,17 @@ POST_2 = {
} }
def test_mentions(): def test_mentions():
post = {'body': 'Who is @abc and @foo and @bar',
'json_metadata': '{"users":["foo","bar"]}'}
assert mentions(post) == {'foo', 'bar'}
def test_text_mentions():
# pylint: disable=invalid-name # pylint: disable=invalid-name
m = text_mentions m = mentions
assert m('Hi @abc, meet @bob') == {'abc', 'bob'} assert m('Hi @abc, meet @bob') == {'abc', 'bob'}
assert m('Hi @abc, meet @abc') == {'abc'} assert m('Hi @abc, meet @abc') == {'abc'}
assert not m('') assert not m('')
assert not m('@') assert not m('@')
assert m('steemit.com/@apple') == {'apple'} assert not m('steemit.com/@apple')
assert not m('joe@apple.com') assert not m('joe@apple.com')
assert m('@longestokaccount') == {'longestokaccount'}
assert not m('@longestokaccountx')
assert m('@abc- @-foo @bar.') == {'abc', 'bar'}
def test_post_basic(): def test_post_basic():
ret = post_basic(POST_1) ret = post_basic(POST_1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment