diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 80e22a43c..bde7573fb 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -34,6 +34,8 @@ class PostQueryBuilder -ordfav ordfav -favgroup favgroup ordfavgroup -pool pool ordpool + -note note + -comment comment -commentary commentary -id id -rating rating @@ -195,6 +197,14 @@ class PostQueryBuilder commentary_matches(value, quoted) when "-commentary" commentary_matches(value, quoted).negate + when "note" + note_matches(value) + when "-note" + note_matches(value).negate + when "comment" + comment_matches(value) + when "-comment" + comment_matches(value).negate when "search" saved_search_matches(value) when "-search" @@ -470,6 +480,14 @@ class PostQueryBuilder favorites_include(username).joins(:favorites).merge(Favorite.for_user(user.id)).order("favorites.id DESC") end + def note_matches(query) + Post.where(notes: Note.search(body_matches: query).reorder(nil)) + end + + def comment_matches(query) + Post.where(comments: Comment.search(body_matches: query).reorder(nil)) + end + def commentary_matches(query, quoted = false) case query.downcase in "none" | "false" unless quoted diff --git a/test/unit/post_query_builder_test.rb b/test/unit/post_query_builder_test.rb index 52c79350a..6d181b657 100644 --- a/test/unit/post_query_builder_test.rb +++ b/test/unit/post_query_builder_test.rb @@ -450,6 +450,50 @@ class PostQueryBuilderTest < ActiveSupport::TestCase assert_tag_match([], "commentary:'untranslated'") end + should "return posts for the comment: metatag" do + post1 = create(:post) + post2 = create(:post) + + comment1 = create(:comment, post: post1, body: "petting cats") + comment2 = create(:comment, post: post2, body: "walking dogs") + + assert_tag_match([post1], "comment:petting") + assert_tag_match([post1], "comment:pet") + assert_tag_match([post1], "comment:cats") + assert_tag_match([post1], "comment:cat") + assert_tag_match([post1], "comment:*at*") + + assert_tag_match([post2], "comment:walk") + assert_tag_match([post2], "comment:dog") + + assert_tag_match([post2], "-comment:cat") + assert_tag_match([post1], "-comment:dog") + + assert_tag_match([post2, post1], "comment:*ing*") + end + + should "return posts for the note: metatag" do + post1 = create(:post) + post2 = create(:post) + + note1 = create(:note, post: post1, body: "petting cats") + note2 = create(:note, post: post2, body: "walking dogs") + + assert_tag_match([post1], "note:petting") + assert_tag_match([post1], "note:pet") + assert_tag_match([post1], "note:cats") + assert_tag_match([post1], "note:cat") + assert_tag_match([post1], "note:*at*") + + assert_tag_match([post2], "note:walk") + assert_tag_match([post2], "note:dog") + + assert_tag_match([post2], "-note:cat") + assert_tag_match([post1], "-note:dog") + + assert_tag_match([post2, post1], "note:*ing*") + end + should "return posts for the date: metatag" do post = create(:post, created_at: Time.parse("2017-01-01 12:00"))