<commit_before># frozen_string_literal: true

module DiscourseAkismet
  class UsersBouncer
    VALID_STATUSES = %w[spam ham]

    def enqueue_for_check(user)
      return unless SiteSetting.akismet_review_users
      profile = user.user_profile
      return if user.trust_level > TrustLevel[0] || profile.bio_raw.blank?

      Jobs.enqueue(:check_users_for_spam, user_id: user.id)
    end

    def check_user(client, user)
      return if Reviewable.exists?(target: user)

      if client.comment_check(args_for_user(user))
        spam_reporter = Discourse.system_user

        reviewable = ReviewableAkismetUser.needs_review!(
          target: user, reviewable_by_moderator: true,
          created_by: spam_reporter,
          payload: { username: user.username, name: user.name, email: user.email, bio: user.user_profile.bio_raw }
        )

        reviewable.add_score(
          spam_reporter, PostActionType.types[:spam],
          created_at: reviewable.created_at,
          reason: 'akismet_spam_user'
        )
      end
    end

    def submit_feedback(user, status)
      raise Discourse::InvalidParameters.new(:status) unless VALID_STATUSES.include?(status)
      feedback = args_for_user(user)

      Jobs.enqueue(:update_akismet_status, feedback: feedback, status: status)
    end

    private

    def args_for_user(user)
      user_auth_token = user.user_auth_token_logs.last
      profile = user.user_profile

      extra_args = {
        content_type: 'signup',
        permalink: "#{Discourse.base_url}/u/#{user.username_lower}",
        comment_author: user.username,
        comment_content: profile.bio_raw,
        user_ip: user_auth_token.client_ip.to_s,
        user_agent: user_auth_token.user_agent,
        comment_author_url: profile.website
      }

      # Sending the email to akismet is optional
      if SiteSetting.akismet_transmit_email?
        extra_args[:comment_author_email] = user.email
      end

      extra_args
    end
  end
end<commit_msg>FIX: don't send user IP/UA when not available<commit_after># frozen_string_literal: true

module DiscourseAkismet
  class UsersBouncer
    VALID_STATUSES = %w[spam ham]

    def enqueue_for_check(user)
      return unless SiteSetting.akismet_review_users
      profile = user.user_profile
      return if user.trust_level > TrustLevel[0] || profile.bio_raw.blank?

      Jobs.enqueue(:check_users_for_spam, user_id: user.id)
    end

    def check_user(client, user)
      return if Reviewable.exists?(target: user)

      if client.comment_check(args_for_user(user))
        spam_reporter = Discourse.system_user

        reviewable = ReviewableAkismetUser.needs_review!(
          target: user, reviewable_by_moderator: true,
          created_by: spam_reporter,
          payload: { username: user.username, name: user.name, email: user.email, bio: user.user_profile.bio_raw }
        )

        reviewable.add_score(
          spam_reporter, PostActionType.types[:spam],
          created_at: reviewable.created_at,
          reason: 'akismet_spam_user'
        )
      end
    end

    def submit_feedback(user, status)
      raise Discourse::InvalidParameters.new(:status) unless VALID_STATUSES.include?(status)
      feedback = args_for_user(user)

      Jobs.enqueue(:update_akismet_status, feedback: feedback, status: status)
    end

    private

    def args_for_user(user)
      profile = user.user_profile

      extra_args = {
        content_type: 'signup',
        permalink: "#{Discourse.base_url}/u/#{user.username_lower}",
        comment_author: user.username,
        comment_content: profile.bio_raw,
        comment_author_url: profile.website,
      }

      # Sending the email to akismet is optional
      if SiteSetting.akismet_transmit_email?
        extra_args[:comment_author_email] = user.email
      end

      if token = user.user_auth_token_logs.last
        extra_args[:user_ip] = token.client_ip.to_s
        extra_args[:user_agent] = token.user_agent
      end

      extra_args
    end
  end
end
{'fraction_non_alphanumeric': 0.08202833706189411, 'fraction_numerical': 0.0004971414367387521, 'mean_word_length': 2.9450980392156865, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4996684', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1256, 'n_words': 240}