2014-10-28 05:06:43 +08:00
|
|
|
require "edit_rate_limiter"
|
2013-12-12 10:41:34 +08:00
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
class PostRevisor
|
2013-02-22 07:09:56 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
POST_TRACKED_FIELDS = %w{raw cooked edit_reason user_id wiki post_type}
|
|
|
|
TOPIC_TRACKED_FIELDS = %w{title category_id}
|
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
attr_reader :category_changed
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def initialize(post, topic=nil)
|
2013-02-09 23:33:07 +08:00
|
|
|
@post = post
|
2014-10-28 05:06:43 +08:00
|
|
|
@topic = topic || post.topic
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08: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-12 06:01:58 +08:00
|
|
|
@editor = editor
|
2014-10-28 05:06:43 +08:00
|
|
|
@fields = fields.with_indifferent_access
|
2014-08-12 06:01:58 +08:00
|
|
|
@opts = opts
|
2014-10-28 05:06:43 +08: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-12 06:01:58 +08:00
|
|
|
|
2014-03-28 13:45:51 +08:00
|
|
|
return false unless should_revise?
|
2014-08-29 12:27:40 +08:00
|
|
|
|
2014-09-04 08:43:57 +08:00
|
|
|
@post.acting_user = @editor
|
2014-10-28 05:06:43 +08: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
|
|
|
|
@topic_successfully_saved = true
|
2014-09-04 08:43:57 +08:00
|
|
|
|
2014-11-17 23:06:43 +08:00
|
|
|
@validate_post = @opts[:validate_post] || !@opts[:skip_validations]
|
|
|
|
@validate_topic = @opts[:validate_topic] || !@opts[:skip_validations]
|
|
|
|
|
2014-08-29 12:27:40 +08:00
|
|
|
Post.transaction do
|
|
|
|
revise_post
|
2014-09-04 08:43:57 +08:00
|
|
|
|
2014-10-28 05:06:43 +08: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 12:27:40 +08:00
|
|
|
plugin_callbacks
|
2014-09-04 08:43:57 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
revise_topic
|
|
|
|
advance_draft_sequence
|
2014-08-29 12:27:40 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08: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-04 08:43:57 +08:00
|
|
|
post_process_post
|
2014-10-28 05:06:43 +08:00
|
|
|
|
2014-09-04 08:43:57 +08:00
|
|
|
update_topic_word_counts
|
2014-10-28 05:06:43 +08:00
|
|
|
alert_users
|
|
|
|
publish_changes
|
|
|
|
grant_badge
|
2014-09-04 08:43:57 +08:00
|
|
|
|
2014-11-03 22:31:11 +08:00
|
|
|
successfully_saved_post_and_topic
|
2014-10-28 05:06:43 +08:00
|
|
|
end
|
2014-09-04 08:43:57 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def cleanup_whitespaces(raw)
|
|
|
|
TextCleaner.normalize_whitespaces(raw).gsub(/\s+\z/, "")
|
|
|
|
end
|
2013-12-12 10:41:34 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def should_revise?
|
|
|
|
post_changed? || topic_changed?
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08: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 23:33:07 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def topic_changed?
|
|
|
|
TOPIC_TRACKED_FIELDS.each do |field|
|
|
|
|
return true if @fields.has_key?(field) && @fields[field] != @topic.send(field)
|
|
|
|
end
|
|
|
|
false
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def revise_post
|
2014-11-03 22:31:11 +08:00
|
|
|
should_create_new_version? ? revise_and_create_new_version : revise
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def should_create_new_version?
|
|
|
|
edited_by_another_user? || !ninja_edit? || owner_changed? || force_new_version?
|
2014-08-29 12:27:40 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def edited_by_another_user?
|
|
|
|
@post.last_editor_id != @editor.id
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08: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-22 08:52:26 +08:00
|
|
|
@opts[:force_new_version] == true
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def revise_and_create_new_version
|
2014-10-28 05:06:43 +08:00
|
|
|
@version_changed = true
|
2014-09-04 08:43:57 +08:00
|
|
|
@post.version += 1
|
2014-10-28 05:06:43 +08:00
|
|
|
@post.public_version += 1
|
|
|
|
@post.last_version_at = @revised_at
|
|
|
|
|
|
|
|
revise
|
|
|
|
perform_edit
|
|
|
|
bump_topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def revise
|
2014-09-04 08:43:57 +08:00
|
|
|
update_post
|
2014-10-28 05:06:43 +08:00
|
|
|
update_topic if topic_changed?
|
|
|
|
create_or_update_revision
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def update_post
|
|
|
|
POST_TRACKED_FIELDS.each do |field|
|
|
|
|
@post.send("#{field}=", @fields[field]) if @fields.has_key?(field)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
2014-10-28 05:06:43 +08: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
|
2014-11-17 23:06:43 +08:00
|
|
|
@post_successfully_saved = @post.save(validate: @validate_post)
|
2014-10-28 05:06:43 +08:00
|
|
|
@post.save_reply_relationships
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def self_edit?
|
|
|
|
@editor == @post.user
|
2013-12-11 02:47:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08: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 23:33:07 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def update_topic
|
|
|
|
@topic.title = @fields[:title] if @fields.has_key?(:title)
|
|
|
|
Topic.transaction do
|
|
|
|
@topic_successfully_saved = @topic.change_category_to_id(@fields[:category_id]) if @fields.has_key?(:category_id)
|
2014-11-17 23:06:43 +08:00
|
|
|
@topic_successfully_saved &&= @topic.save(validate: @validate_topic)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
2014-10-28 05:06:43 +08:00
|
|
|
end
|
2013-02-09 23:33:07 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def create_or_update_revision
|
2014-11-03 22:31:11 +08: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-28 05:06:43 +08:00
|
|
|
end
|
2014-01-01 03:37:43 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def create_revision
|
|
|
|
modifications = post_changes.merge(topic_changes)
|
|
|
|
PostRevision.create!(
|
|
|
|
user_id: @post.last_editor_id,
|
|
|
|
post_id: @post.id,
|
|
|
|
number: @post.version,
|
|
|
|
modifications: modifications
|
|
|
|
)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def update_revision
|
|
|
|
return unless revision = PostRevision.find_by(post_id: @post.id, number: @post.version)
|
|
|
|
revision.user_id = @post.last_editor_id
|
|
|
|
modifications = post_changes.merge(topic_changes)
|
|
|
|
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
|
|
|
|
revision.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_changes
|
|
|
|
@post.previous_changes.slice(*POST_TRACKED_FIELDS)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_changes
|
|
|
|
@topic.previous_changes.slice(*TOPIC_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-22 07:09:56 +08:00
|
|
|
return unless @post.post_number == 1
|
|
|
|
|
2014-10-28 05:06:43 +08: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-18 06:21:05 +08:00
|
|
|
if @topic.archetype == "banner"
|
|
|
|
ApplicationController.banner_json_cache.clear
|
|
|
|
end
|
2014-10-28 05:06:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_category_description
|
|
|
|
return unless category = Category.find_by(topic_id: @topic.id)
|
2013-02-22 07:09:56 +08:00
|
|
|
|
|
|
|
body = @post.cooked
|
|
|
|
matches = body.scan(/\<p\>(.*)\<\/p\>/)
|
2013-03-05 08:42:44 +08:00
|
|
|
if matches && matches[0] && matches[0][0]
|
2013-02-22 07:09:56 +08: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-28 05:06:43 +08:00
|
|
|
def advance_draft_sequence
|
|
|
|
@post.advance_draft_sequence
|
2014-03-19 01:40:40 +08:00
|
|
|
end
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
def post_process_post
|
|
|
|
@post.invalidate_oneboxes = true
|
|
|
|
@post.trigger_post_process
|
|
|
|
end
|
2014-10-28 05:06:43 +08: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 22:31:11 +08:00
|
|
|
def successfully_saved_post_and_topic
|
|
|
|
@post_successfully_saved && @topic_successfully_saved
|
|
|
|
end
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|