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 Mar 7, 2024
1 parent 9755fe6 commit b54b6bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
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
10 changes: 9 additions & 1 deletion isso/tests/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ 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 +187,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 +219,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 +232,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 +246,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 +297,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 b54b6bc

Please # to comment.