2013-02-09 23:33:07 +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
|
|
|
|
|
|
|
attr_reader :category_changed
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
def initialize(post)
|
|
|
|
@post = post
|
|
|
|
end
|
|
|
|
|
2014-03-28 13:45:51 +08:00
|
|
|
# Recognized options:
|
|
|
|
# :edit_reason User-supplied edit reason
|
2014-03-28 09:28:14 +08:00
|
|
|
# :new_user New owner of the post
|
2014-03-28 13:45:51 +08:00
|
|
|
# :revised_at changes the date of the revision
|
|
|
|
# :force_new_version bypass ninja-edit window
|
|
|
|
# :bypass_bump do not bump the topic, even if last post
|
|
|
|
# :skip_validation ask ActiveRecord to skip validations
|
|
|
|
#
|
|
|
|
def revise!(editor, new_raw, opts = {})
|
2014-08-12 06:01:58 +08:00
|
|
|
@editor = editor
|
|
|
|
@opts = opts
|
2014-09-02 07:18:06 +08:00
|
|
|
@new_raw = TextCleaner.normalize_whitespaces(new_raw).gsub(/\s+\z/, "")
|
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-08-29 12:27:40 +08:00
|
|
|
Post.transaction do
|
|
|
|
revise_post
|
2014-09-04 08:43:57 +08:00
|
|
|
|
|
|
|
# TODO these callbacks are being called in a transaction
|
|
|
|
# it is kind of odd, cause 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 positives. This system needs a review
|
2014-08-29 12:27:40 +08:00
|
|
|
plugin_callbacks
|
2014-09-04 08:43:57 +08:00
|
|
|
|
2014-08-29 12:27:40 +08:00
|
|
|
update_category_description
|
|
|
|
update_topic_excerpt
|
|
|
|
@post.advance_draft_sequence
|
|
|
|
end
|
|
|
|
|
2014-09-04 08:43:57 +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
|
|
|
|
post_process_post
|
|
|
|
update_topic_word_counts
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
PostAlerter.new.after_save_post(@post)
|
2014-09-04 08:43:57 +08:00
|
|
|
|
2014-08-29 11:34:32 +08:00
|
|
|
@post.publish_change_to_clients! :revised
|
2014-07-23 09:42:24 +08:00
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostRevision, post: @post)
|
2013-12-12 10:41:34 +08:00
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def should_revise?
|
2014-03-28 09:28:14 +08:00
|
|
|
@post.raw != @new_raw || @opts[:changed_owner]
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def revise_post
|
|
|
|
if should_create_new_version?
|
|
|
|
revise_and_create_new_version
|
|
|
|
else
|
2013-12-12 10:41:34 +08:00
|
|
|
update_post
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-29 12:27:40 +08:00
|
|
|
def plugin_callbacks
|
|
|
|
DiscourseEvent.trigger :before_edit_post, @post
|
|
|
|
DiscourseEvent.trigger :validate_post, @post
|
|
|
|
end
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
def get_revised_at
|
|
|
|
@opts[:revised_at] || Time.now
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_create_new_version?
|
2014-03-28 13:45:51 +08:00
|
|
|
@post.last_editor_id != @editor.id ||
|
2013-11-22 08:52:26 +08:00
|
|
|
get_revised_at - @post.last_version_at > SiteSetting.ninja_edit_window.to_i ||
|
2014-03-28 09:28:14 +08:00
|
|
|
@opts[:changed_owner] == true ||
|
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-09-04 08:43:57 +08:00
|
|
|
@post.version += 1
|
|
|
|
@post.last_version_at = get_revised_at
|
|
|
|
update_post
|
|
|
|
EditRateLimiter.new(@editor).performed! unless @opts[:bypass_rate_limiter] == true
|
|
|
|
bump_topic unless @opts[:bypass_bump]
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def bump_topic
|
|
|
|
unless Post.where('post_number > ? and topic_id = ?', @post.post_number, @post.topic_id).exists?
|
|
|
|
@post.topic.update_column(:bumped_at, Time.now)
|
2014-08-05 11:27:34 +08:00
|
|
|
TopicTrackingState.publish_latest(@post.topic)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-11 02:47:07 +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: @post.topic_id)
|
|
|
|
end
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
def update_post
|
|
|
|
@post.raw = @new_raw
|
2013-12-11 02:47:07 +08:00
|
|
|
@post.word_count = @new_raw.scan(/\w+/).size
|
2014-03-28 13:45:51 +08:00
|
|
|
@post.last_editor_id = @editor.id
|
2013-11-16 06:28:16 +08:00
|
|
|
@post.edit_reason = @opts[:edit_reason] if @opts[:edit_reason]
|
2014-03-28 09:28:14 +08:00
|
|
|
@post.user_id = @opts[:new_user].id if @opts[:new_user]
|
2014-07-07 15:55:25 +08:00
|
|
|
@post.self_edits += 1 if @editor == @post.user
|
2013-02-09 23:33:07 +08:00
|
|
|
|
2014-03-28 13:45:51 +08:00
|
|
|
if @editor == @post.user && @post.hidden && @post.hidden_reason_id == Post.hidden_reasons[:flag_threshold_reached]
|
2014-07-29 01:17:37 +08:00
|
|
|
PostAction.clear_flags!(@post, Discourse.system_user)
|
2014-08-11 16:48:00 +08:00
|
|
|
@post.unhide!
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2013-03-19 03:54:08 +08:00
|
|
|
@post.extract_quoted_post_numbers
|
2014-01-01 03:37:43 +08:00
|
|
|
@post.save(validate: !@opts[:skip_validations])
|
|
|
|
|
2013-03-19 03:54:08 +08:00
|
|
|
@post.save_reply_relationships
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
def update_category_description
|
|
|
|
# If we're revising the first post, we might have to update the category description
|
|
|
|
return unless @post.post_number == 1
|
|
|
|
|
|
|
|
# Is there a category with our topic id?
|
2014-05-06 21:41:59 +08:00
|
|
|
category = Category.find_by(topic_id: @post.topic_id)
|
2013-02-22 07:09:56 +08:00
|
|
|
return unless category.present?
|
|
|
|
|
|
|
|
# If found, update its description
|
|
|
|
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-03-19 01:40:40 +08:00
|
|
|
def update_topic_excerpt
|
|
|
|
@post.topic.update_column(:excerpt, @post.excerpt(220, strip_links: true)) if @post.post_number == 1
|
|
|
|
end
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
def post_process_post
|
|
|
|
@post.invalidate_oneboxes = true
|
|
|
|
@post.trigger_post_process
|
|
|
|
end
|
|
|
|
end
|