Refactors PostCreator.

This commit is contained in:
Wojciech Zawistowski 2014-02-17 01:57:37 -05:00
parent 70c50d321a
commit b097cf4997
2 changed files with 66 additions and 41 deletions

View file

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

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