Skip to content

Commit 0986104

Browse files
authored
Merge pull request #997 from pkvach/fix/total-comments-count
comments: Fix total comments count calculation
2 parents 4560a1d + a952595 commit 0986104

4 files changed

Lines changed: 14 additions & 3 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ Bugfixes & Improvements
3333
- Prevent auto creation of invalid links in comments (`#995`_, pkvach)
3434
- Fix W3C Validation issues (`#999`_, pkvach)
3535
- Handle deleted comments in Disqus migration (`#994`_, pkvach)
36+
- Fix total comments count calculation (`#997`_, pkvach)
3637

3738
.. _#951: https://github.com/posativ/isso/pull/951
3839
.. _#967: https://github.com/posativ/isso/pull/967
3940
.. _#983: https://github.com/posativ/isso/pull/983
4041
.. _#995: https://github.com/isso-comments/isso/pull/995
4142
.. _#999: https://github.com/isso-comments/isso/pull/999
4243
.. _#994: https://github.com/isso-comments/isso/pull/994
44+
.. _#997: https://github.com/isso-comments/isso/pull/997
4345

4446
0.13.1.dev0 (2023-02-05)
4547
------------------------

isso/js/embed.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ function fetchComments() {
139139
if (comment.created > lastcreated) {
140140
lastcreated = comment.created;
141141
}
142-
count = count + comment.total_replies;
143142
});
144143
heading.textContent = i18n.pluralize("num-comments", count);
145144

isso/tests/test_comments.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def testCreateAndGetMultiple(self):
100100

101101
rv = loads(r.data)
102102
self.assertEqual(len(rv['replies']), 20)
103+
self.assertEqual(rv['total_replies'], 20)
103104

104105
def testCreateInvalidParent(self):
105106

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

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

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

198202
def testFetchEmpty(self):
199203

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

215219
rv = loads(r.data)
216220
self.assertEqual(len(rv['replies']), 10)
221+
self.assertEqual(rv['total_replies'], 20)
217222

218223
def testGetNested(self):
219224

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

227232
rv = loads(r.data)
228233
self.assertEqual(len(rv['replies']), 1)
234+
self.assertEqual(rv['total_replies'], 1)
229235

230236
def testGetLimitedNested(self):
231237

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

240246
rv = loads(r.data)
241247
self.assertEqual(len(rv['replies']), 10)
248+
self.assertEqual(rv['total_replies'], 20)
242249

243250
def testUpdate(self):
244251

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

291298
data = loads(client.get("/?uri=%2Fpath%2F").data)
292-
self.assertEqual(data["total_replies"], 1)
299+
self.assertEqual(data["total_replies"], 2)
293300

294301
self.assertEqual(self.get('/?uri=%2Fpath%2F&id=1').status_code, 200)
295302
self.assertEqual(self.get('/?uri=%2Fpath%2F&id=2').status_code, 200)

isso/views/comments.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,9 @@ def fetch(self, environ, request, uri):
912912
if root_id not in reply_counts:
913913
reply_counts[root_id] = 0
914914

915+
# We need to calculate the total number of comments for the root response value
916+
total_replies = sum(reply_counts.values()) if root_id is None else reply_counts[root_id]
917+
915918
try:
916919
nested_limit = int(request.args.get('nested_limit'))
917920
except TypeError:
@@ -921,7 +924,7 @@ def fetch(self, environ, request, uri):
921924

922925
rv = {
923926
'id': root_id,
924-
'total_replies': reply_counts[root_id],
927+
'total_replies': total_replies,
925928
'hidden_replies': reply_counts[root_id] - len(root_list),
926929
'replies': self._process_fetched_list(root_list, plain),
927930
'config': self.public_conf

0 commit comments

Comments
 (0)