Commit 509c96eb by E. Kolpakov Committed by Saqib

Filter by thread ids

parent d9400ce5
...@@ -83,7 +83,7 @@ post "#{APIPREFIX}/users/:user_id/read" do |user_id| ...@@ -83,7 +83,7 @@ post "#{APIPREFIX}/users/:user_id/read" do |user_id|
user.reload.to_hash.to_json user.reload.to_hash.to_json
end end
get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id| def _user_social_stats(user_id, params)
begin begin
return {}.to_json if not params["course_id"] return {}.to_json if not params["course_id"]
...@@ -92,6 +92,7 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id| ...@@ -92,6 +92,7 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id|
thread_type = params["thread_type"] thread_type = params["thread_type"]
course_id = params["course_id"] course_id = params["course_id"]
thread_ids_filter = params["thread_ids"].split(",") if params["thread_ids"]
user_stats = {} user_stats = {}
thread_ids = {} thread_ids = {}
...@@ -126,7 +127,7 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id| ...@@ -126,7 +127,7 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id|
# as we don't need it and we shouldn't push all that data over the wire # as we don't need it and we shouldn't push all that data over the wire
content = Content.where(content_selector).without(:body) content = Content.where(content_selector).without(:body)
if thread_type if thread_type || thread_ids_filter
thread_selector = {course_id: course_id, anonymous: false, anonymous_to_peers: false} thread_selector = {course_id: course_id, anonymous: false, anonymous_to_peers: false}
if end_date if end_date
thread_selector[:created_at.lte] = end_date thread_selector[:created_at.lte] = end_date
...@@ -134,6 +135,9 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id| ...@@ -134,6 +135,9 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id|
if thread_type if thread_type
thread_selector["thread_type"] = thread_type thread_selector["thread_type"] = thread_type
end end
if thread_ids_filter
thread_selector[:commentable_id.in] = thread_ids_filter
end
target_threads = CommentThread.where(thread_selector).only(:_id).map(&:_id) target_threads = CommentThread.where(thread_selector).only(:_id).map(&:_id)
content = content.select do |c| content = content.select do |c|
...@@ -188,4 +192,12 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id| ...@@ -188,4 +192,12 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id|
end end
end end
get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id|
_user_social_stats(user_id, params)
end
post "#{APIPREFIX}/users/:user_id/social_stats" do |user_id|
_user_social_stats(user_id, params)
end
...@@ -412,7 +412,7 @@ describe "app" do ...@@ -412,7 +412,7 @@ describe "app" do
} }
end end
def make_request(user_id, course_id, end_date=nil, thread_type=nil) def make_request(user_id, course_id, end_date=nil, thread_type=nil, thread_ids=nil)
parameters = { :course_id => course_id} parameters = { :course_id => course_id}
if end_date if end_date
parameters['end_date'] = end_date parameters['end_date'] = end_date
...@@ -420,6 +420,9 @@ describe "app" do ...@@ -420,6 +420,9 @@ describe "app" do
if thread_type if thread_type
parameters['thread_type'] = thread_type parameters['thread_type'] = thread_type
end end
if thread_ids
parameters['thread_ids'] = thread_ids.join(",")
end
get "/api/v1/users/#{user_id}/social_stats", parameters get "/api/v1/users/#{user_id}/social_stats", parameters
...@@ -697,6 +700,21 @@ describe "app" do ...@@ -697,6 +700,21 @@ describe "app" do
@user2.id => make_social_stats(1,0,0,1,0,0,1,1,0), @user2.id => make_social_stats(1,0,0,1,0,0,1,1,0),
}) })
end end
it "filters by thread ids" do
check_social_stats(make_request('*', DFLT_COURSE_ID, nil, nil, ["Thread 1"]), {
@user1.id => make_social_stats(1,0,2,3,0,3,5,1,0),
@user2.id => make_social_stats(0,2,1,1,0,2,0,0,0),
})
check_social_stats(make_request('*', DFLT_COURSE_ID, nil, nil, ["Thread 2"]), {
@user1.id => make_social_stats(0,1,0,0,0,1,0,0,0),
@user2.id => make_social_stats(2,1,0,4,0,1,2,3,0),
})
check_social_stats(make_request('*', DFLT_COURSE_ID, nil, nil, ["Thread 1", "Thread 2"]), {
@user1.id => make_social_stats(1,1,2,3,0,4,5,1,0),
@user2.id => make_social_stats(2,3,1,5,0,3,2,3,0),
})
end
end end
end end
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