Skip to content

Commit

Permalink
getcomments API
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Oct 30, 2011
1 parent 224470f commit e739fa7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
31 changes: 31 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,37 @@ def render_comment_subthread(comment,sep="")
return { :status => "ok", :news => news, :count => numitems }.to_json
end

get '/api/getcomments/:news_id' do
return {
:status => "err",
:error => "Wrong news ID."
}.to_json if not get_news_by_id(params[:news_id])
thread = Comments.fetch_thread(params[:news_id])
top_comments = []
thread.each{|parent,replies|
if parent.to_i == -1
top_comments = replies
end
replies.each{|r|
user = get_user_by_id(r['user_id']) || DeletedUser
r['username'] = user['username']
r['replies'] = thread[r['id']] || []
if r['up']
r['voted'] = :up if $user && r['up'].index($user['id'].to_i)
r['up'] = r['up'].length
end
if r['down']
r['voted'] = :down if $user && r['down'].index($user['id'].to_i)
r['down'] = r['down'].length
end
['id','thread_id','score','parent_id','user_id'].each{|f|
r.delete(f)
}
}
}
return { :status => "ok", :comments => top_comments }.to_json
end

# Check that the list of parameters specified exist.
# If at least one is missing false is returned, otherwise true is returned.
#
Expand Down
7 changes: 6 additions & 1 deletion comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def del_comment(thread_id,comment_id)
self.edit(thread_id,comment_id,{"del" => 1})
end

def render_comments(thread_id,root=-1,&block)
def fetch_thread(thread_id)
byparent = {}
@r.hgetall(thread_key(thread_id)).each{|id,comment|
next if id == "nextid"
Expand All @@ -93,6 +93,11 @@ def render_comments(thread_id,root=-1,&block)
byparent[parent_id] = [] if !byparent.has_key?(parent_id)
byparent[parent_id] << c
}
byparent
end

def render_comments(thread_id,root=-1,&block)
byparent = fetch_thread(thread_id)
render_comments_rec(byparent,root,0,block) if byparent[-1]
end

Expand Down

0 comments on commit e739fa7

Please sign in to comment.