<commit_before>class User < ActiveRecord::Base
  has_many :subscriptions
  has_many :repository_subscriptions
  has_many :api_keys
  has_many :repository_permissions
  has_many :adminable_repository_permissions, -> { where admin: true }, anonymous_class: RepositoryPermission
  has_many :adminable_github_repositories, through: :adminable_repository_permissions, source: :github_repository
  has_many :adminable_github_orgs, -> { group('github_organisations.id') }, through: :adminable_github_repositories, source: :github_organisation
  has_many :github_repositories, primary_key: :uid, foreign_key: :owner_id
  has_many :source_github_repositories, -> { where fork: false }, anonymous_class: GithubRepository, primary_key: :github_id, foreign_key: :owner_id
  has_many :dependencies, through: :source_github_repositories
  has_many :favourite_projects, -> { group('projects.id').order("COUNT(projects.id) DESC") }, through: :dependencies, source: :project
  has_one :github_user, primary_key: :uid, foreign_key: :github_id

  after_commit :create_api_key, :ping_andrew, :download_orgs, :update_repo_permissions_async, on: :create

  ADMIN_USERS = ['andrew', 'barisbalic', 'malditogeek', 'olizilla', 'thattommyhall']

  validates_presence_of :email, :on => :update
  validates_format_of :email, :with => /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/, :on => :update

  def admin?
    ADMIN_USERS.include?(nickname)
  end

  def ping_andrew
    PushoverNewUserWorker.perform_async(self.id)
  end

  def create_api_key
    api_keys.create
  end

  def api_key
    api_keys.first.try(:access_token)
  end

  def self.create_from_auth_hash(hash)
    create!(AuthHash.new(hash).user_info)
  end

  def assign_from_auth_hash(hash)
    ignored_fields = new_record? ? [] : %i(email)
    update_attributes(AuthHash.new(hash).user_info.except(*ignored_fields))
  end

  def self.find_by_auth_hash(hash)
    conditions = AuthHash.new(hash).user_info.slice(:provider, :uid)
    where(conditions).first
  end

  def token
    private_repo_token.presence || public_repo_token.presence || read_attribute(:token)
  end

  def github_client
    AuthToken.new_client(token)
  end

  def avatar_url(size = 60)
    "https://avatars.githubusercontent.com/u/#{uid}?size=#{size}"
  end

  def github_url
    "https://github.com/#{nickname}"
  end

  def update_repo_permissions_async
    SyncPermissionsWorker.perform_async(self.id)
  end

  def update_repo_permissions
    r = github_client.repos

    current_repo_ids = []
    r.each do |repo|
      github_repo = GithubRepository.find_by_github_id(repo.id) || GithubRepository.create_from_hash(repo)
      current_repo_ids << github_repo.id unless github_repo.nil?

      rp = repository_permissions.find_or_initialize_by(github_repository: github_repo)
      rp.admin = repo.permissions.admin
      rp.push = repo.permissions.push
      rp.pull = repo.permissions.pull<commit_msg>Avoid more duplicate repo attempts<commit_after>class User < ActiveRecord::Base
  has_many :subscriptions
  has_many :repository_subscriptions
  has_many :api_keys
  has_many :repository_permissions
  has_many :adminable_repository_permissions, -> { where admin: true }, anonymous_class: RepositoryPermission
  has_many :adminable_github_repositories, through: :adminable_repository_permissions, source: :github_repository
  has_many :adminable_github_orgs, -> { group('github_organisations.id') }, through: :adminable_github_repositories, source: :github_organisation
  has_many :github_repositories, primary_key: :uid, foreign_key: :owner_id
  has_many :source_github_repositories, -> { where fork: false }, anonymous_class: GithubRepository, primary_key: :github_id, foreign_key: :owner_id
  has_many :dependencies, through: :source_github_repositories
  has_many :favourite_projects, -> { group('projects.id').order("COUNT(projects.id) DESC") }, through: :dependencies, source: :project
  has_one :github_user, primary_key: :uid, foreign_key: :github_id

  after_commit :create_api_key, :ping_andrew, :download_orgs, :update_repo_permissions_async, on: :create

  ADMIN_USERS = ['andrew', 'barisbalic', 'malditogeek', 'olizilla', 'thattommyhall']

  validates_presence_of :email, :on => :update
  validates_format_of :email, :with => /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/, :on => :update

  def admin?
    ADMIN_USERS.include?(nickname)
  end

  def ping_andrew
    PushoverNewUserWorker.perform_async(self.id)
  end

  def create_api_key
    api_keys.create
  end

  def api_key
    api_keys.first.try(:access_token)
  end

  def self.create_from_auth_hash(hash)
    create!(AuthHash.new(hash).user_info)
  end

  def assign_from_auth_hash(hash)
    ignored_fields = new_record? ? [] : %i(email)
    update_attributes(AuthHash.new(hash).user_info.except(*ignored_fields))
  end

  def self.find_by_auth_hash(hash)
    conditions = AuthHash.new(hash).user_info.slice(:provider, :uid)
    where(conditions).first
  end

  def token
    private_repo_token.presence || public_repo_token.presence || read_attribute(:token)
  end

  def github_client
    AuthToken.new_client(token)
  end

  def avatar_url(size = 60)
    "https://avatars.githubusercontent.com/u/#{uid}?size=#{size}"
  end

  def github_url
    "https://github.com/#{nickname}"
  end

  def update_repo_permissions_async
    SyncPermissionsWorker.perform_async(self.id)
  end

  def update_repo_permissions
    r = github_client.repos

    current_repo_ids = []
    r.each do |repo|
      github_repo = GithubRepository.find_by_github_id(repo.id) || GithubRepository.find_by('lower(full_name) = ?', repo.full_name.downcase) || GithubRepository.create_from_hash(repo)
      current_repo_ids << github_repo.id unless github_repo.nil?

      rp = repository_permissions.find_or_initialize_by(github_repository: github_repo)
      rp.admin = repo.permissions.admin
      rp.push = repo.permissions.push
      rp.pull = repo.permissions.pull
{'fraction_non_alphanumeric': 0.11122414377755171, 'fraction_numerical': 0.0006781959986436081, 'mean_word_length': 5.138397502601457, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1731078', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1966, 'n_words': 345}