<commit_before># frozen_string_literal: true

require 'excon'

class Akismet
  class Error < StandardError; end

  class Client
    PLUGIN_NAME = 'discourse-akismet'.freeze
    VALID_COMMENT_CHECK_RESPONSE = %w{true false}.each(&:freeze)
    VALID_SUBMIT_RESPONSE = 'Thanks for making the web a better place.'.freeze
    UNKNOWN_ERROR_MESSAGE = 'Unknown error'.freeze
    DEBUG_HEADER = 'X-akismet-debug-help'.freeze

    def initialize(api_key:, base_url:)
      @api_url =  "https://#{api_key}.rest.akismet.com/1.1"
      @base_url = base_url
    end

    def self.with_client(api_key:, base_url:)
      client = self.new(api_key: api_key, base_url: base_url)
      yield client if block_given?
    end

    def comment_check(body)
      response = post('comment-check', body)
      response_body = response.body

      if !(VALID_COMMENT_CHECK_RESPONSE.include?(response_body))
        debug_help = response.headers[DEBUG_HEADER] || UNKNOWN_ERROR_MESSAGE
        raise Akismet::Error.new(debug_help)
      end

      response_body == VALID_COMMENT_CHECK_RESPONSE.first
    end

    def submit_spam(body)
      submit_feedback('submit-spam', body)
    end

    def submit_ham(body)
      submit_feedback('submit-ham', body)
    end

    private

    def submit_feedback(method, body)
      response = post(method, body)
      response_body = response.body

      if response_body != VALID_SUBMIT_RESPONSE
        raise Akismet::Error.new(UNKNOWN_ERROR_MESSAGE)
      end

      true
    end

    # From https://akismet.com/development/api/#detailed-docs
    #  If possible, your user agent string should always use the following format:
    #  Application Name/Version | Plugin Name/Version
    def self.user_agent_string
      @user_agent_string ||= begin
        plugin_version = Discourse.plugins.find do |plugin|
          plugin.name == PLUGIN_NAME
        end.metadata.version

        "Discourse/#{Discourse::VERSION::STRING} | #{PLUGIN_NAME}/#{plugin_version}"
      end
    end

    def post(path, body)
      # Send a maximum of 32000 chars which is the default for
      # maximum post length site settings.
      body[:comment_content] = body[:comment_content].strip[0..31999] if body[:comment_content].present?

      response = Excon.post("#{@api_url}/#{path}",
        body: body.merge(blog: @base_url).to_query,
        headers: {
          'Content-Type' => 'application/x-www-form-urlencoded',
          'User-Agent' => self.class.user_agent_string
        }
      )

      if response.status != 200
        raise Akismet::Error.new(response.status_line)
      end

      response
    end
  end
end<commit_msg>Revert "Check for comment_content before stripping"<commit_after># frozen_string_literal: true

require 'excon'

class Akismet
  class Error < StandardError; end

  class Client
    PLUGIN_NAME = 'discourse-akismet'.freeze
    VALID_COMMENT_CHECK_RESPONSE = %w{true false}.each(&:freeze)
    VALID_SUBMIT_RESPONSE = 'Thanks for making the web a better place.'.freeze
    UNKNOWN_ERROR_MESSAGE = 'Unknown error'.freeze
    DEBUG_HEADER = 'X-akismet-debug-help'.freeze

    def initialize(api_key:, base_url:)
      @api_url =  "https://#{api_key}.rest.akismet.com/1.1"
      @base_url = base_url
    end

    def self.with_client(api_key:, base_url:)
      client = self.new(api_key: api_key, base_url: base_url)
      yield client if block_given?
    end

    def comment_check(body)
      response = post('comment-check', body)
      response_body = response.body

      if !(VALID_COMMENT_CHECK_RESPONSE.include?(response_body))
        debug_help = response.headers[DEBUG_HEADER] || UNKNOWN_ERROR_MESSAGE
        raise Akismet::Error.new(debug_help)
      end

      response_body == VALID_COMMENT_CHECK_RESPONSE.first
    end

    def submit_spam(body)
      submit_feedback('submit-spam', body)
    end

    def submit_ham(body)
      submit_feedback('submit-ham', body)
    end

    private

    def submit_feedback(method, body)
      response = post(method, body)
      response_body = response.body

      if response_body != VALID_SUBMIT_RESPONSE
        raise Akismet::Error.new(UNKNOWN_ERROR_MESSAGE)
      end

      true
    end

    # From https://akismet.com/development/api/#detailed-docs
    #  If possible, your user agent string should always use the following format:
    #  Application Name/Version | Plugin Name/Version
    def self.user_agent_string
      @user_agent_string ||= begin
        plugin_version = Discourse.plugins.find do |plugin|
          plugin.name == PLUGIN_NAME
        end.metadata.version

        "Discourse/#{Discourse::VERSION::STRING} | #{PLUGIN_NAME}/#{plugin_version}"
      end
    end

    def post(path, body)
      # Send a maximum of 32000 chars which is the default for
      # maximum post length site settings.
      body[:comment_content] = body[:comment_content].strip[0..31999]

      response = Excon.post("#{@api_url}/#{path}",
        body: body.merge(blog: @base_url).to_query,
        headers: {
          'Content-Type' => 'application/x-www-form-urlencoded',
          'User-Agent' => self.class.user_agent_string
        }
      )

      if response.status != 200
        raise Akismet::Error.new(response.status_line)
      end

      response
    end
  end
end
{'fraction_non_alphanumeric': 0.10051301539046172, 'fraction_numerical': 0.0060801824054721645, 'mean_word_length': 2.925428784489187, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '6667586', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1660, 'n_words': 350}