Skip to content

Commit

Permalink
comments: Fix total comments count calculation
Browse files Browse the repository at this point in the history
Changes to properly count hidden comments when calculating the total number of comments.

Fixes isso-comments#448
  • Loading branch information
pkvach committed Apr 14, 2024
1 parent eb35b17 commit a952595
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ Bugfixes & Improvements
- Prevent auto creation of invalid links in comments (`#995`_, pkvach)
- Fix W3C Validation issues (`#999`_, pkvach)
- Handle deleted comments in Disqus migration (`#994`_, pkvach)
- Fix total comments count calculation (`#997`_, pkvach)

.. _#951: https://github.com/posativ/isso/pull/951
.. _#967: https://github.com/posativ/isso/pull/967
.. _#983: https://github.com/posativ/isso/pull/983
.. _#995: https://github.com/isso-comments/isso/pull/995
.. _#999: https://github.com/isso-comments/isso/pull/999
.. _#994: https://github.com/isso-comments/isso/pull/994
.. _#997: https://github.com/isso-comments/isso/pull/997

0.13.1.dev0 (2023-02-05)
------------------------
Expand Down
1 change: 0 additions & 1 deletion isso/js/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ function fetchComments() {
if (comment.created > lastcreated) {
lastcreated = comment.created;
}
count = count + comment.total_replies;
});
heading.textContent = i18n.pluralize("num-comments", count);

Expand Down
9 changes: 8 additions & 1 deletion isso/tests/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def testCreateAndGetMultiple(self):

rv = loads(r.data)
self.assertEqual(len(rv['replies']), 20)
self.assertEqual(rv['total_replies'], 20)

def testCreateInvalidParent(self):

Expand Down Expand Up @@ -185,15 +186,18 @@ def testGetInvalid(self):
self.assertEqual(self.get('/?uri=%2Fpath%2F&id=123').status_code, 200)
data = loads(self.get('/?uri=%2Fpath%2F&id=123').data)
self.assertEqual(len(data['replies']), 0)
self.assertEqual(data['total_replies'], 0)

self.assertEqual(
self.get('/?uri=%2Fpath%2Fspam%2F&id=123').status_code, 200)
data = loads(self.get('/?uri=%2Fpath%2Fspam%2F&id=123').data)
self.assertEqual(len(data['replies']), 0)
self.assertEqual(data['total_replies'], 0)

self.assertEqual(self.get('/?uri=?uri=%foo%2F').status_code, 200)
data = loads(self.get('/?uri=?uri=%foo%2F').data)
self.assertEqual(len(data['replies']), 0)
self.assertEqual(data['total_replies'], 0)

def testFetchEmpty(self):

Expand All @@ -214,6 +218,7 @@ def testGetLimited(self):

rv = loads(r.data)
self.assertEqual(len(rv['replies']), 10)
self.assertEqual(rv['total_replies'], 20)

def testGetNested(self):

Expand All @@ -226,6 +231,7 @@ def testGetNested(self):

rv = loads(r.data)
self.assertEqual(len(rv['replies']), 1)
self.assertEqual(rv['total_replies'], 1)

def testGetLimitedNested(self):

Expand All @@ -239,6 +245,7 @@ def testGetLimitedNested(self):

rv = loads(r.data)
self.assertEqual(len(rv['replies']), 10)
self.assertEqual(rv['total_replies'], 20)

def testUpdate(self):

Expand Down Expand Up @@ -289,7 +296,7 @@ def testDeleteWithReference(self):
self.assertIn('/path/', self.app.db.threads)

data = loads(client.get("/?uri=%2Fpath%2F").data)
self.assertEqual(data["total_replies"], 1)
self.assertEqual(data["total_replies"], 2)

self.assertEqual(self.get('/?uri=%2Fpath%2F&id=1').status_code, 200)
self.assertEqual(self.get('/?uri=%2Fpath%2F&id=2').status_code, 200)
Expand Down
5 changes: 4 additions & 1 deletion isso/views/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,9 @@ def fetch(self, environ, request, uri):
if root_id not in reply_counts:
reply_counts[root_id] = 0

# We need to calculate the total number of comments for the root response value
total_replies = sum(reply_counts.values()) if root_id is None else reply_counts[root_id]

try:
nested_limit = int(request.args.get('nested_limit'))
except TypeError:
Expand All @@ -889,7 +892,7 @@ def fetch(self, environ, request, uri):

rv = {
'id': root_id,
'total_replies': reply_counts[root_id],
'total_replies': total_replies,
'hidden_replies': reply_counts[root_id] - len(root_list),
'replies': self._process_fetched_list(root_list, plain),
'config': self.public_conf
Expand Down

0 comments on commit a952595

Please sign in to comment.