2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2014-09-25 13:51:00 -04:00
|
|
|
def self.destroy_old_hidden_posts
|
2015-12-29 16:59:26 -05:00
|
|
|
Post
|
|
|
|
.where(deleted_at: nil, hidden: true)
|
2014-09-25 13:51:00 -04:00
|
|
|
.where("hidden_at < ?", 30.days.ago)
|
|
|
|
.find_each { |post| PostDestroyer.new(Discourse.system_user, post).destroy }
|
|
|
|
end
|
|
|
|
|
2013-07-22 03:48:24 -04:00
|
|
|
def self.destroy_stubs
|
2017-04-24 22:19:21 -04:00
|
|
|
context = I18n.t("remove_posts_deleted_by_author")
|
|
|
|
|
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
|
|
|
|
)",
|
|
|
|
)
|
2019-05-07 09:25:52 -04:00
|
|
|
.where("updated_at < ?", SiteSetting.delete_removed_posts_after.hours.ago)
|
2013-07-23 02:11:44 -04:00
|
|
|
.where(
|
|
|
|
"NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM post_actions pa
|
2018-08-09 14:54:23 -04:00
|
|
|
WHERE pa.post_id = posts.id
|
|
|
|
AND pa.deleted_at IS NULL
|
|
|
|
AND pa.deferred_at IS NULL
|
|
|
|
AND pa.post_action_type_id IN (?)
|
2013-07-23 02:11:44 -04:00
|
|
|
)",
|
|
|
|
PostActionType.notify_flag_type_ids,
|
|
|
|
)
|
2017-04-24 22:19:21 -04:00
|
|
|
.find_each { |post| PostDestroyer.new(Discourse.system_user, post, context: context).destroy }
|
2013-07-22 03:48:24 -04:00
|
|
|
end
|
|
|
|
|
2019-11-27 15:19:44 -05:00
|
|
|
def self.delete_with_replies(performed_by, post, reviewable = nil, defer_reply_flags: true)
|
2019-05-27 12:27:16 -04:00
|
|
|
reply_ids = post.reply_ids(Guardian.new(performed_by), only_replies_to_single_post: false)
|
|
|
|
replies = Post.where(id: reply_ids.map { |r| r[:id] })
|
2019-08-30 09:27:52 -04:00
|
|
|
PostDestroyer.new(performed_by, post, reviewable: reviewable).destroy
|
2021-11-24 10:28:20 -05:00
|
|
|
|
|
|
|
options = { defer_flags: defer_reply_flags }
|
|
|
|
if SiteSetting.notify_users_after_responses_deleted_on_flagged_post
|
|
|
|
options.merge!({ reviewable: reviewable, notify_responders: true, parent_post: post })
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2021-11-24 10:28:20 -05:00
|
|
|
replies.each { |reply| PostDestroyer.new(performed_by, reply, options).destroy }
|
2019-05-27 12:27:16 -04:00
|
|
|
end
|
|
|
|
|
2014-10-01 11:40:13 -04:00
|
|
|
def initialize(user, post, opts = {})
|
2014-08-07 13:12:35 -04:00
|
|
|
@user = user
|
|
|
|
@post = post
|
2023-01-27 09:15:33 -05:00
|
|
|
@topic = post.topic || Topic.with_deleted.find_by(id: @post.topic_id)
|
2014-10-01 11:40:13 -04:00
|
|
|
@opts = opts
|
2013-03-18 17:52:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2023-10-08 23:35:31 -04:00
|
|
|
payload = WebHook.generate_payload(:post, @post) if WebHook.active_web_hooks(
|
|
|
|
:post_destroyed,
|
|
|
|
).exists?
|
2023-01-27 09:15:33 -05:00
|
|
|
is_first_post = @post.is_first_post? && @topic
|
2023-10-08 23:35:31 -04:00
|
|
|
has_topic_web_hooks = is_first_post && WebHook.active_web_hooks(:topic_destroyed).exists?
|
2018-10-05 04:53:59 -04:00
|
|
|
|
2020-09-01 20:10:42 -04:00
|
|
|
if has_topic_web_hooks
|
2023-01-27 09:15:33 -05:00
|
|
|
topic_view = TopicView.new(@topic.id, Discourse.system_user, skip_staff_action: true)
|
2020-09-01 20:10:42 -04:00
|
|
|
topic_payload = WebHook.generate_payload(:topic, topic_view, WebHookTopicViewSerializer)
|
2018-10-05 04:53:59 -04:00
|
|
|
end
|
|
|
|
|
2017-06-15 23:27:51 -04:00
|
|
|
delete_removed_posts_after =
|
|
|
|
@opts[:delete_removed_posts_after] || SiteSetting.delete_removed_posts_after
|
|
|
|
|
2020-11-09 23:40:48 -05:00
|
|
|
if delete_removed_posts_after < 1 || post_is_reviewable? ||
|
2023-01-27 09:15:33 -05:00
|
|
|
Guardian.new(@user).can_moderate_topic?(@topic) || permanent?
|
2014-10-06 16:29:20 -04:00
|
|
|
perform_delete
|
2013-03-18 17:52:29 -04:00
|
|
|
elsif @user.id == @post.user_id
|
2017-06-15 23:27:51 -04:00
|
|
|
mark_for_deletion(delete_removed_posts_after)
|
2013-03-18 17:52:29 -04:00
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
UserActionManager.post_destroyed(@post)
|
|
|
|
|
2015-05-15 03:24:48 -04:00
|
|
|
DiscourseEvent.trigger(:post_destroyed, @post, @opts, @user)
|
2019-03-06 12:22:54 -05:00
|
|
|
WebHook.enqueue_post_hooks(:post_destroyed, @post, payload)
|
2023-01-27 09:15:33 -05:00
|
|
|
Jobs.enqueue(:sync_topic_user_bookmarked, topic_id: @topic.id) if @topic
|
2016-09-05 07:00:49 -04:00
|
|
|
|
2019-12-04 15:13:31 -05:00
|
|
|
if is_first_post
|
2023-01-27 09:15:33 -05:00
|
|
|
UserProfile.remove_featured_topic_from_all_profiles(@topic)
|
|
|
|
UserActionManager.topic_destroyed(@topic)
|
|
|
|
DiscourseEvent.trigger(:topic_destroyed, @topic, @user)
|
|
|
|
WebHook.enqueue_topic_hooks(:topic_destroyed, @topic, topic_payload) if has_topic_web_hooks
|
2023-06-07 14:31:20 -04:00
|
|
|
if SiteSetting.tos_topic_id == @topic.id || SiteSetting.privacy_topic_id == @topic.id
|
|
|
|
Discourse.clear_urls!
|
|
|
|
end
|
2016-09-05 07:00:49 -04:00
|
|
|
end
|
2013-03-18 17:52:29 -04:00
|
|
|
end
|
|
|
|
|
2013-07-22 03:48:24 -04:00
|
|
|
def recover
|
2020-11-05 12:18:26 -05:00
|
|
|
if (post_is_reviewable? || Guardian.new(@user).can_moderate_topic?(@post.topic)) &&
|
|
|
|
@post.deleted_at
|
2013-07-22 03:48:24 -04:00
|
|
|
staff_recovered
|
|
|
|
elsif @user.staff? || @user.id == @post.user_id
|
|
|
|
user_recovered
|
|
|
|
end
|
2023-01-27 09:15:33 -05:00
|
|
|
|
|
|
|
@topic.update_column(:user_id, Discourse::SYSTEM_USER_ID) if !@topic.user_id
|
|
|
|
@topic.recover!(@user) if @post.is_first_post?
|
|
|
|
@topic.update_statistics
|
|
|
|
Topic.publish_stats_to_clients!(@topic.id, :recovered)
|
2021-06-15 18:30:40 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
UserActionManager.post_created(@post)
|
2015-05-15 03:24:48 -04:00
|
|
|
DiscourseEvent.trigger(:post_recovered, @post, @opts, @user)
|
2023-01-27 09:15:33 -05:00
|
|
|
Jobs.enqueue(:sync_topic_user_bookmarked, topic_id: @topic.id) if @topic
|
2023-01-17 18:13:45 -05:00
|
|
|
Jobs.enqueue(:notify_mailing_list_subscribers, post_id: @post.id)
|
2021-06-15 18:30:40 -04:00
|
|
|
|
2018-03-21 00:15:16 -04:00
|
|
|
if @post.is_first_post?
|
2023-01-27 09:15:33 -05:00
|
|
|
UserActionManager.topic_created(@topic)
|
|
|
|
DiscourseEvent.trigger(:topic_recovered, @topic, @user)
|
2018-03-21 00:15:16 -04:00
|
|
|
if @user.id != @post.user_id
|
|
|
|
StaffActionLogger.new(@user).log_topic_delete_recover(
|
2023-01-27 09:15:33 -05:00
|
|
|
@topic,
|
2018-03-21 00:15:16 -04:00
|
|
|
"recover_topic",
|
|
|
|
@opts.slice(:context),
|
|
|
|
)
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2020-08-11 20:16:26 -04:00
|
|
|
update_imap_sync(@post, false)
|
2023-06-07 14:31:20 -04:00
|
|
|
if SiteSetting.tos_topic_id == @topic.id || SiteSetting.privacy_topic_id == @topic.id
|
|
|
|
Discourse.clear_urls!
|
|
|
|
end
|
2018-03-21 00:15:16 -04:00
|
|
|
end
|
2013-07-22 03:48:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def staff_recovered
|
2020-07-16 10:15:01 -04:00
|
|
|
new_post_attrs = { user_deleted: false }
|
|
|
|
new_post_attrs[:user_id] = Discourse::SYSTEM_USER_ID if !@post.user_id
|
|
|
|
@post.update_columns(new_post_attrs)
|
2013-07-22 03:48:24 -04:00
|
|
|
@post.recover!
|
2016-06-12 23:25:06 -04:00
|
|
|
|
2019-03-28 02:28:01 -04:00
|
|
|
mark_topic_changed
|
|
|
|
|
2018-07-03 22:50:51 -04:00
|
|
|
if @post.topic && !@post.topic.private_message?
|
|
|
|
if author = @post.user
|
|
|
|
if @post.is_first_post?
|
|
|
|
author.user_stat.topic_count += 1
|
|
|
|
else
|
|
|
|
author.user_stat.post_count += 1
|
|
|
|
end
|
|
|
|
author.user_stat.save!
|
2017-11-09 18:05:53 -05:00
|
|
|
end
|
2016-06-12 23:25:06 -04:00
|
|
|
|
2018-07-03 22:50:51 -04:00
|
|
|
if @post.is_first_post?
|
|
|
|
# Update stats of all people who replied
|
2022-02-15 20:49:11 -05:00
|
|
|
update_post_counts(:increment)
|
2017-11-09 18:05:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-27 17:21:05 -04:00
|
|
|
# skip also publishing topic stats because they weren't updated yet
|
|
|
|
@post.publish_change_to_clients! :recovered, { skip_topic_stats: true }
|
2017-11-09 18:05:53 -05:00
|
|
|
TopicTrackingState.publish_recover(@post.topic) if @post.topic && @post.is_first_post?
|
2013-07-22 03:48:24 -04:00
|
|
|
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
|
2020-11-09 23:40:48 -05:00
|
|
|
# Permanent option allows to hard delete.
|
2014-10-06 16:29:20 -04:00
|
|
|
def perform_delete
|
2022-08-10 05:11:50 -04:00
|
|
|
# All posts in the topic must be force deleted if the first is force
|
|
|
|
# deleted (except @post which is destroyed by current instance).
|
|
|
|
if @topic && @post.is_first_post? && permanent?
|
|
|
|
@topic.ordered_posts.with_deleted.reverse_order.find_each do |post|
|
|
|
|
PostDestroyer.new(@user, post, @opts).destroy if post.id != @post.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-18 17:52:29 -04:00
|
|
|
Post.transaction do
|
2020-11-09 23:40:48 -05:00
|
|
|
permanent? ? @post.destroy! : @post.trash!(@user)
|
2013-07-23 11:50:58 -04:00
|
|
|
if @post.topic
|
2014-02-06 07:54:34 -05:00
|
|
|
make_previous_post_the_last_one
|
2019-03-28 02:28:01 -04:00
|
|
|
mark_topic_changed
|
2014-02-06 07:54:34 -05:00
|
|
|
clear_user_posted_flag
|
2013-07-09 15:20:18 -04:00
|
|
|
end
|
2022-02-06 22:23:34 -05:00
|
|
|
|
|
|
|
Topic.reset_highest(@post.topic_id)
|
2014-07-28 13:17:37 -04:00
|
|
|
trash_public_post_actions
|
2021-03-24 21:34:53 -04:00
|
|
|
trash_revisions
|
2014-06-04 11:41:11 -04:00
|
|
|
trash_user_actions
|
2014-02-06 07:54:34 -05:00
|
|
|
remove_associated_replies
|
|
|
|
remove_associated_notifications
|
2020-09-23 02:55:39 -04:00
|
|
|
|
|
|
|
if @user.id != @post.user_id && !@opts[:skip_staff_log]
|
|
|
|
if @post.topic && @post.is_first_post?
|
|
|
|
StaffActionLogger.new(@user).log_topic_delete_recover(
|
|
|
|
@post.topic,
|
2024-07-23 03:27:11 -04:00
|
|
|
permanent? ? "delete_topic_permanently" : "delete_topic",
|
2020-09-23 02:55:39 -04:00
|
|
|
@opts.slice(:context),
|
|
|
|
)
|
|
|
|
else
|
2024-07-23 03:27:11 -04:00
|
|
|
StaffActionLogger.new(@user).log_post_deletion(
|
|
|
|
@post,
|
|
|
|
**@opts.slice(:context),
|
|
|
|
permanent: permanent?,
|
|
|
|
)
|
2020-09-23 02:55:39 -04:00
|
|
|
end
|
2014-10-01 11:40:13 -04:00
|
|
|
end
|
2020-09-23 02:55:39 -04:00
|
|
|
|
2023-01-27 09:15:33 -05:00
|
|
|
if @topic && @post.is_first_post?
|
|
|
|
permanent? ? @topic.destroy! : @topic.trash!(@user)
|
|
|
|
PublishedPage.unpublish!(@user, @topic) if @topic.published_page
|
2020-11-09 23:40:48 -05:00
|
|
|
end
|
2023-01-27 09:15:33 -05:00
|
|
|
|
2021-06-01 14:02:53 -04:00
|
|
|
TopicLink.where(link_post_id: @post.id).destroy_all
|
2014-02-06 07:54:34 -05:00
|
|
|
update_associated_category_latest_topic
|
2022-02-06 22:23:34 -05:00
|
|
|
update_user_counts if !permanent?
|
2015-06-17 19:58:32 -04:00
|
|
|
TopicUser.update_post_action_cache(post_id: @post.id)
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2024-07-23 03:27:11 -04:00
|
|
|
if permanent?
|
|
|
|
if @post.topic && @post.is_first_post?
|
|
|
|
UserHistory.where(topic_id: @post.topic.id).update_all(details: "(permanently deleted)")
|
|
|
|
end
|
|
|
|
UserHistory.where(post_id: @post.id).update_all(details: "(permanently deleted)")
|
|
|
|
end
|
|
|
|
|
2018-11-21 21:25:19 -05:00
|
|
|
DB.after_commit do
|
2019-08-30 09:27:52 -04:00
|
|
|
if @opts[:reviewable]
|
2021-11-24 10:28:20 -05:00
|
|
|
notify_deletion(
|
|
|
|
@opts[:reviewable],
|
|
|
|
{ notify_responders: @opts[:notify_responders], parent_post: @opts[:parent_post] },
|
|
|
|
)
|
2022-01-05 13:37:15 -05:00
|
|
|
if @post.reviewable_flag &&
|
|
|
|
SiteSetting.notify_users_after_responses_deleted_on_flagged_post
|
|
|
|
ignore(@post.reviewable_flag)
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2019-08-30 09:27:52 -04:00
|
|
|
elsif reviewable = @post.reviewable_flag
|
|
|
|
@opts[:defer_flags] ? ignore(reviewable) : agree(reviewable)
|
2018-11-29 12:14:18 -05:00
|
|
|
end
|
2018-11-21 21:25:19 -05:00
|
|
|
end
|
2013-03-18 17:52:29 -04:00
|
|
|
end
|
2014-06-04 00:10:34 -04:00
|
|
|
|
2020-08-11 20:16:26 -04:00
|
|
|
update_imap_sync(@post, true) if @post.topic&.deleted_at
|
2015-04-06 03:27:05 -04:00
|
|
|
feature_users_in_the_topic if @post.topic
|
2020-11-09 23:40:48 -05:00
|
|
|
@post.publish_change_to_clients!(permanent? ? :destroyed : :deleted) if @post.topic
|
|
|
|
if @post.topic && @post.post_number == 1
|
|
|
|
TopicTrackingState.send(permanent? ? :publish_destroy : :publish_delete, @post.topic)
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2020-11-09 23:40:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def permanent?
|
2021-02-01 16:57:31 -05:00
|
|
|
@opts[:force_destroy] ||
|
|
|
|
(@opts[:permanent] && @user == @post.user && @post.topic.private_message?)
|
2013-03-18 17:52:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# When a user 'deletes' their own post. We just change the text.
|
2017-06-15 23:27:51 -04:00
|
|
|
def mark_for_deletion(delete_removed_posts_after = SiteSetting.delete_removed_posts_after)
|
2015-01-24 21:19:57 -05:00
|
|
|
I18n.with_locale(SiteSetting.default_locale) do
|
2016-06-22 03:28:46 -04:00
|
|
|
# don't call revise from within transaction, high risk of deadlock
|
2021-05-24 22:04:10 -04:00
|
|
|
key =
|
2023-01-09 07:10:19 -05:00
|
|
|
(
|
2021-05-24 22:04:10 -04:00
|
|
|
if @post.is_first_post?
|
|
|
|
"js.topic.deleted_by_author_simple"
|
2023-01-09 07:10:19 -05:00
|
|
|
else
|
2021-05-24 22:04:10 -04:00
|
|
|
"js.post.deleted_by_author_simple"
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
|
|
|
)
|
2017-06-15 23:27:51 -04:00
|
|
|
@post.revise(
|
|
|
|
@user,
|
2021-05-24 22:04:10 -04:00
|
|
|
{ raw: I18n.t(key) },
|
2019-06-20 11:23:49 -04:00
|
|
|
force_new_version: true,
|
2021-11-07 20:33:41 -05:00
|
|
|
deleting_post: true,
|
|
|
|
skip_validations: true,
|
2017-06-15 23:27:51 -04:00
|
|
|
)
|
2016-06-22 03:28:46 -04:00
|
|
|
|
2015-01-24 21:19:57 -05:00
|
|
|
Post.transaction do
|
|
|
|
@post.update_column(:user_deleted, true)
|
|
|
|
@post.topic_links.each(&:destroy)
|
2019-05-07 09:25:52 -04:00
|
|
|
@post.topic.update_column(:closed, true) if @post.is_first_post?
|
2015-01-24 21:19:57 -05:00
|
|
|
end
|
2013-03-18 17:52:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-22 03:48:24 -04:00
|
|
|
def user_recovered
|
2019-04-11 16:21:00 -04:00
|
|
|
return unless @post.user_deleted?
|
|
|
|
|
2013-07-22 03:48:24 -04:00
|
|
|
Post.transaction do
|
|
|
|
@post.update_column(:user_deleted, false)
|
2013-09-06 11:50:05 -04:00
|
|
|
@post.skip_unique_check = true
|
2019-05-07 09:25:52 -04:00
|
|
|
@post.topic.update_column(:closed, false) if @post.is_first_post?
|
2013-07-22 03:48:24 -04:00
|
|
|
end
|
2016-06-22 03:28:46 -04:00
|
|
|
|
|
|
|
# has internal transactions, if we nest then there are some very high risk deadlocks
|
2018-09-21 04:34:45 -04:00
|
|
|
last_revision = @post.revisions.last
|
2019-04-10 13:10:21 -04:00
|
|
|
if last_revision.present? && last_revision.modifications["raw"].present?
|
|
|
|
@post.revise(@user, { raw: last_revision.modifications["raw"][0] }, force_new_version: true)
|
|
|
|
end
|
2013-07-22 03:48:24 -04:00
|
|
|
end
|
|
|
|
|
2014-02-06 07:54:34 -05:00
|
|
|
private
|
|
|
|
|
2020-07-21 21:57:16 -04:00
|
|
|
def post_is_reviewable?
|
2020-11-05 12:18:26 -05:00
|
|
|
return true if @user.staff?
|
|
|
|
|
2023-01-27 09:15:33 -05:00
|
|
|
Guardian.new(@user).can_review_topic?(@topic) && Reviewable.exists?(target: @post)
|
2020-07-21 21:57:16 -04:00
|
|
|
end
|
|
|
|
|
2019-03-28 02:28:01 -04:00
|
|
|
# we need topics to change if ever a post in them is deleted or created
|
|
|
|
# this ensures users relying on this information can keep unread tracking
|
|
|
|
# working as desired
|
|
|
|
def mark_topic_changed
|
|
|
|
# make this as fast as possible, can bypass everything
|
|
|
|
DB.exec(<<~SQL, updated_at: Time.now, id: @post.topic_id)
|
|
|
|
UPDATE topics
|
|
|
|
SET updated_at = :updated_at
|
|
|
|
WHERE id = :id
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2014-02-06 07:54:34 -05:00
|
|
|
def make_previous_post_the_last_one
|
2021-02-01 16:57:31 -05:00
|
|
|
last_post =
|
|
|
|
Post
|
2019-03-28 02:28:01 -04:00
|
|
|
.select(:created_at, :user_id, :post_number)
|
|
|
|
.where("topic_id = ? and id <> ?", @post.topic_id, @post.id)
|
2021-02-01 16:57:31 -05:00
|
|
|
.where.not(user_id: nil)
|
2021-07-23 02:50:28 -04:00
|
|
|
.where.not(post_type: Post.types[:whisper])
|
2019-03-28 02:28:01 -04:00
|
|
|
.order("created_at desc")
|
|
|
|
.first
|
|
|
|
|
2020-11-16 15:40:36 -05:00
|
|
|
if last_post.present?
|
2018-07-13 12:15:59 -04:00
|
|
|
topic = @post.topic
|
|
|
|
topic.last_posted_at = last_post.created_at
|
|
|
|
topic.last_post_user_id = last_post.user_id
|
|
|
|
topic.highest_post_number = last_post.post_number
|
2019-03-28 02:28:01 -04:00
|
|
|
|
|
|
|
# we go via save here cause we need to run hooks
|
2018-07-13 12:15:59 -04:00
|
|
|
topic.save!(validate: false)
|
2014-02-06 07:54:34 -05:00
|
|
|
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
|
2015-04-06 03:27:05 -04:00
|
|
|
Jobs.enqueue(:feature_topic_users, topic_id: @post.topic_id)
|
2014-02-06 07:54:34 -05:00
|
|
|
end
|
|
|
|
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-13 22:13:46 -04:00
|
|
|
def post_action_type_view
|
|
|
|
@post_action_type_view ||= PostActionTypeView.new
|
|
|
|
end
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
def trash_public_post_actions
|
2018-10-02 11:25:08 -04:00
|
|
|
if public_post_actions = PostAction.publics.where(post_id: @post.id)
|
2020-11-16 15:40:36 -05:00
|
|
|
public_post_actions.each { |pa| permanent? ? pa.destroy! : pa.trash!(@user) }
|
|
|
|
|
|
|
|
return if permanent?
|
2014-02-06 07:54:34 -05:00
|
|
|
|
2018-10-02 11:25:08 -04:00
|
|
|
@post.custom_fields["deleted_public_actions"] = public_post_actions.ids
|
|
|
|
@post.save_custom_fields
|
|
|
|
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-13 22:13:46 -04:00
|
|
|
f = post_action_type_view.public_types.map { |k, _| ["#{k}_count", 0] }
|
2018-10-02 11:25:08 -04:00
|
|
|
Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten])
|
|
|
|
end
|
2014-02-06 07:54:34 -05:00
|
|
|
end
|
|
|
|
|
2021-03-24 21:34:53 -04:00
|
|
|
def trash_revisions
|
|
|
|
return unless permanent?
|
|
|
|
@post.revisions.each(&:destroy!)
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def agree(reviewable)
|
2019-08-30 09:27:52 -04:00
|
|
|
notify_deletion(reviewable)
|
2019-01-03 12:03:01 -05:00
|
|
|
result = reviewable.perform(@user, :agree_and_keep, post_was_deleted: true)
|
|
|
|
reviewable.transition_to(result.transition_to, @user)
|
2018-11-26 10:58:37 -05:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def ignore(reviewable)
|
2023-03-02 10:40:53 -05:00
|
|
|
reviewable.perform_ignore_and_do_nothing(@user, post_was_deleted: true)
|
2019-01-03 12:03:01 -05:00
|
|
|
reviewable.transition_to(:ignored, @user)
|
2018-11-29 12:14:18 -05:00
|
|
|
end
|
|
|
|
|
2021-11-24 10:28:20 -05:00
|
|
|
def notify_deletion(reviewable, options = {})
|
2020-03-11 08:03:20 -04:00
|
|
|
return if @post.user.blank?
|
|
|
|
|
2019-08-30 09:27:52 -04:00
|
|
|
allowed_user = @user.human? && @user.staff?
|
|
|
|
return unless allowed_user && rs = reviewable.reviewable_scores.order("created_at DESC").first
|
|
|
|
|
2022-11-29 12:18:07 -05:00
|
|
|
# ReviewableScore#types is a superset of PostActionType#flag_types.
|
|
|
|
# If the reviewable score type is not on the latter, it means it's not a flag by a user and
|
|
|
|
# must be an automated flag like `needs_approval`. There's no flag reason for these kind of types.
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-13 22:13:46 -04:00
|
|
|
flag_type = post_action_type_view.flag_types[rs.reviewable_score_type]
|
2022-11-29 12:18:07 -05:00
|
|
|
return unless flag_type
|
|
|
|
|
2021-11-24 10:28:20 -05:00
|
|
|
notify_responders = options[:notify_responders]
|
|
|
|
|
2019-08-30 09:27:52 -04:00
|
|
|
Jobs.enqueue(
|
|
|
|
:send_system_message,
|
|
|
|
user_id: @post.user_id,
|
2022-08-13 11:52:31 -04:00
|
|
|
message_type:
|
2023-01-09 07:10:19 -05:00
|
|
|
(
|
2022-08-13 11:52:31 -04:00
|
|
|
if notify_responders
|
|
|
|
"flags_agreed_and_post_deleted_for_responders"
|
2023-01-09 07:10:19 -05:00
|
|
|
else
|
2022-08-13 11:52:31 -04:00
|
|
|
"flags_agreed_and_post_deleted"
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2022-08-13 11:52:31 -04:00
|
|
|
),
|
2019-08-30 09:27:52 -04:00
|
|
|
message_options: {
|
2021-11-24 10:28:20 -05:00
|
|
|
flagged_post_raw_content: notify_responders ? options[:parent_post].raw : @post.raw,
|
2022-01-05 13:37:15 -05:00
|
|
|
flagged_post_response_raw_content: @post.raw,
|
2021-11-24 10:28:20 -05:00
|
|
|
url: notify_responders ? options[:parent_post].url : @post.url,
|
2019-08-30 09:27:52 -04:00
|
|
|
flag_reason:
|
|
|
|
I18n.t(
|
2022-11-29 12:18:07 -05:00
|
|
|
"flag_reasons#{".responder" if notify_responders}.#{flag_type}",
|
2019-08-30 09:27:52 -04:00
|
|
|
locale: SiteSetting.default_locale,
|
|
|
|
base_path: Discourse.base_path,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-06-04 11:41:11 -04: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 07:54:34 -05:00
|
|
|
def remove_associated_replies
|
2020-01-17 11:24:49 -05:00
|
|
|
post_ids = PostReply.where(reply_post_id: @post.id).pluck(:post_id)
|
2014-02-06 07:54:34 -05:00
|
|
|
|
|
|
|
if post_ids.present?
|
2020-01-17 11:24:49 -05:00
|
|
|
PostReply.where(reply_post_id: @post.id).delete_all
|
2014-02-06 07:54:34 -05:00
|
|
|
Post.where(id: post_ids).each { |p| p.update_column :reply_count, p.replies.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_associated_notifications
|
2017-08-31 00:06:56 -04:00
|
|
|
Notification.where(topic_id: @post.topic_id, post_number: @post.post_number).delete_all
|
2014-02-06 07:54:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_associated_category_latest_topic
|
|
|
|
return unless @post.topic && @post.topic.category
|
2023-02-16 04:40:11 -05:00
|
|
|
if @post.id != @post.topic.category.latest_post_id &&
|
|
|
|
!(@post.is_first_post? && @post.topic_id == @post.topic.category.latest_topic_id)
|
2015-04-23 13:33:29 -04:00
|
|
|
return
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2014-02-06 07:54:34 -05:00
|
|
|
|
|
|
|
@post.topic.category.update_latest
|
|
|
|
end
|
|
|
|
|
2014-08-14 15:21:10 -04: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
|
2022-02-06 22:23:34 -05:00
|
|
|
author.user_stat.update!(
|
|
|
|
first_post_created_at: author.posts.order("created_at ASC").first.try(:created_at),
|
|
|
|
)
|
2017-11-02 16:43:09 -04:00
|
|
|
end
|
2014-08-14 15:21:10 -04:00
|
|
|
|
2022-02-06 22:23:34 -05:00
|
|
|
UserStatCountUpdater.decrement!(@post)
|
2014-08-14 15:21:10 -04:00
|
|
|
|
|
|
|
if @post.created_at == author.last_posted_at
|
2022-02-24 19:20:54 -05:00
|
|
|
author.update_column(
|
|
|
|
:last_posted_at,
|
|
|
|
author.posts.order("created_at DESC").first.try(:created_at),
|
|
|
|
)
|
2014-08-14 15:21:10 -04:00
|
|
|
end
|
2017-11-02 15:33:40 -04:00
|
|
|
|
|
|
|
if @post.is_first_post? && @post.topic && !@post.topic.private_message?
|
|
|
|
# Update stats of all people who replied
|
2022-02-15 20:49:11 -05:00
|
|
|
update_post_counts(:decrement)
|
2017-11-02 15:33:40 -04:00
|
|
|
end
|
2014-08-14 15:21:10 -04:00
|
|
|
end
|
|
|
|
|
2020-08-11 20:16:26 -04:00
|
|
|
def update_imap_sync(post, sync)
|
|
|
|
return if !SiteSetting.enable_imap
|
|
|
|
incoming = IncomingEmail.find_by(post_id: post.id, topic_id: post.topic_id)
|
|
|
|
return if !incoming || !incoming.imap_uid
|
|
|
|
incoming.update(imap_sync: sync)
|
|
|
|
end
|
|
|
|
|
2022-02-15 20:49:11 -05:00
|
|
|
def update_post_counts(operator)
|
|
|
|
counts =
|
|
|
|
Post
|
|
|
|
.where(post_type: Post.types[:regular], topic_id: @post.topic_id)
|
|
|
|
.where("post_number > 1")
|
|
|
|
.group(:user_id)
|
|
|
|
.count
|
|
|
|
|
|
|
|
counts.each do |user_id, count|
|
|
|
|
if user_stat = UserStat.where(user_id: user_id).first
|
|
|
|
if operator == :decrement
|
|
|
|
UserStatCountUpdater.set!(
|
|
|
|
user_stat: user_stat,
|
|
|
|
count: user_stat.post_count - count,
|
|
|
|
count_column: :post_count,
|
|
|
|
)
|
|
|
|
else
|
|
|
|
UserStatCountUpdater.set!(
|
|
|
|
user_stat: user_stat,
|
|
|
|
count: user_stat.post_count + count,
|
|
|
|
count_column: :post_count,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-05-03 03:56:23 -04:00
|
|
|
end
|