Commit e5b50f4e by Greg Price

Allow more endpoints to use commentable_ids param

The commentable_ids param is a comma-delimited list of ids indicating
that only threads belonging to one of the specified commentables should
be returned. It can now be used by any endpoint that delegates to
handle_threads_query (most importantly "#{APIPREFIX}/threads"), and
the commentable_id field is now indexed for efficient filtering. The
motivation for this change is to allow clients that are filtering by
commentable_ids to avoid using the /search/threads endpoint, which does
not do sorting and pagination correctly.
parent 31ef160d
...@@ -14,3 +14,5 @@ Christina Roberts <christina@edx.org> ...@@ -14,3 +14,5 @@ Christina Roberts <christina@edx.org>
Calen Pennington <calen.pennington@gmail.com> Calen Pennington <calen.pennington@gmail.com>
Ed Zarecor <ed@edx.org> Ed Zarecor <ed@edx.org>
Jay Zoldak <jzoldak@edx.org> Jay Zoldak <jzoldak@edx.org>
Jim Abramson <jsa@edx.org>
Greg Price <gprice@edx.org>
...@@ -5,6 +5,9 @@ These are notable changes in cs_comments_service. This is a rolling list of cha ...@@ -5,6 +5,9 @@ These are notable changes in cs_comments_service. This is a rolling list of cha
in roughly chronological order, most recent first. Add your entries at or near in roughly chronological order, most recent first. Add your entries at or near
the top. Include a label indicating the component affected. the top. Include a label indicating the component affected.
**api:** Add the ability to filter by commentable id to more endpoints
(in particular, /threads).
**models:** added a new sorting key and index to `Comment` documents, removing the need **models:** added a new sorting key and index to `Comment` documents, removing the need
for certain hierarchical db queries. Also added a copy of the author's username for certain hierarchical db queries. Also added a copy of the author's username
to `Comment` and `CommentThread` models, to reduce the number db queries. to `Comment` and `CommentThread` models, to reduce the number db queries.
......
...@@ -138,6 +138,10 @@ helpers do ...@@ -138,6 +138,10 @@ helpers do
end end
end end
if params[:commentable_ids]
comment_threads = comment_threads.in(commentable_id: params[:commentable_ids].split(","))
end
sort_key_mapper = { sort_key_mapper = {
"date" => :created_at, "date" => :created_at,
"activity" => :last_activity_at, "activity" => :last_activity_at,
......
...@@ -14,6 +14,7 @@ class Content ...@@ -14,6 +14,7 @@ class Content
index({comment_thread_id: 1, sk: 1}, {sparse: true}) index({comment_thread_id: 1, sk: 1}, {sparse: true})
index({comment_thread_id: 1, endorsed: 1}, {sparse: true}) index({comment_thread_id: 1, endorsed: 1}, {sparse: true})
index({commentable_id: 1}, {sparse: true, background: true})
before_save :set_username before_save :set_username
def set_username def set_username
......
db.contents.ensureIndex({commentable_id: 1}, {sparse: true, background: true})
db.contents.dropIndex({commentable_id: 1})
...@@ -26,6 +26,24 @@ describe "app" do ...@@ -26,6 +26,24 @@ describe "app" do
res["course_id"].should == "abc" res["course_id"].should == "abc"
} }
end end
it "returns only threads where course id and commentable id match" do
@threads["t1"].course_id = "course1"
@threads["t1"].commentable_id = "commentable1"
@threads["t1"].save!
@threads["t2"].course_id = "course1"
@threads["t2"].commentable_id = "commentable2"
@threads["t2"].save!
@threads["t3"].course_id = "course1"
@threads["t3"].commentable_id = "commentable3"
@threads["t3"].save!
@threads["t4"].course_id = "course2"
@threads["t4"].commentable_id = "commentable1"
@threads["t4"].save!
rs = thread_result course_id: "course1", commentable_ids: "commentable1,commentable3"
rs.length.should == 2
check_thread_result(nil, @threads["t3"], rs[0])
check_thread_result(nil, @threads["t1"], rs[1])
end
it "returns only threads where course id and group id match" do it "returns only threads where course id and group id match" do
@threads["t1"].course_id = "omg" @threads["t1"].course_id = "omg"
@threads["t1"].group_id = 100 @threads["t1"].group_id = 100
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment