fixes #2432, fix tests

This commit is contained in:
r888888888
2015-07-14 15:13:04 -07:00
parent 2c8cacd50e
commit 6ad6aa44c4
13 changed files with 6809 additions and 841 deletions

View File

@@ -3,7 +3,7 @@ class LegacyController < ApplicationController
rescue_from PostSets::SearchError, :with => :rescue_exception
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
end

View File

@@ -3,7 +3,7 @@ class Mobile::PostsController < ApplicationController
before_filter :set_mobile_mode
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
end

View File

@@ -15,7 +15,7 @@ class PostsController < ApplicationController
else
limit = params[:limit] || (params[:tags] =~ /(?:^|\s)limit:(\d+)(?:$|\s)/ && $1) || CurrentUser.user.per_page
@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
respond_with(@posts) do |format|
format.atom

View File

@@ -28,6 +28,14 @@ module PostSets
nil
end
def unknown_post_count?
false
end
def use_sequential_paginator?
false
end
def presenter
raise NotImplementedError
end

View File

@@ -1,14 +1,15 @@
module PostSets
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)
@page = page
@per_page = (per_page || CurrentUser.per_page).to_i
@per_page = 200 if @per_page > 200
@raw = raw.present?
@random = random.present?
@raw = options[:raw].present?
@random = options[:random].present?
@format = options[:format] || "html"
end
def tag_string
@@ -75,44 +76,69 @@ module PostSets
posts.any? {|x| x.rating == "e"}
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
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")
end
@posts ||= begin
if random
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
@post_count = get_post_count()
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
if random
temp = get_random_posts()
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
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
temp.each # hack to force rails to eager load
temp
end
end
def unknown_post_count?
post_count == Danbooru.config.blank_tag_search_fast_count
end
def is_single_tag?
tag_array.size == 1
end

View File

@@ -128,12 +128,12 @@ class ForumPost < ActiveRecord::Base
end
def delete!
update_attributes(:is_deleted => true)
update_attribute(:is_deleted, true)
update_topic_updated_at_on_delete
end
def undelete!
update_attributes(:is_deleted => false)
update_attribute(:is_deleted, false)
update_topic_updated_at_on_undelete
end

View File

@@ -1003,6 +1003,7 @@ class Post < ActiveRecord::Base
if CurrentUser.safe_mode?
tags = "#{tags} rating:s".strip
end
if CurrentUser.user && CurrentUser.hide_deleted_posts? && tags !~ /(?:^|\s)(?:-)?status:.+/
tags = "#{tags} -status:deleted".strip
end
@@ -1010,16 +1011,16 @@ class Post < ActiveRecord::Base
"pfc:#{Cache.sanitize(tags)}"
end
def slow_query?(tags)
!CurrentUser.is_gold? && (CurrentUser.safe_mode? || tags.blank? || tags =~ /(?:#{Tag::METATAGS}):/ || tags =~ / /)
end
def fast_count(tags = "", options = {})
tags = tags.to_s.strip
max_count = Danbooru.config.blank_tag_search_fast_count
if tags.blank? && Danbooru.config.blank_tag_search_fast_count
count = Danbooru.config.blank_tag_search_fast_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)
if max_count && slow_query?(tags)
count = max_count
else
count = get_count_from_cache(tags)

View File

@@ -10,6 +10,10 @@
<% end %>
<% 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 %>
</div>