From 9b887c3c3a759516048da39310fe4a9d3ffb6c18 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 19 Nov 2017 20:27:30 -0600 Subject: [PATCH] Fix #3387: Safebooru: Two tag searches fail for members. Makes the `rating:s` and `-status:deleted` tags not count against the tag limit. --- app/models/tag.rb | 2 +- config/danbooru_default_config.rb | 5 +++++ test/unit/post_test.rb | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index a71ec7807..e4baa1896 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -427,7 +427,7 @@ class Tag < ApplicationRecord } scan_query(query).each do |token| - q[:tag_count] += 1 unless token == "status:deleted" || token =~ /\Alimit:.+\Z/ + q[:tag_count] += 1 unless Danbooru.config.is_unlimited_tag?(token) if token =~ /\A(#{METATAGS}):(.+)\Z/i g1 = $1.downcase diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index e6d322395..4f040d895 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -169,6 +169,11 @@ module Danbooru end end + # Return true if the given tag shouldn't count against the user's tag search limit. + def is_unlimited_tag?(tag) + !!(tag =~ /\A(-?status:deleted|rating:s.*|limit:.+)\z/i) + end + # After this many pages, the paginator will switch to sequential mode. def max_numbered_pages 1_000 diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 22b293011..f53d2f8b0 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -2292,6 +2292,14 @@ class PostTest < ActiveSupport::TestCase end end + should "not count free tags against the user's search limit" do + post1 = FactoryGirl.create(:post, tag_string: "aaa bbb rating:s") + + Danbooru.config.expects(:is_unlimited_tag?).with("rating:s").once.returns(true) + Danbooru.config.expects(:is_unlimited_tag?).with(anything).twice.returns(false) + assert_tag_match([post1], "aaa bbb rating:s") + end + should "succeed for exclusive tag searches with no other tag" do post1 = FactoryGirl.create(:post, :rating => "s", :tag_string => "aaa") assert_nothing_raised do @@ -2425,6 +2433,15 @@ class PostTest < ActiveSupport::TestCase assert_equal(1, Post.fast_count("")) end + should "not fail for a two tag search by a member" do + post1 = FactoryGirl.create(:post, tag_string: "aaa bbb rating:s") + post2 = FactoryGirl.create(:post, tag_string: "aaa bbb rating:e") + + Danbooru.config.expects(:is_unlimited_tag?).with("rating:s").once.returns(true) + Danbooru.config.expects(:is_unlimited_tag?).with(anything).twice.returns(false) + assert_equal(1, Post.fast_count("aaa bbb")) + end + should "set the value in cache" do Post.expects(:set_count_in_cache).with("rating:s", kind_of(Integer)).once Post.fast_count("")