Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added API to get subscribers of a thread. #415

Merged
merged 6 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to return the whole collection, or only the ids?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subscription object has only 4 attributes, so I guess we can return all without taking any performance hit.

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