Skip to content

Commit

Permalink
Fix #321. Handle continue this thread type more objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
bboe committed Jan 11, 2015
1 parent f297ad1 commit 1262327
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Unreleased
* **[BUGFIX]** Attempting to lazyload an attribute of a comment that has been
removed will explicitly raise a :meth:`praw.errors.InvalidComment`
exception, rather than an ``IndexError`` (issue #339).
* **[BUGFIX]** :meth:`.replace_more_comments` handles `continue this thread`
type ``MoreComments`` objects.
* **[FEATURE]** Added :meth:`praw.helpers.valid_redditors`.
* **[FEATURE]** Added a ``nsfw`` parameter to :meth:`.get_random_subreddit`
that permits fetching a random NSFW Subreddit. This change also supports
Expand Down
44 changes: 28 additions & 16 deletions praw/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,25 @@ def __unicode__(self):
"""Return a string representation of the MoreComments object."""
return '[More Comments: %d]' % self.count

def _continue_comments(self, update):
assert len(self.children) == 1 and self.id == self.children[0]
self._comments = self.reddit_session.get_submission(
urljoin(self.submission.permalink, self.id)).comments
assert len(self._comments) == 1
if update:
# pylint: disable-msg=W0212
self._comments[0]._update_submission(self.submission)
# pylint: enable-msg=W0212
return self._comments

def _update_submission(self, submission):
self.submission = submission

def comments(self, update=True):
"""Fetch and return the comments for a single MoreComments object."""
if not self._comments:
if self.count == 0: # Handle 'continue this thread' type
return self._continue_comments(update)
# pylint: disable-msg=W0212
children = [x for x in self.children if 't1_%s' % x
not in self.submission._comments_by_id]
Expand Down Expand Up @@ -888,9 +901,9 @@ def _extract_more_comments(tree):
parent, comm = queue.pop(0)
if isinstance(comm, MoreComments):
heappush(more_comments, comm)
if parent: # Remove from parent listing
if parent:
parent.replies.remove(comm)
elif parent is None: # Remove from tree root
else:
tree.remove(comm)
else:
for item in comm.replies:
Expand Down Expand Up @@ -1093,13 +1106,17 @@ def replace_more_comments(self, limit=32, threshold=1):

remaining = limit
more_comments = self._extract_more_comments(self.comments)
skipped = []

# Fetch largest more_comments until reaching the limit or the threshold
while more_comments:
item = heappop(more_comments)
# Skip after reaching the limit or below threshold
if remaining is 0 or item.count < threshold:
if remaining == 0: # We're not going to replace any more
heappush(more_comments, item) # It wasn't replaced
break
elif len(item.children) == 0 or 0 < item.count < threshold:
heappush(skipped, item) # It wasn't replaced
continue

# Fetch new comments and decrease remaining if a request was made
new_comments = item.comments(update=False)
Expand All @@ -1108,21 +1125,16 @@ def replace_more_comments(self, limit=32, threshold=1):
elif new_comments is None:
continue

# Insert into the tree or re-add to the list of more_comments
# Re-add new MoreComment objects to the heap of more_comments
for more in self._extract_more_comments(new_comments):
more._update_submission(self)
heappush(more_comments, more)
# Insert the new comments into the tree
for comment in new_comments:
# pylint: disable-msg=W0212
if isinstance(comment, MoreComments):
comment._update_submission(self)
heappush(more_comments, comment)
else:
# Replies needs to be an empty list
assert not comment._replies
comment._replies = []
self._insert_comment(comment)
# pylint: enable-msg=W0212
self._insert_comment(comment)

self._replaced_more = True
return more_comments
return more_comments + skipped

def set_flair(self, *args, **kwargs):
"""Set flair for this submission.
Expand Down
15 changes: 11 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def configure(self):
self.link_id = 't3_dtg4j'
self.link_url = self.url('/r/UCSantaBarbara/comments/m77nc/')
self.link_url_link = 'http://imgur.com/Vr8ZZ'
self.more_comments_url = self.url('/r/redditdev/comments/dqkfz/')
self.more_comments_url = self.url('/r/redditdev/comments/yjk55')
self.other_user_id = '6c1xj'
self.priv_submission_id = '16kbb7'
self.refresh_token = {
Expand Down Expand Up @@ -631,14 +631,21 @@ class MoreCommentsTest(unittest.TestCase, AuthenticatedHelper):
def setUp(self):
self.configure()
self.submission = self.r.get_submission(url=self.more_comments_url,
comment_limit=10)
comment_limit=130)

def test_all_comments(self):
c_len = len(self.submission.comments)
cf_len = len(helpers.flatten_tree(self.submission.comments))
flat = helpers.flatten_tree(self.submission.comments)
continue_items = [x for x in flat if isinstance(x, MoreComments) and
x.count == 0]
self.assertTrue(continue_items)
cf_len = len(flat)
saved = self.submission.replace_more_comments(threshold=2)
ac_len = len(self.submission.comments)
acf_len = len(helpers.flatten_tree(self.submission.comments))
flat = helpers.flatten_tree(self.submission.comments)
acf_len = len(flat)
for item in continue_items:
self.assertTrue(item.id in [x.id for x in flat])

# pylint: disable-msg=W0212
self.assertEqual(len(self.submission._comments_by_id), acf_len)
Expand Down

0 comments on commit 1262327

Please sign in to comment.