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.
This commit is contained in:
Greg Price 2013-10-28 17:31:26 -04:00
Родитель 31ef160d5a
Коммит e5b50f4e7e
7 изменённых файлов: 30 добавлений и 0 удалений

Просмотреть файл

@ -14,3 +14,5 @@ Christina Roberts <christina@edx.org>
Calen Pennington <calen.pennington@gmail.com>
Ed Zarecor <ed@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
in roughly chronological order, most recent first. Add your entries at or near
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
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.

Просмотреть файл

@ -138,6 +138,10 @@ helpers do
end
end
if params[:commentable_ids]
comment_threads = comment_threads.in(commentable_id: params[:commentable_ids].split(","))
end
sort_key_mapper = {
"date" => :created_at,
"activity" => :last_activity_at,

Просмотреть файл

@ -14,6 +14,7 @@ class Content
index({comment_thread_id: 1, sk: 1}, {sparse: true})
index({comment_thread_id: 1, endorsed: 1}, {sparse: true})
index({commentable_id: 1}, {sparse: true, background: true})
before_save :set_username
def set_username

Просмотреть файл

@ -0,0 +1 @@
db.contents.ensureIndex({commentable_id: 1}, {sparse: true, background: true})

Просмотреть файл

@ -0,0 +1 @@
db.contents.dropIndex({commentable_id: 1})

Просмотреть файл

@ -26,6 +26,24 @@ describe "app" do
res["course_id"].should == "abc"
}
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
@threads["t1"].course_id = "omg"
@threads["t1"].group_id = 100