Add max-comments limit for nested replies

This commit is contained in:
Srijan Choudhary 2014-04-27 11:44:18 +05:30
parent f5da45e525
commit 54b156844b
5 changed files with 50 additions and 22 deletions

View File

@ -128,19 +128,21 @@ define(["app/lib/promise", "app/globals"], function(Q, globals) {
return deferred.promise; 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(limit) == 'undefined') limit = 0;
if(typeof(nested_limit) == 'undefined') nested_limit = 0;
if(typeof(parent) == 'undefined') parent = null; if(typeof(parent) == 'undefined') parent = null;
var deferred = Q.defer(); var deferred = Q.defer();
curl("GET", endpoint + "/?" + qs({uri: tid || location, limit: limit, parent: parent, after: lastcreated}), null, function(rv) { curl("GET", endpoint + "/?" +
if (rv.status === 200) { qs({uri: tid || location, limit: limit, nested_limit: nested_limit, after: lastcreated, parent: parent}), null, function(rv) {
deferred.resolve(JSON.parse(rv.body)); if (rv.status === 200) {
} else if (rv.status === 404) { deferred.resolve(JSON.parse(rv.body));
deferred.resolve({total_replies: 0}); } else if (rv.status === 404) {
} else { deferred.resolve({total_replies: 0});
deferred.reject(rv.body); } else {
} deferred.reject(rv.body);
}); }
});
return deferred.promise; return deferred.promise;
}; };

View File

@ -5,7 +5,8 @@ define(function() {
"css": true, "css": true,
"lang": (navigator.language || navigator.userLanguage).split("-")[0], "lang": (navigator.language || navigator.userLanguage).split("-")[0],
"reply-to-self": false, "reply-to-self": false,
"max-comments": 20, "max-comments-top": 20,
"max-comments-nested": 5,
"avatar-bg": "#f0f0f0", "avatar-bg": "#f0f0f0",
"avatar-fg": ["#9abf88", "#5698c4", "#e279a3", "#9163b6", "avatar-fg": ["#9abf88", "#5698c4", "#e279a3", "#9163b6",
"#be5168", "#f19670", "#e4bf80", "#447c69"].join(" ") "#be5168", "#f19670", "#e4bf80", "#447c69"].join(" ")

View File

@ -100,7 +100,10 @@ define(["app/text/html", "app/dom", "app/utils", "app/config", "app/api", "app/m
$("a.load_hidden", el).toggle("click", $("a.load_hidden", el).toggle("click",
function() { function() {
el.remove(); 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) { function(rv) {
if (rv.total_replies == 0) { if (rv.total_replies == 0) {
return; return;

View File

@ -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(new isso.Postbox(null));
$("#isso-thread").append('<div id="isso-root"></div>'); $("#isso-thread").append('<div id="isso-root"></div>');
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) { function(rv) {
if (rv.total_replies == 0) { if (rv.total_replies == 0) {
$("#isso-thread > h4").textContent = Mark.up("{{ i18n-no-comments }}"); $("#isso-thread > h4").textContent = Mark.up("{{ i18n-no-comments }}");

View File

@ -330,14 +330,20 @@ class API(object):
try: try:
fetch_args['limit'] = int(request.args.get('limit')) fetch_args['limit'] = int(request.args.get('limit'))
except ValueError: except ValueError:
return BadRequest("Limit should be integer") return BadRequest("limit should be integer")
if request.args.get('parent'): if request.args.get('parent'):
try: parent_arg = request.args.get('parent')
fetch_args['parent'] = int(request.args.get('parent')) if parent_arg == 'NULL':
root_id = int(request.args.get('parent')) fetch_args['parent'] = parent_arg
except ValueError: root_id = None
return BadRequest("Parent should be integer") else:
try:
fetch_args['parent'] = int(parent_arg)
root_id = int(parent_arg)
except ValueError:
return BadRequest("parent should be integer or NULL")
else: else:
fetch_args['parent'] = 'NULL'
root_id = None root_id = None
if request.args.get('plain', '0') == '0': if request.args.get('plain', '0') == '0':
@ -347,13 +353,20 @@ class API(object):
reply_counts = self.comments.reply_count(uri, after) reply_counts = self.comments.reply_count(uri, after)
full_list = list(self.comments.fetch(**fetch_args)) root_list = list(self.comments.fetch(**fetch_args))
root_list = [i for i in full_list if i['parent'] == root_id]
if not root_list: if not root_list:
raise NotFound raise NotFound
if root_id not in reply_counts: if root_id not in reply_counts:
reply_counts[root_id] = 0 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 = { rv = {
'id' : root_id, 'id' : root_id,
'total_replies' : reply_counts[root_id], 'total_replies' : reply_counts[root_id],
@ -363,11 +376,17 @@ class API(object):
# We are only checking for one level deep comments # We are only checking for one level deep comments
if root_id is None: if root_id is None:
for comment in rv['replies']: for comment in rv['replies']:
replies = [i for i in full_list if i['parent'] == comment['id']]
if comment['id'] in reply_counts: if comment['id'] in reply_counts:
comment['total_replies'] = reply_counts[comment['id']] 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: else:
comment['total_replies'] = 0 comment['total_replies'] = 0
replies = []
comment['hidden_replies'] = comment['total_replies'] - len(replies) comment['hidden_replies'] = comment['total_replies'] - len(replies)
comment['replies'] = self.process_fetched_list(replies, plain) comment['replies'] = self.process_fetched_list(replies, plain)