Commit 1e31c03a by Rocky Duan

user recent active threads

parent d62ac7eb
get "#{APIPREFIX}/threads" do # retrieve threads by course
handle_threads_query(CommentThread.where(course_id: params["course_id"]))
end
get "#{APIPREFIX}/threads/:thread_id" do |thread_id|
CommentThread.find(thread_id).to_hash(recursive: bool_recursive).to_json
end
......
......@@ -5,37 +5,8 @@ end
get "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
sort_key_mapper = {
"date" => :created_at,
"activity" => :last_activity_at,
"votes" => :"votes.point",
"comments" => :comment_count,
}
sort_order_mapper = {
"desc" => :desc,
"asc" => :asc,
}
handle_threads_query(commentable.comment_threads)
sort_key = sort_key_mapper[params["sort_key"]]
sort_order = sort_order_mapper[params["sort_order"]]
sort_keyword_valid = (!params["sort_key"] && !params["sort_order"] || sort_key && sort_order)
if not sort_keyword_valid
{}.to_json
else
page = (params["page"] || DEFAULT_PAGE).to_i
per_page = (params["per_page"] || DEFAULT_PER_PAGE).to_i
comment_threads = commentable.comment_threads
comment_threads = comment_threads.order_by("#{sort_key} #{sort_order}") if sort_key && sort_order
num_pages = [1, (comment_threads.count / per_page.to_f).ceil].max
page = [num_pages, [1, page].max].min
paged_comment_threads = comment_threads.page(page).per(per_page)
{
collection: paged_comment_threads.map{|t| t.to_hash(recursive: bool_recursive)},
num_pages: num_pages,
page: page,
}.to_json
end
end
post "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
......
......@@ -14,6 +14,35 @@ get "#{APIPREFIX}/users/:user_id" do |user_id|
user.to_hash(complete: bool_complete, course_id: params["course_id"]).to_json
end
get "#{APIPREFIX}/users/:user_id/active_threads" do |user_id|
return {}.to_json if not params["course_id"]
get_thread_id = lambda {|c| c._type == "Comment" ? c.comment_thread_id : c.id}
get_thread = lambda {|thread_id| CommentThread.find(thread_id)}
page = (params["page"] || DEFAULT_PAGE).to_i
per_page = (params["per_page"] || DEFAULT_PER_PAGE).to_i
active_contents = Content.where(author_id: user_id, anonymous: false, course_id: params["course_id"])
.order_by(updated_at: :desc)
num_pages = [1, (active_contents.count / per_page.to_f).ceil].max
page = [num_pages, [1, page].max].min
paged_active_contents = active_contents.page(page).per(per_page)
paged_active_threads = paged_active_contents.map(&get_thread_id)
.uniq.map(&get_thread)
collection = paged_active_threads.map{|t| t.to_hash(recursive: true)}
collection = author_contents_only(collection, user_id)
{
collection: collection,
num_pages: num_pages,
page: page,
}.to_json
end
put "#{APIPREFIX}/users/:user_id" do |user_id|
user = User.where(external_id: user_id).first
if not user
......
......@@ -63,4 +63,53 @@ helpers do
value_to_boolean params["anonymous"]
end
def handle_paged_threads_query(paged_comment_threads)
end
def handle_threads_query(comment_threads)
sort_key_mapper = {
"date" => :created_at,
"activity" => :last_activity_at,
"votes" => :"votes.point",
"comments" => :comment_count,
}
sort_order_mapper = {
"desc" => :desc,
"asc" => :asc,
}
sort_key = sort_key_mapper[params["sort_key"]]
sort_order = sort_order_mapper[params["sort_order"]]
sort_keyword_valid = (!params["sort_key"] && !params["sort_order"] || sort_key && sort_order)
if not sort_keyword_valid
{}.to_json
else
page = (params["page"] || DEFAULT_PAGE).to_i
per_page = (params["per_page"] || DEFAULT_PER_PAGE).to_i
comment_threads = comment_threads.order_by("#{sort_key} #{sort_order}") if sort_key && sort_order
num_pages = [1, (comment_threads.count / per_page.to_f).ceil].max
page = [num_pages, [1, page].max].min
paged_comment_threads = comment_threads.page(page).per(per_page)
{
collection: paged_comment_threads.map{|t| t.to_hash(recursive: bool_recursive)},
num_pages: num_pages,
page: page,
}.to_json
end
end
def author_contents_only(contents, author_id)
contents.map do |content|
content['children'] = author_contents_only(content['children'], author_id)
if content['children'].length > 0 or \
(content['user_id'] == author_id and not content['anonymous'])
content
else
nil
end
end.compact
end
end
......@@ -142,9 +142,8 @@ class CommentThread < Content
if params[:recursive]
doc = doc.merge("children" => root_comments.map{|c| c.to_hash(recursive: true)})
else
doc = doc.merge("comments_count" => comments.count)
end
doc = doc.merge("comments_count" => comments.count)
doc
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment