From 54b156844b9fe291468cd7b2eea79c98c5908e99 Mon Sep 17 00:00:00 2001 From: Srijan Choudhary Date: Sun, 27 Apr 2014 11:44:18 +0530 Subject: [PATCH] Add max-comments limit for nested replies --- isso/js/app/api.js | 22 ++++++++++++---------- isso/js/app/config.js | 3 ++- isso/js/app/isso.js | 5 ++++- isso/js/embed.js | 5 ++++- isso/views/comments.py | 37 ++++++++++++++++++++++++++++--------- 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/isso/js/app/api.js b/isso/js/app/api.js index 247cfd7..2c5eba5 100644 --- a/isso/js/app/api.js +++ b/isso/js/app/api.js @@ -128,19 +128,21 @@ define(["app/lib/promise", "app/globals"], function(Q, globals) { return deferred.promise; }; - var fetch = function(tid, limit, parent, lastcreated) { + var fetch = function(tid, limit, nested_limit, parent, lastcreated) { if(typeof(limit) == 'undefined') limit = 0; + if(typeof(nested_limit) == 'undefined') nested_limit = 0; if(typeof(parent) == 'undefined') parent = null; var deferred = Q.defer(); - curl("GET", endpoint + "/?" + qs({uri: tid || location, limit: limit, parent: parent, after: lastcreated}), null, function(rv) { - if (rv.status === 200) { - deferred.resolve(JSON.parse(rv.body)); - } else if (rv.status === 404) { - deferred.resolve({total_replies: 0}); - } else { - deferred.reject(rv.body); - } - }); + curl("GET", endpoint + "/?" + + qs({uri: tid || location, limit: limit, nested_limit: nested_limit, after: lastcreated, parent: parent}), null, function(rv) { + if (rv.status === 200) { + deferred.resolve(JSON.parse(rv.body)); + } else if (rv.status === 404) { + deferred.resolve({total_replies: 0}); + } else { + deferred.reject(rv.body); + } + }); return deferred.promise; }; diff --git a/isso/js/app/config.js b/isso/js/app/config.js index a2cc767..d9fa133 100644 --- a/isso/js/app/config.js +++ b/isso/js/app/config.js @@ -5,7 +5,8 @@ define(function() { "css": true, "lang": (navigator.language || navigator.userLanguage).split("-")[0], "reply-to-self": false, - "max-comments": 20, + "max-comments-top": 20, + "max-comments-nested": 5, "avatar-bg": "#f0f0f0", "avatar-fg": ["#9abf88", "#5698c4", "#e279a3", "#9163b6", "#be5168", "#f19670", "#e4bf80", "#447c69"].join(" ") diff --git a/isso/js/app/isso.js b/isso/js/app/isso.js index 90c28af..3cc780a 100644 --- a/isso/js/app/isso.js +++ b/isso/js/app/isso.js @@ -100,7 +100,10 @@ define(["app/text/html", "app/dom", "app/utils", "app/config", "app/api", "app/m $("a.load_hidden", el).toggle("click", function() { el.remove(); - api.fetch($("#isso-thread").getAttribute("data-isso-id"), limit = config["max-comments"], parent=commentWrapper.id, lastcreated=lastcreated).then( + api.fetch($("#isso-thread").getAttribute("data-isso-id"), + limit = config["max-comments-top"], nested_limit=0, + parent=commentWrapper.id, + lastcreated=lastcreated).then( function(rv) { if (rv.total_replies == 0) { return; diff --git a/isso/js/embed.js b/isso/js/embed.js index 11a807a..4bdd98a 100644 --- a/isso/js/embed.js +++ b/isso/js/embed.js @@ -26,7 +26,10 @@ require(["app/lib/ready", "app/config", "app/api", "app/isso", "app/count", "app $("#isso-thread").append(new isso.Postbox(null)); $("#isso-thread").append('
'); - api.fetch($("#isso-thread").getAttribute("data-isso-id"), limit = config["max-comments"]).then( + api.fetch($("#isso-thread").getAttribute("data-isso-id"), + limit = config["max-comments-top"], + nested_limit = config["max-comments-nested"], + parent='NULL').then( function(rv) { if (rv.total_replies == 0) { $("#isso-thread > h4").textContent = Mark.up("{{ i18n-no-comments }}"); diff --git a/isso/views/comments.py b/isso/views/comments.py index 52d72e6..f172bcd 100644 --- a/isso/views/comments.py +++ b/isso/views/comments.py @@ -330,14 +330,20 @@ class API(object): try: fetch_args['limit'] = int(request.args.get('limit')) except ValueError: - return BadRequest("Limit should be integer") + return BadRequest("limit should be integer") if request.args.get('parent'): - try: - fetch_args['parent'] = int(request.args.get('parent')) - root_id = int(request.args.get('parent')) - except ValueError: - return BadRequest("Parent should be integer") + parent_arg = request.args.get('parent') + if parent_arg == 'NULL': + fetch_args['parent'] = parent_arg + root_id = None + else: + try: + fetch_args['parent'] = int(parent_arg) + root_id = int(parent_arg) + except ValueError: + return BadRequest("parent should be integer or NULL") else: + fetch_args['parent'] = 'NULL' root_id = None if request.args.get('plain', '0') == '0': @@ -347,13 +353,20 @@ class API(object): reply_counts = self.comments.reply_count(uri, after) - full_list = list(self.comments.fetch(**fetch_args)) - root_list = [i for i in full_list if i['parent'] == root_id] + root_list = list(self.comments.fetch(**fetch_args)) if not root_list: raise NotFound if root_id not in reply_counts: reply_counts[root_id] = 0 + if request.args.get('nested_limit'): + try: + nested_limit = int(request.args.get('nested_limit')) + except ValueError: + return BadRequest("nested_limit should be integer") + else: + nested_limit = 0 + rv = { 'id' : root_id, 'total_replies' : reply_counts[root_id], @@ -363,11 +376,17 @@ class API(object): # We are only checking for one level deep comments if root_id is None: for comment in rv['replies']: - replies = [i for i in full_list if i['parent'] == comment['id']] if comment['id'] in reply_counts: comment['total_replies'] = reply_counts[comment['id']] + if nested_limit > 0: + fetch_args['parent'] = comment['id'] + fetch_args['limit'] = nested_limit + replies = list(self.comments.fetch(**fetch_args)) + else: + replies = [] else: comment['total_replies'] = 0 + replies = [] comment['hidden_replies'] = comment['total_replies'] - len(replies) comment['replies'] = self.process_fetched_list(replies, plain)