2013-03-18 17:52:29 -04:00
|
|
|
#
|
|
|
|
# How a post is deleted is affected by who is performing the action.
|
|
|
|
# this class contains the logic to delete it.
|
|
|
|
#
|
|
|
|
class PostDestroyer
|
|
|
|
|
2013-07-22 03:48:24 -04:00
|
|
|
def self.destroy_stubs
|
2013-07-23 02:11:44 -04:00
|
|
|
# exclude deleted topics and posts that are actively flagged
|
2013-07-22 03:48:24 -04:00
|
|
|
Post.where(deleted_at: nil, user_deleted: true)
|
2013-07-23 02:11:44 -04:00
|
|
|
.where("NOT EXISTS (
|
|
|
|
SELECT 1 FROM topics t
|
|
|
|
WHERE t.deleted_at IS NOT NULL AND
|
|
|
|
t.id = posts.topic_id
|
|
|
|
)")
|
2013-08-02 13:35:42 -04:00
|
|
|
.where("updated_at < ? AND post_number > 1", SiteSetting.delete_removed_posts_after.hours.ago)
|
2013-07-23 02:11:44 -04:00
|
|
|
.where("NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM post_actions pa
|
|
|
|
WHERE pa.post_id = posts.id AND
|
|
|
|
pa.deleted_at IS NULL AND
|
|
|
|
pa.post_action_type_id IN (?)
|
|
|
|
)", PostActionType.notify_flag_type_ids)
|
|
|
|
.each do |post|
|
2013-07-22 03:48:24 -04:00
|
|
|
PostDestroyer.new(Discourse.system_user, post).destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-18 17:52:29 -04:00
|
|
|
def initialize(user, post)
|
|
|
|
@user, @post = user, post
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2013-05-03 03:56:23 -04:00
|
|
|
if @user.staff?
|
|
|
|
staff_destroyed
|
2013-03-18 17:52:29 -04:00
|
|
|
elsif @user.id == @post.user_id
|
|
|
|
user_destroyed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-22 03:48:24 -04:00
|
|
|
def recover
|
|
|
|
if @user.staff? && @post.deleted_at
|
|
|
|
staff_recovered
|
|
|
|
elsif @user.staff? || @user.id == @post.user_id
|
|
|
|
user_recovered
|
|
|
|
end
|
|
|
|
@post.topic.update_statistics
|
|
|
|
end
|
|
|
|
|
|
|
|
def staff_recovered
|
|
|
|
@post.recover!
|
|
|
|
end
|
|
|
|
|
2013-03-18 17:52:29 -04:00
|
|
|
# When a post is properly deleted. Well, it's still soft deleted, but it will no longer
|
|
|
|
# show up in the topic
|
2013-05-03 03:56:23 -04:00
|
|
|
def staff_destroyed
|
2013-03-18 17:52:29 -04:00
|
|
|
Post.transaction do
|
|
|
|
|
2013-07-23 11:50:58 -04:00
|
|
|
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
|
2013-03-18 17:52:29 -04:00
|
|
|
end
|
|
|
|
|
2013-07-23 11:50:58 -04:00
|
|
|
# Feature users in the topic
|
|
|
|
Jobs.enqueue(:feature_topic_users, topic_id: @post.topic_id, except_post_id: @post.id)
|
|
|
|
end
|
2013-03-18 17:52:29 -04:00
|
|
|
|
2013-07-09 15:20:18 -04:00
|
|
|
@post.post_actions.each do |pa|
|
|
|
|
pa.trash!(@user)
|
|
|
|
end
|
2013-06-05 16:00:45 -04:00
|
|
|
|
|
|
|
f = PostActionType.types.map{|k,v| ["#{k}_count", 0]}
|
2013-07-01 14:45:52 -04:00
|
|
|
Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten])
|
2013-06-05 16:00:45 -04:00
|
|
|
|
2013-07-09 15:20:18 -04:00
|
|
|
@post.trash!(@user)
|
2013-03-18 17:52:29 -04:00
|
|
|
|
2013-07-23 11:50:58 -04:00
|
|
|
Topic.reset_highest(@post.topic_id) if @post.topic
|
2013-06-05 16:00:45 -04:00
|
|
|
|
2013-03-18 17:52:29 -04:00
|
|
|
@post.update_flagged_posts_count
|
|
|
|
|
|
|
|
# Remove any reply records that point to deleted posts
|
2013-05-17 15:11:37 -04:00
|
|
|
post_ids = PostReply.where(reply_id: @post.id).pluck(:post_id)
|
2013-03-18 17:52:29 -04:00
|
|
|
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
|
2013-06-05 16:00:45 -04:00
|
|
|
|
2013-07-23 11:50:58 -04:00
|
|
|
@post.topic.trash!(@user) if @post.topic and @post.post_number == 1
|
2013-03-18 17:52:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# When a user 'deletes' their own post. We just change the text.
|
|
|
|
def user_destroyed
|
|
|
|
Post.transaction do
|
2013-08-02 13:35:42 -04:00
|
|
|
@post.revise(@user, I18n.t('js.post.deleted_by_author', count: SiteSetting.delete_removed_posts_after), force_new_version: true)
|
2013-03-18 17:52:29 -04:00
|
|
|
@post.update_column(:user_deleted, true)
|
|
|
|
@post.update_flagged_posts_count
|
2013-06-13 13:41:45 -04:00
|
|
|
@post.topic_links.each(&:destroy)
|
2013-03-18 17:52:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-22 03:48:24 -04:00
|
|
|
def user_recovered
|
|
|
|
Post.transaction do
|
|
|
|
@post.update_column(:user_deleted, false)
|
|
|
|
@post.revise(@user, @post.versions.last.modifications["raw"][0], force_new_version: true)
|
|
|
|
@post.update_flagged_posts_count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-03 03:56:23 -04:00
|
|
|
end
|