Refactors PostDestroyer

This commit is contained in:
Wojciech Zawistowski 2014-02-06 07:54:34 -05:00
parent c4cfd5a7fe
commit 4485ae7b05
1 changed files with 63 additions and 50 deletions

View File

@ -54,59 +54,19 @@ class PostDestroyer
# show up in the topic # show up in the topic
def staff_destroyed def staff_destroyed
Post.transaction do Post.transaction do
if @post.topic
# Update the last post id to the previous post if it exists
last_post = Post.where("topic_id = ? and id <> ?", @post.topic_id, @post.id).order('created_at desc').limit(1).first
if last_post.present?
@post.topic.update_attributes(last_posted_at: last_post.created_at,
last_post_user_id: last_post.user_id,
highest_post_number: last_post.post_number)
# If the poster doesn't have any other posts in the topic, clear their posted flag
unless Post.exists?(["topic_id = ? and user_id = ? and id <> ?", @post.topic_id, @post.user_id, @post.id])
TopicUser.where(topic_id: @post.topic_id, user_id: @post.user_id).update_all 'posted = false'
end
end
# Feature users in the topic
Jobs.enqueue(:feature_topic_users, topic_id: @post.topic_id, except_post_id: @post.id)
end
@post.post_actions.each do |pa|
pa.trash!(@user)
end
f = PostActionType.types.map{|k,v| ["#{k}_count", 0]}
Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten])
@post.trash!(@user) @post.trash!(@user)
if @post.topic
Topic.reset_highest(@post.topic_id) if @post.topic make_previous_post_the_last_one
clear_user_posted_flag
feature_users_in_the_topic
Topic.reset_highest(@post.topic_id)
end
trash_post_actions
@post.update_flagged_posts_count @post.update_flagged_posts_count
remove_associated_replies
# Remove any reply records that point to deleted posts remove_associated_notifications
post_ids = PostReply.where(reply_id: @post.id).pluck(:post_id)
PostReply.delete_all reply_id: @post.id
if post_ids.present?
Post.where(id: post_ids).each { |p| p.update_column :reply_count, p.replies.count }
end
# Remove any notifications that point to this deleted post
Notification.delete_all topic_id: @post.topic_id, post_number: @post.post_number
@post.topic.trash!(@user) if @post.topic and @post.post_number == 1 @post.topic.trash!(@user) if @post.topic and @post.post_number == 1
update_associated_category_latest_topic
if @post.topic && @post.topic.category && @post.id == @post.topic.category.latest_post_id
@post.topic.category.update_latest
end
if @post.post_number == 1 && @post.topic && @post.topic.category && @post.topic_id == @post.topic.category.latest_topic_id
@post.topic.category.update_latest
end
end end
end end
@ -129,4 +89,57 @@ class PostDestroyer
end end
end end
private
def make_previous_post_the_last_one
last_post = Post.where("topic_id = ? and id <> ?", @post.topic_id, @post.id).order('created_at desc').limit(1).first
if last_post.present?
@post.topic.update_attributes(
last_posted_at: last_post.created_at,
last_post_user_id: last_post.user_id,
highest_post_number: last_post.post_number
)
end
end
def clear_user_posted_flag
unless Post.exists?(["topic_id = ? and user_id = ? and id <> ?", @post.topic_id, @post.user_id, @post.id])
TopicUser.where(topic_id: @post.topic_id, user_id: @post.user_id).update_all 'posted = false'
end
end
def feature_users_in_the_topic
Jobs.enqueue(:feature_topic_users, topic_id: @post.topic_id, except_post_id: @post.id)
end
def trash_post_actions
@post.post_actions.each do |pa|
pa.trash!(@user)
end
f = PostActionType.types.map{|k,v| ["#{k}_count", 0]}
Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten])
end
def remove_associated_replies
post_ids = PostReply.where(reply_id: @post.id).pluck(:post_id)
if post_ids.present?
PostReply.delete_all reply_id: @post.id
Post.where(id: post_ids).each { |p| p.update_column :reply_count, p.replies.count }
end
end
def remove_associated_notifications
Notification.delete_all topic_id: @post.topic_id, post_number: @post.post_number
end
def update_associated_category_latest_topic
return unless @post.topic && @post.topic.category
return unless @post.id == @post.topic.category.latest_post_id || (@post.post_number == 1 && @post.topic_id == @post.topic.category.latest_topic_id)
@post.topic.category.update_latest
end
end end