Add ability to mark tags as deprecated

* Deprecated tags can't be added to posts, but existing deprecated tags
  in a post won't be removed
* Only empty tags can be marked as deprecated manually
* No tags can be manually undeprecated
** These limits don't apply to admins
* Deprecating or undeprecating a tag will create a new mod action to
  prevent people from going rogue
* Added deprecate/undeprecate commands for BURs
* Deprecating a tag via BUR removes all implications to and from it as well
This commit is contained in:
nonamethanks
2022-04-08 00:51:53 +02:00
parent 98a9b2484b
commit ea76a889db
16 changed files with 194 additions and 5 deletions

View File

@@ -60,6 +60,8 @@ class ModAction < ApplicationRecord
tag_implication_create: 140,
tag_implication_update: 141, # XXX unused
tag_implication_delete: 142,
tag_deprecate: 240,
tag_undeprecate: 242,
ip_ban_create: 160,
ip_ban_delete: 162,
ip_ban_undelete: 163,

View File

@@ -411,6 +411,7 @@ class Post < ApplicationRecord
normalized_tags = Tag.convert_cosplay_tags(normalized_tags)
normalized_tags += Tag.create_for_list(Tag.automatic_tags_for(normalized_tags))
normalized_tags += TagImplication.tags_implied_by(normalized_tags).map(&:name)
normalized_tags -= added_deprecated_tags
normalized_tags = normalized_tags.compact.uniq.sort
normalized_tags = Tag.create_for_list(normalized_tags)
self.tag_string = normalized_tags.join(" ")
@@ -428,6 +429,16 @@ class Post < ApplicationRecord
tag_names - invalid_tags.map(&:name)
end
def added_deprecated_tags
added_deprecated_tags = added_tags.select(&:is_deprecated)
if added_deprecated_tags.present?
added_deprecated_tags_list = added_deprecated_tags.map { |t| "[[#{t.name}]]" }.to_sentence
warnings.add(:base, "The following tags are deprecated and could not be added: #{added_deprecated_tags_list}")
end
added_deprecated_tags.pluck(:name)
end
def remove_negated_tags(tags)
@negated_tags, tags = tags.partition {|x| x =~ /\A-/i}
@negated_tags = @negated_tags.map {|x| x[1..-1]}

View File

@@ -16,6 +16,7 @@ class Tag < ApplicationRecord
validates :category, inclusion: { in: TagCategory.category_ids }
before_create :create_character_tag_for_cosplay_tag, if: :is_cosplay_tag?
before_save :create_mod_action
after_save :update_category_cache, if: :saved_change_to_category?
after_save :update_category_post_counts, if: :saved_change_to_category?
@@ -217,6 +218,17 @@ class Tag < ApplicationRecord
end
end
concerning :DeprecationMethods do
def create_mod_action
return if CurrentUser.user == User.system
if is_deprecated_was == true and is_deprecated == false
ModAction.log("marked the tag [[#{name}]] as not deprecated", :tag_undeprecate)
elsif is_deprecated_was == false and is_deprecated == true
ModAction.log("marked the tag [[#{name}]] as deprecated", :tag_deprecate)
end
end
end
module SearchMethods
def autocorrect_matches(name)
fuzzy_name_matches(name).order_similarity(name)
@@ -265,7 +277,7 @@ class Tag < ApplicationRecord
end
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :category, :post_count, :name, :wiki_page, :artist, :antecedent_alias, :consequent_aliases, :antecedent_implications, :consequent_implications, :dtext_links)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deprecated, :category, :post_count, :name, :wiki_page, :artist, :antecedent_alias, :consequent_aliases, :antecedent_implications, :consequent_implications, :dtext_links)
if params[:fuzzy_name_matches].present?
q = q.fuzzy_name_matches(params[:fuzzy_name_matches])