Skip to content

Commit

Permalink
Merge pull request #114 from edx/gprice/test-group-id
Browse files Browse the repository at this point in the history
Allow filtering by group_id in more endpoints
  • Loading branch information
Greg Price committed Sep 2, 2014
2 parents fd45541 + 18620ca commit d26bc6b
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 67 deletions.
8 changes: 1 addition & 7 deletions api/comment_threads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
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"]),
Expand Down
7 changes: 1 addition & 6 deletions api/commentables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@

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"]),
Expand Down
1 change: 1 addition & 0 deletions api/notifications_and_subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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"]),
Expand Down
33 changes: 1 addition & 32 deletions api/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
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"]),
Expand All @@ -89,35 +90,3 @@
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
23 changes: 13 additions & 10 deletions api/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,22 @@
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
Expand Down
2 changes: 0 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
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"
Expand Down
22 changes: 20 additions & 2 deletions lib/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,26 @@ def handle_paged_threads_query(paged_comment_threads)

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?
Expand Down
28 changes: 20 additions & 8 deletions spec/api/commentable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,32 @@
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

Expand Down
12 changes: 12 additions & 0 deletions spec/api/notifications_and_subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ def thread_result(params)
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
Expand Down
15 changes: 15 additions & 0 deletions spec/api/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ def thread_result(user_id, params)
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
Expand Down

0 comments on commit d26bc6b

Please sign in to comment.