<commit_before>class BotUser < User

  EVENTS = ['create_project_media', 'update_project_media', 'create_source', 'update_source', 'update_annotation_own']
  if ActiveRecord::Base.connection.table_exists?(:dynamic_annotation_annotation_types)
    annotation_types = DynamicAnnotation::AnnotationType.all.map(&:annotation_type) + ['comment', 'flag', 'tag', 'task', 'geolocation']
    annotation_types.each do |type|
      EVENTS << "create_annotation_#{type}"
      EVENTS << "update_annotation_#{type}"
    end
    Task.task_types.each do |type|
      EVENTS << "create_annotation_task_#{type}"
      EVENTS << "update_annotation_task_#{type}"
    end
  end
  JSON_SCHEMA_PATH = File.join(Rails.root, 'public', 'events.json')
  JSON_SCHEMA_TEMPLATE_PATH = File.join(Rails.root, 'public', 'events-template.json')
  File.atomic_write(JSON_SCHEMA_PATH) { |file| file.write(File.read(JSON_SCHEMA_TEMPLATE_PATH).gsub('<%= BotUser::EVENTS %>', EVENTS.to_json)) }

  scope :not_approved, -> { where('settings LIKE ?', '%approved: false%') }

  before_validation :set_team_author_id
  before_validation :format_settings
  before_validation :set_version
  before_validation :set_fields
  before_validation :set_identifier, on: :create
  before_validation :create_api_key, on: :create
  after_create :add_to_team
  after_update :update_role_if_changed
  
  validates_uniqueness_of :login
  validates :api_key_id, presence: true, uniqueness: true
  validate :request_url_format
  validate :events_is_valid
  validate :can_approve

  belongs_to :api_key, dependent: :destroy
  belongs_to :source, dependent: :destroy

  devise
  
  check_settings

  def identifier
    self.login
  end

  def avatar
    self.profile_image
  end

  def team_author_id
    id = @team_author_id || self.get_team_author_id
    id.blank? ? nil : id.to_i
  end

  def team_author_id=(id)
    @team_author_id = id
    self.set_team_author_id(id)
  end

  def team_author
    Team.where(id: self.team_author_id).last
  end

  def team_bot_installations<commit_msg>Ticket #7357: Fixing reference to bot image<commit_after>class BotUser < User

  EVENTS = ['create_project_media', 'update_project_media', 'create_source', 'update_source', 'update_annotation_own']
  if ActiveRecord::Base.connection.table_exists?(:dynamic_annotation_annotation_types)
    annotation_types = DynamicAnnotation::AnnotationType.all.map(&:annotation_type) + ['comment', 'flag', 'tag', 'task', 'geolocation']
    annotation_types.each do |type|
      EVENTS << "create_annotation_#{type}"
      EVENTS << "update_annotation_#{type}"
    end
    Task.task_types.each do |type|
      EVENTS << "create_annotation_task_#{type}"
      EVENTS << "update_annotation_task_#{type}"
    end
  end
  JSON_SCHEMA_PATH = File.join(Rails.root, 'public', 'events.json')
  JSON_SCHEMA_TEMPLATE_PATH = File.join(Rails.root, 'public', 'events-template.json')
  File.atomic_write(JSON_SCHEMA_PATH) { |file| file.write(File.read(JSON_SCHEMA_TEMPLATE_PATH).gsub('<%= BotUser::EVENTS %>', EVENTS.to_json)) }

  scope :not_approved, -> { where('settings LIKE ?', '%approved: false%') }

  before_validation :set_team_author_id
  before_validation :format_settings
  before_validation :set_version
  before_validation :set_fields
  before_validation :set_identifier, on: :create
  before_validation :create_api_key, on: :create
  after_create :add_to_team
  after_update :update_role_if_changed
  
  validates_uniqueness_of :login
  validates :api_key_id, presence: true, uniqueness: true
  validate :request_url_format
  validate :events_is_valid
  validate :can_approve

  belongs_to :api_key, dependent: :destroy
  belongs_to :source, dependent: :destroy

  devise
  
  check_settings

  def identifier
    self.login
  end

  def avatar
    self.source&.image&.to_s
  end

  def team_author_id
    id = @team_author_id || self.get_team_author_id
    id.blank? ? nil : id.to_i
  end

  def team_author_id=(id)
    @team_author_id = id
    self.set_team_author_id(id)
  end

  def team_author
    Team.where(id: self.team_author_id).last
  end

  def team_bot_installations
{'fraction_non_alphanumeric': 0.10703812316715543, 'fraction_numerical': 0.0009775171065493646, 'mean_word_length': 4.992679355783309, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 7, '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': '2448381', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1379, 'n_words': 261}