diff --git a/AUTHORS b/AUTHORS index 21a714c..ce0aadd 100644 --- a/AUTHORS +++ b/AUTHORS @@ -14,3 +14,5 @@ Christina Roberts Calen Pennington Ed Zarecor Jay Zoldak +Jim Abramson +Greg Price diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c288568..17a6233 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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. diff --git a/lib/helpers.rb b/lib/helpers.rb index 1e4d81d..70fa82f 100644 --- a/lib/helpers.rb +++ b/lib/helpers.rb @@ -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, diff --git a/models/content.rb b/models/content.rb index 7eb7752..0cd3a40 100644 --- a/models/content.rb +++ b/models/content.rb @@ -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 diff --git a/scripts/db/migrate-005-update-indexes.js b/scripts/db/migrate-005-update-indexes.js new file mode 100644 index 0000000..f330959 --- /dev/null +++ b/scripts/db/migrate-005-update-indexes.js @@ -0,0 +1 @@ +db.contents.ensureIndex({commentable_id: 1}, {sparse: true, background: true}) diff --git a/scripts/db/revert-migrate-005-update-indexes.js b/scripts/db/revert-migrate-005-update-indexes.js new file mode 100644 index 0000000..4201b59 --- /dev/null +++ b/scripts/db/revert-migrate-005-update-indexes.js @@ -0,0 +1 @@ +db.contents.dropIndex({commentable_id: 1}) diff --git a/spec/api/comment_thread_spec.rb b/spec/api/comment_thread_spec.rb index 480b5d8..c9a7e6f 100644 --- a/spec/api/comment_thread_spec.rb +++ b/spec/api/comment_thread_spec.rb @@ -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