<commit_before># frozen_string_literal: true

require 'open-uri'

class User < ApplicationRecord
  include Websocket

  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :feedbacks, dependent: :nullify
  has_many :api_tokens, dependent: :destroy
  has_many :api_keys, dependent: :nullify
  has_many :user_site_settings, dependent: :destroy
  has_many :flag_conditions, dependent: :destroy
  has_many :flag_logs, dependent: :nullify
  has_many :smoke_detectors, dependent: :destroy
  has_many :moderator_sites, dependent: :destroy
  has_many :reviews, class_name: 'ReviewResult', dependent: :nullify
  has_many :post_comments, dependent: :nullify
  has_many :post_spam_domains, foreign_key: 'added_by_id', dependent: :nullify

  has_one :channels_user, required: false, dependent: :destroy

  validate :gdpr_bollocks

  after_create do
    # All accounts start with flagger role enabled, if that site setting is active.
    add_role :flagger if SiteSetting['auto_flagger_role']

    # Prepare a message for SmokeDetector to forward to chat.
    # A link to a filtered list of users on MS, with the user ID in a tooltip.
    # We don't need the filter to return only one result, just make it easier to find the user on MS.
    username_filter = CGI.escape(username.first(10))
    message = "New [metasmoke user](//metasmoke.erwaysoftware.com/users?filter=#{username_filter} \"user ID: #{id}\")"
    # Redact long names. 40 characters is the limit for SE usernames.
    name = username.length > 40 ? '\\[long username redacted\\]' : SmokeDetectorsHelper.escape_markdown(username)
    # Add the username to the message. If we know it, link the username to the user's Stack Exchange account.
    message += if stack_exchange_account_id.present?
                 " '[#{name}](//stackexchange.com/users/#{stack_exchange_account_id})'"
               else
                 " '#{name}'"
               end
    message += ' created'
    # Actually send the messsage to SmokeDetector, unless that's turned off.
    if SiteSetting['new_account_messages_enabled']
      SmokeDetector.send_message_to_charcoal message
    end
  end

  before_save do
    # Retroactively update
    (changed & %w[stackexchange_chat_id meta_stackexchange_chat_id stackoverflow_chat_id]).each do
      # todo
    end
  end

  def self.smokey
    # Return the account matching SmokeDetector
    User.where(stack_exchange_account_id: 4606062).first # rubocop:disable Style/NumericLiterals
  end

  def active_for_authentication?
    super && roles.present?
  end

  def inactive_message
    if !has_role?(:reviewer)
      :not_approved<commit_msg>Don't send new usernames to chat without an SE account<commit_after># frozen_string_literal: true

require 'open-uri'

class User < ApplicationRecord
  include Websocket

  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :feedbacks, dependent: :nullify
  has_many :api_tokens, dependent: :destroy
  has_many :api_keys, dependent: :nullify
  has_many :user_site_settings, dependent: :destroy
  has_many :flag_conditions, dependent: :destroy
  has_many :flag_logs, dependent: :nullify
  has_many :smoke_detectors, dependent: :destroy
  has_many :moderator_sites, dependent: :destroy
  has_many :reviews, class_name: 'ReviewResult', dependent: :nullify
  has_many :post_comments, dependent: :nullify
  has_many :post_spam_domains, foreign_key: 'added_by_id', dependent: :nullify

  has_one :channels_user, required: false, dependent: :destroy

  validate :gdpr_bollocks

  after_create do
    # All accounts start with flagger role enabled, if that site setting is active.
    add_role :flagger if SiteSetting['auto_flagger_role']

    # Prepare a message for SmokeDetector to forward to chat.
    # A link to a filtered list of users on MS, with the user ID in a tooltip.
    # We don't need the filter to return only one result, just make it easier to find the user on MS.
    username_filter = CGI.escape(username.first(10))
    message = "New [metasmoke user](//metasmoke.erwaysoftware.com/users?filter=#{username_filter} \"user ID: #{id}\")"
    # Redact long names. 40 characters is the limit for SE usernames.
    name = username.length > 40 ? '\\[long username redacted\\]' : SmokeDetectorsHelper.escape_markdown(username)
    # Add the username to the message. If we know it, link the username to the user's Stack Exchange account.
    message += if stack_exchange_account_id.present?
                 " '[#{name}](//stackexchange.com/users/#{stack_exchange_account_id})'"
               else
                 ' without an associated SE account'
               end
    message += ' created'
    # Actually send the messsage to SmokeDetector, unless that's turned off.
    if SiteSetting['new_account_messages_enabled']
      SmokeDetector.send_message_to_charcoal message
    end
  end

  before_save do
    # Retroactively update
    (changed & %w[stackexchange_chat_id meta_stackexchange_chat_id stackoverflow_chat_id]).each do
      # todo
    end
  end

  def self.smokey
    # Return the account matching SmokeDetector
    User.where(stack_exchange_account_id: 4606062).first # rubocop:disable Style/NumericLiterals
  end

  def active_for_authentication?
    super && roles.present?
  end

  def inactive_message
    if !has_role?(:reviewer)
      :not_approved
{'fraction_non_alphanumeric': 0.0815677966101695, 'fraction_numerical': 0.004590395480225989, 'mean_word_length': 3.951923076923077, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3172399', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1693, 'n_words': 522}