<commit_before># frozen_string_literal: true

class IChainRecordNotFound < StandardError
end

class UserDisabled < StandardError
end

class User < ApplicationRecord
  rolify
  # prevent N+1 queries with has_cached_role? by preloading roles *always*
  default_scope { preload(:roles) }

  has_many :physical_tickets, through: :ticket_purchases do
    def by_conference(conference)
      where('ticket_purchases.conference_id = ?', conference)
    end
  end
  has_many :users_roles
  has_many :roles, through: :users_roles, dependent: :destroy

  has_paper_trail on: [:create, :update], ignore: [:sign_in_count, :remember_created_at, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :unconfirmed_email,
                                                   :avatar_content_type, :avatar_file_size, :avatar_updated_at, :updated_at, :confirmation_sent_at, :confirmation_token, :reset_password_token]

  include Gravtastic
  gravtastic size: 32

  before_create :setup_role

  after_save :touch_events

  # add scope
  scope :comment_notifiable, ->(conference) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_type = ? AND roles.resource_id = ?', 'Conference', conference.id)}

  # scopes for user distributions
  scope :active, lambda {
    where('last_sign_in_at > ?', Date.today - 3.months).where(is_disabled: false)
  }
  scope :unconfirmed, -> { where('confirmed_at IS NULL') }
  scope :dead, -> { where('last_sign_in_at < ?', Date.today - 1.year) }

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise_modules = []

  devise_modules += if ENV['OSEM_ICHAIN_ENABLED'] == 'true'
                      [:ichain_authenticatable, :ichain_registerable, :omniauthable, omniauth_providers: []]
                    else
                      [:database_authenticatable, :registerable,
                       :recoverable, :rememberable, :trackable, :validatable, :confirmable,
                       :omniauthable, omniauth_providers: [:suse, :google, :facebook, :github]]
                    end

  devise(*devise_modules)

  has_many :openids

  attr_accessor :login

  has_many :event_users, dependent: :destroy
  has_many :events, -> { distinct }, through: :event_users
  has_many :presented_events, -> { joins(:event_users).where(event_users: {event_role: 'speaker'}).distinct }, through: :event_users, source: :event
  has_many :registrations, dependent: :destroy do
    def for_conference conference
      where(conference: conference).first
    end
  end
  has_many :events_registrations, through: :registrations
  has_many :ticket_purchases, dependent: :destroy
  has_many :payments, dependent: :destroy
  has_many :tickets, through: :ticket_purchases, source: :ticket do
    def for_registration conference
      where(conference: conference, registration_ticket: true).first
    end
  end
  has_many :votes, dependent: :destroy
  has_many :voted_events, through: :votes, source: :events
  has_many :subscriptions, dependent: :destroy
  has_many :tracks, foreign_key: 'submitter_id'
  has_many :booth_requests
  has_many :booth_requests, dependent: :destroy
  has_many :booths, through: :booth_requests
  has_many :survey_replies
  has_many :survey_submissions
  accepts_nested_attributes_for :roles

  scope :admin, -> { where(is_admin: true) }

  validates :email, presence: true

  validates :username,
            uniqueness: {
                case_sensitive: false
            },
            presence:   true

  validate :biography_limit

  DISTRIBUTION_COLORS = {<commit_msg>Define ticket_purchases before through association<commit_after># frozen_string_literal: true

class IChainRecordNotFound < StandardError
end

class UserDisabled < StandardError
end

class User < ApplicationRecord
  rolify
  # prevent N+1 queries with has_cached_role? by preloading roles *always*
  default_scope { preload(:roles) }

  has_many :ticket_purchases, dependent: :destroy
  has_many :physical_tickets, through: :ticket_purchases do
    def by_conference(conference)
      where('ticket_purchases.conference_id = ?', conference)
    end
  end
  has_many :users_roles
  has_many :roles, through: :users_roles, dependent: :destroy

  has_paper_trail on: [:create, :update], ignore: [:sign_in_count, :remember_created_at, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :unconfirmed_email,
                                                   :avatar_content_type, :avatar_file_size, :avatar_updated_at, :updated_at, :confirmation_sent_at, :confirmation_token, :reset_password_token]

  include Gravtastic
  gravtastic size: 32

  before_create :setup_role

  after_save :touch_events

  # add scope
  scope :comment_notifiable, ->(conference) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_type = ? AND roles.resource_id = ?', 'Conference', conference.id)}

  # scopes for user distributions
  scope :active, lambda {
    where('last_sign_in_at > ?', Date.today - 3.months).where(is_disabled: false)
  }
  scope :unconfirmed, -> { where('confirmed_at IS NULL') }
  scope :dead, -> { where('last_sign_in_at < ?', Date.today - 1.year) }

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise_modules = []

  devise_modules += if ENV['OSEM_ICHAIN_ENABLED'] == 'true'
                      [:ichain_authenticatable, :ichain_registerable, :omniauthable, omniauth_providers: []]
                    else
                      [:database_authenticatable, :registerable,
                       :recoverable, :rememberable, :trackable, :validatable, :confirmable,
                       :omniauthable, omniauth_providers: [:suse, :google, :facebook, :github]]
                    end

  devise(*devise_modules)

  has_many :openids

  attr_accessor :login

  has_many :event_users, dependent: :destroy
  has_many :events, -> { distinct }, through: :event_users
  has_many :presented_events, -> { joins(:event_users).where(event_users: {event_role: 'speaker'}).distinct }, through: :event_users, source: :event
  has_many :registrations, dependent: :destroy do
    def for_conference conference
      where(conference: conference).first
    end
  end
  has_many :events_registrations, through: :registrations
  has_many :payments, dependent: :destroy
  has_many :tickets, through: :ticket_purchases, source: :ticket do
    def for_registration conference
      where(conference: conference, registration_ticket: true).first
    end
  end
  has_many :votes, dependent: :destroy
  has_many :voted_events, through: :votes, source: :events
  has_many :subscriptions, dependent: :destroy
  has_many :tracks, foreign_key: 'submitter_id'
  has_many :booth_requests
  has_many :booth_requests, dependent: :destroy
  has_many :booths, through: :booth_requests
  has_many :survey_replies
  has_many :survey_submissions
  accepts_nested_attributes_for :roles

  scope :admin, -> { where(is_admin: true) }

  validates :email, presence: true

  validates :username,
            uniqueness: {
                case_sensitive: false
            },
            presence:   true

  validate :biography_limit

  DISTRIBUTION_COLORS = {
{'fraction_non_alphanumeric': 0.09933142311365807, 'fraction_numerical': 0.0013644426251876109, 'mean_word_length': 3.942683749157114, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4580884', 'n_tokens_mistral': 2394, 'n_tokens_neox': 2278, 'n_words': 522}