2013-03-19 05:52:29 +08:00
|
|
|
#
|
|
|
|
# How a post is deleted is affected by who is performing the action.
|
|
|
|
# this class contains the logic to delete it.
|
|
|
|
#
|
|
|
|
class PostDestroyer
|
|
|
|
|
2014-09-26 01:51:00 +08:00
|
|
|
def self.destroy_old_hidden_posts
|
|
|
|
Post.where(deleted_at: nil)
|
|
|
|
.where("hidden_at < ?", 30.days.ago)
|
|
|
|
.find_each do |post|
|
|
|
|
PostDestroyer.new(Discourse.system_user, post).destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-22 15:48:24 +08:00
|
|
|
def self.destroy_stubs
|
2013-07-23 14:11:44 +08:00
|
|
|
# exclude deleted topics and posts that are actively flagged
|
2013-07-22 15:48:24 +08:00
|
|
|
Post.where(deleted_at: nil, user_deleted: true)
|
2013-07-23 14:11:44 +08:00
|
|
|
.where("NOT EXISTS (
|
|
|
|
SELECT 1 FROM topics t
|
|
|
|
WHERE t.deleted_at IS NOT NULL AND
|
|
|
|
t.id = posts.topic_id
|
|
|
|
)")
|
2013-08-03 01:35:42 +08:00
|
|
|
.where("updated_at < ? AND post_number > 1", SiteSetting.delete_removed_posts_after.hours.ago)
|
2013-07-23 14:11:44 +08: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 15:48:24 +08:00
|
|
|
PostDestroyer.new(Discourse.system_user, post).destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-01 23:40:13 +08:00
|
|
|
def initialize(user, post, opts={})
|
2014-08-08 01:12:35 +08:00
|
|
|
@user = user
|
|
|
|
@post = post
|
2014-08-15 03:21:10 +08:00
|
|
|
@topic = post.topic if post
|
2014-10-01 23:40:13 +08:00
|
|
|
@opts = opts
|
2013-03-19 05:52:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2014-10-07 04:29:20 +08:00
|
|
|
if @user.staff? || SiteSetting.delete_removed_posts_after < 1
|
|
|
|
perform_delete
|
2013-03-19 05:52:29 +08:00
|
|
|
elsif @user.id == @post.user_id
|
2014-10-07 04:29:20 +08:00
|
|
|
mark_for_deletion
|
2013-03-19 05:52:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-22 15:48:24 +08:00
|
|
|
def recover
|
|
|
|
if @user.staff? && @post.deleted_at
|
|
|
|
staff_recovered
|
|
|
|
elsif @user.staff? || @user.id == @post.user_id
|
|
|
|
user_recovered
|
|
|
|
end
|
2014-08-08 01:12:35 +08:00
|
|
|
@post.topic.recover! if @post.post_number == 1
|
2013-07-22 15:48:24 +08:00
|
|
|
@post.topic.update_statistics
|
|
|
|
end
|
|
|
|
|
|
|
|
def staff_recovered
|
|
|
|
@post.recover!
|
2014-08-29 11:34:32 +08:00
|
|
|
@post.publish_change_to_clients! :recovered
|
2013-07-22 15:48:24 +08:00
|
|
|
end
|
|
|
|
|
2013-03-19 05:52:29 +08:00
|
|
|
# When a post is properly deleted. Well, it's still soft deleted, but it will no longer
|
|
|
|
# show up in the topic
|
2014-10-07 04:29:20 +08:00
|
|
|
def perform_delete
|
2013-03-19 05:52:29 +08:00
|
|
|
Post.transaction do
|
2014-02-06 20:54:34 +08:00
|
|
|
@post.trash!(@user)
|
2013-07-23 23:50:58 +08:00
|
|
|
if @post.topic
|
2014-02-06 20:54:34 +08:00
|
|
|
make_previous_post_the_last_one
|
|
|
|
clear_user_posted_flag
|
|
|
|
feature_users_in_the_topic
|
|
|
|
Topic.reset_highest(@post.topic_id)
|
2013-07-10 03:20:18 +08:00
|
|
|
end
|
2014-07-29 01:17:37 +08:00
|
|
|
trash_public_post_actions
|
|
|
|
agree_with_flags
|
2014-06-04 23:41:11 +08:00
|
|
|
trash_user_actions
|
2013-03-19 05:52:29 +08:00
|
|
|
@post.update_flagged_posts_count
|
2014-02-06 20:54:34 +08:00
|
|
|
remove_associated_replies
|
|
|
|
remove_associated_notifications
|
2014-10-01 23:40:13 +08:00
|
|
|
if @post.topic && @post.post_number == 1
|
2014-10-07 04:29:20 +08:00
|
|
|
StaffActionLogger.new(@user).log_topic_deletion(@post.topic, @opts.slice(:context)) if @user.id != @post.user_id
|
2014-10-01 23:40:13 +08:00
|
|
|
@post.topic.trash!(@user)
|
2014-10-07 04:29:20 +08:00
|
|
|
elsif @user.id != @post.user_id
|
2014-10-01 23:40:13 +08:00
|
|
|
StaffActionLogger.new(@user).log_post_deletion(@post, @opts.slice(:context))
|
|
|
|
end
|
2014-02-06 20:54:34 +08:00
|
|
|
update_associated_category_latest_topic
|
2014-08-15 03:21:10 +08:00
|
|
|
update_user_counts
|
2013-03-19 05:52:29 +08:00
|
|
|
end
|
2014-06-04 12:10:34 +08:00
|
|
|
|
2014-08-29 11:34:32 +08:00
|
|
|
@post.publish_change_to_clients! :deleted if @post.topic
|
2013-03-19 05:52:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# When a user 'deletes' their own post. We just change the text.
|
2014-10-07 04:29:20 +08:00
|
|
|
def mark_for_deletion
|
2013-03-19 05:52:29 +08:00
|
|
|
Post.transaction do
|
2014-10-28 05:06:43 +08:00
|
|
|
@post.revise(@user, { raw: I18n.t('js.post.deleted_by_author', count: SiteSetting.delete_removed_posts_after) }, force_new_version: true)
|
2013-03-19 05:52:29 +08:00
|
|
|
@post.update_column(:user_deleted, true)
|
|
|
|
@post.update_flagged_posts_count
|
2013-06-14 01:41:45 +08:00
|
|
|
@post.topic_links.each(&:destroy)
|
2013-03-19 05:52:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-22 15:48:24 +08:00
|
|
|
def user_recovered
|
|
|
|
Post.transaction do
|
|
|
|
@post.update_column(:user_deleted, false)
|
2013-09-06 23:50:05 +08:00
|
|
|
@post.skip_unique_check = true
|
2014-10-28 05:06:43 +08:00
|
|
|
@post.revise(@user, { raw: @post.revisions.last.modifications["raw"][0] }, force_new_version: true)
|
2013-07-22 15:48:24 +08:00
|
|
|
@post.update_flagged_posts_count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-06 20:54:34 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-07-29 01:17:37 +08:00
|
|
|
def trash_public_post_actions
|
|
|
|
public_post_actions = PostAction.publics.where(post_id: @post.id)
|
|
|
|
public_post_actions.each { |pa| pa.trash!(@user) }
|
2014-02-06 20:54:34 +08:00
|
|
|
|
2014-08-15 05:54:55 +08:00
|
|
|
f = PostActionType.public_types.map { |k, _| ["#{k}_count", 0] }
|
2014-02-06 20:54:34 +08:00
|
|
|
Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten])
|
|
|
|
end
|
|
|
|
|
2014-07-29 01:17:37 +08:00
|
|
|
def agree_with_flags
|
|
|
|
PostAction.agree_flags!(@post, @user, delete_post: true)
|
|
|
|
end
|
|
|
|
|
2014-06-04 23:41:11 +08:00
|
|
|
def trash_user_actions
|
|
|
|
UserAction.where(target_post_id: @post.id).each do |ua|
|
|
|
|
row = {
|
|
|
|
action_type: ua.action_type,
|
|
|
|
user_id: ua.user_id,
|
|
|
|
acting_user_id: ua.acting_user_id,
|
|
|
|
target_topic_id: ua.target_topic_id,
|
|
|
|
target_post_id: ua.target_post_id
|
|
|
|
}
|
|
|
|
UserAction.remove_action!(row)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-06 20:54:34 +08:00
|
|
|
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
|
|
|
|
|
2014-08-15 03:21:10 +08:00
|
|
|
def update_user_counts
|
|
|
|
author = @post.user
|
|
|
|
|
|
|
|
return unless author
|
|
|
|
|
|
|
|
author.create_user_stat if author.user_stat.nil?
|
|
|
|
|
|
|
|
if @post.created_at == author.user_stat.first_post_created_at
|
|
|
|
author.user_stat.first_post_created_at = author.posts.order('created_at ASC').first.try(:created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
author.user_stat.post_count -= 1
|
|
|
|
author.user_stat.topic_count -= 1 if @post.post_number == 1
|
|
|
|
|
|
|
|
# We don't count replies to your own topics
|
|
|
|
if @topic && author.id != @topic.user_id
|
|
|
|
author.user_stat.update_topic_reply_count
|
|
|
|
end
|
|
|
|
|
|
|
|
author.user_stat.save!
|
|
|
|
|
|
|
|
if @post.created_at == author.last_posted_at
|
|
|
|
author.last_posted_at = author.posts.order('created_at DESC').first.try(:created_at)
|
|
|
|
author.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-03 15:56:23 +08:00
|
|
|
end
|