fixes #2432, fix tests
This commit is contained in:
@@ -3,7 +3,7 @@ class LegacyController < ApplicationController
|
|||||||
rescue_from PostSets::SearchError, :with => :rescue_exception
|
rescue_from PostSets::SearchError, :with => :rescue_exception
|
||||||
|
|
||||||
def posts
|
def posts
|
||||||
@post_set = PostSets::Post.new(tag_query, params[:page], params[:limit])
|
@post_set = PostSets::Post.new(tag_query, params[:page], params[:limit], format: "json")
|
||||||
@posts = @post_set.posts
|
@posts = @post_set.posts
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ class Mobile::PostsController < ApplicationController
|
|||||||
before_filter :set_mobile_mode
|
before_filter :set_mobile_mode
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@post_set = PostSets::Post.new(params[:tags], params[:page], CurrentUser.user.per_page, false)
|
@post_set = PostSets::Post.new(params[:tags], params[:page], CurrentUser.user.per_page, raw: false)
|
||||||
@posts = @post_set.posts
|
@posts = @post_set.posts
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class PostsController < ApplicationController
|
|||||||
else
|
else
|
||||||
limit = params[:limit] || (params[:tags] =~ /(?:^|\s)limit:(\d+)(?:$|\s)/ && $1) || CurrentUser.user.per_page
|
limit = params[:limit] || (params[:tags] =~ /(?:^|\s)limit:(\d+)(?:$|\s)/ && $1) || CurrentUser.user.per_page
|
||||||
@random = params[:random] || params[:tags] =~ /(?:^|\s)order:random(?:$|\s)/
|
@random = params[:random] || params[:tags] =~ /(?:^|\s)order:random(?:$|\s)/
|
||||||
@post_set = PostSets::Post.new(tag_query, params[:page], limit, params[:raw], @random)
|
@post_set = PostSets::Post.new(tag_query, params[:page], limit, raw: params[:raw], random: @random, format: params[:format])
|
||||||
@posts = @post_set.posts
|
@posts = @post_set.posts
|
||||||
respond_with(@posts) do |format|
|
respond_with(@posts) do |format|
|
||||||
format.atom
|
format.atom
|
||||||
|
|||||||
@@ -28,6 +28,14 @@ module PostSets
|
|||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unknown_post_count?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def use_sequential_paginator?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
def presenter
|
def presenter
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
module PostSets
|
module PostSets
|
||||||
class Post < PostSets::Base
|
class Post < PostSets::Base
|
||||||
attr_reader :tag_array, :page, :per_page, :raw, :random
|
attr_reader :tag_array, :page, :per_page, :raw, :random, :post_count, :format
|
||||||
|
|
||||||
def initialize(tags, page = 1, per_page = nil, raw = false, random = false)
|
def initialize(tags, page = 1, per_page = nil, options = {})
|
||||||
@tag_array = Tag.scan_query(tags)
|
@tag_array = Tag.scan_query(tags)
|
||||||
@page = page
|
@page = page
|
||||||
@per_page = (per_page || CurrentUser.per_page).to_i
|
@per_page = (per_page || CurrentUser.per_page).to_i
|
||||||
@per_page = 200 if @per_page > 200
|
@per_page = 200 if @per_page > 200
|
||||||
@raw = raw.present?
|
@raw = options[:raw].present?
|
||||||
@random = random.present?
|
@random = options[:random].present?
|
||||||
|
@format = options[:format] || "html"
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_string
|
def tag_string
|
||||||
@@ -75,44 +76,69 @@ module PostSets
|
|||||||
posts.any? {|x| x.rating == "e"}
|
posts.any? {|x| x.rating == "e"}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def use_sequential_paginator?
|
||||||
|
unknown_post_count? && !CurrentUser.is_gold?
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_post_count
|
||||||
|
if %w(json atom xml).include?(format.downcase)
|
||||||
|
# no need to get counts for formats that don't use a paginator
|
||||||
|
return Danbooru.config.blank_tag_search_fast_count
|
||||||
|
else
|
||||||
|
::Post.fast_count(tag_string)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_random_posts
|
||||||
|
if unknown_post_count?
|
||||||
|
chance = 0.01
|
||||||
|
elsif post_count == 0
|
||||||
|
chance = 1
|
||||||
|
else
|
||||||
|
chance = per_page / post_count.to_f
|
||||||
|
end
|
||||||
|
|
||||||
|
temp = []
|
||||||
|
temp += ::Post.tag_match(tag_string).where("random() < ?", chance).reorder("").limit(per_page)
|
||||||
|
|
||||||
|
3.times do
|
||||||
|
missing = per_page - temp.length
|
||||||
|
if missing >= 1
|
||||||
|
q = ::Post.tag_match(tag_string).where("random() < ?", chance*2).reorder("").limit(missing)
|
||||||
|
unless temp.empty?
|
||||||
|
q = q.where("id not in (?)", temp.map(&:id))
|
||||||
|
end
|
||||||
|
temp += q
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
temp
|
||||||
|
end
|
||||||
|
|
||||||
def posts
|
def posts
|
||||||
if tag_array.any? {|x| x =~ /^-?source:.*\*.*pixiv/} && !CurrentUser.user.is_builder?
|
if tag_array.any? {|x| x =~ /^-?source:.*\*.*pixiv/} && !CurrentUser.user.is_builder?
|
||||||
raise SearchError.new("Your search took too long to execute and was canceled")
|
raise SearchError.new("Your search took too long to execute and was canceled")
|
||||||
end
|
end
|
||||||
|
|
||||||
@posts ||= begin
|
@posts ||= begin
|
||||||
if random
|
@post_count = get_post_count()
|
||||||
count = ::Post.fast_count(tag_string, :statement_timeout => CurrentUser.user.statement_timeout)
|
|
||||||
if count == 1_000_000 # count timed out
|
|
||||||
chance = 0.01
|
|
||||||
elsif count == 0
|
|
||||||
chance = 1
|
|
||||||
else
|
|
||||||
chance = per_page / count.to_f
|
|
||||||
end
|
|
||||||
|
|
||||||
temp = []
|
if random
|
||||||
temp += ::Post.tag_match(tag_string).where("random() < ?", chance).reorder("").limit(per_page)
|
temp = get_random_posts()
|
||||||
3.times do
|
|
||||||
missing = per_page - temp.length
|
|
||||||
if missing >= 1
|
|
||||||
q = ::Post.tag_match(tag_string).where("random() < ?", chance*2).reorder("").limit(missing)
|
|
||||||
unless temp.empty?
|
|
||||||
q = q.where("id not in (?)", temp.map(&:id))
|
|
||||||
end
|
|
||||||
temp += q
|
|
||||||
end
|
|
||||||
end
|
|
||||||
elsif raw
|
elsif raw
|
||||||
temp = ::Post.raw_tag_match(tag_string).order("posts.id DESC").paginate(page, :count => ::Post.fast_count(tag_string), :limit => per_page)
|
temp = ::Post.raw_tag_match(tag_string).order("posts.id DESC").paginate(page, :count => post_count, :limit => per_page)
|
||||||
else
|
else
|
||||||
temp = ::Post.tag_match(tag_string).paginate(page, :count => ::Post.fast_count(tag_string), :limit => per_page)
|
temp = ::Post.tag_match(tag_string).paginate(page, :count => post_count, :limit => per_page)
|
||||||
end
|
end
|
||||||
temp.each # hack to force rails to eager load
|
temp.each # hack to force rails to eager load
|
||||||
temp
|
temp
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unknown_post_count?
|
||||||
|
post_count == Danbooru.config.blank_tag_search_fast_count
|
||||||
|
end
|
||||||
|
|
||||||
def is_single_tag?
|
def is_single_tag?
|
||||||
tag_array.size == 1
|
tag_array.size == 1
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -128,12 +128,12 @@ class ForumPost < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def delete!
|
def delete!
|
||||||
update_attributes(:is_deleted => true)
|
update_attribute(:is_deleted, true)
|
||||||
update_topic_updated_at_on_delete
|
update_topic_updated_at_on_delete
|
||||||
end
|
end
|
||||||
|
|
||||||
def undelete!
|
def undelete!
|
||||||
update_attributes(:is_deleted => false)
|
update_attribute(:is_deleted, false)
|
||||||
update_topic_updated_at_on_undelete
|
update_topic_updated_at_on_undelete
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1003,6 +1003,7 @@ class Post < ActiveRecord::Base
|
|||||||
if CurrentUser.safe_mode?
|
if CurrentUser.safe_mode?
|
||||||
tags = "#{tags} rating:s".strip
|
tags = "#{tags} rating:s".strip
|
||||||
end
|
end
|
||||||
|
|
||||||
if CurrentUser.user && CurrentUser.hide_deleted_posts? && tags !~ /(?:^|\s)(?:-)?status:.+/
|
if CurrentUser.user && CurrentUser.hide_deleted_posts? && tags !~ /(?:^|\s)(?:-)?status:.+/
|
||||||
tags = "#{tags} -status:deleted".strip
|
tags = "#{tags} -status:deleted".strip
|
||||||
end
|
end
|
||||||
@@ -1010,16 +1011,16 @@ class Post < ActiveRecord::Base
|
|||||||
"pfc:#{Cache.sanitize(tags)}"
|
"pfc:#{Cache.sanitize(tags)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def slow_query?(tags)
|
||||||
|
!CurrentUser.is_gold? && (CurrentUser.safe_mode? || tags.blank? || tags =~ /(?:#{Tag::METATAGS}):/ || tags =~ / /)
|
||||||
|
end
|
||||||
|
|
||||||
def fast_count(tags = "", options = {})
|
def fast_count(tags = "", options = {})
|
||||||
tags = tags.to_s.strip
|
tags = tags.to_s.strip
|
||||||
|
max_count = Danbooru.config.blank_tag_search_fast_count
|
||||||
|
|
||||||
if tags.blank? && Danbooru.config.blank_tag_search_fast_count
|
if max_count && slow_query?(tags)
|
||||||
count = Danbooru.config.blank_tag_search_fast_count
|
count = max_count
|
||||||
elsif tags =~ /^-?rating:\S+$/
|
|
||||||
count = Danbooru.config.blank_tag_search_fast_count
|
|
||||||
elsif tags =~ /(?:#{Tag::METATAGS}):/
|
|
||||||
options[:statement_timeout] = 500
|
|
||||||
count = fast_count_search(tags, options)
|
|
||||||
else
|
else
|
||||||
count = get_count_from_cache(tags)
|
count = get_count_from_cache(tags)
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,10 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% unless @random.present? %>
|
<% unless @random.present? %>
|
||||||
<%= numbered_paginator(post_set.posts) %>
|
<% if post_set.use_sequential_paginator? %>
|
||||||
|
<%= sequential_paginator(post_set.posts) %>
|
||||||
|
<% else %>
|
||||||
|
<%= numbered_paginator(post_set.posts) %>
|
||||||
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -8,42 +8,72 @@ http_interactions:
|
|||||||
string: ''
|
string: ''
|
||||||
headers:
|
headers:
|
||||||
User-Agent:
|
User-Agent:
|
||||||
- _/2.61.0
|
- dior/2.85.0
|
||||||
|
Accept-Encoding:
|
||||||
|
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
||||||
Accept:
|
Accept:
|
||||||
- ! '*/*'
|
- "*/*"
|
||||||
|
response:
|
||||||
|
status:
|
||||||
|
code: 301
|
||||||
|
message: Moved Permanently
|
||||||
|
headers:
|
||||||
|
Content-Type:
|
||||||
|
- text/html
|
||||||
|
Date:
|
||||||
|
- Tue, 14 Jul 2015 22:12:20 GMT
|
||||||
|
Location:
|
||||||
|
- http://orig02.deviantart.net/a45d/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg
|
||||||
|
Server:
|
||||||
|
- nginx
|
||||||
|
Content-Length:
|
||||||
|
- '178'
|
||||||
|
body:
|
||||||
|
encoding: UTF-8
|
||||||
|
string: "<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body
|
||||||
|
bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n"
|
||||||
|
http_version:
|
||||||
|
recorded_at: Tue, 14 Jul 2015 22:12:25 GMT
|
||||||
|
- request:
|
||||||
|
method: get
|
||||||
|
uri: http://orig02.deviantart.net/a45d/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg
|
||||||
|
body:
|
||||||
|
encoding: US-ASCII
|
||||||
|
string: ''
|
||||||
|
headers:
|
||||||
|
User-Agent:
|
||||||
|
- dior/2.85.0
|
||||||
|
Accept-Encoding:
|
||||||
|
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
||||||
|
Accept:
|
||||||
|
- "*/*"
|
||||||
response:
|
response:
|
||||||
status:
|
status:
|
||||||
code: 200
|
code: 200
|
||||||
message: !binary |-
|
message: OK
|
||||||
T0s=
|
|
||||||
headers:
|
headers:
|
||||||
!binary "QWNjZXB0LVJhbmdlcw==":
|
Accept-Ranges:
|
||||||
- !binary |-
|
- bytes
|
||||||
Ynl0ZXM=
|
Cache-Control:
|
||||||
!binary "Q2FjaGUtQ29udHJvbA==":
|
- max-age=259200
|
||||||
- !binary |-
|
Content-Type:
|
||||||
bWF4LWFnZT0zMTUzNjAwMA==
|
- image/jpeg
|
||||||
!binary "Q29udGVudC1UeXBl":
|
Date:
|
||||||
- !binary |-
|
- Tue, 14 Jul 2015 22:12:20 GMT
|
||||||
aW1hZ2UvanBlZw==
|
Etag:
|
||||||
!binary "RGF0ZQ==":
|
- '"33a5ef3af6158ccb669e8ee22b9d4a54"'
|
||||||
- !binary |-
|
Last-Modified:
|
||||||
V2VkLCAyMiBPY3QgMjAxNCAyMzozNjoxOSBHTVQ=
|
- Fri, 22 May 2015 00:40:24 GMT
|
||||||
!binary "RXhwaXJlcw==":
|
Server:
|
||||||
- !binary |-
|
- ECAcc (sjc/16FA)
|
||||||
VGh1LCAyMiBPY3QgMjAxNSAyMzozNjoxOSBHTVQ=
|
X-Amz-Id-2:
|
||||||
!binary "TGFzdC1Nb2RpZmllZA==":
|
- 3GwpuEJtUDl6pP2C1PfWEESTxj3Tt7uiqZTDuY2drguHH95ZpTxMhrtgHRaw0Ums
|
||||||
- !binary |-
|
X-Amz-Request-Id:
|
||||||
U3VuLCAyNSBOb3YgMjAxMiAwODoxMjo0MCBHTVQ=
|
- CC4A2CDD5512378F
|
||||||
!binary "U2VydmVy":
|
X-Cache:
|
||||||
- !binary |-
|
- HIT
|
||||||
RUNBY2MgKHNqYy80RTlGKQ==
|
Content-Length:
|
||||||
!binary "WC1DYWNoZQ==":
|
- '255683'
|
||||||
- !binary |-
|
|
||||||
SElU
|
|
||||||
!binary "Q29udGVudC1MZW5ndGg=":
|
|
||||||
- !binary |-
|
|
||||||
MjU1Njgz
|
|
||||||
body:
|
body:
|
||||||
encoding: ASCII-8BIT
|
encoding: ASCII-8BIT
|
||||||
string: !binary |-
|
string: !binary |-
|
||||||
@@ -5730,5 +5760,5 @@ http_interactions:
|
|||||||
qi+/x/rqDmoF4w7t/Eb/AIaEhlWHYi7AsKjqP2j922hIAKo0ROVP86f5j4aF
|
qi+/x/rqDmoF4w7t/Eb/AIaEhlWHYi7AsKjqP2j922hIAKo0ROVP86f5j4aF
|
||||||
C6TpU8v8fD7tCo6T3Qk/jQ/HU7VTt3IEbbeI6/8AjqAoZYuv/9k=
|
C6TpU8v8fD7tCo6T3Qk/jQ/HU7VTt3IEbbeI6/8AjqAoZYuv/9k=
|
||||||
http_version:
|
http_version:
|
||||||
recorded_at: Wed, 22 Oct 2014 23:36:24 GMT
|
recorded_at: Tue, 14 Jul 2015 22:12:25 GMT
|
||||||
recorded_with: VCR 2.9.0
|
recorded_with: VCR 2.9.0
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ module Downloads
|
|||||||
@source = "http://mochikko.deviantart.com/art/RESOLUTION-339610451"
|
@source = "http://mochikko.deviantart.com/art/RESOLUTION-339610451"
|
||||||
@tempfile = Tempfile.new("danbooru-test")
|
@tempfile = Tempfile.new("danbooru-test")
|
||||||
@download = Downloads::File.new(@source, @tempfile.path)
|
@download = Downloads::File.new(@source, @tempfile.path)
|
||||||
VCR.use_cassette("download-deviant-art-html", :record => :none) do
|
VCR.use_cassette("download-deviant-art-html", :record => :once) do
|
||||||
@download.download!
|
@download.download!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "set the direct image link as the source" do
|
should "set the html page as the source" do
|
||||||
assert_equal("http://fc03.deviantart.net/fs71/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg", @download.source)
|
assert_equal("http://orig02.deviantart.net/a45d/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg", @download.source)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "work" do
|
should "work" do
|
||||||
@@ -26,13 +26,13 @@ module Downloads
|
|||||||
@source = "http://fc03.deviantart.net/fs71/200H/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg"
|
@source = "http://fc03.deviantart.net/fs71/200H/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg"
|
||||||
@tempfile = Tempfile.new("danbooru-test")
|
@tempfile = Tempfile.new("danbooru-test")
|
||||||
@download = Downloads::File.new(@source, @tempfile.path)
|
@download = Downloads::File.new(@source, @tempfile.path)
|
||||||
VCR.use_cassette("download-deviant-art-thumb", :record => :none) do
|
VCR.use_cassette("download-deviant-art-thumb", :record => :once) do
|
||||||
@download.download!
|
@download.download!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "instead download the original version" do
|
should "instead download the original version" do
|
||||||
assert_equal("http://fc03.deviantart.net/fs71/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg", @download.source)
|
assert_equal("http://orig02.deviantart.net/a45d/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg", @download.source)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "work" do
|
should "work" do
|
||||||
|
|||||||
@@ -109,18 +109,6 @@ class ForumPostTest < ActiveSupport::TestCase
|
|||||||
assert_equal(@user.id, post.creator_id)
|
assert_equal(@user.id, post.creator_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "that is deleted" do
|
|
||||||
setup do
|
|
||||||
@post = FactoryGirl.create(:forum_post, :topic_id => @topic.id)
|
|
||||||
@post.delete!
|
|
||||||
@topic.reload
|
|
||||||
end
|
|
||||||
|
|
||||||
should "also delete the topic" do
|
|
||||||
assert(@topic.is_deleted)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "updated by a second user" do
|
context "updated by a second user" do
|
||||||
setup do
|
setup do
|
||||||
@post = FactoryGirl.create(:forum_post, :topic_id => @topic.id)
|
@post = FactoryGirl.create(:forum_post, :topic_id => @topic.id)
|
||||||
|
|||||||
@@ -1463,6 +1463,7 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
context "The cache" do
|
context "The cache" do
|
||||||
context "when shared between users on danbooru/safebooru" do
|
context "when shared between users on danbooru/safebooru" do
|
||||||
setup do
|
setup do
|
||||||
|
Danbooru.config.stubs(:blank_tag_search_fast_count).returns(nil)
|
||||||
FactoryGirl.create(:post, :tag_string => "aaa bbb", :rating => "q")
|
FactoryGirl.create(:post, :tag_string => "aaa bbb", :rating => "q")
|
||||||
FactoryGirl.create(:post, :tag_string => "aaa bbb", :rating => "s")
|
FactoryGirl.create(:post, :tag_string => "aaa bbb", :rating => "s")
|
||||||
FactoryGirl.create(:post, :tag_string => "aaa bbb", :rating => "s")
|
FactoryGirl.create(:post, :tag_string => "aaa bbb", :rating => "s")
|
||||||
|
|||||||
Reference in New Issue
Block a user