Commit 18620cae by Greg Price

Allow filtering by group_id in more endpoints

All endpoints that return lists of threads (except the endpoint used by
the notifier component) now allow filtering by group_id and are tested
accordingly.
parent bbd83e3c
...@@ -4,18 +4,12 @@ get "#{APIPREFIX}/threads" do # retrieve threads by course ...@@ -4,18 +4,12 @@ get "#{APIPREFIX}/threads" do # retrieve threads by course
if params[:commentable_ids] if params[:commentable_ids]
threads = threads.in({"commentable_id" => params[:commentable_ids].split(",")}) threads = threads.in({"commentable_id" => params[:commentable_ids].split(",")})
end end
#if a group id is sent, then process the set of threads with that group id or with no group id
if params["group_id"]
threads = threads.any_of(
{"group_id" => params[:group_id].to_i},
{"group_id" => {"$exists" => false}},
)
end
handle_threads_query( handle_threads_query(
threads, threads,
params["user_id"], params["user_id"],
params["course_id"], params["course_id"],
params["group_id"],
value_to_boolean(params["flagged"]), value_to_boolean(params["flagged"]),
value_to_boolean(params["unread"]), value_to_boolean(params["unread"]),
value_to_boolean(params["unanswered"]), value_to_boolean(params["unanswered"]),
......
...@@ -5,17 +5,12 @@ end ...@@ -5,17 +5,12 @@ end
get "#{APIPREFIX}/:commentable_id/threads" do |commentable_id| get "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
threads = Content.where({"_type" => "CommentThread", "commentable_id" => commentable_id}) threads = Content.where({"_type" => "CommentThread", "commentable_id" => commentable_id})
if params["group_id"]
threads = threads.any_of(
{"group_id" => params[:group_id].to_i},
{"group_id" => {"$exists" => false}},
)
end
handle_threads_query( handle_threads_query(
threads, threads,
params["user_id"], params["user_id"],
params["course_id"], params["course_id"],
params["group_id"],
value_to_boolean(params["flagged"]), value_to_boolean(params["flagged"]),
value_to_boolean(params["unread"]), value_to_boolean(params["unread"]),
value_to_boolean(params["unanswered"]), value_to_boolean(params["unanswered"]),
......
...@@ -7,6 +7,7 @@ get "#{APIPREFIX}/users/:user_id/subscribed_threads" do |user_id| ...@@ -7,6 +7,7 @@ get "#{APIPREFIX}/users/:user_id/subscribed_threads" do |user_id|
user.subscribed_threads.where({"course_id" => params[:course_id]}), user.subscribed_threads.where({"course_id" => params[:course_id]}),
params["user_id"], params["user_id"],
params["course_id"], params["course_id"],
params["group_id"],
value_to_boolean(params["flagged"]), value_to_boolean(params["flagged"]),
value_to_boolean(params["unread"]), value_to_boolean(params["unread"]),
value_to_boolean(params["unanswered"]), value_to_boolean(params["unanswered"]),
......
...@@ -71,6 +71,7 @@ get "#{APIPREFIX}/search/threads" do ...@@ -71,6 +71,7 @@ get "#{APIPREFIX}/search/threads" do
CommentThread.in({"_id" => thread_ids.to_a}), CommentThread.in({"_id" => thread_ids.to_a}),
local_params["user_id"], local_params["user_id"],
local_params["course_id"], local_params["course_id"],
local_params["group_id"],
value_to_boolean(local_params["flagged"]), value_to_boolean(local_params["flagged"]),
value_to_boolean(local_params["unread"]), value_to_boolean(local_params["unread"]),
value_to_boolean(local_params["unanswered"]), value_to_boolean(local_params["unanswered"]),
......
...@@ -37,19 +37,22 @@ get "#{APIPREFIX}/users/:user_id/active_threads" do |user_id| ...@@ -37,19 +37,22 @@ get "#{APIPREFIX}/users/:user_id/active_threads" do |user_id|
thread_ids thread_ids
end end
num_pages = [1, (active_thread_ids.count / per_page.to_f).ceil].max threads = CommentThread.in({"_id" => active_thread_ids})
page = [num_pages, [1, page].max].min
paged_thread_ids = active_thread_ids[(page - 1) * per_page, per_page]
# Find all the threads by id, and then put them in the order found earlier. if params["group_id"]
# Necessary because CommentThread.find does return results in the same threads = threads.any_of(
# order as the provided ids. {"group_id" => params["group_id"].to_i},
paged_active_threads = CommentThread.find(paged_thread_ids).sort_by do |t| {"group_id" => {"$exists" => false}}
paged_thread_ids.index(t.id) )
end end
presenter = ThreadListPresenter.new(paged_active_threads.to_a, user, params[:course_id]) num_pages = [1, (threads.count / per_page.to_f).ceil].max
page = [num_pages, [1, page].max].min
sorted_threads = threads.sort_by {|t| active_thread_ids.index(t.id)}
paged_threads = sorted_threads[(page - 1) * per_page, per_page]
presenter = ThreadListPresenter.new(paged_threads, user, params[:course_id])
collection = presenter.to_hash collection = presenter.to_hash
json_output = nil json_output = nil
......
...@@ -116,7 +116,25 @@ helpers do ...@@ -116,7 +116,25 @@ helpers do
end end
def handle_threads_query(comment_threads, user_id, course_id, filter_flagged, filter_unread, filter_unanswered, sort_key, sort_order, page, per_page) def handle_threads_query(
comment_threads,
user_id,
course_id,
group_id,
filter_flagged,
filter_unread,
filter_unanswered,
sort_key,
sort_order,
page,
per_page
)
if group_id
comment_threads = comment_threads.any_of(
{"group_id" => group_id.to_i},
{"group_id" => {"$exists" => false}}
)
end
if filter_flagged if filter_flagged
self.class.trace_execution_scoped(['Custom/handle_threads_query/find_flagged']) do self.class.trace_execution_scoped(['Custom/handle_threads_query/find_flagged']) do
......
...@@ -21,20 +21,32 @@ describe "app" do ...@@ -21,20 +21,32 @@ describe "app" do
end end
end end
describe "GET /api/v1/:commentable_id/threads" do describe "GET /api/v1/:commentable_id/threads" do
it "get all comment threads associated with a commentable object" do def thread_result(commentable_id, params={})
get "/api/v1/question_1/threads" get "/api/v1/#{commentable_id}/threads", params
last_response.should be_ok last_response.should be_ok
response = parse last_response.body parse(last_response.body)["collection"]
threads = response['collection'] end
it "get all comment threads associated with a commentable object" do
threads = thread_result "question_1"
threads.length.should == 2 threads.length.should == 2
threads.index{|c| c["body"] == "can anyone help me?"}.should_not be_nil threads.index{|c| c["body"] == "can anyone help me?"}.should_not be_nil
threads.index{|c| c["body"] == "it is unsolvable"}.should_not be_nil threads.index{|c| c["body"] == "it is unsolvable"}.should_not be_nil
end end
it "filters by group_id" do
group_thread = Commentable.find("question_1").comment_threads.first
threads = thread_result "question_1", group_id: 42
threads.length.should == 2
group_thread.group_id = 43
group_thread.save!
threads = thread_result "question_1", group_id: 42
threads.length.should == 1
group_thread.group_id = 42
group_thread.save!
threads = thread_result "question_1", group_id: 42
threads.length.should == 2
end
it "returns an empty array when the commentable object does not exist (no threads)" do it "returns an empty array when the commentable object does not exist (no threads)" do
get "/api/v1/does_not_exist/threads" threads = thread_result "does_not_exist"
last_response.should be_ok
response = parse last_response.body
threads = response['collection']
threads.length.should == 0 threads.length.should == 0
end end
......
...@@ -41,6 +41,18 @@ describe "app" do ...@@ -41,6 +41,18 @@ describe "app" do
rs.length.should == 0 rs.length.should == 0
end end
end end
it "filters by group_id" do
rs = thread_result course_id: DFLT_COURSE_ID, group_id: 42
rs.length.should == 5
@threads["t3"].group_id = 43
@threads["t3"].save!
rs = thread_result course_id: DFLT_COURSE_ID, group_id: 42
rs.length.should == 4
@threads["t3"].group_id = 42
@threads["t3"].save!
rs = thread_result course_id: DFLT_COURSE_ID, group_id: 42
rs.length.should == 5
end
it "filters unread posts" do it "filters unread posts" do
rs = thread_result course_id: DFLT_COURSE_ID rs = thread_result course_id: DFLT_COURSE_ID
rs.length.should == 5 rs.length.should == 5
......
...@@ -85,6 +85,21 @@ describe "app" do ...@@ -85,6 +85,21 @@ describe "app" do
check_thread_result_json(@users["u100"], @threads["t0"], rs[1]) check_thread_result_json(@users["u100"], @threads["t0"], rs[1])
end end
it "filters by group_id" do
@threads["t1"].author = @users["u100"]
@threads["t1"].save!
rs = thread_result 100, course_id: DFLT_COURSE_ID, group_id: 42
rs.length.should == 2
@threads["t1"].group_id = 43
@threads["t1"].save!
rs = thread_result 100, course_id: DFLT_COURSE_ID, group_id: 42
rs.length.should == 1
@threads["t1"].group_id = 42
@threads["t1"].save!
rs = thread_result 100, course_id: DFLT_COURSE_ID, group_id: 42
rs.length.should == 2
end
it "does not return threads in which the user has only participated anonymously" do it "does not return threads in which the user has only participated anonymously" do
@comments["t3 c4"].author = @users["u100"] @comments["t3 c4"].author = @users["u100"]
@comments["t3 c4"].anonymous_to_peers = true @comments["t3 c4"].anonymous_to_peers = true
......
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