diff --git a/app/logical/related_tag_query.rb b/app/logical/related_tag_query.rb index c5479cb77..bd78d13f2 100644 --- a/app/logical/related_tag_query.rb +++ b/app/logical/related_tag_query.rb @@ -58,7 +58,11 @@ class RelatedTagQuery def ai_tags return AITag.none if media_asset.nil? - media_asset.ai_tags.joins(:tag).undeprecated.nonempty.in_order_of(:"tags.category", TagCategory.canonical_mapping.values).order("ai_tags.score DESC, tags.name ASC").take(limit) + + tags = media_asset.ai_tags.includes(:tag, :aliased_tag) + tags = tags.reject(&:is_deprecated?).reject { |t| t.empty? && !t.metatag? } + tags = tags.sort_by { |t| [TagCategory.canonical_mapping.keys.index(t.category_name), -t.score, t.name] } + tags.take(limit) end # Returns the top 20 most frequently added tags within the last 20 edits made by the user in the last hour. diff --git a/app/models/ai_tag.rb b/app/models/ai_tag.rb index c18222db8..467383e8e 100644 --- a/app/models/ai_tag.rb +++ b/app/models/ai_tag.rb @@ -6,6 +6,7 @@ class AITag < ApplicationRecord belongs_to :tag belongs_to :media_asset has_one :post, through: :media_asset + has_one :aliased_tag, through: :tag validates :score, inclusion: { in: (0..100) } @@ -14,7 +15,7 @@ class AITag < ApplicationRecord scope :empty, -> { where(tag: Tag.empty) } scope :nonempty, -> { where(tag: Tag.nonempty) } - delegate :name, :pretty_name, :post_count, :category, :category_name, to: :tag + delegate :name, :pretty_name, :post_count, :category, :category_name, :is_deprecated?, :empty?, :is_aliased?, :metatag?, to: :tag def self.named(name) name = $1.downcase if name =~ /\A(rating:.)/i diff --git a/app/models/tag.rb b/app/models/tag.rb index ad642511c..0a6c0597c 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -9,6 +9,7 @@ class Tag < ApplicationRecord 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" + has_one :aliased_tag, through: :antecedent_alias, source: :consequent_tag has_many :consequent_aliases, -> {active}, :class_name => "TagAlias", :foreign_key => "consequent_name", :primary_key => "name" has_many :antecedent_implications, -> {active}, :class_name => "TagImplication", :foreign_key => "antecedent_name", :primary_key => "name" has_many :consequent_implications, -> {active}, :class_name => "TagImplication", :foreign_key => "consequent_name", :primary_key => "name" @@ -405,6 +406,14 @@ class Tag < ApplicationRecord end end + def is_aliased? + aliased_tag.present? + end + + def metatag? + name.match?(/\A#{PostQueryBuilder::METATAGS.join("|")}:/i) + end + def self.model_restriction(table) super.where(table[:post_count].gt(0)) end diff --git a/app/views/related_tags/_ai_tags_column.html.erb b/app/views/related_tags/_ai_tags_column.html.erb index 6c54eda35..dbd88cb3e 100644 --- a/app/views/related_tags/_ai_tags_column.html.erb +++ b/app/views/related_tags/_ai_tags_column.html.erb @@ -5,12 +5,14 @@