Skip to content

Commit

Permalink
Merge pull request #415 from openedx/ahtisham/INF-943
Browse files Browse the repository at this point in the history
feat: added API to get subscribers of a thread.
  • Loading branch information
AhtishamShahid authored Oct 2, 2023
2 parents 57a95ff + 5c447b0 commit 81e11c7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
24 changes: 23 additions & 1 deletion api/notifications_and_subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
50 changes: 49 additions & 1 deletion spec/api/notifications_and_subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 81e11c7

Please sign in to comment.