2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
require "edit_rate_limiter"
|
2018-01-26 13:45:52 -05:00
|
|
|
require 'post_locker'
|
2013-12-11 21:41:34 -05:00
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
class PostRevisor
|
2013-02-21 18:09:56 -05:00
|
|
|
|
2015-01-27 12:13:45 -05:00
|
|
|
# Helps us track changes to a topic.
|
|
|
|
#
|
2015-01-28 11:01:01 -05:00
|
|
|
# It's passed to `track_topic_fields` callbacks so they can record if they
|
2015-01-27 12:13:45 -05: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
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
POST_TRACKED_FIELDS = %w{raw cooked edit_reason user_id wiki post_type}
|
|
|
|
|
2013-02-21 18:09:56 -05:00
|
|
|
attr_reader :category_changed
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(post, topic = nil)
|
2013-02-09 10:33:07 -05:00
|
|
|
@post = post
|
2014-10-27 17:06:43 -04:00
|
|
|
@topic = topic || post.topic
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2015-01-28 11:01:01 -05:00
|
|
|
def self.tracked_topic_fields
|
|
|
|
@@tracked_topic_fields ||= {}
|
|
|
|
@@tracked_topic_fields
|
2015-01-27 12:13:45 -05:00
|
|
|
end
|
|
|
|
|
2015-01-28 11:01:01 -05:00
|
|
|
def self.track_topic_field(field, &block)
|
|
|
|
tracked_topic_fields[field] = block
|
|
|
|
|
|
|
|
# Define it in the serializer unless it already has been defined
|
|
|
|
unless PostRevisionSerializer.instance_methods(false).include?("#{field}_changes".to_sym)
|
|
|
|
PostRevisionSerializer.add_compared_field(field)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Fields we want to record revisions for by default
|
2019-04-16 03:16:23 -04:00
|
|
|
%i{title archetype}.each do |field|
|
|
|
|
track_topic_field(field) do |tc, attribute|
|
|
|
|
tc.record_change(field, tc.topic.public_send(field), attribute)
|
|
|
|
tc.topic.public_send("#{field}=", attribute)
|
|
|
|
end
|
2015-01-28 11:01:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
track_topic_field(:category_id) do |tc, category_id|
|
2019-04-16 03:16:23 -04:00
|
|
|
if category_id == 0 && tc.topic.private_message?
|
|
|
|
tc.record_change('category_id', tc.topic.category_id, nil)
|
|
|
|
tc.topic.category_id = nil
|
|
|
|
elsif category_id == 0 || tc.guardian.can_move_topic_to_category?(category_id)
|
2018-03-01 20:13:04 -05:00
|
|
|
tc.record_change('category_id', tc.topic.category_id, category_id)
|
|
|
|
tc.check_result(tc.topic.change_category_to_id(category_id))
|
|
|
|
end
|
2015-01-27 12:13:45 -05:00
|
|
|
end
|
|
|
|
|
2016-04-25 15:55:15 -04:00
|
|
|
track_topic_field(:tags) do |tc, tags|
|
2016-05-04 14:02:47 -04: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 15:55:15 -04:00
|
|
|
end
|
2019-03-12 13:09:34 -04:00
|
|
|
if prev_tags.sort != tags.sort
|
|
|
|
tc.record_change('tags', prev_tags, tags)
|
|
|
|
DB.after_commit do
|
|
|
|
post = tc.topic.ordered_posts.first
|
2019-06-11 11:06:54 -04:00
|
|
|
Jobs.enqueue(:notify_tag_change, post_id: post.id)
|
2019-03-12 13:09:34 -04:00
|
|
|
end
|
|
|
|
end
|
2016-04-25 15:55:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-05 07:31:43 -05:00
|
|
|
track_topic_field(:featured_link) do |topic_changes, featured_link|
|
|
|
|
if SiteSetting.topic_featured_link_enabled &&
|
|
|
|
topic_changes.guardian.can_edit_featured_link?(topic_changes.topic.category_id)
|
|
|
|
|
|
|
|
topic_changes.record_change('featured_link', topic_changes.topic.featured_link, featured_link)
|
|
|
|
topic_changes.topic.featured_link = featured_link
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
# AVAILABLE OPTIONS:
|
|
|
|
# - revised_at: changes the date of the revision
|
|
|
|
# - force_new_version: bypass ninja-edit window
|
|
|
|
# - bypass_rate_limiter:
|
|
|
|
# - bypass_bump: do not bump the topic, even if last post
|
|
|
|
# - skip_validations: ask ActiveRecord to skip validations
|
2016-08-19 13:13:22 -04:00
|
|
|
# - skip_revision: do not create a new PostRevision record
|
2017-07-27 21:20:09 -04:00
|
|
|
def revise!(editor, fields, opts = {})
|
2014-08-11 18:01:58 -04:00
|
|
|
@editor = editor
|
2014-10-27 17:06:43 -04:00
|
|
|
@fields = fields.with_indifferent_access
|
2014-08-11 18:01:58 -04:00
|
|
|
@opts = opts
|
2014-10-27 17:06:43 -04:00
|
|
|
|
2015-01-27 12:13:45 -05:00
|
|
|
@topic_changes = TopicChanges.new(@topic, editor)
|
|
|
|
|
2014-10-27 17:06:43 -04: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)
|
|
|
|
|
|
|
|
# always reset edit_reason unless provided
|
2015-05-29 14:08:39 -04:00
|
|
|
@fields[:edit_reason] = nil unless @fields[:edit_reason].present?
|
2014-08-11 18:01:58 -04:00
|
|
|
|
2014-03-28 01:45:51 -04:00
|
|
|
return false unless should_revise?
|
2014-08-29 00:27:40 -04:00
|
|
|
|
2014-09-03 20:43:57 -04:00
|
|
|
@post.acting_user = @editor
|
2014-10-27 17:06:43 -04:00
|
|
|
@topic.acting_user = @editor
|
|
|
|
@revised_at = @opts[:revised_at] || Time.now
|
|
|
|
@last_version_at = @post.last_version_at || Time.now
|
|
|
|
|
|
|
|
@version_changed = false
|
|
|
|
@post_successfully_saved = true
|
2014-09-03 20:43:57 -04:00
|
|
|
|
2014-11-25 10:53:26 -05: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)
|
|
|
|
@validate_topic = !@opts[:validate_topic] if @opts.has_key?(:skip_validations)
|
2014-11-17 10:06:43 -05:00
|
|
|
|
2016-08-19 13:13:22 -04:00
|
|
|
@skip_revision = false
|
|
|
|
@skip_revision = @opts[:skip_revision] if @opts.has_key?(:skip_revision)
|
|
|
|
|
2018-03-14 15:01:36 -04:00
|
|
|
old_raw = @post.raw
|
|
|
|
|
2014-08-29 00:27:40 -04:00
|
|
|
Post.transaction do
|
|
|
|
revise_post
|
2014-09-03 20:43:57 -04:00
|
|
|
|
2016-08-10 01:55:51 -04:00
|
|
|
yield if block_given?
|
2014-10-27 17:06:43 -04: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-29 00:27:40 -04:00
|
|
|
plugin_callbacks
|
2014-09-03 20:43:57 -04:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
revise_topic
|
|
|
|
advance_draft_sequence
|
2014-08-29 00:27:40 -04:00
|
|
|
end
|
|
|
|
|
2018-01-26 13:45:52 -05:00
|
|
|
# Lock the post by default if the appropriate setting is true
|
2018-03-01 20:33:04 -05:00
|
|
|
if (
|
|
|
|
SiteSetting.staff_edit_locks_post? &&
|
2018-03-05 14:50:06 -05:00
|
|
|
!@post.wiki? &&
|
2018-03-01 20:33:04 -05:00
|
|
|
@fields.has_key?('raw') &&
|
|
|
|
@editor.staff? &&
|
2018-12-02 14:59:00 -05:00
|
|
|
@editor != Discourse.system_user &&
|
2018-03-01 20:33:04 -05:00
|
|
|
!@post.user.staff?
|
|
|
|
)
|
2018-01-26 13:45:52 -05:00
|
|
|
PostLocker.new(@post, @editor).lock
|
|
|
|
end
|
|
|
|
|
2018-03-12 13:49:52 -04:00
|
|
|
# We log staff edits to posts
|
|
|
|
if @editor.staff? && @editor.id != @post.user.id && @fields.has_key?('raw')
|
|
|
|
StaffActionLogger.new(@editor).log_post_edit(
|
|
|
|
@post,
|
2018-03-14 15:01:36 -04:00
|
|
|
old_raw: old_raw
|
2018-03-12 13:49:52 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04: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 05:31:45 -04:00
|
|
|
QuotedPost.extract_from(@post)
|
2014-09-03 20:43:57 -04:00
|
|
|
post_process_post
|
2014-10-27 17:06:43 -04:00
|
|
|
|
2014-09-03 20:43:57 -04:00
|
|
|
update_topic_word_counts
|
2014-10-27 17:06:43 -04:00
|
|
|
alert_users
|
|
|
|
publish_changes
|
|
|
|
grant_badge
|
2014-09-03 20:43:57 -04:00
|
|
|
|
2015-08-14 13:33:32 -04:00
|
|
|
TopicLink.extract_from(@post)
|
|
|
|
|
2014-11-03 09:31:11 -05:00
|
|
|
successfully_saved_post_and_topic
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
2014-09-03 20:43:57 -04:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def cleanup_whitespaces(raw)
|
2019-01-19 07:38:25 -05:00
|
|
|
raw.present? ? TextCleaner.normalize_whitespaces(raw).gsub(/\s+\z/, "") : ""
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
2013-12-11 21:41:34 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def should_revise?
|
|
|
|
post_changed? || topic_changed?
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def post_changed?
|
|
|
|
POST_TRACKED_FIELDS.each do |field|
|
2019-05-06 21:27:05 -04:00
|
|
|
if @fields.has_key?(field) && @fields[field] != @post.public_send(field)
|
|
|
|
return true
|
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
2016-03-14 20:31:50 -04:00
|
|
|
advance_draft_sequence
|
2014-10-27 17:06:43 -04:00
|
|
|
false
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def topic_changed?
|
2017-07-27 21:20:09 -04:00
|
|
|
PostRevisor.tracked_topic_fields.keys.any? { |f| @fields.has_key?(f) }
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def revise_post
|
2018-03-07 00:44:21 -05: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 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def should_create_new_version?
|
2016-08-19 13:13:22 -04:00
|
|
|
return false if @skip_revision
|
2014-10-27 17:06:43 -04:00
|
|
|
edited_by_another_user? || !ninja_edit? || owner_changed? || force_new_version?
|
2014-08-29 00:27:40 -04:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def edited_by_another_user?
|
|
|
|
@post.last_editor_id != @editor.id
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2018-03-07 00:44:21 -05: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
|
|
|
|
@cached_original_raw ||= $redis.get(original_raw_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_original_cooked
|
|
|
|
@cached_original_cooked ||= $redis.get(original_cooked_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def original_raw
|
|
|
|
cached_original_raw || @post.raw
|
|
|
|
end
|
|
|
|
|
|
|
|
def original_raw=(val)
|
|
|
|
@cached_original_raw = val
|
|
|
|
$redis.setex(original_raw_key, SiteSetting.editing_grace_period + 1, val)
|
|
|
|
end
|
|
|
|
|
|
|
|
def original_cooked=(val)
|
|
|
|
@cached_original_cooked = val
|
|
|
|
$redis.setex(original_cooked_key, SiteSetting.editing_grace_period + 1, val)
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff_size(before, after)
|
|
|
|
changes = 0
|
|
|
|
ONPDiff.new(before, after).short_diff.each do |str, type|
|
|
|
|
next if type == :common
|
|
|
|
changes += str.length
|
|
|
|
end
|
|
|
|
changes
|
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def ninja_edit?
|
2018-03-07 00:44:21 -05:00
|
|
|
return false if (@revised_at - @last_version_at) > SiteSetting.editing_grace_period.to_i
|
2019-01-03 12:03:01 -05:00
|
|
|
return false if @post.reviewable_flag.present?
|
2018-03-07 00:44:21 -05:00
|
|
|
|
|
|
|
if new_raw = @fields[:raw]
|
2018-03-08 19:58:50 -05: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
|
|
|
|
|
|
|
|
if (original_raw.length - new_raw.length).abs > max_diff ||
|
|
|
|
diff_size(original_raw, new_raw) > max_diff
|
2018-03-07 00:44:21 -05:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def owner_changed?
|
|
|
|
@fields.has_key?(:user_id) && @fields[:user_id] != @post.user_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def force_new_version?
|
2013-11-21 19:52:26 -05:00
|
|
|
@opts[:force_new_version] == true
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def revise_and_create_new_version
|
2014-10-27 17:06:43 -04:00
|
|
|
@version_changed = true
|
2014-09-03 20:43:57 -04:00
|
|
|
@post.version += 1
|
2014-10-27 17:06:43 -04:00
|
|
|
@post.public_version += 1
|
|
|
|
@post.last_version_at = @revised_at
|
|
|
|
|
|
|
|
revise
|
|
|
|
perform_edit
|
|
|
|
bump_topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def revise
|
2014-09-03 20:43:57 -04:00
|
|
|
update_post
|
2014-10-27 17:06:43 -04:00
|
|
|
update_topic if topic_changed?
|
|
|
|
create_or_update_revision
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2015-05-29 17:39:24 -04:00
|
|
|
USER_ACTIONS_TO_REMOVE ||= [UserAction::REPLY, UserAction::RESPONSE]
|
2015-05-29 14:08:39 -04:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def update_post
|
2015-07-15 18:22:01 -04:00
|
|
|
if @fields.has_key?("user_id") && @fields["user_id"] != @post.user_id && @post.user_id != nil
|
2015-05-29 14:08:39 -04:00
|
|
|
prev_owner = User.find(@post.user_id)
|
|
|
|
new_owner = User.find(@fields["user_id"])
|
2015-03-11 15:54:11 -04:00
|
|
|
|
2015-05-29 14:08:39 -04:00
|
|
|
UserAction.where(target_post_id: @post.id)
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(user_id: prev_owner.id)
|
|
|
|
.where(action_type: USER_ACTIONS_TO_REMOVE)
|
2019-01-03 12:03:01 -05:00
|
|
|
.update_all(user_id: new_owner.id)
|
2015-05-29 17:39:24 -04:00
|
|
|
|
|
|
|
if @post.post_number == 1
|
|
|
|
UserAction.where(target_topic_id: @post.topic_id)
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(user_id: prev_owner.id)
|
|
|
|
.where(action_type: UserAction::NEW_TOPIC)
|
2019-01-03 12:03:01 -05:00
|
|
|
.update_all(user_id: new_owner.id)
|
2015-05-29 17:39:24 -04:00
|
|
|
end
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
POST_TRACKED_FIELDS.each do |field|
|
2019-05-06 21:27:05 -04:00
|
|
|
@post.public_send("#{field}=", @fields[field]) if @fields.has_key?(field)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
@post.last_editor_id = @editor.id
|
2016-01-11 05:16:23 -05:00
|
|
|
@post.word_count = @fields[:raw].scan(/[[:word:]]+/).size if @fields.has_key?(:raw)
|
2014-10-27 17:06:43 -04:00
|
|
|
@post.self_edits += 1 if self_edit?
|
|
|
|
|
2015-12-29 16:59:26 -05:00
|
|
|
remove_flags_and_unhide_post
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
@post.extract_quoted_post_numbers
|
2015-02-02 12:44:21 -05:00
|
|
|
|
2014-11-17 10:06:43 -05:00
|
|
|
@post_successfully_saved = @post.save(validate: @validate_post)
|
2014-10-27 17:06:43 -04:00
|
|
|
@post.save_reply_relationships
|
2015-03-11 15:54:11 -04:00
|
|
|
|
|
|
|
# post owner changed
|
|
|
|
if prev_owner && new_owner && prev_owner != new_owner
|
2015-05-29 14:08:39 -04:00
|
|
|
likes = UserAction.where(target_post_id: @post.id)
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(user_id: prev_owner.id)
|
|
|
|
.where(action_type: UserAction::WAS_LIKED)
|
|
|
|
.update_all(user_id: new_owner.id)
|
2015-03-11 15:54:11 -04:00
|
|
|
|
2017-01-15 21:18:10 -05:00
|
|
|
private_message = @post.topic.private_message?
|
|
|
|
|
|
|
|
prev_owner_user_stat = prev_owner.user_stat
|
2017-11-02 17:48:48 -04:00
|
|
|
unless private_message
|
|
|
|
prev_owner_user_stat.post_count -= 1 if @post.post_type == Post.types[:regular]
|
|
|
|
prev_owner_user_stat.topic_count -= 1 if @post.is_first_post?
|
|
|
|
prev_owner_user_stat.likes_received -= likes
|
|
|
|
end
|
2017-01-15 21:18:10 -05:00
|
|
|
prev_owner_user_stat.update_topic_reply_count
|
2015-05-29 14:08:39 -04:00
|
|
|
|
2015-03-11 15:54:11 -04:00
|
|
|
if @post.created_at == prev_owner.user_stat.first_post_created_at
|
2017-01-15 21:18:10 -05:00
|
|
|
prev_owner_user_stat.first_post_created_at = prev_owner.posts.order('created_at ASC').first.try(:created_at)
|
2015-03-11 15:54:11 -04:00
|
|
|
end
|
2015-05-29 14:08:39 -04:00
|
|
|
|
2017-01-15 21:18:10 -05:00
|
|
|
prev_owner_user_stat.save!
|
2015-03-11 15:54:11 -04:00
|
|
|
|
2017-01-15 21:18:10 -05:00
|
|
|
new_owner_user_stat = new_owner.user_stat
|
2017-11-02 17:48:48 -04:00
|
|
|
unless private_message
|
|
|
|
new_owner_user_stat.post_count += 1 if @post.post_type == Post.types[:regular]
|
|
|
|
new_owner_user_stat.topic_count += 1 if @post.is_first_post?
|
|
|
|
new_owner_user_stat.likes_received += likes
|
|
|
|
end
|
2017-01-15 21:18:10 -05:00
|
|
|
new_owner_user_stat.update_topic_reply_count
|
|
|
|
new_owner_user_stat.save!
|
2015-03-11 15:54:11 -04:00
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def self_edit?
|
|
|
|
@editor == @post.user
|
2013-12-10 13:47:07 -05:00
|
|
|
end
|
|
|
|
|
2015-12-29 16:59:26 -05:00
|
|
|
def remove_flags_and_unhide_post
|
2014-10-27 17:06:43 -04:00
|
|
|
return unless editing_a_flagged_and_hidden_post?
|
2018-04-06 17:37:29 -04:00
|
|
|
|
|
|
|
flaggers = []
|
2017-10-17 13:31:45 -04:00
|
|
|
@post.post_actions.where(post_action_type_id: PostActionType.flag_types_without_custom.values).each do |action|
|
2018-04-06 17:37:29 -04:00
|
|
|
flaggers << action.user if action.user
|
2015-12-29 16:59:26 -05:00
|
|
|
action.remove_act!(Discourse.system_user)
|
|
|
|
end
|
2018-04-06 17:37:29 -04:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
@post.unhide!
|
2018-04-06 17:37:29 -04:00
|
|
|
PostActionNotifier.after_post_unhide(@post, flaggers)
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def editing_a_flagged_and_hidden_post?
|
|
|
|
self_edit? &&
|
|
|
|
@post.hidden &&
|
|
|
|
@post.hidden_reason_id == Post.hidden_reasons[:flag_threshold_reached]
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def update_topic
|
|
|
|
Topic.transaction do
|
2015-01-28 11:01:01 -05:00
|
|
|
PostRevisor.tracked_topic_fields.each do |f, cb|
|
2015-01-27 12:13:45 -05:00
|
|
|
if !@topic_changes.errored? && @fields.has_key?(f)
|
|
|
|
cb.call(@topic_changes, @fields[f])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless @topic_changes.errored?
|
|
|
|
@topic_changes.check_result(@topic.save(validate: @validate_topic))
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def create_or_update_revision
|
2016-08-19 13:13:22 -04:00
|
|
|
return if @skip_revision
|
2014-11-03 09:31:11 -05: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 17:06:43 -04:00
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def create_revision
|
2015-01-27 12:13:45 -05:00
|
|
|
modifications = post_changes.merge(@topic_changes.diff)
|
2018-03-07 00:44:21 -05:00
|
|
|
|
|
|
|
if modifications["raw"]
|
|
|
|
modifications["raw"][0] = cached_original_raw || modifications["raw"][0]
|
|
|
|
end
|
|
|
|
|
|
|
|
if modifications["cooked"]
|
|
|
|
modifications["cooked"][0] = cached_original_cooked || modifications["cooked"][0]
|
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
PostRevision.create!(
|
|
|
|
user_id: @post.last_editor_id,
|
|
|
|
post_id: @post.id,
|
|
|
|
number: @post.version,
|
2019-06-04 15:48:06 -04:00
|
|
|
modifications: modifications,
|
|
|
|
hidden: only_hidden_tags_changed?
|
2014-10-27 17:06:43 -04:00
|
|
|
)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04: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 12:13:45 -05:00
|
|
|
modifications = post_changes.merge(@topic_changes.diff)
|
2018-03-07 00:44:21 -05:00
|
|
|
|
2015-04-18 07:53:53 -04:00
|
|
|
modifications.each_key do |field|
|
2014-10-27 17:06:43 -04:00
|
|
|
if revision.modifications.has_key?(field)
|
2015-05-29 14:08:39 -04:00
|
|
|
old_value = revision.modifications[field][0].to_s
|
|
|
|
new_value = modifications[field][1].to_s
|
|
|
|
if old_value != new_value
|
|
|
|
revision.modifications[field] = [old_value, new_value]
|
|
|
|
else
|
|
|
|
revision.modifications.delete(field)
|
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
else
|
|
|
|
revision.modifications[field] = modifications[field]
|
|
|
|
end
|
|
|
|
end
|
2015-05-29 14:08:39 -04:00
|
|
|
# should probably do this before saving the post!
|
|
|
|
if revision.modifications.empty?
|
|
|
|
revision.destroy
|
|
|
|
@post.version -= 1
|
|
|
|
@post.public_version -= 1
|
|
|
|
@post.save
|
|
|
|
else
|
|
|
|
revision.save
|
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_changes
|
|
|
|
@post.previous_changes.slice(*POST_TRACKED_FIELDS)
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
TopicTrackingState.publish_latest(@topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bypass_bump?
|
2019-05-06 14:51:51 -04:00
|
|
|
!@post_successfully_saved ||
|
|
|
|
@topic_changes.errored? ||
|
|
|
|
@opts[:bypass_bump] == true ||
|
|
|
|
@post.whisper? ||
|
|
|
|
only_hidden_tags_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_hidden_tags_changed?
|
|
|
|
modifications = post_changes.merge(@topic_changes.diff)
|
|
|
|
if modifications.keys.size == 1 && tags_diff = modifications["tags"]
|
|
|
|
a, b = tags_diff[0] || [], tags_diff[1] || []
|
2019-06-07 14:25:42 -04:00
|
|
|
changed_tags = ((a + b) - (a & b)).map(&:presence).compact
|
2019-05-06 14:51:51 -04:00
|
|
|
if (changed_tags - DiscourseTagging.hidden_tag_names(nil)).empty?
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
false
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_last_post?
|
|
|
|
!Post.where(topic_id: @topic.id)
|
2017-07-27 21:20:09 -04:00
|
|
|
.where("post_number > ?", @post.post_number)
|
|
|
|
.exists?
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def plugin_callbacks
|
|
|
|
DiscourseEvent.trigger(:before_edit_post, @post)
|
|
|
|
DiscourseEvent.trigger(:validate_post, @post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def revise_topic
|
2015-04-23 13:33:29 -04:00
|
|
|
return unless @post.is_first_post?
|
2013-02-21 18:09:56 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
update_topic_excerpt
|
|
|
|
update_category_description
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_topic_excerpt
|
2018-04-17 15:08:13 -04:00
|
|
|
excerpt = @post.excerpt_for_topic
|
2014-10-27 17:06:43 -04:00
|
|
|
@topic.update_column(:excerpt, excerpt)
|
2014-11-17 17:21:05 -05:00
|
|
|
if @topic.archetype == "banner"
|
|
|
|
ApplicationController.banner_json_cache.clear
|
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_category_description
|
|
|
|
return unless category = Category.find_by(topic_id: @topic.id)
|
2013-02-21 18:09:56 -05:00
|
|
|
|
2016-12-07 17:05:14 -05:00
|
|
|
doc = Nokogiri::HTML.fragment(@post.cooked)
|
|
|
|
doc.css("img").remove
|
|
|
|
|
2017-07-21 15:07:29 -04: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 02:58:18 -04:00
|
|
|
@post.errors.add(:base, I18n.t("category.errors.description_incomplete"))
|
2017-07-21 15:07:29 -04:00
|
|
|
end
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def advance_draft_sequence
|
|
|
|
@post.advance_draft_sequence
|
2014-03-18 13:40:40 -04:00
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
def post_process_post
|
|
|
|
@post.invalidate_oneboxes = true
|
|
|
|
@post.trigger_post_process
|
2016-11-04 16:16:30 -04:00
|
|
|
DiscourseEvent.trigger(:post_edited, @post, self.topic_changed?)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
def update_topic_word_counts
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec("UPDATE topics
|
2014-10-27 17:06:43 -04:00
|
|
|
SET word_count = (
|
|
|
|
SELECT SUM(COALESCE(posts.word_count, 0))
|
|
|
|
FROM posts
|
|
|
|
WHERE posts.topic_id = :topic_id
|
|
|
|
)
|
|
|
|
WHERE topics.id = :topic_id", topic_id: @topic.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def alert_users
|
2018-01-30 16:21:07 -05:00
|
|
|
return if @editor.id == Discourse::SYSTEM_USER_ID
|
2018-05-24 11:27:43 -04:00
|
|
|
Jobs.enqueue(:post_alert, post_id: @post.id)
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def publish_changes
|
2016-04-07 10:29:01 -04:00
|
|
|
options =
|
|
|
|
if !@topic_changes.diff.empty? && !@topic_changes.errored?
|
|
|
|
{ reload_topic: true }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
@post.publish_change_to_clients!(:revised, options)
|
2014-10-27 17:06:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def grant_badge
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostRevision, post: @post)
|
|
|
|
end
|
|
|
|
|
2014-11-03 09:31:11 -05:00
|
|
|
def successfully_saved_post_and_topic
|
2015-01-27 12:13:45 -05:00
|
|
|
@post_successfully_saved && !@topic_changes.errored?
|
2014-11-03 09:31:11 -05:00
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|