From 1ac14a142af39a13f37bcc76cec6a4f9bba82701 Mon Sep 17 00:00:00 2001 From: AhtishamShahid Date: Tue, 15 Aug 2023 13:31:22 +0500 Subject: [PATCH 1/5] feat: added api to get subscriptions for thread --- api/notifications_and_subscriptions.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/api/notifications_and_subscriptions.rb b/api/notifications_and_subscriptions.rb index 41787bf83f..bd763feb0b 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,26 @@ delete "#{APIPREFIX}/users/:user_id/subscriptions" do |user_id| user.unsubscribe(source).to_hash.to_json end + +get "#{APIPREFIX}/subscriptions/:thread_id" 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[:subscriber_id] = params[:subscriber_id] if params[:subscriber_id] + query[:source_id] = thread_id + query[:source_type] = params[:source_type] if params[:source_type] + + 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 From 1dd23fadad283c0c5a3ab72f4e881bbe61390dfb Mon Sep 17 00:00:00 2001 From: AhtishamShahid Date: Thu, 17 Aug 2023 16:17:52 +0500 Subject: [PATCH 2/5] feat: added unit test for subscriptions api --- .../notifications_and_subscriptions_spec.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/spec/api/notifications_and_subscriptions_spec.rb b/spec/api/notifications_and_subscriptions_spec.rb index 827bbe1e13..1487910cc6 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,23 @@ def thread_result(params) expect(thread.subscribers.length).to eq(0) end end + describe "GET /api/v1/subscriptions/:thread_id" do + it "Get subscribers of thread" do + thread = @threads["t2"] + subscriber.subscribe(thread) + expect(thread.subscribers.length).to eq(1) + + get "/api/v1/subscriptions/#{thread.id}", { '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 end end From aa6c81474ebae9e9dde50df737e48886273ba921 Mon Sep 17 00:00:00 2001 From: AhtishamShahid Date: Thu, 31 Aug 2023 16:24:30 +0500 Subject: [PATCH 3/5] feat: added unit test for pagination --- .../notifications_and_subscriptions_spec.rb | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/spec/api/notifications_and_subscriptions_spec.rb b/spec/api/notifications_and_subscriptions_spec.rb index 1487910cc6..534d974431 100644 --- a/spec/api/notifications_and_subscriptions_spec.rb +++ b/spec/api/notifications_and_subscriptions_spec.rb @@ -151,5 +151,36 @@ def thread_result(params) end end + describe "GET /api/v1/subscriptions/:thread_id" 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/subscriptions/#{thread.id}", { '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/subscriptions/#{thread.id}", { '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 From 76f1c08e29044e79db6f70902ee03b32218a831a Mon Sep 17 00:00:00 2001 From: AhtishamShahid Date: Thu, 7 Sep 2023 14:30:21 +0500 Subject: [PATCH 4/5] fix: udpated url pattren for subscriptions api --- api/notifications_and_subscriptions.rb | 3 +-- spec/api/notifications_and_subscriptions_spec.rb | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/api/notifications_and_subscriptions.rb b/api/notifications_and_subscriptions.rb index bd763feb0b..4dd3daa572 100644 --- a/api/notifications_and_subscriptions.rb +++ b/api/notifications_and_subscriptions.rb @@ -29,13 +29,12 @@ user.unsubscribe(source).to_hash.to_json end -get "#{APIPREFIX}/subscriptions/:thread_id" do |thread_id| +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[:subscriber_id] = params[:subscriber_id] if params[:subscriber_id] query[:source_id] = thread_id query[:source_type] = params[:source_type] if params[:source_type] diff --git a/spec/api/notifications_and_subscriptions_spec.rb b/spec/api/notifications_and_subscriptions_spec.rb index 534d974431..11c6a8f71f 100644 --- a/spec/api/notifications_and_subscriptions_spec.rb +++ b/spec/api/notifications_and_subscriptions_spec.rb @@ -133,13 +133,13 @@ def thread_result(params) expect(thread.subscribers.length).to eq(0) end end - describe "GET /api/v1/subscriptions/:thread_id" do + 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/subscriptions/#{thread.id}", { 'page': 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) @@ -151,7 +151,7 @@ def thread_result(params) end end - describe "GET /api/v1/subscriptions/:thread_id" do + describe "GET /api/v1/threads/:thread_id/subscriptions" do it "Get subscribers of thread with pagination" do thread = @threads["t2"] @@ -164,7 +164,7 @@ def thread_result(params) expect(thread.subscribers.length).to eq(6) - get "/api/v1/subscriptions/#{thread.id}", { 'page': 1, 'per_page': 2 } + 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) @@ -172,7 +172,7 @@ def thread_result(params) expect(response['page']).to eq(1) expect(response['subscriptions_count']).to eq(6) - get "/api/v1/subscriptions/#{thread.id}", { 'page': 2, 'per_page': 2 } + 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) From bb4dfdfa70fe589ae8d329c7d508197d8059110e Mon Sep 17 00:00:00 2001 From: AhtishamShahid Date: Mon, 11 Sep 2023 14:54:19 +0500 Subject: [PATCH 5/5] fix: made source_type fix --- api/notifications_and_subscriptions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/notifications_and_subscriptions.rb b/api/notifications_and_subscriptions.rb index 4dd3daa572..b5e3b33188 100644 --- a/api/notifications_and_subscriptions.rb +++ b/api/notifications_and_subscriptions.rb @@ -36,7 +36,7 @@ # Build a query hash based on the query parameters query = {} query[:source_id] = thread_id - query[:source_type] = params[:source_type] if params[:source_type] + query[:source_type] = 'CommentThread' subscriptions = Subscription.where(query).paginate(:page => page, :per_page => per_page) subscriptions_count = subscriptions.total_entries