From 4fa53aab4bfa330a2c7a673f3bacab4979622ff3 Mon Sep 17 00:00:00 2001 From: Pavel Kvach Date: Thu, 7 Mar 2024 11:42:57 +0200 Subject: [PATCH] comments: Fix total comments count calculation Changes to properly count hidden comments when calculating the total number of comments. Fixes https://github.com/isso-comments/isso/issues/448 --- CHANGES.rst | 2 ++ isso/js/embed.js | 1 - isso/tests/test_comments.py | 9 ++++++++- isso/views/comments.py | 5 ++++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 718368a99..3ac495712 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -27,6 +27,7 @@ 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 @@ -34,6 +35,7 @@ Bugfixes & Improvements .. _#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) ------------------------ diff --git a/isso/js/embed.js b/isso/js/embed.js index 6c7c0c61a..1c1cfb8e3 100644 --- a/isso/js/embed.js +++ b/isso/js/embed.js @@ -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); diff --git a/isso/tests/test_comments.py b/isso/tests/test_comments.py index ed4b86550..8f6b4579c 100644 --- a/isso/tests/test_comments.py +++ b/isso/tests/test_comments.py @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): @@ -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) diff --git a/isso/views/comments.py b/isso/views/comments.py index 3e31f2293..33d218a73 100644 --- a/isso/views/comments.py +++ b/isso/views/comments.py @@ -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: @@ -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