<commit_before>#
# How a post is deleted is affected by who is performing the action.
# this class contains the logic to delete it.
#
class PostDestroyer

  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

  def self.destroy_stubs
    # exclude deleted topics and posts that are actively flagged
    Post.where(deleted_at: nil, user_deleted: true)
        .where("NOT EXISTS (
            SELECT 1 FROM topics t
            WHERE t.deleted_at IS NOT NULL AND
                  t.id = posts.topic_id
        )")
        .where("updated_at < ? AND post_number > 1", SiteSetting.delete_removed_posts_after.hours.ago)
        .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|
      PostDestroyer.new(Discourse.system_user, post).destroy
    end
  end

  def initialize(user, post, opts={})
    @user = user
    @post = post
    @topic = post.topic if post
    @opts = opts
  end

  def destroy
    if @user.staff? || SiteSetting.delete_removed_posts_after < 1
      perform_delete
    elsif @user.id == @post.user_id
      mark_for_deletion
    end
  end

  def recover
    if @user.staff? && @post.deleted_at
      staff_recovered
    elsif @user.staff? || @user.id == @post.user_id
      user_recovered
    end
    topic = Topic.with_deleted.find @post.topic_id
    topic.recover! if @post.post_number == 1
    topic.update_statistics
  end

  def staff_recovered
    @post.recover!
    @post.publish_change_to_clients! :recovered
  end

  # When a post is properly deleted. Well, it's still soft deleted, but it will no longer
  # show up in the topic
  def perform_delete
    Post.transaction do
      @post.trash!(@user)
      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_public_post_actions
      agree_with_flags
      trash_user_actions
      @post.update_flagged_posts_count
      remove_associated_replies
      remove_associated_notifications
      if @post.topic && @post.post_number == 1
        StaffActionLogger.new(@user).log_topic_deletion(@post.topic, @opts.slice(:context)) if @user.id != @post.user_id
        @post.topic.trash!(@user)
      elsif @user.id != @post.user_id
        StaffActionLogger.new(@user).log_post_deletion(@post, @opts.slice(:context))
      end
      update_associated_category_latest_topic
      update_user_counts
      TopicUser.update_post_action_cache(topic_id: @post.topic_id)
    end

    @post.publish_change_to_clients! :deleted if @post.topic
  end

  # When a user 'deletes' their own post. We just change the text.
  def mark_for_deletion
    Post.transaction do
      @post.revise(@user, { raw: I18n.t('js.post.deleted_by_author', count: SiteSetting.delete_removed_posts_after) }, force_new_version: true)
      @post.update_column(:user_deleted, true)
      @post.update_flagged_posts_count
      @post.topic_links.each(&:destroy)
    end
  end

  def user_recovered
    Post.transaction do
      @post.update_column(:user_deleted, false)
      @post.skip_unique_check = true
      @post.revise(@user, { raw: @post.revisions.last.modifications["raw"][0] }, force_new_version: true)
      @post.update_flagged_posts_count
    end
  end

<commit_msg>FIX: :sa: Use site-wide locale for deletion stubs<commit_after>#
# How a post is deleted is affected by who is performing the action.
# this class contains the logic to delete it.
#
class PostDestroyer

  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

  def self.destroy_stubs
    # exclude deleted topics and posts that are actively flagged
    Post.where(deleted_at: nil, user_deleted: true)
        .where("NOT EXISTS (
            SELECT 1 FROM topics t
            WHERE t.deleted_at IS NOT NULL AND
                  t.id = posts.topic_id
        )")
        .where("updated_at < ? AND post_number > 1", SiteSetting.delete_removed_posts_after.hours.ago)
        .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|
      PostDestroyer.new(Discourse.system_user, post).destroy
    end
  end

  def initialize(user, post, opts={})
    @user = user
    @post = post
    @topic = post.topic if post
    @opts = opts
  end

  def destroy
    if @user.staff? || SiteSetting.delete_removed_posts_after < 1
      perform_delete
    elsif @user.id == @post.user_id
      mark_for_deletion
    end
  end

  def recover
    if @user.staff? && @post.deleted_at
      staff_recovered
    elsif @user.staff? || @user.id == @post.user_id
      user_recovered
    end
    topic = Topic.with_deleted.find @post.topic_id
    topic.recover! if @post.post_number == 1
    topic.update_statistics
  end

  def staff_recovered
    @post.recover!
    @post.publish_change_to_clients! :recovered
  end

  # When a post is properly deleted. Well, it's still soft deleted, but it will no longer
  # show up in the topic
  def perform_delete
    Post.transaction do
      @post.trash!(@user)
      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_public_post_actions
      agree_with_flags
      trash_user_actions
      @post.update_flagged_posts_count
      remove_associated_replies
      remove_associated_notifications
      if @post.topic && @post.post_number == 1
        StaffActionLogger.new(@user).log_topic_deletion(@post.topic, @opts.slice(:context)) if @user.id != @post.user_id
        @post.topic.trash!(@user)
      elsif @user.id != @post.user_id
        StaffActionLogger.new(@user).log_post_deletion(@post, @opts.slice(:context))
      end
      update_associated_category_latest_topic
      update_user_counts
      TopicUser.update_post_action_cache(topic_id: @post.topic_id)
    end

    @post.publish_change_to_clients! :deleted if @post.topic
  end

  # When a user 'deletes' their own post. We just change the text.
  def mark_for_deletion
    I18n.with_locale(SiteSetting.default_locale) do
      Post.transaction do
        @post.revise(@user, { raw: I18n.t('js.post.deleted_by_author', count: SiteSetting.delete_removed_posts_after) }, force_new_version: true)
        @post.update_column(:user_deleted, true)
        @post.update_flagged_posts_count
        @post.topic_links.each(&:destroy)
      end
    end
  end

  def user_recovered
    Post.transaction do
      @post.update_column(:user_deleted, false)
      @post.skip_unique_check = true
      @post.revise(@user, { raw: @post.revisions.last.modifications["raw"][0] }, force_new_version: true)
      @post.update_flagged_posts_count
    end
  end


{'fraction_non_alphanumeric': 0.08692163680128377, 'fraction_numerical': 0.0032094142818935543, 'mean_word_length': 2.655425219941349, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '76325', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2373, 'n_words': 534}