steemit / steem-python

The official Python (3) library for the Steem Blockchain.
https://steem.io
MIT License
154 stars 100 forks source link

utils.is_comment() should not rely on "re-" in permlink, giving wrong Blog().take() results #228

Closed crokkon closed 6 years ago

crokkon commented 6 years ago

Version of Python you are running

# python --version
Python 3.6.4

Version of steem-python you are running

# steempy --version
steempy 1.0.0

Expected Behavior

steem.blog.take() should return top-level posts for Blog(comments_only=False) and comments for Blog(comments_only=True). The distinction between post and comment internally uses steem.utils.is_comment(). This should returns False for root posts, True or others for comments.

Actual Behavior

steem.utils.is_comment() returns False if the post is a comment but the permlink doesn't start with "re-". As a result, the Blog().take() results may be lacking comments or list comments as root posts.

Steps to reproduce

Reply to a post with a custom permlink that doesn't start with "re-".

Example:

>>> from steem.post import Post
>>> from steem.utils import is_comment
>>> is_comment(Post("stmdev/steem-utils-is-comment-root-post"))
False
>>> is_comment(Post("stmdev/re-steem-utils-is-comment-root-post-20180427t113939"))
'stmdev'
>>> is_comment(Post("stmdev/lorem-ipsum-dolor-sit-amet"))
False

steem.utils.is_comment is used in steem.blog.take() and therefore possibly returns wrong results for Blog(comments_only=True) and Blog(comments_only=False):

>>> from steem.blog import Blog
>>> b = Blog("stmdev", comments_only=False)
>>> b.take(5)
[<Post-stmdev/utils-iscomment-should-not-rely-on-re-in-permlink>, <Post-stmdev/lorem-ipsum-dolor-sit-amet>, <Post-stmdev/steem-utils-is-comment-root-post>, <Post-stmdev/difftest>, <Post-stmdev/clicking-tag-in-body-exceeding-1169-chars-triggers-a-502-bad-gateway>]

The second result in this example is actually a comment.

Stack Trace

none.

Possible fixes:

diff --git a/steem/utils.py b/steem/utils.py
index 856ebb8..8842fef 100755
--- a/steem/utils.py
+++ b/steem/utils.py
@@ -186,7 +186,7 @@ def is_comment(item):
     The item can be a Post object or just a raw comment object from the
     blockchain.
     """
-    return item['permlink'][:3] == "re-" and item['parent_author']
+    return item['parent_author'] != ""

 def time_elapsed(posting_time):

This would also avoid the inconsistency of returning False for root posts but the parent author as string for comments.

Alternatively: remove steem.utils.is_comment() and use steem.post.is_comment() instead?



Posted on Utopian.io - Rewarding Open Source Contributors

roadscape commented 6 years ago

Thanks for the report, we'd be happy to accept a PR for this fix!