<commit_before>module DiscourseAkismet
  class AdminModQueueController < Admin::AdminController
    requires_plugin 'discourse-akismet'

    def index
      render_json_dump(
        posts: serialize_data(DiscourseAkismet.needs_review, AkismetPostSerializer),
        enabled: SiteSetting.akismet_enabled?,
        stats: DiscourseAkismet.stats
      )
    end

    def confirm_spam
      post = Post.with_deleted.find(params[:post_id])
      DiscourseAkismet.move_to_state(post, 'confirmed_spam')
      log_confirmation(post, 'confirmed_spam')
      render body: nil
    end

    def allow
      post = Post.with_deleted.find(params[:post_id])

      Jobs.enqueue(:update_akismet_status, post_id: post.id, status: 'ham')

      # It's possible the post was recovered already
      if post.deleted_at
        PostDestroyer.new(current_user, post).recover
      end

      DiscourseAkismet.move_to_state(post, 'confirmed_ham')
      log_confirmation(post, 'confirmed_ham')

      render body: nil
    end

    def dismiss
      post = Post.with_deleted.find(params[:post_id])

      DiscourseAkismet.move_to_state(post, 'dismissed')
      log_confirmation(post, 'dismissed')

      render body: nil
    end

    def delete_user
      post = Post.with_deleted.find(params[:post_id])
      user = post.user
      DiscourseAkismet.move_to_state(post, 'confirmed_spam')
      log_confirmation(post, 'confirmed_spam_deleted')

      if guardian.can_delete_user?(user)
        UserDestroyer.new(current_user).destroy(user, user_deletion_opts)
      end

      render body: nil
    end

    private

      def log_confirmation(post, custom_type)
        topic = post.topic || Topic.with_deleted.find(post.topic_id)

        StaffActionLogger.new(current_user).log_custom(custom_type,
          post_id: post.id,
          topic_id: topic.id,
          created_at: post.created_at,
          topic: topic.title,
          post_number: post.post_number,
          raw: post.raw
        )
      end

      def user_deletion_opts
        base = {
          context:           I18n.t('akismet.delete_reason', performed_by: current_user.username),
          delete_posts:      true,
          delete_as_spammer: true
        }

        if Rails.env.production? && ENV["Staging"].nil?
          base.merge!(block_email: true, block_ip: true)
        end

        base
      end
  end
end
<commit_msg>DEV: Apply rubocop<commit_after>module DiscourseAkismet
  class AdminModQueueController < Admin::AdminController
    requires_plugin 'discourse-akismet'

    def index
      render_json_dump(
        posts: serialize_data(DiscourseAkismet.needs_review, AkismetPostSerializer),
        enabled: SiteSetting.akismet_enabled?,
        stats: DiscourseAkismet.stats
      )
    end

    def confirm_spam
      post = Post.with_deleted.find(params[:post_id])
      DiscourseAkismet.move_to_state(post, 'confirmed_spam')
      log_confirmation(post, 'confirmed_spam')
      render body: nil
    end

    def allow
      post = Post.with_deleted.find(params[:post_id])

      Jobs.enqueue(:update_akismet_status, post_id: post.id, status: 'ham')

      # It's possible the post was recovered already
      if post.deleted_at
        PostDestroyer.new(current_user, post).recover
      end

      DiscourseAkismet.move_to_state(post, 'confirmed_ham')
      log_confirmation(post, 'confirmed_ham')

      render body: nil
    end

    def dismiss
      post = Post.with_deleted.find(params[:post_id])

      DiscourseAkismet.move_to_state(post, 'dismissed')
      log_confirmation(post, 'dismissed')

      render body: nil
    end

    def delete_user
      post = Post.with_deleted.find(params[:post_id])
      user = post.user
      DiscourseAkismet.move_to_state(post, 'confirmed_spam')
      log_confirmation(post, 'confirmed_spam_deleted')

      if guardian.can_delete_user?(user)
        UserDestroyer.new(current_user).destroy(user, user_deletion_opts)
      end

      render body: nil
    end

    private

    def log_confirmation(post, custom_type)
      topic = post.topic || Topic.with_deleted.find(post.topic_id)

      StaffActionLogger.new(current_user).log_custom(custom_type,
        post_id: post.id,
        topic_id: topic.id,
        created_at: post.created_at,
        topic: topic.title,
        post_number: post.post_number,
        raw: post.raw
      )
    end

    def user_deletion_opts
      base = {
        context:           I18n.t('akismet.delete_reason', performed_by: current_user.username),
        delete_posts:      true,
        delete_as_spammer: true
      }

      if Rails.env.production? && ENV["Staging"].nil?
        base.merge!(block_email: true, block_ip: true)
      end

      base
    end
  end
end

{'fraction_non_alphanumeric': 0.08729655463961107, 'fraction_numerical': 0.0008454872120059184, 'mean_word_length': 2.7172034564021996, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '677395', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1489, 'n_words': 270}