diff --git a/app/controllers/counts_controller.rb b/app/controllers/counts_controller.rb index c3901ff8e..560b1b785 100644 --- a/app/controllers/counts_controller.rb +++ b/app/controllers/counts_controller.rb @@ -6,7 +6,7 @@ class CountsController < ApplicationController def posts estimate_count = params.fetch(:estimate_count, "true").truthy? skip_cache = params.fetch(:skip_cache, "false").truthy? - @count = PostQueryBuilder.new(params[:tags], CurrentUser.user, tag_limit: CurrentUser.user.tag_query_limit).normalized_query.fast_count(timeout: CurrentUser.statement_timeout, estimate_count: estimate_count, skip_cache: skip_cache) + @count = PostQuery.new(params[:tags], current_user: CurrentUser.user, tag_limit: CurrentUser.user.tag_query_limit).fast_count(timeout: CurrentUser.statement_timeout, estimate_count: estimate_count, skip_cache: skip_cache) if request.format.xml? respond_with({ posts: @count }, root: "counts") diff --git a/app/logical/discord_slash_command/count_command.rb b/app/logical/discord_slash_command/count_command.rb index beca11e5f..adcb2d100 100644 --- a/app/logical/discord_slash_command/count_command.rb +++ b/app/logical/discord_slash_command/count_command.rb @@ -13,8 +13,7 @@ class DiscordSlashCommand def call tags = params[:tags] - query = PostQueryBuilder.new(tags, User.anonymous, tag_limit: nil).normalized_query - count = query.fast_count(timeout: 9_000, estimate_count: false, skip_cache: true) + count = PostQuery.new(tags).fast_count(timeout: 9_000, estimate_count: false, skip_cache: true) respond_with("`#{tags}`: #{count} posts") end diff --git a/app/logical/post_query.rb b/app/logical/post_query.rb index 73fbd30ae..bf581f37b 100644 --- a/app/logical/post_query.rb +++ b/app/logical/post_query.rb @@ -3,15 +3,20 @@ class PostQuery extend Memoist - attr_reader :search, :parser, :ast + attr_reader :search, :parser, :builder, :ast delegate :tag_names, :metatags, to: :ast - def initialize(search) + def initialize(search, current_user: User.anonymous, tag_limit: nil, safe_mode: false, hide_deleted_posts: false) @search = search @parser = Parser.new(search) + @builder = PostQueryBuilder.new(search, current_user, tag_limit: tag_limit, safe_mode: safe_mode, hide_deleted_posts: hide_deleted_posts) @ast = parser.parse.simplify end + def fast_count(...) + builder.normalized_query.fast_count(...) + end + def tags Tag.where(name: tag_names) end diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index 28c2feb9a..bcee82de8 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -126,7 +126,7 @@ class TagImplication < TagRelationship errors.add(:base, "'#{antecedent_name}' must have at least #{(MINIMUM_TAG_PERCENTAGE * consequent_tag.post_count).to_i} posts") end - max_count = MAXIMUM_TAG_PERCENTAGE * PostQueryBuilder.new("~#{antecedent_name} ~#{consequent_name}").fast_count(timeout: 0).to_i + max_count = MAXIMUM_TAG_PERCENTAGE * PostQuery.new("~#{antecedent_name} ~#{consequent_name}").fast_count(timeout: 0).to_i if antecedent_tag.post_count > max_count && max_count > 0 errors.add(:base, "'#{antecedent_name}' can't make up more than #{(MAXIMUM_TAG_PERCENTAGE * 100).to_i}% of '#{consequent_name}'") end diff --git a/app/presenters/user_presenter.rb b/app/presenters/user_presenter.rb index 52e7f5539..200a113ad 100644 --- a/app/presenters/user_presenter.rb +++ b/app/presenters/user_presenter.rb @@ -70,7 +70,7 @@ class UserPresenter end def commented_posts_count(template) - count = PostQueryBuilder.new("commenter:#{user.name}").fast_count + count = PostQuery.new("commenter:#{user.name}").fast_count count = "?" if count.nil? template.link_to(count, template.posts_path(tags: "commenter:#{user.name} order:comment_bumped"), rel: "nofollow") end @@ -84,7 +84,7 @@ class UserPresenter end def noted_posts_count(template) - count = PostQueryBuilder.new("noteupdater:#{user.name}").fast_count + count = PostQuery.new("noteupdater:#{user.name}").fast_count count = "?" if count.nil? template.link_to(count, template.posts_path(tags: "noteupdater:#{user.name} order:note"), rel: "nofollow") end