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 3 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
25 changes: 24 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,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|
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better to follow the pattern above and put this under threads.
i.e.

Suggested change
get "#{APIPREFIX}/subscriptions/:thread_id" do |thread_id|
get "#{APIPREFIX}/threads/:thread_id/subscriptions" do |thread_id|

That way it is not ambiguous what we are managing. As mentioned elsewhere, if in the future we need subscriptions for a different resource, it will be hard to disambiguate between the ids.

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]
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we only need the source_id, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added subscriber_id and source type to provide filtering of all available attributes. They are not used yet

Copy link
Contributor

Choose a reason for hiding this comment

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

From what I understand, here the source type should always be that of a thread, otherwise this will be a general purpose API for subscriptions, but this seems to be focussed on threads.
If that is the case, you should only have a one subscription between one use and thread, so specifying a subscriber_id might not make sense.

AhtishamShahid marked this conversation as resolved.
Show resolved Hide resolved

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/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

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