discord: fix tag search commands being limited to 2 tags.

This commit is contained in:
evazion
2021-03-14 16:20:55 -05:00
parent 808c039f03
commit 28c0a48117
10 changed files with 29 additions and 21 deletions

View File

@@ -19,7 +19,7 @@ class DiscordSlashCommand
def call
tags = params[:tags]
query = PostQueryBuilder.new(tags, User.anonymous).normalized_query
query = PostQueryBuilder.new(tags, User.anonymous, tag_limit: nil).normalized_query
count = query.fast_count(estimate_count: true, skip_cache: true)
respond_with("`#{tags}`: #{count} posts")

View File

@@ -28,7 +28,7 @@ class DiscordSlashCommand
def call
tags = params[:tags]
limit = params.fetch(:limit, 3).clamp(1, 10)
posts = Post.user_tag_match(tags, User.anonymous).limit(limit)
posts = Post.user_tag_match(tags, User.anonymous, tag_limit: nil).limit(limit)
respond_with(posts: posts)
end

View File

@@ -26,7 +26,7 @@ class DiscordSlashCommand
def call
tags = params[:tags]
limit = params.fetch(:limit, 1).clamp(1, 10)
posts = Post.user_tag_match(tags, User.anonymous).random(limit)
posts = Post.user_tag_match(tags, User.anonymous, tag_limit: nil).random(limit)
respond_with(posts: posts)
end

View File

@@ -3,6 +3,8 @@ require "strscan"
class PostQueryBuilder
extend Memoist
class TagLimitError < StandardError; end
# How many tags a `blah*` search should match.
MAX_WILDCARD_TAGS = 100
@@ -58,13 +60,14 @@ class PostQueryBuilder
UNLIMITED_METATAGS = %w[status rating limit]
attr_reader :query_string, :current_user, :safe_mode, :hide_deleted_posts
attr_reader :query_string, :current_user, :tag_limit, :safe_mode, :hide_deleted_posts
alias_method :safe_mode?, :safe_mode
alias_method :hide_deleted_posts?, :hide_deleted_posts
def initialize(query_string, current_user = User.anonymous, safe_mode: false, hide_deleted_posts: false)
def initialize(query_string, current_user = User.anonymous, tag_limit: nil, safe_mode: false, hide_deleted_posts: false)
@query_string = query_string
@current_user = current_user
@tag_limit = tag_limit
@safe_mode = safe_mode
@hide_deleted_posts = hide_deleted_posts
end
@@ -468,15 +471,8 @@ class PostQueryBuilder
relation
end
def self.is_unlimited_tag?(term)
term.type == :metatag && term.name.in?(UNLIMITED_METATAGS)
end
def build
tag_count = terms.count { |term| !PostQueryBuilder.is_unlimited_tag?(term) }
if tag_count > current_user.tag_query_limit
raise ::Post::SearchError
end
validate!
relation = Post.all
relation = add_joins(relation)
@@ -642,6 +638,18 @@ class PostQueryBuilder
relation.find_ordered(ids)
end
def validate!
tag_count = terms.count { |term| !is_unlimited_tag?(term) }
if tag_limit.present? && tag_count > tag_limit
raise TagLimitError
end
end
def is_unlimited_tag?(term)
term.type == :metatag && term.name.in?(UNLIMITED_METATAGS)
end
concerning :ParseMethods do
def scan_query
terms = []

View File

@@ -6,7 +6,7 @@ module PostSets
attr_reader :page, :random, :post_count, :format, :tag_string, :query, :normalized_query
def initialize(tags, page = 1, per_page = nil, user: CurrentUser.user, random: false, format: "html")
@query = PostQueryBuilder.new(tags, user, safe_mode: CurrentUser.safe_mode?, hide_deleted_posts: user.hide_deleted_posts?)
@query = PostQueryBuilder.new(tags, user, tag_limit: user.tag_query_limit, safe_mode: CurrentUser.safe_mode?, hide_deleted_posts: user.hide_deleted_posts?)
@normalized_query = query.normalized_query
@tag_string = tags
@page = page