Commit 88b0cd88 by Kevin Chugh

flagged search started

parent 6a27f7aa
...@@ -107,62 +107,68 @@ helpers do ...@@ -107,62 +107,68 @@ helpers do
end end
def handle_threads_query(comment_threads) def handle_threads_query(comment_threads)
if params[:course_id] if params[:flagged]
comment_threads = comment_threads.where(:course_id=>params[:course_id]) #get flagged threads and threads containing flagged responses
end comment_threads = comment_threads.where(:course_id=>params[:course_id]).where(:abuse_flaggers)
if CommentService.config[:cache_enabled]
query_params = params.slice(*%w[course_id commentable_id sort_key sort_order page per_page user_id]) else
memcached_key = "threads_query_#{query_params.hash}" if params[:course_id]
cached_results = Sinatra::Application.cache.get(memcached_key) comment_threads = comment_threads.where(:course_id=>params[:course_id])
if cached_results end
return { if CommentService.config[:cache_enabled]
collection: cached_results[:collection_ids].map{|id| CommentThread.find(id).to_hash(recursive: bool_recursive, user_id: params["user_id"])}, query_params = params.slice(*%w[course_id commentable_id sort_key sort_order page per_page user_id])
num_pages: cached_results[:num_pages], memcached_key = "threads_query_#{query_params.hash}"
page: cached_results[:page], cached_results = Sinatra::Application.cache.get(memcached_key)
}.to_json if cached_results
return {
collection: cached_results[:collection_ids].map{|id| CommentThread.find(id).to_hash(recursive: bool_recursive, user_id: params["user_id"])},
num_pages: cached_results[:num_pages],
page: cached_results[:page],
}.to_json
end
end end
end
sort_key_mapper = { sort_key_mapper = {
"date" => :created_at, "date" => :created_at,
"activity" => :last_activity_at, "activity" => :last_activity_at,
"votes" => :"votes.point", "votes" => :"votes.point",
"comments" => :comment_count, "comments" => :comment_count,
} }
sort_order_mapper = { sort_order_mapper = {
"desc" => :desc, "desc" => :desc,
"asc" => :asc, "asc" => :asc,
} }
sort_key = sort_key_mapper[params["sort_key"]] sort_key = sort_key_mapper[params["sort_key"]]
sort_order = sort_order_mapper[params["sort_order"]] sort_order = sort_order_mapper[params["sort_order"]]
sort_keyword_valid = (!params["sort_key"] && !params["sort_order"] || sort_key && sort_order) sort_keyword_valid = (!params["sort_key"] && !params["sort_order"] || sort_key && sort_order)
if not sort_keyword_valid if not sort_keyword_valid
{}.to_json {}.to_json
else else
page = (params["page"] || DEFAULT_PAGE).to_i page = (params["page"] || DEFAULT_PAGE).to_i
per_page = (params["per_page"] || DEFAULT_PER_PAGE).to_i per_page = (params["per_page"] || DEFAULT_PER_PAGE).to_i
#KChugh turns out we don't need to go through all the extra work on the back end because the client is resorting anyway #KChugh turns out we don't need to go through all the extra work on the back end because the client is resorting anyway
#KChugh boy was I wrong, we need to sort for pagination #KChugh boy was I wrong, we need to sort for pagination
comment_threads = comment_threads.order_by("pinned DESC,#{sort_key} #{sort_order}") if sort_key && sort_order comment_threads = comment_threads.order_by("pinned DESC,#{sort_key} #{sort_order}") if sort_key && sort_order
num_pages = [1, (comment_threads.count / per_page.to_f).ceil].max num_pages = [1, (comment_threads.count / per_page.to_f).ceil].max
page = [num_pages, [1, page].max].min page = [num_pages, [1, page].max].min
paged_comment_threads = comment_threads.page(page).per(per_page) paged_comment_threads = comment_threads.page(page).per(per_page)
if CommentService.config[:cache_enabled] if CommentService.config[:cache_enabled]
cached_results = { cached_results = {
collection_ids: paged_comment_threads.map(&:id), collection_ids: paged_comment_threads.map(&:id),
num_pages: num_pages,
page: page,
}
Sinatra::Application.cache.set(memcached_key, cached_results, CommentService.config[:cache_timeout][:threads_query].to_i)
end
{
collection: paged_comment_threads.map{|t| t.to_hash(recursive: bool_recursive, user_id: params["user_id"])},
num_pages: num_pages, num_pages: num_pages,
page: page, page: page,
} }.to_json
Sinatra::Application.cache.set(memcached_key, cached_results, CommentService.config[:cache_timeout][:threads_query].to_i)
end end
{
collection: paged_comment_threads.map{|t| t.to_hash(recursive: bool_recursive, user_id: params["user_id"])},
num_pages: num_pages,
page: page,
}.to_json
end end
end end
...@@ -170,8 +176,8 @@ helpers do ...@@ -170,8 +176,8 @@ helpers do
contents.map do |content| contents.map do |content|
content['children'] = author_contents_only(content['children'], author_id) content['children'] = author_contents_only(content['children'], author_id)
if content['children'].length > 0 or \ if content['children'].length > 0 or \
(content['user_id'] == author_id and not content['anonymous'] and not content['anonymous_to_peers']) (content['user_id'] == author_id and not content['anonymous'] and not content['anonymous_to_peers'])
content content
else else
nil nil
end 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