Commit d26bc6b7 by Greg Price

Merge pull request #114 from edx/gprice/test-group-id

Allow filtering by group_id in more endpoints
parents fd455413 18620cae
......@@ -4,18 +4,12 @@ get "#{APIPREFIX}/threads" do # retrieve threads by course
if params[:commentable_ids]
threads = threads.in({"commentable_id" => params[:commentable_ids].split(",")})
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(
threads,
params["user_id"],
params["course_id"],
params["group_id"],
value_to_boolean(params["flagged"]),
value_to_boolean(params["unread"]),
value_to_boolean(params["unanswered"]),
......
......@@ -5,17 +5,12 @@ end
get "#{APIPREFIX}/:commentable_id/threads" do |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(
threads,
params["user_id"],
params["course_id"],
params["group_id"],
value_to_boolean(params["flagged"]),
value_to_boolean(params["unread"]),
value_to_boolean(params["unanswered"]),
......
......@@ -7,6 +7,7 @@ get "#{APIPREFIX}/users/:user_id/subscribed_threads" do |user_id|
user.subscribed_threads.where({"course_id" => params[:course_id]}),
params["user_id"],
params["course_id"],
params["group_id"],
value_to_boolean(params["flagged"]),
value_to_boolean(params["unread"]),
value_to_boolean(params["unanswered"]),
......
......@@ -71,6 +71,7 @@ get "#{APIPREFIX}/search/threads" do
CommentThread.in({"_id" => thread_ids.to_a}),
local_params["user_id"],
local_params["course_id"],
local_params["group_id"],
value_to_boolean(local_params["flagged"]),
value_to_boolean(local_params["unread"]),
value_to_boolean(local_params["unanswered"]),
......@@ -89,35 +90,3 @@ get "#{APIPREFIX}/search/threads" do
result_obj.to_json
end
end
get "#{APIPREFIX}/search/threads/more_like_this" do
CommentThread.tire.search page: 1, per_page: 5, load: true do |search|
search.query do |query|
query.more_like_this params["text"], fields: ["title", "body"], min_doc_freq: 1, min_term_freq: 1
end
end.results.map(&:to_hash).to_json
end
get "#{APIPREFIX}/search/threads/recent_active" do
return [].to_json if not params["course_id"]
follower_id = params["follower_id"]
from_time = {
"today" => Date.today.to_time,
"this_week" => Date.today.to_time - 1.weeks,
"this_month" => Date.today.to_time - 1.months,
}[params["from_time"] || "this_week"]
query_params = {}
query_params["course_id"] = params["course_id"] if params["course_id"]
query_params["commentable_id"] = params["commentable_id"] if params["commentable_id"]
comment_threads = if follower_id
User.find(follower_id).subscribed_threads
else
CommentThread.all
end
comment_threads.where(query_params.merge(:last_activity_at => {:$gte => from_time})).order_by(:last_activity_at.desc).limit(5).to_a.map(&:to_hash).to_json
end
......@@ -37,19 +37,22 @@ get "#{APIPREFIX}/users/:user_id/active_threads" do |user_id|
thread_ids
end
num_pages = [1, (active_thread_ids.count / per_page.to_f).ceil].max
page = [num_pages, [1, page].max].min
paged_thread_ids = active_thread_ids[(page - 1) * per_page, per_page]
threads = CommentThread.in({"_id" => active_thread_ids})
# Find all the threads by id, and then put them in the order found earlier.
# Necessary because CommentThread.find does return results in the same
# order as the provided ids.
paged_active_threads = CommentThread.find(paged_thread_ids).sort_by do |t|
paged_thread_ids.index(t.id)
if params["group_id"]
threads = threads.any_of(
{"group_id" => params["group_id"].to_i},
{"group_id" => {"$exists" => false}}
)
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
json_output = nil
......
......@@ -5,8 +5,6 @@ require 'erb'
Bundler.setup
Bundler.require
require 'tire/queries/more_like_this'
env_index = ARGV.index("-e")
env_arg = ARGV[env_index + 1] if env_index
environment = env_arg || ENV["SINATRA_ENV"] || "development"
......
......@@ -116,8 +116,26 @@ helpers do
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
self.class.trace_execution_scoped(['Custom/handle_threads_query/find_flagged']) do
# TODO replace with aggregate query?
......
......@@ -21,20 +21,32 @@ describe "app" do
end
end
describe "GET /api/v1/:commentable_id/threads" do
it "get all comment threads associated with a commentable object" do
get "/api/v1/question_1/threads"
def thread_result(commentable_id, params={})
get "/api/v1/#{commentable_id}/threads", params
last_response.should be_ok
response = parse last_response.body
threads = response['collection']
parse(last_response.body)["collection"]
end
it "get all comment threads associated with a commentable object" do
threads = thread_result "question_1"
threads.length.should == 2
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
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
get "/api/v1/does_not_exist/threads"
last_response.should be_ok
response = parse last_response.body
threads = response['collection']
threads = thread_result "does_not_exist"
threads.length.should == 0
end
......
......@@ -41,6 +41,18 @@ describe "app" do
rs.length.should == 0
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
rs = thread_result course_id: DFLT_COURSE_ID
rs.length.should == 5
......
......@@ -85,6 +85,21 @@ describe "app" do
check_thread_result_json(@users["u100"], @threads["t0"], rs[1])
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
@comments["t3 c4"].author = @users["u100"]
@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