Merge pull request #4007 from evazion/fix-4004
Fix #4004: Add additional order by metatags for posts
This commit is contained in:
@@ -5,6 +5,7 @@ let Autocomplete = {};
|
||||
|
||||
Autocomplete.METATAGS = <%= Tag::METATAGS.to_json.html_safe %>;
|
||||
Autocomplete.TAG_CATEGORIES = <%= TagCategory.mapping.to_json.html_safe %>;
|
||||
Autocomplete.ORDER_METATAGS = <%= Tag::ORDER_METATAGS.to_json.html_safe %>;
|
||||
|
||||
Autocomplete.TAG_PREFIXES = "-|~|" + Object.keys(Autocomplete.TAG_CATEGORIES).map(category => category + ":").join("|");
|
||||
Autocomplete.TAG_PREFIXES_REGEX = new RegExp("^(" + Autocomplete.TAG_PREFIXES + ")(.*)$", "i");
|
||||
@@ -389,24 +390,7 @@ Autocomplete.render_item = function(list, item) {
|
||||
};
|
||||
|
||||
Autocomplete.static_metatags = {
|
||||
order: [
|
||||
"id", "id_desc",
|
||||
"score", "score_asc",
|
||||
"favcount", "favcount_asc",
|
||||
"created_at", "created_at_asc",
|
||||
"change", "change_asc",
|
||||
"comment", "comment_asc",
|
||||
"comment_bumped", "comment_bumped_asc",
|
||||
"note", "note_asc",
|
||||
"artcomm", "artcomm_asc",
|
||||
"mpixels", "mpixels_asc",
|
||||
"portrait", "landscape",
|
||||
"filesize", "filesize_asc",
|
||||
"tagcount", "tagcount_asc",
|
||||
"rank",
|
||||
"random",
|
||||
"custom"
|
||||
].concat(<%= TagCategory.short_name_list.map {|category| [category + "tags", category + "tags_asc"]}.flatten %>),
|
||||
order: Autocomplete.ORDER_METATAGS,
|
||||
status: [
|
||||
"any", "deleted", "active", "pending", "flagged", "banned", "modqueue", "unmoderated"
|
||||
],
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
class PostQueryBuilder
|
||||
attr_accessor :query_string
|
||||
attr_accessor :query_string, :read_only
|
||||
|
||||
def initialize(query_string)
|
||||
def initialize(query_string, read_only: false)
|
||||
@query_string = query_string
|
||||
@read_only = read_only
|
||||
end
|
||||
|
||||
def add_range_relation(arr, field, relation)
|
||||
@@ -84,6 +85,28 @@ class PostQueryBuilder
|
||||
relation
|
||||
end
|
||||
|
||||
def table_for_metatag(metatag)
|
||||
if metatag.in?(Tag::COUNT_METATAGS)
|
||||
metatag[/(?<table>[a-z]+)_count\z/i, :table]
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def tables_for_query(q)
|
||||
metatags = q.keys
|
||||
metatags << q[:order].remove(/_(asc|desc)\z/i) if q[:order].present?
|
||||
|
||||
tables = metatags.map { |metatag| table_for_metatag(metatag.to_s) }
|
||||
tables.compact.uniq
|
||||
end
|
||||
|
||||
def add_joins(q, relation)
|
||||
tables = tables_for_query(q)
|
||||
relation = relation.with_stats(tables)
|
||||
relation
|
||||
end
|
||||
|
||||
def hide_deleted_posts?(q)
|
||||
return false if CurrentUser.admin_mode?
|
||||
return false if q[:status].in?(%w[deleted active any all])
|
||||
@@ -91,14 +114,12 @@ class PostQueryBuilder
|
||||
return CurrentUser.user.hide_deleted_posts?
|
||||
end
|
||||
|
||||
def build(relation = nil)
|
||||
def build
|
||||
unless query_string.is_a?(Hash)
|
||||
q = Tag.parse_query(query_string)
|
||||
end
|
||||
|
||||
if relation.nil?
|
||||
relation = Post.where("true")
|
||||
end
|
||||
relation = read_only ? PostReadOnly.all : Post.all
|
||||
|
||||
if q[:tag_count].to_i > Danbooru.config.tag_query_limit
|
||||
raise ::Post::SearchError.new("You cannot search for more than #{Danbooru.config.tag_query_limit} tags at a time")
|
||||
@@ -108,6 +129,7 @@ class PostQueryBuilder
|
||||
relation = relation.where("posts.rating = 's'")
|
||||
end
|
||||
|
||||
relation = add_joins(q, relation)
|
||||
relation = add_range_relation(q[:post_id], "posts.id", relation)
|
||||
relation = add_range_relation(q[:mpixels], "posts.image_width * posts.image_height / 1000000.0", relation)
|
||||
relation = add_range_relation(q[:ratio], "ROUND(1.0 * posts.image_width / GREATEST(1, posts.image_height), 2)", relation)
|
||||
@@ -123,6 +145,10 @@ class PostQueryBuilder
|
||||
end
|
||||
relation = add_range_relation(q[:post_tag_count], "posts.tag_count", relation)
|
||||
|
||||
Tag::COUNT_METATAGS.each do |column|
|
||||
relation = add_range_relation(q[column.to_sym], "posts.#{column}", relation)
|
||||
end
|
||||
|
||||
if q[:md5]
|
||||
relation = relation.where("posts.md5": q[:md5])
|
||||
end
|
||||
@@ -545,6 +571,11 @@ class PostQueryBuilder
|
||||
when "filesize_asc"
|
||||
relation = relation.order("posts.file_size ASC")
|
||||
|
||||
when /\A(?<column>#{Tag::COUNT_METATAGS.join("|")})(_(?<direction>asc|desc))?\z/i
|
||||
column = $~[:column]
|
||||
direction = $~[:direction] || "desc"
|
||||
relation = relation.order(column => direction, :id => direction)
|
||||
|
||||
when "tagcount", "tagcount_desc"
|
||||
relation = relation.order("posts.tag_count DESC")
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class ArtistCommentary < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def deleted
|
||||
|
||||
@@ -52,7 +52,7 @@ class Comment < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
|
||||
@@ -18,7 +18,7 @@ class Note < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
|
||||
@@ -1609,6 +1609,79 @@ class Post < ApplicationRecord
|
||||
joins("CROSS JOIN unnest(string_to_array(tag_string, ' ')) AS tag")
|
||||
end
|
||||
|
||||
def with_comment_stats
|
||||
relation = left_outer_joins(:comments).group(:id).select("posts.*")
|
||||
relation = relation.select("COUNT(comments.id) AS comment_count")
|
||||
relation = relation.select("COUNT(comments.id) FILTER (WHERE comments.is_deleted = TRUE) AS deleted_comment_count")
|
||||
relation = relation.select("COUNT(comments.id) FILTER (WHERE comments.is_deleted = FALSE) AS active_comment_count")
|
||||
relation
|
||||
end
|
||||
|
||||
def with_note_stats
|
||||
relation = left_outer_joins(:notes).group(:id).select("posts.*")
|
||||
relation = relation.select("COUNT(notes.id) AS note_count")
|
||||
relation = relation.select("COUNT(notes.id) FILTER (WHERE notes.is_active = TRUE) AS active_note_count")
|
||||
relation = relation.select("COUNT(notes.id) FILTER (WHERE notes.is_active = FALSE) AS deleted_note_count")
|
||||
relation
|
||||
end
|
||||
|
||||
def with_flag_stats
|
||||
relation = left_outer_joins(:flags).group(:id).select("posts.*")
|
||||
relation = relation.select("COUNT(post_flags.id) AS flag_count")
|
||||
relation = relation.select("COUNT(post_flags.id) FILTER (WHERE post_flags.is_resolved = TRUE) AS resolved_flag_count")
|
||||
relation = relation.select("COUNT(post_flags.id) FILTER (WHERE post_flags.is_resolved = FALSE) AS unresolved_flag_count")
|
||||
relation
|
||||
end
|
||||
|
||||
def with_appeal_stats
|
||||
relation = left_outer_joins(:appeals).group(:id).select("posts.*")
|
||||
relation = relation.select("COUNT(post_appeals.id) AS appeal_count")
|
||||
relation
|
||||
end
|
||||
|
||||
def with_approval_stats
|
||||
relation = left_outer_joins(:approvals).group(:id).select("posts.*")
|
||||
relation = relation.select("COUNT(post_approvals.id) AS approval_count")
|
||||
relation
|
||||
end
|
||||
|
||||
def with_replacement_stats
|
||||
relation = left_outer_joins(:replacements).group(:id).select("posts.*")
|
||||
relation = relation.select("COUNT(post_replacements.id) AS replacement_count")
|
||||
relation
|
||||
end
|
||||
|
||||
def with_child_stats
|
||||
relation = left_outer_joins(:children).group(:id).select("posts.*")
|
||||
relation = relation.select("COUNT(children_posts.id) AS child_count")
|
||||
relation = relation.select("COUNT(children_posts.id) FILTER (WHERE children_posts.is_deleted = TRUE) AS deleted_child_count")
|
||||
relation = relation.select("COUNT(children_posts.id) FILTER (WHERE children_posts.is_deleted = FALSE) AS active_child_count")
|
||||
relation
|
||||
end
|
||||
|
||||
def with_pool_stats
|
||||
pool_posts = Pool.joins("CROSS JOIN unnest(post_ids) AS post_id").select(:id, :is_deleted, :category, "post_id")
|
||||
relation = joins("LEFT OUTER JOIN (#{pool_posts.to_sql}) pools ON pools.post_id = posts.id").group(:id).select("posts.*")
|
||||
|
||||
relation = relation.select("COUNT(pools.id) AS pool_count")
|
||||
relation = relation.select("COUNT(pools.id) FILTER (WHERE pools.is_deleted = TRUE) AS deleted_pool_count")
|
||||
relation = relation.select("COUNT(pools.id) FILTER (WHERE pools.is_deleted = FALSE) AS active_pool_count")
|
||||
relation = relation.select("COUNT(pools.id) FILTER (WHERE pools.category = 'series') AS series_pool_count")
|
||||
relation = relation.select("COUNT(pools.id) FILTER (WHERE pools.category = 'collection') AS collection_pool_count")
|
||||
relation
|
||||
end
|
||||
|
||||
def with_stats(tables)
|
||||
return all if tables.empty?
|
||||
|
||||
relation = all
|
||||
tables.each do |table|
|
||||
relation = relation.send("with_#{table}_stats")
|
||||
end
|
||||
|
||||
from(relation.arel.as("posts"))
|
||||
end
|
||||
|
||||
def pending
|
||||
where(is_pending: true)
|
||||
end
|
||||
@@ -1662,7 +1735,7 @@ class Post < ApplicationRecord
|
||||
|
||||
if read_only
|
||||
begin
|
||||
PostQueryBuilder.new(query).build(PostReadOnly.where("true"))
|
||||
PostQueryBuilder.new(query, read_only: true).build
|
||||
rescue PG::ConnectionBad
|
||||
PostQueryBuilder.new(query).build
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@ class PostAppeal < ApplicationRecord
|
||||
|
||||
module SearchMethods
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def resolved
|
||||
|
||||
@@ -35,7 +35,7 @@ class PostApproval < ApplicationRecord
|
||||
concerning :SearchMethods do
|
||||
class_methods do
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def search(params)
|
||||
|
||||
@@ -33,7 +33,7 @@ class PostFlag < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def resolved
|
||||
|
||||
@@ -21,7 +21,7 @@ class PostReplacement < ApplicationRecord
|
||||
concerning :Search do
|
||||
class_methods do
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def search(params = {})
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
class Tag < ApplicationRecord
|
||||
COSINE_SIMILARITY_RELATED_TAG_THRESHOLD = 300
|
||||
COUNT_METATAGS = %w[
|
||||
comment_count deleted_comment_count active_comment_count
|
||||
note_count deleted_note_count active_note_count
|
||||
flag_count resolved_flag_count unresolved_flag_count
|
||||
child_count deleted_child_count active_child_count
|
||||
pool_count deleted_pool_count active_pool_count series_pool_count collection_pool_count
|
||||
appeal_count approval_count replacement_count
|
||||
]
|
||||
|
||||
# allow e.g. `deleted_comments` as a synonym for `deleted_comment_count`
|
||||
COUNT_METATAG_SYNONYMS = COUNT_METATAGS.map { |str| str.delete_suffix("_count").pluralize }
|
||||
|
||||
METATAGS = %w[
|
||||
-user user -approver approver commenter comm noter noteupdater artcomm
|
||||
-pool pool ordpool -favgroup favgroup -fav fav ordfav md5 -rating rating
|
||||
@@ -7,10 +19,32 @@ class Tag < ApplicationRecord
|
||||
-source id -id date age order limit -status status tagcount parent -parent
|
||||
child pixiv_id pixiv search upvote downvote filetype -filetype flagger
|
||||
-flagger appealer -appealer disapproval -disapproval
|
||||
] + TagCategory.short_name_list.map {|x| "#{x}tags"}
|
||||
] + TagCategory.short_name_list.map {|x| "#{x}tags"} + COUNT_METATAGS + COUNT_METATAG_SYNONYMS
|
||||
|
||||
SUBQUERY_METATAGS = %w[commenter comm noter noteupdater artcomm flagger -flagger appealer -appealer]
|
||||
|
||||
ORDER_METATAGS = %w[
|
||||
id id_desc
|
||||
score score_asc
|
||||
favcount favcount_asc
|
||||
created_at created_at_asc
|
||||
change change_asc
|
||||
comment comment_asc
|
||||
comment_bumped comment_bumped_asc
|
||||
note note_asc
|
||||
artcomm artcomm_asc
|
||||
mpixels mpixels_asc
|
||||
portrait landscape
|
||||
filesize filesize_asc
|
||||
tagcount tagcount_asc
|
||||
rank
|
||||
random
|
||||
custom
|
||||
] +
|
||||
COUNT_METATAGS +
|
||||
COUNT_METATAG_SYNONYMS.flat_map { |str| [str, "#{str}_asc"] } +
|
||||
TagCategory.short_name_list.flat_map { |str| ["#{str}tags", "#{str}tags_asc"] }
|
||||
|
||||
has_one :wiki_page, :foreign_key => "title", :primary_key => "name"
|
||||
has_one :artist, :foreign_key => "name", :primary_key => "name"
|
||||
has_one :antecedent_alias, -> {active}, :class_name => "TagAlias", :foreign_key => "antecedent_name", :primary_key => "name"
|
||||
@@ -757,7 +791,14 @@ class Tag < ApplicationRecord
|
||||
q[:child] = g2.downcase
|
||||
|
||||
when "order"
|
||||
q[:order] = g2.downcase
|
||||
g2 = g2.downcase
|
||||
|
||||
order, suffix, _ = g2.partition(/_(asc|desc)\z/i)
|
||||
if order.in?(COUNT_METATAG_SYNONYMS)
|
||||
g2 = order.singularize + "_count" + suffix
|
||||
end
|
||||
|
||||
q[:order] = g2
|
||||
|
||||
when "limit"
|
||||
# Do nothing. The controller takes care of it.
|
||||
@@ -791,6 +832,13 @@ class Tag < ApplicationRecord
|
||||
q[:downvote] = User.name_to_id(g2)
|
||||
end
|
||||
|
||||
when *COUNT_METATAGS
|
||||
q[g1.to_sym] = parse_helper(g2)
|
||||
|
||||
when *COUNT_METATAG_SYNONYMS
|
||||
g1 = "#{g1.singularize}_count"
|
||||
q[g1.to_sym] = parse_helper(g2)
|
||||
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
@@ -175,7 +175,7 @@ class Upload < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def search(params)
|
||||
|
||||
@@ -2105,6 +2105,20 @@ class PostTest < ActiveSupport::TestCase
|
||||
assert_tag_match([posts[1]], "noter:none")
|
||||
end
|
||||
|
||||
should "return posts for the note_count:<N> metatag" do
|
||||
posts = FactoryBot.create_list(:post, 3)
|
||||
FactoryBot.create(:note, post: posts[0], is_active: true)
|
||||
FactoryBot.create(:note, post: posts[1], is_active: false)
|
||||
|
||||
assert_tag_match([posts[1], posts[0]], "note_count:1")
|
||||
assert_tag_match([posts[0]], "active_note_count:1")
|
||||
assert_tag_match([posts[1]], "deleted_note_count:1")
|
||||
|
||||
assert_tag_match([posts[1], posts[0]], "notes:1")
|
||||
assert_tag_match([posts[0]], "active_notes:1")
|
||||
assert_tag_match([posts[1]], "deleted_notes:1")
|
||||
end
|
||||
|
||||
should "return posts for the artcomm:<name> metatag" do
|
||||
users = FactoryBot.create_list(:user, 2)
|
||||
posts = FactoryBot.create_list(:post, 2)
|
||||
@@ -2389,6 +2403,8 @@ class PostTest < ActiveSupport::TestCase
|
||||
p
|
||||
end
|
||||
|
||||
FactoryBot.create(:note, post: posts.second)
|
||||
|
||||
assert_tag_match(posts.reverse, "order:id_desc")
|
||||
assert_tag_match(posts.reverse, "order:score")
|
||||
assert_tag_match(posts.reverse, "order:favcount")
|
||||
@@ -2406,6 +2422,10 @@ class PostTest < ActiveSupport::TestCase
|
||||
assert_tag_match(posts.reverse, "order:chartags")
|
||||
assert_tag_match(posts.reverse, "order:copytags")
|
||||
assert_tag_match(posts.reverse, "order:rank")
|
||||
assert_tag_match(posts.reverse, "order:note_count")
|
||||
assert_tag_match(posts.reverse, "order:note_count_desc")
|
||||
assert_tag_match(posts.reverse, "order:notes")
|
||||
assert_tag_match(posts.reverse, "order:notes_desc")
|
||||
|
||||
assert_tag_match(posts, "order:id_asc")
|
||||
assert_tag_match(posts, "order:score_asc")
|
||||
@@ -2423,6 +2443,8 @@ class PostTest < ActiveSupport::TestCase
|
||||
assert_tag_match(posts, "order:arttags_asc")
|
||||
assert_tag_match(posts, "order:chartags_asc")
|
||||
assert_tag_match(posts, "order:copytags_asc")
|
||||
assert_tag_match(posts, "order:note_count_asc")
|
||||
assert_tag_match(posts, "order:notes_asc")
|
||||
end
|
||||
|
||||
should "return posts for order:comment_bumped" do
|
||||
|
||||
Reference in New Issue
Block a user