Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
hivemind
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hive
hivemind
Commits
4894f005
Commit
4894f005
authored
5 years ago
by
roadscape
Browse files
Options
Downloads
Patches
Plain Diff
test basic mentions match
parent
9e17ecb0
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
hive/indexer/cached_post.py
+5
-1
5 additions, 1 deletion
hive/indexer/cached_post.py
hive/utils/post.py
+10
-32
10 additions, 32 deletions
hive/utils/post.py
tests/utils/test_utils_post.py
+5
-8
5 additions, 8 deletions
tests/utils/test_utils_post.py
with
20 additions
and
41 deletions
hive/indexer/cached_post.py
+
5
−
1
View file @
4894f005
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
hive/utils/post.py
+
10
−
32
View file @
4894f005
...
@@ -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.
"""
...
...
This diff is collapsed.
Click to expand it.
tests/utils/test_utils_post.py
+
5
−
8
View file @
4894f005
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment