From b097cf4997ddc0126cc3c93c17808863f3423162 Mon Sep 17 00:00:00 2001 From: Wojciech Zawistowski Date: Mon, 17 Feb 2014 01:57:37 -0500 Subject: [PATCH] Refactors PostCreator. --- lib/post_creator.rb | 49 ++++++--------------------------- lib/post_jobs_enqueuer.rb | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 lib/post_jobs_enqueuer.rb diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 59d1aabd9..8aaf6248d 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -2,6 +2,7 @@ # require_dependency 'rate_limiter' require_dependency 'topic_creator' +require_dependency 'post_jobs_enqueuer' class PostCreator @@ -51,7 +52,6 @@ class PostCreator def create @topic = nil @post = nil - @new_topic = false Post.transaction do setup_topic @@ -138,35 +138,6 @@ class PostCreator end end - def after_post_create - return if @topic.private_message? || @post.post_type == Post.types[:moderator_action] - - if @post.post_number > 1 - TopicTrackingState.publish_unread(@post) - end - - if SiteSetting.enable_mailing_list_mode - Jobs.enqueue_in( - SiteSetting.email_time_window_mins.minutes, - :notify_mailing_list_subscribers, - post_id: @post.id - ) - end - end - - def after_topic_create - return unless @new_topic - # Don't publish invisible topics - return unless @topic.visible? - return if @topic.private_message? || @post.post_type == Post.types[:moderator_action] - - @topic.posters = @topic.posters_summary - @topic.posts_count = 1 - - TopicTrackingState.publish_new(@topic) - end - - def clear_possible_flags(topic) # at this point we know the topic is a PM and has been replied to ... check if we need to clear any flags # @@ -189,7 +160,7 @@ class PostCreator private def setup_topic - if @opts[:topic_id].blank? + if new_topic? topic_creator = TopicCreator.new(@user, guardian, @opts) begin @@ -200,8 +171,6 @@ class PostCreator @errors = topic_creator.errors raise ex end - - @new_topic = true else topic = Topic.where(id: @opts[:topic_id]).first guardian.ensure_can_create!(Post, topic) @@ -301,13 +270,11 @@ class PostCreator def enqueue_jobs return unless @post && !@post.errors.present? - - # We need to enqueue jobs after the transaction. Otherwise they might begin before the data has - # been comitted. - topic_id = @opts[:topic_id] || @topic.try(:id) - Jobs.enqueue(:feature_topic_users, topic_id: @topic.id) if topic_id.present? - @post.trigger_post_process - after_post_create - after_topic_create + PostJobsEnqueuer.new(@post, @topic, new_topic?).enqueue_jobs end + + def new_topic? + @opts[:topic_id].blank? + end + end diff --git a/lib/post_jobs_enqueuer.rb b/lib/post_jobs_enqueuer.rb new file mode 100644 index 000000000..33c98259b --- /dev/null +++ b/lib/post_jobs_enqueuer.rb @@ -0,0 +1,58 @@ +class PostJobsEnqueuer + def initialize(post, topic, new_topic) + @post = post + @topic = topic + @new_topic = new_topic + end + + def enqueue_jobs + # We need to enqueue jobs after the transaction. Otherwise they might begin before the data has + # been comitted. + feature_topic_users + trigger_post_post_process + unless skip_after_create? + after_post_create + after_topic_create + end + end + + + private + + def feature_topic_users + Jobs.enqueue(:feature_topic_users, topic_id: @topic.id) + end + + def trigger_post_post_process + @post.trigger_post_process + end + + def after_post_create + if @post.post_number > 1 + TopicTrackingState.publish_unread(@post) + end + + if SiteSetting.enable_mailing_list_mode + Jobs.enqueue_in( + SiteSetting.email_time_window_mins.minutes, + :notify_mailing_list_subscribers, + post_id: @post.id + ) + end + end + + def after_topic_create + return unless @new_topic + # Don't publish invisible topics + return unless @topic.visible? + + @topic.posters = @topic.posters_summary + @topic.posts_count = 1 + + TopicTrackingState.publish_new(@topic) + end + + def skip_after_create? + @topic.private_message? || @post.post_type == Post.types[:moderator_action] + end +end