Merge pull request #1938 from velesin/post_creator_jobs_extraction

Refactors PostCreator.
This commit is contained in:
Régis Hanol 2014-02-17 09:52:52 +01:00
commit 40b8e09122
2 changed files with 66 additions and 41 deletions

View file

@ -2,6 +2,7 @@
# #
require_dependency 'rate_limiter' require_dependency 'rate_limiter'
require_dependency 'topic_creator' require_dependency 'topic_creator'
require_dependency 'post_jobs_enqueuer'
class PostCreator class PostCreator
@ -51,7 +52,6 @@ class PostCreator
def create def create
@topic = nil @topic = nil
@post = nil @post = nil
@new_topic = false
Post.transaction do Post.transaction do
setup_topic setup_topic
@ -138,35 +138,6 @@ class PostCreator
end end
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) 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 # 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 private
def setup_topic def setup_topic
if @opts[:topic_id].blank? if new_topic?
topic_creator = TopicCreator.new(@user, guardian, @opts) topic_creator = TopicCreator.new(@user, guardian, @opts)
begin begin
@ -200,8 +171,6 @@ class PostCreator
@errors = topic_creator.errors @errors = topic_creator.errors
raise ex raise ex
end end
@new_topic = true
else else
topic = Topic.where(id: @opts[:topic_id]).first topic = Topic.where(id: @opts[:topic_id]).first
guardian.ensure_can_create!(Post, topic) guardian.ensure_can_create!(Post, topic)
@ -301,13 +270,11 @@ class PostCreator
def enqueue_jobs def enqueue_jobs
return unless @post && !@post.errors.present? return unless @post && !@post.errors.present?
PostJobsEnqueuer.new(@post, @topic, new_topic?).enqueue_jobs
# 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
end end
def new_topic?
@opts[:topic_id].blank?
end
end end

58
lib/post_jobs_enqueuer.rb Normal file
View file

@ -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