diff --git a/api/notifications_and_subscriptions.rb b/api/notifications_and_subscriptions.rb index 41787bf83f..b5e3b33188 100644 --- a/api/notifications_and_subscriptions.rb +++ b/api/notifications_and_subscriptions.rb @@ -4,7 +4,7 @@ get "#{APIPREFIX}/users/:user_id/subscribed_threads" do |user_id| handle_threads_query( - user.subscribed_threads.where({"course_id" => params[:course_id]}), + user.subscribed_threads.where({ "course_id" => params[:course_id] }), params["user_id"], params["course_id"], get_group_ids_from_params(params), @@ -28,3 +28,25 @@ delete "#{APIPREFIX}/users/:user_id/subscriptions" do |user_id| user.unsubscribe(source).to_hash.to_json end + +get "#{APIPREFIX}/threads/:thread_id/subscriptions" do |thread_id| + page = (params['page'] || DEFAULT_PAGE).to_i + per_page = (params['per_page'] || DEFAULT_PER_PAGE).to_i + + # Build a query hash based on the query parameters + query = {} + query[:source_id] = thread_id + query[:source_type] = 'CommentThread' + + subscriptions = Subscription.where(query).paginate(:page => page, :per_page => per_page) + subscriptions_count = subscriptions.total_entries + + content_type :json + + { + collection: subscriptions.map(&:to_hash), + num_pages: [1, (subscriptions_count / per_page.to_f).ceil].max, + page: page, + subscriptions_count: subscriptions_count + }.to_json +end diff --git a/spec/api/notifications_and_subscriptions_spec.rb b/spec/api/notifications_and_subscriptions_spec.rb index 827bbe1e13..11c6a8f71f 100644 --- a/spec/api/notifications_and_subscriptions_spec.rb +++ b/spec/api/notifications_and_subscriptions_spec.rb @@ -38,7 +38,7 @@ def thread_result(params) end it "returns an empty result when no posts were flagged" do rs = thread_result course_id: DFLT_COURSE_ID, flagged: true - expect(rs.length).to eq(0) + expect(rs.length).to eq(0) end end it "filters by group_id" do @@ -133,6 +133,54 @@ def thread_result(params) expect(thread.subscribers.length).to eq(0) end end + describe "GET /api/v1/threads/:thread_id/subscriptions" do + it "Get subscribers of thread" do + thread = @threads["t2"] + subscriber.subscribe(thread) + expect(thread.subscribers.length).to eq(1) + + get "/api/v1/threads/#{thread.id}/subscriptions", { 'page': 1 } + expect(last_response).to be_ok + response = parse(last_response.body) + expect(response['collection'].length).to eq(1) + expect(response['num_pages']).to eq(1) + expect(response['page']).to eq(1) + expect(response['subscriptions_count']).to eq(1) + puts last_response.body + + end + end + + describe "GET /api/v1/threads/:thread_id/subscriptions" do + it "Get subscribers of thread with pagination" do + thread = @threads["t2"] + + subscriber.subscribe(thread) + create_test_user(43).subscribe(thread) + create_test_user(44).subscribe(thread) + create_test_user(45).subscribe(thread) + create_test_user(46).subscribe(thread) + create_test_user(47).subscribe(thread) + + expect(thread.subscribers.length).to eq(6) + + get "/api/v1/threads/#{thread.id}/subscriptions", { 'page': 1, 'per_page': 2 } + expect(last_response).to be_ok + response = parse(last_response.body) + expect(response['collection'].length).to eq(2) + expect(response['num_pages']).to eq(3) + expect(response['page']).to eq(1) + expect(response['subscriptions_count']).to eq(6) + + get "/api/v1/threads/#{thread.id}/subscriptions", { 'page': 2, 'per_page': 2 } + expect(last_response).to be_ok + response = parse(last_response.body) + expect(response['collection'].length).to eq(2) + expect(response['num_pages']).to eq(3) + expect(response['page']).to eq(2) + expect(response['subscriptions_count']).to eq(6) + end + end end end