Revert "DEV: Removal of create_post_for_category_and_tag_changes setting (#28…" (#28587)

This reverts commit fc33826dc5.
This commit is contained in:
Gabriel Grubba
2024-08-27 16:19:51 -03:00
committed by GitHub
parent ade898a7ca
commit c8f5445030
5 changed files with 167 additions and 6 deletions

View File

@@ -79,14 +79,15 @@ class PostRevisor
track_and_revise topic_changes, :archetype, attribute
end
track_topic_field(:category_id) do |tc, category_id, fields|
track_topic_field(:category_id) do |tc, new_category_id, fields|
current_category = tc.topic.category
new_category = (category_id.nil? || category_id.zero?) ? nil : Category.find(category_id)
new_category =
(new_category_id.nil? || new_category_id.zero?) ? nil : Category.find(new_category_id)
if new_category.nil? && tc.topic.private_message?
tc.record_change("category_id", current_category.id, nil)
tc.topic.category_id = nil
elsif new_category.nil? || tc.guardian.can_move_topic_to_category?(category_id)
elsif new_category.nil? || tc.guardian.can_move_topic_to_category?(new_category_id)
tags = fields[:tags] || tc.topic.tags.map(&:name)
if new_category &&
!DiscourseTagging.validate_category_tags(tc.guardian, tc.topic, new_category, tags)
@@ -94,8 +95,14 @@ class PostRevisor
next
end
tc.record_change("category_id", tc.topic.category_id, category_id)
tc.check_result(tc.topic.change_category_to_id(category_id))
tc.record_change("category_id", current_category&.id, new_category&.id)
tc.check_result(tc.topic.change_category_to_id(new_category_id))
create_small_action_for_category_change(
topic: tc.topic,
user: tc.user,
old_category: current_category,
new_category: new_category,
)
end
end
@@ -112,14 +119,25 @@ class PostRevisor
DB.after_commit do
post = tc.topic.ordered_posts.first
notified_user_ids = [post.user_id, post.last_editor_id].uniq
added_tags = tags - prev_tags
removed_tags = prev_tags - tags
if !SiteSetting.disable_tags_edit_notifications
Jobs.enqueue(
:notify_tag_change,
post_id: post.id,
notified_user_ids: notified_user_ids,
diff_tags: ((tags - prev_tags) | (prev_tags - tags)),
diff_tags: (added_tags | removed_tags),
)
end
create_small_action_for_tag_changes(
topic: tc.topic,
user: tc.user,
added_tags: added_tags,
removed_tags: removed_tags,
)
end
end
end
@@ -135,6 +153,56 @@ class PostRevisor
end
end
def self.create_small_action_for_category_change(topic:, user:, old_category:, new_category:)
if !old_category || !new_category || !SiteSetting.create_post_for_category_and_tag_changes
return
end
topic.add_moderator_post(
user,
I18n.t(
"topic_category_changed",
from: "##{old_category.slug_ref}",
to: "##{new_category.slug_ref}",
),
post_type: Post.types[:small_action],
action_code: "category_changed",
)
end
def self.create_small_action_for_tag_changes(topic:, user:, added_tags:, removed_tags:)
return if !SiteSetting.create_post_for_category_and_tag_changes
topic.add_moderator_post(
user,
tags_changed_raw(added: added_tags, removed: removed_tags),
post_type: Post.types[:small_action],
action_code: "tags_changed",
custom_fields: {
tags_added: added_tags,
tags_removed: removed_tags,
},
)
end
def self.tags_changed_raw(added:, removed:)
if removed.present? && added.present?
I18n.t(
"topic_tag_changed.added_and_removed",
added: tag_list_to_raw(added),
removed: tag_list_to_raw(removed),
)
elsif added.present?
I18n.t("topic_tag_changed.added", added: tag_list_to_raw(added))
elsif removed.present?
I18n.t("topic_tag_changed.removed", removed: tag_list_to_raw(removed))
end
end
def self.tag_list_to_raw(tag_list)
tag_list.sort.map { |tag_name| "##{tag_name}" }.join(", ")
end
# AVAILABLE OPTIONS:
# - revised_at: changes the date of the revision
# - force_new_version: bypass grace period edit window