From 627f079e3f8929dbd04310161f2179ef209cf8dd Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 23 Apr 2020 21:14:38 -0500 Subject: [PATCH] search: fix order:custom metatag. Fix order:custom not working. Also change order:custom to return no posts under the following error conditions: * {{order:custom}} (id metatag isn't present) * {{id:42 order:custom}} (id metatag isn't a list) * {{id:>42 order:custom}} (id metatag isn't a list) * {{id:1,2 id:2,3 order:custom}} (id metatag is present twice) --- app/logical/post_query_builder.rb | 13 +++++++++++-- test/unit/post_query_builder_test.rb | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 565a73c62..021d3ec7f 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -607,8 +607,8 @@ class PostQueryBuilder relation = relation.where("posts.image_width IS NOT NULL and posts.image_height IS NOT NULL") end - if q[:order] == "custom" && q[:post_id].present? && q[:post_id][0] == :in - relation = relation.find_ordered(q[:post_id][1]) + if q[:order] == "custom" + relation = search_order_custom(relation, q[:id]) else relation = search_order(relation, q[:order]) end @@ -739,6 +739,15 @@ class PostQueryBuilder relation end + def search_order_custom(relation, id_metatags) + return relation.none unless id_metatags.present? && id_metatags.size == 1 + + operator, ids = parse_range(id_metatags.first, :integer) + return relation.none unless operator == :in + + relation.find_ordered(ids) + end + concerning :ParseMethods do def scan_query terms = [] diff --git a/test/unit/post_query_builder_test.rb b/test/unit/post_query_builder_test.rb index 7bb17b57f..84be27a70 100644 --- a/test/unit/post_query_builder_test.rb +++ b/test/unit/post_query_builder_test.rb @@ -809,6 +809,20 @@ class PostQueryBuilderTest < ActiveSupport::TestCase assert_tag_match([post2, post1, post3], "order:comment_bumped_asc") end + should "return posts for order:custom" do + p1 = create(:post) + p2 = create(:post) + p3 = create(:post) + + as(create(:gold_user)) do + assert_tag_match([p2, p1, p3], "id:#{p2.id},#{p1.id},#{p3.id} order:custom") + assert_tag_match([], "id:#{p1.id} order:custom") + assert_tag_match([], "id:>0 order:custom") + assert_tag_match([], "id:1,2 id:2,3 order:custom") + assert_tag_match([], "order:custom") + end + end + should "return posts for a filesize search" do post = create(:post, file_size: 1.megabyte)