From 8fefe3a6160545c0094b595d672fbc47ce1013ea Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Tue, 27 May 2014 16:10:15 +0200 Subject: [PATCH] fix hidden reply to deleted comment (and change a few names) --- isso/db/comments.py | 13 ++++++++----- isso/js/embed.js | 15 ++++++++------- isso/tests/test_comments.py | 3 +++ isso/views/comments.py | 2 +- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/isso/db/comments.py b/isso/db/comments.py index 350a3c9..84ed27e 100644 --- a/isso/db/comments.py +++ b/isso/db/comments.py @@ -197,16 +197,19 @@ class Comments: return {'likes': likes + 1, 'dislikes': dislikes} return {'likes': likes, 'dislikes': dislikes + 1} - def reply_count(self, url, after=0): + def reply_count(self, url, mode=5, after=0): """ Return comment count for main thread and all reply threads for one url. """ - sql = [ 'SELECT comments.parent,count(*) FROM comments INNER JOIN threads ON', - ' threads.uri=? AND comments.tid=threads.id', - ' AND comments.mode = 1 AND comments.created>? GROUP BY comments.parent;'] + sql = ['SELECT comments.parent,count(*)', + 'FROM comments INNER JOIN threads ON', + ' threads.uri=? AND comments.tid=threads.id AND', + ' (? | comments.mode = ?) AND', + ' comments.created > ?', + 'GROUP BY comments.parent'] - return dict(self.db.execute(sql, [url, after]).fetchall()) + return dict(self.db.execute(sql, [url, mode, mode, after]).fetchall()) def count(self, *urls): """ diff --git a/isso/js/embed.js b/isso/js/embed.js index 4328356..c3e9cfb 100644 --- a/isso/js/embed.js +++ b/isso/js/embed.js @@ -41,15 +41,16 @@ require(["app/lib/ready", "app/config", "app/i18n", "app/api", "app/isso", "app/ } var lastcreated = 0; - var total_count = rv.total_replies; - rv.replies.forEach(function(commentObject) { - isso.insert(commentObject, false); - if(commentObject.created > lastcreated) { - lastcreated = commentObject.created; + var count = rv.total_replies; + console.log(rv.replies); + rv.replies.forEach(function(comment) { + isso.insert(comment, false); + if(comment.created > lastcreated) { + lastcreated = comment.created; } - total_count = total_count + commentObject.total_replies; + count = count + comment.total_replies; }); - $("#isso-thread > h4").textContent = i18n.pluralize("num-comments", total_count); + $("#isso-thread > h4").textContent = i18n.pluralize("num-comments", count); if(rv.hidden_replies > 0) { isso.insert_loader(rv, lastcreated); diff --git a/isso/tests/test_comments.py b/isso/tests/test_comments.py index 43d7ecf..b278c8b 100644 --- a/isso/tests/test_comments.py +++ b/isso/tests/test_comments.py @@ -223,6 +223,9 @@ class TestComments(unittest.TestCase): self.assertEqual(loads(r.data)['mode'], 4) self.assertIn('/path/', self.app.db.threads) + data = loads(client.get("/?uri=%2Fpath%2F").data) + self.assertEqual(data["total_replies"], 1) + 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 cb2bfd6..5ce369a 100644 --- a/isso/views/comments.py +++ b/isso/views/comments.py @@ -374,7 +374,7 @@ class API(object): plain = request.args.get('plain', '0') == '0' - reply_counts = self.comments.reply_count(uri, args['after']) + reply_counts = self.comments.reply_count(uri, after=args['after']) if args['limit'] == 0: root_list = []