2014-10-27 17:06:43 -04:00
|
|
|
require "edit_rate_limiter"
|
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
|
|
|
|
|
2014-10-27 17:06:43 -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
|
|
|
|
track_topic_field(:title) do |tc, title|
|
|
|
|
tc.record_change('title', tc.topic.title, title)
|
|
|
|
tc.topic.title = title
|
|
|
|
end
|
|
|
|
|
|
|
|
track_topic_field(:category_id) do |tc, category_id|
|
|
|
|
tc.record_change('category_id', tc.topic.category_id, category_id)
|
|
|
|
tc.check_result(tc.topic.change_category_to_id(category_id))
|
2015-01-27 12:13:45 -05:00
|
|
|
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
|
|
|
|
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
|
|
|
|
@fields[:edit_reason] = nil unless @fields.has_key?(:edit_reason)
|
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
|
|
|
|
2014-08-29 00:27:40 -04:00
|
|
|
Post.transaction do
|
|
|
|
revise_post
|
2014-09-03 20:43:57 -04:00
|
|
|
|
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
|
|
|
|
|
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
|
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
|
|
|
|
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)
|
|
|
|
TextCleaner.normalize_whitespaces(raw).gsub(/\s+\z/, "")
|
|
|
|
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|
|
|
|
|
return true if @fields.has_key?(field) && @fields[field] != @post.send(field)
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def topic_changed?
|
2015-01-28 11:01:01 -05:00
|
|
|
PostRevisor.tracked_topic_fields.keys.any? {|f| @fields.has_key?(f)}
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def revise_post
|
2014-11-03 09:31:11 -05:00
|
|
|
should_create_new_version? ? revise_and_create_new_version : revise
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def should_create_new_version?
|
|
|
|
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
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def ninja_edit?
|
|
|
|
@revised_at - @last_version_at <= SiteSetting.ninja_edit_window.to_i
|
|
|
|
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
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def update_post
|
2015-03-11 15:54:11 -04:00
|
|
|
prev_owner, new_owner = nil, nil
|
2015-03-02 11:17:11 -05:00
|
|
|
if @fields.has_key?("user_id") && @fields["user_id"] != @post.user_id
|
2015-03-11 15:54:11 -04:00
|
|
|
prev_owner = User.find_by_id(@post.user_id)
|
|
|
|
new_owner = User.find_by_id(@fields["user_id"])
|
|
|
|
|
2015-03-03 14:15:11 -05:00
|
|
|
UserAction.where( target_post_id: @post.id,
|
2015-03-11 15:54:11 -04:00
|
|
|
user_id: prev_owner.id,
|
2015-03-03 14:15:11 -05:00
|
|
|
action_type: [UserAction::NEW_TOPIC, UserAction::REPLY, UserAction::RESPONSE] )
|
|
|
|
.find_each { |ua| ua.destroy }
|
|
|
|
# UserActionObserver will create new UserAction records for the new owner
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
POST_TRACKED_FIELDS.each do |field|
|
|
|
|
@post.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
|
|
|
|
@post.word_count = @fields[:raw].scan(/\w+/).size if @fields.has_key?(:raw)
|
|
|
|
@post.self_edits += 1 if self_edit?
|
|
|
|
|
|
|
|
clear_flags_and_unhide_post
|
|
|
|
|
|
|
|
@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
|
|
|
|
likes = UserAction.where( target_post_id: @post.id,
|
|
|
|
user_id: prev_owner.id,
|
|
|
|
action_type: UserAction::WAS_LIKED ).update_all(user_id: new_owner.id)
|
|
|
|
|
|
|
|
prev_owner.user_stat.post_count -= 1
|
|
|
|
prev_owner.user_stat.topic_count -= 1 if @post.is_first_post?
|
|
|
|
prev_owner.user_stat.likes_received -= likes
|
|
|
|
prev_owner.user_stat.update_topic_reply_count
|
|
|
|
if @post.created_at == prev_owner.user_stat.first_post_created_at
|
|
|
|
prev_owner.user_stat.first_post_created_at = prev_owner.posts.order('created_at ASC').first.try(:created_at)
|
|
|
|
end
|
|
|
|
prev_owner.user_stat.save
|
|
|
|
|
|
|
|
new_owner.user_stat.post_count += 1
|
|
|
|
new_owner.user_stat.topic_count += 1 if @post.is_first_post?
|
|
|
|
new_owner.user_stat.likes_received += likes
|
|
|
|
new_owner.user_stat.update_topic_reply_count
|
|
|
|
new_owner.user_stat.save
|
|
|
|
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
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def clear_flags_and_unhide_post
|
|
|
|
return unless editing_a_flagged_and_hidden_post?
|
|
|
|
PostAction.clear_flags!(@post, Discourse.system_user)
|
|
|
|
@post.unhide!
|
|
|
|
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
|
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)
|
2014-10-27 17:06:43 -04:00
|
|
|
PostRevision.create!(
|
|
|
|
user_id: @post.last_editor_id,
|
|
|
|
post_id: @post.id,
|
|
|
|
number: @post.version,
|
|
|
|
modifications: modifications
|
|
|
|
)
|
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)
|
2014-10-27 17:06:43 -04:00
|
|
|
modifications.keys.each do |field|
|
|
|
|
if revision.modifications.has_key?(field)
|
|
|
|
old_value = revision.modifications[field][0]
|
|
|
|
new_value = modifications[field][1]
|
|
|
|
revision.modifications[field] = [old_value, new_value]
|
|
|
|
else
|
|
|
|
revision.modifications[field] = modifications[field]
|
|
|
|
end
|
|
|
|
end
|
2015-01-27 12:13:45 -05:00
|
|
|
revision.save if modifications.length
|
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?
|
|
|
|
@opts[:bypass_bump] == true
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_last_post?
|
|
|
|
!Post.where(topic_id: @topic.id)
|
|
|
|
.where("post_number > ?", @post.post_number)
|
|
|
|
.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def plugin_callbacks
|
|
|
|
DiscourseEvent.trigger(:before_edit_post, @post)
|
|
|
|
DiscourseEvent.trigger(:validate_post, @post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def revise_topic
|
2013-02-21 18:09:56 -05:00
|
|
|
return unless @post.post_number == 1
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
update_topic_excerpt
|
|
|
|
update_category_description
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_topic_excerpt
|
|
|
|
excerpt = @post.excerpt(220, strip_links: true)
|
|
|
|
@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
|
|
|
|
|
|
|
body = @post.cooked
|
|
|
|
matches = body.scan(/\<p\>(.*)\<\/p\>/)
|
2013-03-04 19:42:44 -05:00
|
|
|
if matches && matches[0] && matches[0][0]
|
2013-02-21 18:09:56 -05:00
|
|
|
new_description = matches[0][0]
|
|
|
|
new_description = nil if new_description == I18n.t("category.replace_paragraph")
|
|
|
|
category.update_column(:description, new_description)
|
|
|
|
@category_changed = category
|
|
|
|
end
|
|
|
|
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
|
|
|
|
end
|
2014-10-27 17:06:43 -04:00
|
|
|
|
|
|
|
def update_topic_word_counts
|
|
|
|
Topic.exec_sql("UPDATE topics
|
|
|
|
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
|
|
|
|
PostAlerter.new.after_save_post(@post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_changes
|
|
|
|
@post.publish_change_to_clients!(:revised)
|
|
|
|
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
|
2015-01-27 12:13:45 -05:00
|
|
|
|