2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
require "edit_rate_limiter"
|
2023-01-09 06:10:19 -06:00
|
|
|
require "post_locker"
|
2013-12-11 20:41:34 -06:00
|
|
|
|
2013-02-09 09:33:07 -06:00
|
|
|
class PostRevisor
|
2015-01-27 11:13:45 -06:00
|
|
|
# Helps us track changes to a topic.
|
|
|
|
#
|
2015-01-28 10:01:01 -06:00
|
|
|
# It's passed to `track_topic_fields` callbacks so they can record if they
|
2015-01-27 11:13:45 -06:00
|
|
|
# changed a value or not. This is needed for things like custom fields.
|
|
|
|
class TopicChanges
|
|
|
|
attr_reader :topic, :user
|
|
|
|
|
|
|
|
def initialize(topic, user)
|
|
|
|
@topic = topic
|
|
|
|
@user = user
|
|
|
|
@changed = {}
|
|
|
|
@errored = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def errored?
|
|
|
|
@errored
|
|
|
|
end
|
|
|
|
|
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(@user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def record_change(field_name, previous_val, new_val)
|
|
|
|
return if previous_val == new_val
|
|
|
|
diff[field_name] = [previous_val, new_val]
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_result(res)
|
|
|
|
@errored = true if !res
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff
|
|
|
|
@diff ||= {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
POST_TRACKED_FIELDS = %w[raw cooked edit_reason user_id wiki post_type]
|
2014-10-27 16:06:43 -05:00
|
|
|
|
2020-05-11 03:32:41 -05:00
|
|
|
attr_reader :category_changed, :post_revision
|
2013-02-21 17:09:56 -06:00
|
|
|
|
2020-04-20 20:50:20 -05:00
|
|
|
def initialize(post, topic = post.topic)
|
2013-02-09 09:33:07 -06:00
|
|
|
@post = post
|
2020-04-20 20:50:20 -05:00
|
|
|
@topic = topic
|
|
|
|
|
|
|
|
# Make sure we have only one Topic instance
|
|
|
|
post.topic = topic
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
|
|
|
|
2015-01-28 10:01:01 -06:00
|
|
|
def self.tracked_topic_fields
|
|
|
|
@@tracked_topic_fields ||= {}
|
|
|
|
@@tracked_topic_fields
|
2015-01-27 11:13:45 -06:00
|
|
|
end
|
|
|
|
|
2015-01-28 10:01:01 -06:00
|
|
|
def self.track_topic_field(field, &block)
|
|
|
|
tracked_topic_fields[field] = block
|
|
|
|
|
|
|
|
# Define it in the serializer unless it already has been defined
|
2024-05-27 05:27:13 -05:00
|
|
|
if PostRevisionSerializer.instance_methods(false).exclude?("#{field}_changes".to_sym)
|
2015-01-28 10:01:01 -06:00
|
|
|
PostRevisionSerializer.add_compared_field(field)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-05 04:38:39 -05:00
|
|
|
def self.track_and_revise(topic_changes, field, attribute)
|
2023-01-09 06:10:19 -06:00
|
|
|
topic_changes.record_change(field, topic_changes.topic.public_send(field), attribute)
|
2021-08-05 04:38:39 -05:00
|
|
|
topic_changes.topic.public_send("#{field}=", attribute)
|
|
|
|
end
|
|
|
|
|
|
|
|
track_topic_field(:title) do |topic_changes, attribute|
|
2021-08-06 11:07:42 -05:00
|
|
|
track_and_revise topic_changes, :title, attribute
|
2015-01-28 10:01:01 -06:00
|
|
|
end
|
|
|
|
|
2021-08-05 04:38:39 -05:00
|
|
|
track_topic_field(:archetype) do |topic_changes, attribute|
|
|
|
|
track_and_revise topic_changes, :archetype, attribute
|
|
|
|
end
|
|
|
|
|
2024-08-27 14:19:51 -05:00
|
|
|
track_topic_field(:category_id) do |tc, new_category_id, fields|
|
2023-04-05 13:31:31 -05:00
|
|
|
current_category = tc.topic.category
|
2024-08-27 14:19:51 -05:00
|
|
|
new_category =
|
|
|
|
(new_category_id.nil? || new_category_id.zero?) ? nil : Category.find(new_category_id)
|
2023-04-05 13:31:31 -05:00
|
|
|
|
|
|
|
if new_category.nil? && tc.topic.private_message?
|
|
|
|
tc.record_change("category_id", current_category.id, nil)
|
2019-04-16 02:16:23 -05:00
|
|
|
tc.topic.category_id = nil
|
2024-08-27 14:19:51 -05:00
|
|
|
elsif new_category.nil? || tc.guardian.can_move_topic_to_category?(new_category_id)
|
2021-03-02 17:59:23 -06:00
|
|
|
tags = fields[:tags] || tc.topic.tags.map(&:name)
|
2023-04-05 13:31:31 -05:00
|
|
|
if new_category &&
|
FIX: Miscellaneous tagging errors (#21490)
* FIX: Displaying the wrong number of minimum tags in the composer
When the minimum number of tags set for the category is larger than the minimum number of tags
set in the category tag-groups, the composer was displaying the wrong value.
This commit fixes the value displayed in the composer to show the max value between the required
for the category and the tag-groups set for the category.
This bug was reported on Meta in https://meta.discourse.org/t/tags-from-multiple-tag-groups-required-only-suggest-select-at-least-one-tag/263817
* FIX: Limiting tags in categories not working as expected
When a category was restricted to a tag group A, which was set to only allow
one tag from the group per topic, selecting a tag belonging only to A returned
other tags from A that also belonged to other group/s (if any).
Example:
Tag group A: alpha, beta, gamma, epsilon, delta
Tag group B: alpha, beta, gamma
Both tag groups set to only allow one tag from the group per topic.
If Category 1 was set to only allow tags from the tag group A, and the first tag
selected was epsilon, then, because they also belonged to tag group B, the tags
alpha, beta, and gamma were still returned as valid options when they should not be.
This commit ensures that once a tag from a tag group that restricts its tags to
one per topic is selected, no other tag from this group is returned.
This bug was reported on Meta in https://meta.discourse.org/t/limiting-tags-to-categories-not-working-as-expected/263143.
* FIX: Moving topics does not prompt to add required tag for new category
When a topic moved from a category to another, the tag requirements
of the new category were not being checked.
This allowed a topic to be created and moved to a category:
- that limited the tags to a tag group, with the topic containing tags
not allowed.
- that required N tags from a tag group, with the topic not containing
the required tags.
This bug was reported on Meta in https://meta.discourse.org/t/moving-tagged-topics-does-not-prompt-to-add-required-tag-for-new-category/264138.
* FIX: Editing topics with tag groups from parents allows incorrect tagging
When there was a combination between parent tags defined in a tag group
set to allow only one tag from the group per topic, and other tag groups
relying on this restriction to combine the children tag types with the
parent tag, editing a topic could allow the user to insert an invalid
combination of these tags.
Example:
Automakers tag group: landhover, toyota
- group set to limit one tag from the group per topic
Toyota models group: land-cruiser, hilux, corolla
Landhover models group: evoque, defender, discovery
If a topic was initially set up with the tags toyota, land-cruiser it was
possible to edit it by removing the tag toyota and adding the tag landhover
and other landhover model tags like evoque for example.
In this case, the topic would end up with the tags toyota, land-cruiser,
landhover, evoque because Discourse will automatically insert the
missing parent tag toyota when it detects the tag land-cruiser.
This combination of tags would violate the restriction specified in
the Automakers tag group resulting in an invalid combination of tags.
This commit enforces that the "one tag from the group per topic"
restriction is verified before updating the topic tags and also
make sure the verification checks the compatibility of parent tags that
would be automatically inserted.
After the changes, the user will receive an error similar to:
The tags land-cruiser, landhover cannot be used simultaneously.
Please include only one of them.
2023-05-15 15:19:41 -05:00
|
|
|
!DiscourseTagging.validate_category_tags(tc.guardian, tc.topic, new_category, tags)
|
2021-03-02 17:59:23 -06:00
|
|
|
tc.check_result(false)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2024-08-27 14:19:51 -05:00
|
|
|
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,
|
|
|
|
)
|
2018-03-01 19:13:04 -06:00
|
|
|
end
|
2015-01-27 11:13:45 -06:00
|
|
|
end
|
|
|
|
|
2016-04-25 14:55:15 -05:00
|
|
|
track_topic_field(:tags) do |tc, tags|
|
2016-05-04 13:02:47 -05:00
|
|
|
if tc.guardian.can_tag_topics?
|
|
|
|
prev_tags = tc.topic.tags.map(&:name)
|
|
|
|
next if tags.blank? && prev_tags.blank?
|
|
|
|
if !DiscourseTagging.tag_topic_by_names(tc.topic, tc.guardian, tags)
|
|
|
|
tc.check_result(false)
|
|
|
|
next
|
2016-04-25 14:55:15 -05:00
|
|
|
end
|
2019-03-12 12:09:34 -05:00
|
|
|
if prev_tags.sort != tags.sort
|
2023-01-09 06:10:19 -06:00
|
|
|
tc.record_change("tags", prev_tags, tags)
|
2019-03-12 12:09:34 -05:00
|
|
|
DB.after_commit do
|
|
|
|
post = tc.topic.ordered_posts.first
|
2019-11-06 15:20:15 -06:00
|
|
|
notified_user_ids = [post.user_id, post.last_editor_id].uniq
|
2024-08-27 14:19:51 -05:00
|
|
|
|
|
|
|
added_tags = tags - prev_tags
|
|
|
|
removed_tags = prev_tags - tags
|
|
|
|
|
2022-07-15 07:36:27 -05:00
|
|
|
if !SiteSetting.disable_tags_edit_notifications
|
|
|
|
Jobs.enqueue(
|
|
|
|
:notify_tag_change,
|
|
|
|
post_id: post.id,
|
|
|
|
notified_user_ids: notified_user_ids,
|
2024-08-27 14:19:51 -05:00
|
|
|
diff_tags: (added_tags | removed_tags),
|
2022-07-15 07:36:27 -05:00
|
|
|
)
|
|
|
|
end
|
2024-08-27 14:19:51 -05:00
|
|
|
|
|
|
|
create_small_action_for_tag_changes(
|
|
|
|
topic: tc.topic,
|
|
|
|
user: tc.user,
|
|
|
|
added_tags: added_tags,
|
|
|
|
removed_tags: removed_tags,
|
|
|
|
)
|
2019-03-12 12:09:34 -05:00
|
|
|
end
|
|
|
|
end
|
2016-04-25 14:55:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-05 06:31:43 -06:00
|
|
|
track_topic_field(:featured_link) do |topic_changes, featured_link|
|
2021-08-05 04:38:39 -05:00
|
|
|
if !SiteSetting.topic_featured_link_enabled ||
|
2023-01-09 06:10:19 -06:00
|
|
|
!topic_changes.guardian.can_edit_featured_link?(topic_changes.topic.category_id)
|
2021-08-05 04:38:39 -05:00
|
|
|
topic_changes.check_result(false)
|
|
|
|
else
|
2023-01-09 06:10:19 -06:00
|
|
|
topic_changes.record_change("featured_link", topic_changes.topic.featured_link, featured_link)
|
2016-12-05 06:31:43 -06:00
|
|
|
topic_changes.topic.featured_link = featured_link
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-08-27 14:19:51 -05:00
|
|
|
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
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
# AVAILABLE OPTIONS:
|
|
|
|
# - revised_at: changes the date of the revision
|
2021-06-30 20:27:11 -05:00
|
|
|
# - force_new_version: bypass grace period edit window
|
2014-10-27 16:06:43 -05:00
|
|
|
# - bypass_rate_limiter:
|
|
|
|
# - bypass_bump: do not bump the topic, even if last post
|
|
|
|
# - skip_validations: ask ActiveRecord to skip validations
|
2016-08-19 12:13:22 -05:00
|
|
|
# - skip_revision: do not create a new PostRevision record
|
2019-09-12 09:55:45 -05:00
|
|
|
# - skip_staff_log: skip creating an entry in the staff action log
|
2017-07-27 20:20:09 -05:00
|
|
|
def revise!(editor, fields, opts = {})
|
2014-08-11 17:01:58 -05:00
|
|
|
@editor = editor
|
2014-10-27 16:06:43 -05:00
|
|
|
@fields = fields.with_indifferent_access
|
2014-08-11 17:01:58 -05:00
|
|
|
@opts = opts
|
2014-10-27 16:06:43 -05:00
|
|
|
|
2015-01-27 11:13:45 -06:00
|
|
|
@topic_changes = TopicChanges.new(@topic, editor)
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
# some normalization
|
|
|
|
@fields[:raw] = cleanup_whitespaces(@fields[:raw]) if @fields.has_key?(:raw)
|
|
|
|
@fields[:user_id] = @fields[:user_id].to_i if @fields.has_key?(:user_id)
|
|
|
|
@fields[:category_id] = @fields[:category_id].to_i if @fields.has_key?(:category_id)
|
|
|
|
|
2019-11-17 21:08:54 -06:00
|
|
|
# always reset edit_reason unless provided, do not set to nil else
|
|
|
|
# previous reasons are lost
|
|
|
|
@fields.delete(:edit_reason) if @fields[:edit_reason].blank?
|
2014-08-11 17:01:58 -05:00
|
|
|
|
2021-03-24 10:22:16 -05:00
|
|
|
Post.plugin_permitted_update_params.each do |field, val|
|
2023-01-09 06:10:19 -06:00
|
|
|
val[:handler].call(@post, @fields[field]) if @fields.key?(field) && val[:plugin].enabled?
|
2021-03-24 10:22:16 -05:00
|
|
|
end
|
|
|
|
|
2022-02-23 01:39:54 -06:00
|
|
|
if !should_revise?
|
|
|
|
# the draft sequence is advanced here to handle the edge case where a
|
|
|
|
# user opens the composer to edit a post and makes some changes (which
|
|
|
|
# saves a draft), but then un-does the changes and clicks save. In this
|
|
|
|
# case, should_revise? returns false because nothing has really changed
|
|
|
|
# in the post, but we want to get rid of the draft so we advance the
|
|
|
|
# sequence.
|
|
|
|
advance_draft_sequence if !opts[:keep_existing_draft]
|
|
|
|
return false
|
|
|
|
end
|
2014-08-28 23:27:40 -05:00
|
|
|
|
2014-09-03 19:43:57 -05:00
|
|
|
@post.acting_user = @editor
|
2014-10-27 16:06:43 -05:00
|
|
|
@topic.acting_user = @editor
|
|
|
|
@revised_at = @opts[:revised_at] || Time.now
|
|
|
|
@last_version_at = @post.last_version_at || Time.now
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
if guardian.affected_by_slow_mode?(@topic) && !grace_period_edit? &&
|
|
|
|
SiteSetting.slow_mode_prevents_editing
|
2020-10-28 14:47:50 -05:00
|
|
|
@post.errors.add(:base, I18n.t("cannot_edit_on_slow_mode"))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
@version_changed = false
|
|
|
|
@post_successfully_saved = true
|
2014-09-03 19:43:57 -05:00
|
|
|
|
2014-11-25 09:53:26 -06:00
|
|
|
@validate_post = true
|
|
|
|
@validate_post = @opts[:validate_post] if @opts.has_key?(:validate_post)
|
|
|
|
@validate_post = !@opts[:skip_validations] if @opts.has_key?(:skip_validations)
|
|
|
|
|
|
|
|
@validate_topic = true
|
|
|
|
@validate_topic = @opts[:validate_topic] if @opts.has_key?(:validate_topic)
|
2021-05-23 18:10:22 -05:00
|
|
|
@validate_topic = !@opts[:skip_validations] if @opts.has_key?(:skip_validations)
|
2014-11-17 09:06:43 -06:00
|
|
|
|
2016-08-19 12:13:22 -05:00
|
|
|
@skip_revision = false
|
|
|
|
@skip_revision = @opts[:skip_revision] if @opts.has_key?(:skip_revision)
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
@post.incoming_email&.update(imap_sync: true) if @post.incoming_email&.imap_uid
|
2020-07-10 04:05:55 -05:00
|
|
|
|
2018-03-14 14:01:36 -05:00
|
|
|
old_raw = @post.raw
|
|
|
|
|
2014-08-28 23:27:40 -05:00
|
|
|
Post.transaction do
|
|
|
|
revise_post
|
2014-09-03 19:43:57 -05:00
|
|
|
|
2016-08-10 00:55:51 -05:00
|
|
|
yield if block_given?
|
2014-10-27 16:06:43 -05:00
|
|
|
# TODO: these callbacks are being called in a transaction
|
|
|
|
# it is kind of odd, because the callback is called "before_edit"
|
|
|
|
# but the post is already edited at this point
|
|
|
|
# Trouble is that much of the logic of should I edit? is deeper
|
|
|
|
# down so yanking this in front of the transaction will lead to
|
|
|
|
# false positive.
|
2014-08-28 23:27:40 -05:00
|
|
|
plugin_callbacks
|
2014-09-03 19:43:57 -05:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
revise_topic
|
2022-02-23 01:39:54 -06:00
|
|
|
advance_draft_sequence if !opts[:keep_existing_draft]
|
2014-08-28 23:27:40 -05:00
|
|
|
end
|
|
|
|
|
2024-05-17 10:05:49 -05:00
|
|
|
# bail out if the post or topic failed to save
|
|
|
|
return false if !successfully_saved_post_and_topic
|
|
|
|
|
2018-01-26 12:45:52 -06:00
|
|
|
# Lock the post by default if the appropriate setting is true
|
2018-03-01 19:33:04 -06:00
|
|
|
if (
|
2023-01-09 06:10:19 -06:00
|
|
|
SiteSetting.staff_edit_locks_post? && !@post.wiki? && @fields.has_key?("raw") &&
|
|
|
|
@editor.staff? && @editor != Discourse.system_user && !@post.user&.staff?
|
|
|
|
)
|
2018-01-26 12:45:52 -06:00
|
|
|
PostLocker.new(@post, @editor).lock
|
|
|
|
end
|
|
|
|
|
2020-07-23 08:50:00 -05:00
|
|
|
# We log staff/group moderator edits to posts
|
|
|
|
if (
|
2023-01-09 06:10:19 -06:00
|
|
|
(
|
|
|
|
@editor.staff? ||
|
|
|
|
(
|
|
|
|
@post.is_category_description? &&
|
|
|
|
guardian.can_edit_category_description?(@post.topic.category)
|
|
|
|
)
|
|
|
|
) && @editor.id != @post.user_id && @fields.has_key?("raw") && !@opts[:skip_staff_log]
|
|
|
|
)
|
|
|
|
StaffActionLogger.new(@editor).log_post_edit(@post, old_raw: old_raw)
|
2018-03-12 12:49:52 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
# WARNING: do not pull this into the transaction
|
|
|
|
# it can fire events in sidekiq before the post is done saving
|
|
|
|
# leading to corrupt state
|
2018-05-25 04:31:45 -05:00
|
|
|
QuotedPost.extract_from(@post)
|
2024-05-17 10:05:49 -05:00
|
|
|
TopicLink.extract_from(@post)
|
2021-05-20 22:32:32 -05:00
|
|
|
|
2024-05-17 10:05:49 -05:00
|
|
|
Topic.reset_highest(@topic.id)
|
2014-10-27 16:06:43 -05:00
|
|
|
|
2024-05-17 10:05:49 -05:00
|
|
|
post_process_post
|
2014-10-27 16:06:43 -05:00
|
|
|
alert_users
|
|
|
|
publish_changes
|
|
|
|
grant_badge
|
2014-09-03 19:43:57 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
ReviewablePost.queue_for_review_if_possible(@post, @editor) if should_create_new_version?
|
2021-04-21 06:41:36 -05:00
|
|
|
|
2014-11-03 08:31:11 -06:00
|
|
|
successfully_saved_post_and_topic
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
2014-09-03 19:43:57 -05:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def cleanup_whitespaces(raw)
|
2023-01-04 16:09:17 -06:00
|
|
|
raw.present? ? TextCleaner.normalize_whitespaces(raw).rstrip : ""
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
2013-12-11 20:41:34 -06:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def should_revise?
|
|
|
|
post_changed? || topic_changed?
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def post_changed?
|
|
|
|
POST_TRACKED_FIELDS.each do |field|
|
2023-01-09 06:10:19 -06:00
|
|
|
return true if @fields.has_key?(field) && @fields[field] != @post.public_send(field)
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
2013-02-09 09:33:07 -06:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def topic_changed?
|
2017-07-27 20:20:09 -05:00
|
|
|
PostRevisor.tracked_topic_fields.keys.any? { |f| @fields.has_key?(f) }
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def revise_post
|
2018-03-06 23:44:21 -06:00
|
|
|
if should_create_new_version?
|
|
|
|
revise_and_create_new_version
|
|
|
|
else
|
|
|
|
unless cached_original_raw
|
|
|
|
self.original_raw = @post.raw
|
|
|
|
self.original_cooked = @post.cooked
|
|
|
|
end
|
|
|
|
revise
|
|
|
|
end
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def should_create_new_version?
|
2016-08-19 12:13:22 -05:00
|
|
|
return false if @skip_revision
|
2023-05-03 21:22:07 -05:00
|
|
|
edited_by_another_user? || flagged? || !grace_period_edit? || owner_changed? ||
|
|
|
|
force_new_version? || edit_reason_specified?
|
2019-11-17 21:08:54 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit_reason_specified?
|
|
|
|
@fields[:edit_reason].present? && @fields[:edit_reason] != @post.edit_reason
|
2014-08-28 23:27:40 -05:00
|
|
|
end
|
|
|
|
|
2023-05-03 21:22:07 -05:00
|
|
|
def flagged?
|
|
|
|
@post.is_flagged?
|
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def edited_by_another_user?
|
|
|
|
@post.last_editor_id != @editor.id
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
|
|
|
|
2018-03-06 23:44:21 -06:00
|
|
|
def original_raw_key
|
|
|
|
"original_raw_#{(@last_version_at.to_f * 1000).to_i}#{@post.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def original_cooked_key
|
|
|
|
"original_cooked_#{(@last_version_at.to_f * 1000).to_i}#{@post.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_original_raw
|
2019-12-03 03:05:53 -06:00
|
|
|
@cached_original_raw ||= Discourse.redis.get(original_raw_key)
|
2018-03-06 23:44:21 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def cached_original_cooked
|
2019-12-03 03:05:53 -06:00
|
|
|
@cached_original_cooked ||= Discourse.redis.get(original_cooked_key)
|
2018-03-06 23:44:21 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def original_raw
|
|
|
|
cached_original_raw || @post.raw
|
|
|
|
end
|
|
|
|
|
|
|
|
def original_raw=(val)
|
|
|
|
@cached_original_raw = val
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.setex(original_raw_key, SiteSetting.editing_grace_period + 1, val)
|
2018-03-06 23:44:21 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def original_cooked=(val)
|
|
|
|
@cached_original_cooked = val
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.setex(original_cooked_key, SiteSetting.editing_grace_period + 1, val)
|
2018-03-06 23:44:21 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff_size(before, after)
|
2023-01-09 06:10:19 -06:00
|
|
|
@diff_size ||=
|
|
|
|
begin
|
|
|
|
ONPDiff.new(before, after).short_diff.sum { |str, type| type == :common ? 0 : str.size }
|
2020-02-12 11:47:48 -06:00
|
|
|
end
|
2018-03-06 23:44:21 -06:00
|
|
|
end
|
|
|
|
|
2021-06-30 20:27:11 -05:00
|
|
|
def grace_period_edit?
|
2018-03-06 23:44:21 -06:00
|
|
|
return false if (@revised_at - @last_version_at) > SiteSetting.editing_grace_period.to_i
|
|
|
|
|
|
|
|
if new_raw = @fields[:raw]
|
2018-03-08 18:58:50 -06:00
|
|
|
max_diff = SiteSetting.editing_grace_period_max_diff.to_i
|
|
|
|
if @editor.staff? || (@editor.trust_level > 1)
|
|
|
|
max_diff = SiteSetting.editing_grace_period_max_diff_high_trust.to_i
|
|
|
|
end
|
|
|
|
|
2019-06-26 18:45:52 -05:00
|
|
|
if (original_raw.size - new_raw.size).abs > max_diff ||
|
2023-01-09 06:10:19 -06:00
|
|
|
diff_size(original_raw, new_raw) > max_diff
|
2018-03-06 23:44:21 -06:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def owner_changed?
|
|
|
|
@fields.has_key?(:user_id) && @fields[:user_id] != @post.user_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def force_new_version?
|
2013-11-21 18:52:26 -06:00
|
|
|
@opts[:force_new_version] == true
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def revise_and_create_new_version
|
2014-10-27 16:06:43 -05:00
|
|
|
@version_changed = true
|
2014-09-03 19:43:57 -05:00
|
|
|
@post.version += 1
|
2014-10-27 16:06:43 -05:00
|
|
|
@post.public_version += 1
|
|
|
|
@post.last_version_at = @revised_at
|
|
|
|
|
|
|
|
revise
|
|
|
|
perform_edit
|
|
|
|
bump_topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def revise
|
2014-09-03 19:43:57 -05:00
|
|
|
update_post
|
2014-10-27 16:06:43 -05:00
|
|
|
update_topic if topic_changed?
|
|
|
|
create_or_update_revision
|
2020-01-27 10:05:48 -06:00
|
|
|
remove_flags_and_unhide_post
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
|
|
|
|
2015-05-29 16:39:24 -05:00
|
|
|
USER_ACTIONS_TO_REMOVE ||= [UserAction::REPLY, UserAction::RESPONSE]
|
2015-05-29 13:08:39 -05:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def update_post
|
2015-07-15 17:22:01 -05:00
|
|
|
if @fields.has_key?("user_id") && @fields["user_id"] != @post.user_id && @post.user_id != nil
|
2015-05-29 13:08:39 -05:00
|
|
|
prev_owner = User.find(@post.user_id)
|
|
|
|
new_owner = User.find(@fields["user_id"])
|
2015-03-11 14:54:11 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
UserAction
|
|
|
|
.where(target_post_id: @post.id)
|
2017-07-27 20:20:09 -05:00
|
|
|
.where(user_id: prev_owner.id)
|
|
|
|
.where(action_type: USER_ACTIONS_TO_REMOVE)
|
2019-01-03 11:03:01 -06:00
|
|
|
.update_all(user_id: new_owner.id)
|
2015-05-29 16:39:24 -05:00
|
|
|
|
|
|
|
if @post.post_number == 1
|
2023-01-09 06:10:19 -06:00
|
|
|
UserAction
|
|
|
|
.where(target_topic_id: @post.topic_id)
|
2017-07-27 20:20:09 -05:00
|
|
|
.where(user_id: prev_owner.id)
|
|
|
|
.where(action_type: UserAction::NEW_TOPIC)
|
2019-01-03 11:03:01 -06:00
|
|
|
.update_all(user_id: new_owner.id)
|
2015-05-29 16:39:24 -05:00
|
|
|
end
|
2015-03-02 10:17:11 -06:00
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
POST_TRACKED_FIELDS.each do |field|
|
2023-01-09 06:10:19 -06:00
|
|
|
@post.public_send("#{field}=", @fields[field]) if @fields.has_key?(field)
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
2014-10-27 16:06:43 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
@post.edit_reason = @fields[:edit_reason] if should_create_new_version?
|
2014-10-27 16:06:43 -05:00
|
|
|
@post.last_editor_id = @editor.id
|
2023-01-09 06:10:19 -06:00
|
|
|
@post.word_count = @fields[:raw].scan(/[[:word:]]+/).size if @fields.has_key?(:raw)
|
|
|
|
@post.self_edits += 1 if self_edit?
|
2014-10-27 16:06:43 -05:00
|
|
|
|
|
|
|
@post.extract_quoted_post_numbers
|
2015-02-02 11:44:21 -06:00
|
|
|
|
2014-11-17 09:06:43 -06:00
|
|
|
@post_successfully_saved = @post.save(validate: @validate_post)
|
2020-09-11 04:48:25 -05:00
|
|
|
@post.link_post_uploads
|
2014-10-27 16:06:43 -05:00
|
|
|
@post.save_reply_relationships
|
2015-03-11 14:54:11 -05:00
|
|
|
|
2021-08-02 09:15:53 -05:00
|
|
|
@editor.increment_post_edits_count if @post_successfully_saved
|
|
|
|
|
2015-03-11 14:54:11 -05:00
|
|
|
# post owner changed
|
|
|
|
if prev_owner && new_owner && prev_owner != new_owner
|
2023-01-09 06:10:19 -06:00
|
|
|
likes =
|
|
|
|
UserAction
|
|
|
|
.where(target_post_id: @post.id)
|
|
|
|
.where(user_id: prev_owner.id)
|
|
|
|
.where(action_type: UserAction::WAS_LIKED)
|
|
|
|
.update_all(user_id: new_owner.id)
|
2015-03-11 14:54:11 -05:00
|
|
|
|
2020-04-20 20:50:20 -05:00
|
|
|
private_message = @topic.private_message?
|
2017-01-15 20:18:10 -06:00
|
|
|
|
|
|
|
prev_owner_user_stat = prev_owner.user_stat
|
2022-02-06 21:23:34 -06:00
|
|
|
|
2017-11-02 16:48:48 -05:00
|
|
|
unless private_message
|
2022-02-06 21:23:34 -06:00
|
|
|
UserStatCountUpdater.decrement!(@post, user_stat: prev_owner_user_stat) if !@post.trashed?
|
2017-11-02 16:48:48 -05:00
|
|
|
prev_owner_user_stat.likes_received -= likes
|
|
|
|
end
|
2015-05-29 13:08:39 -05:00
|
|
|
|
2015-03-11 14:54:11 -05:00
|
|
|
if @post.created_at == prev_owner.user_stat.first_post_created_at
|
2023-01-09 06:10:19 -06:00
|
|
|
prev_owner_user_stat.update!(
|
|
|
|
first_post_created_at: prev_owner.posts.order("created_at ASC").first.try(:created_at),
|
|
|
|
)
|
2015-03-11 14:54:11 -05:00
|
|
|
end
|
2015-05-29 13:08:39 -05:00
|
|
|
|
2017-01-15 20:18:10 -06:00
|
|
|
new_owner_user_stat = new_owner.user_stat
|
2017-11-02 16:48:48 -05:00
|
|
|
unless private_message
|
2022-02-06 21:23:34 -06:00
|
|
|
UserStatCountUpdater.increment!(@post, user_stat: new_owner_user_stat) if !@post.trashed?
|
2017-11-02 16:48:48 -05:00
|
|
|
new_owner_user_stat.likes_received += likes
|
|
|
|
end
|
2017-01-15 20:18:10 -06:00
|
|
|
new_owner_user_stat.save!
|
2015-03-11 14:54:11 -05:00
|
|
|
end
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def self_edit?
|
|
|
|
@editor == @post.user
|
2013-12-10 12:47:07 -06:00
|
|
|
end
|
|
|
|
|
2015-12-29 15:59:26 -06:00
|
|
|
def remove_flags_and_unhide_post
|
2019-06-20 10:23:49 -05:00
|
|
|
return if @opts[:deleting_post]
|
2014-10-27 16:06:43 -05:00
|
|
|
return unless editing_a_flagged_and_hidden_post?
|
2018-04-06 16:37:29 -05:00
|
|
|
|
|
|
|
flaggers = []
|
2023-01-09 06:10:19 -06:00
|
|
|
@post
|
|
|
|
.post_actions
|
2024-07-17 19:10:22 -05:00
|
|
|
.where(post_action_type_id: PostActionType.flag_types_without_additional_message.values)
|
2023-01-09 06:10:19 -06:00
|
|
|
.each do |action|
|
|
|
|
flaggers << action.user if action.user
|
|
|
|
action.remove_act!(Discourse.system_user)
|
|
|
|
end
|
2018-04-06 16:37:29 -05:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
@post.unhide!
|
2018-04-06 16:37:29 -05:00
|
|
|
PostActionNotifier.after_post_unhide(@post, flaggers)
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def editing_a_flagged_and_hidden_post?
|
2023-01-09 06:10:19 -06:00
|
|
|
self_edit? && @post.hidden &&
|
|
|
|
@post.hidden_reason_id == Post.hidden_reasons[:flag_threshold_reached]
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
2013-02-09 09:33:07 -06:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def update_topic
|
|
|
|
Topic.transaction do
|
2015-01-28 10:01:01 -06:00
|
|
|
PostRevisor.tracked_topic_fields.each do |f, cb|
|
2015-01-27 11:13:45 -06:00
|
|
|
if !@topic_changes.errored? && @fields.has_key?(f)
|
2021-03-02 17:59:23 -06:00
|
|
|
cb.call(@topic_changes, @fields[f], @fields)
|
2015-01-27 11:13:45 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless @topic_changes.errored?
|
|
|
|
@topic_changes.check_result(@topic.save(validate: @validate_topic))
|
|
|
|
end
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
2013-02-09 09:33:07 -06:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def create_or_update_revision
|
2016-08-19 12:13:22 -05:00
|
|
|
return if @skip_revision
|
2014-11-03 08:31:11 -06:00
|
|
|
# don't create an empty revision if something failed
|
|
|
|
return unless successfully_saved_post_and_topic
|
|
|
|
@version_changed ? create_revision : update_revision
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
2013-12-31 13:37:43 -06:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def create_revision
|
2015-01-27 11:13:45 -06:00
|
|
|
modifications = post_changes.merge(@topic_changes.diff)
|
2018-03-06 23:44:21 -06:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
modifications["raw"][0] = cached_original_raw || modifications["raw"][0] if modifications["raw"]
|
2018-03-06 23:44:21 -06:00
|
|
|
|
|
|
|
if modifications["cooked"]
|
|
|
|
modifications["cooked"][0] = cached_original_cooked || modifications["cooked"][0]
|
|
|
|
end
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
@post_revision =
|
|
|
|
PostRevision.create!(
|
|
|
|
user_id: @post.last_editor_id,
|
|
|
|
post_id: @post.id,
|
|
|
|
number: @post.version,
|
|
|
|
modifications: modifications,
|
|
|
|
hidden: only_hidden_tags_changed?,
|
|
|
|
)
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def update_revision
|
|
|
|
return unless revision = PostRevision.find_by(post_id: @post.id, number: @post.version)
|
|
|
|
revision.user_id = @post.last_editor_id
|
2015-01-27 11:13:45 -06:00
|
|
|
modifications = post_changes.merge(@topic_changes.diff)
|
2018-03-06 23:44:21 -06:00
|
|
|
|
2015-04-18 06:53:53 -05:00
|
|
|
modifications.each_key do |field|
|
2014-10-27 16:06:43 -05:00
|
|
|
if revision.modifications.has_key?(field)
|
2021-11-09 08:29:37 -06:00
|
|
|
old_value = revision.modifications[field][0]
|
|
|
|
new_value = modifications[field][1]
|
|
|
|
if old_value.to_s != new_value.to_s
|
2015-05-29 13:08:39 -05:00
|
|
|
revision.modifications[field] = [old_value, new_value]
|
|
|
|
else
|
|
|
|
revision.modifications.delete(field)
|
|
|
|
end
|
2014-10-27 16:06:43 -05:00
|
|
|
else
|
|
|
|
revision.modifications[field] = modifications[field]
|
|
|
|
end
|
|
|
|
end
|
2015-05-29 13:08:39 -05:00
|
|
|
# should probably do this before saving the post!
|
|
|
|
if revision.modifications.empty?
|
|
|
|
revision.destroy
|
2023-01-09 06:10:19 -06:00
|
|
|
@post.last_editor_id =
|
2023-02-12 22:39:45 -06:00
|
|
|
PostRevision.where(post_id: @post.id).order(number: :desc).pick(:user_id) || @post.user_id
|
2015-05-29 13:08:39 -05:00
|
|
|
@post.version -= 1
|
|
|
|
@post.public_version -= 1
|
2022-10-19 10:54:32 -05:00
|
|
|
@post.save(validate: @validate_post)
|
2015-05-29 13:08:39 -05:00
|
|
|
else
|
|
|
|
revision.save
|
|
|
|
end
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_changes
|
|
|
|
@post.previous_changes.slice(*POST_TRACKED_FIELDS)
|
|
|
|
end
|
|
|
|
|
2021-03-24 19:24:50 -05:00
|
|
|
def topic_diff
|
|
|
|
@topic_changes.diff
|
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def perform_edit
|
|
|
|
return if bypass_rate_limiter?
|
|
|
|
EditRateLimiter.new(@editor).performed!
|
|
|
|
end
|
|
|
|
|
|
|
|
def bypass_rate_limiter?
|
|
|
|
@opts[:bypass_rate_limiter] == true
|
|
|
|
end
|
|
|
|
|
|
|
|
def bump_topic
|
|
|
|
return if bypass_bump? || !is_last_post?
|
|
|
|
@topic.update_column(:bumped_at, Time.now)
|
2020-04-30 17:33:57 -05:00
|
|
|
TopicTrackingState.publish_muted(@topic)
|
2020-12-09 23:49:05 -06:00
|
|
|
TopicTrackingState.publish_unmuted(@topic)
|
2014-10-27 16:06:43 -05:00
|
|
|
TopicTrackingState.publish_latest(@topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bypass_bump?
|
2023-01-09 06:10:19 -06:00
|
|
|
!@post_successfully_saved || @topic_changes.errored? || @opts[:bypass_bump] == true ||
|
|
|
|
@post.whisper? || only_hidden_tags_changed?
|
2019-05-06 13:51:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def only_hidden_tags_changed?
|
2019-10-21 07:27:31 -05:00
|
|
|
return false if (hidden_tag_names = DiscourseTagging.hidden_tag_names).blank?
|
|
|
|
|
2019-05-06 13:51:51 -05:00
|
|
|
modifications = post_changes.merge(@topic_changes.diff)
|
2019-10-21 07:27:31 -05:00
|
|
|
if modifications.keys.size == 1 && (tags_diff = modifications["tags"]).present?
|
2019-05-06 13:51:51 -05:00
|
|
|
a, b = tags_diff[0] || [], tags_diff[1] || []
|
2019-06-07 13:25:42 -05:00
|
|
|
changed_tags = ((a + b) - (a & b)).map(&:presence).compact
|
2023-01-09 06:10:19 -06:00
|
|
|
return true if (changed_tags - hidden_tag_names).empty?
|
2019-05-06 13:51:51 -05:00
|
|
|
end
|
2019-10-21 07:27:31 -05:00
|
|
|
|
2019-05-06 13:51:51 -05:00
|
|
|
false
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_last_post?
|
2023-01-09 06:10:19 -06:00
|
|
|
!Post.where(topic_id: @topic.id).where("post_number > ?", @post.post_number).exists?
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def plugin_callbacks
|
2022-11-21 10:11:29 -06:00
|
|
|
DiscourseEvent.trigger(:before_edit_post, @post, @fields)
|
2014-10-27 16:06:43 -05:00
|
|
|
DiscourseEvent.trigger(:validate_post, @post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def revise_topic
|
2015-04-23 12:33:29 -05:00
|
|
|
return unless @post.is_first_post?
|
2013-02-21 17:09:56 -06:00
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
update_topic_excerpt
|
|
|
|
update_category_description
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_topic_excerpt
|
2020-05-22 23:56:13 -05:00
|
|
|
@topic.update_excerpt(@post.excerpt_for_topic)
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_category_description
|
|
|
|
return unless category = Category.find_by(topic_id: @topic.id)
|
2013-02-21 17:09:56 -06:00
|
|
|
|
2020-05-04 22:46:57 -05:00
|
|
|
doc = Nokogiri::HTML5.fragment(@post.cooked)
|
2016-12-07 16:05:14 -06:00
|
|
|
doc.css("img").remove
|
|
|
|
|
2017-07-21 14:07:29 -05:00
|
|
|
if html = doc.css("p").first&.inner_html&.strip
|
|
|
|
new_description = html unless html.starts_with?(Category.post_template[0..50])
|
|
|
|
category.update_column(:description, new_description)
|
|
|
|
@category_changed = category
|
|
|
|
else
|
2019-04-30 01:58:18 -05:00
|
|
|
@post.errors.add(:base, I18n.t("category.errors.description_incomplete"))
|
2017-07-21 14:07:29 -05:00
|
|
|
end
|
2013-02-21 17:09:56 -06:00
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def advance_draft_sequence
|
|
|
|
@post.advance_draft_sequence
|
2014-03-18 12:40:40 -05:00
|
|
|
end
|
|
|
|
|
2013-02-09 09:33:07 -06:00
|
|
|
def post_process_post
|
|
|
|
@post.invalidate_oneboxes = true
|
|
|
|
@post.trigger_post_process
|
2020-05-11 03:32:41 -05:00
|
|
|
DiscourseEvent.trigger(:post_edited, @post, self.topic_changed?, self)
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|
2014-10-27 16:06:43 -05:00
|
|
|
|
|
|
|
def alert_users
|
2018-01-30 15:21:07 -06:00
|
|
|
return if @editor.id == Discourse::SYSTEM_USER_ID
|
2018-05-24 10:27:43 -05:00
|
|
|
Jobs.enqueue(:post_alert, post_id: @post.id)
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def publish_changes
|
2016-04-07 09:29:01 -05:00
|
|
|
options =
|
|
|
|
if !@topic_changes.diff.empty? && !@topic_changes.errored?
|
|
|
|
{ reload_topic: true }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2021-01-20 02:51:31 -06:00
|
|
|
DiscourseEvent.trigger(:before_post_publish_changes, post_changes, @topic_changes, options)
|
|
|
|
|
2016-04-07 09:29:01 -05:00
|
|
|
@post.publish_change_to_clients!(:revised, options)
|
2014-10-27 16:06:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def grant_badge
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostRevision, post: @post)
|
|
|
|
end
|
|
|
|
|
2014-11-03 08:31:11 -06:00
|
|
|
def successfully_saved_post_and_topic
|
2015-01-27 11:13:45 -06:00
|
|
|
@post_successfully_saved && !@topic_changes.errored?
|
2014-11-03 08:31:11 -06:00
|
|
|
end
|
|
|
|
|
2020-10-28 14:47:50 -05:00
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(@editor)
|
|
|
|
end
|
2023-06-05 13:02:46 -05:00
|
|
|
|
|
|
|
def raw_changed?
|
|
|
|
@fields.has_key?(:raw) && @fields[:raw] != cached_original_raw && @post_successfully_saved
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_title_changed?
|
|
|
|
topic_changed? && @fields.has_key?(:title) && topic_diff.has_key?(:title) &&
|
|
|
|
!@topic_changes.errored?
|
|
|
|
end
|
|
|
|
|
|
|
|
def reviewable_content_changed?
|
|
|
|
raw_changed? || topic_title_changed?
|
|
|
|
end
|
2013-02-09 09:33:07 -06:00
|
|
|
end
|