<commit_before>require 'activerecord/uuid'

class Conversation < ActiveRecord::Base
  include ActiveRecord::UUID

  STATUS_ARCHIVED = 'archived'
  STATUS_OPEN = 'open'

  # Internal: Regex to extract account slug and conversation number from
  # Conversation#mailbox addresses.
  MAILBOX_REGEX = Regexp.new(
                    /^(?<slug>(\w|-)+)(\+\S+)?\+(?<number>\d+)?@.+$/
                  ).freeze

  belongs_to :account
  has_many :messages, :after_add => :new_message,
                      :dependent => :destroy
  has_many :notes, :dependent => :destroy

  has_many :participants, -> { uniq }, through: :messages, source: :person

  sequential column: :number, scope: :account_id

  validates :account, presence: true

  scope :open, -> { where.not(status: STATUS_ARCHIVED) }
  scope :archived, -> { where(status: STATUS_ARCHIVED) }
  scope :most_stale, -> { joins(:messages).order('messages.updated_at ASC') }

  def ordered_messages
    messages.order(:created_at => :asc)
  end

  def messages_visible_for(user)
    m = messages
    user.agent_or_higher?(account_id) ? m : m.not_internal
  end

  def archived?
    status == STATUS_ARCHIVED
  end

  def archive<commit_msg>Wire up association between conversation and agent<commit_after>require 'activerecord/uuid'

class Conversation < ActiveRecord::Base
  include ActiveRecord::UUID

  STATUS_ARCHIVED = 'archived'
  STATUS_OPEN = 'open'

  # Internal: Regex to extract account slug and conversation number from
  # Conversation#mailbox addresses.
  MAILBOX_REGEX = Regexp.new(
                    /^(?<slug>(\w|-)+)(\+\S+)?\+(?<number>\d+)?@.+$/
                  ).freeze

  belongs_to :account

  belongs_to :agent,
    class_name: "User",
    foreign_key: "user_id"

  has_many :messages, :after_add => :new_message,
                      :dependent => :destroy
  has_many :notes, :dependent => :destroy

  has_many :participants, -> { uniq }, through: :messages, source: :person

  sequential column: :number, scope: :account_id

  validates :account, presence: true

  scope :open, -> { where.not(status: STATUS_ARCHIVED) }
  scope :archived, -> { where(status: STATUS_ARCHIVED) }
  scope :most_stale, -> { joins(:messages).order('messages.updated_at ASC') }

  def ordered_messages
    messages.order(:created_at => :asc)
  end

  def messages_visible_for(user)
    m = messages
    user.agent_or_higher?(account_id) ? m : m.not_internal
  end

  def archived?
    status == STATUS_ARCHIVED
  end

  def archive
{'fraction_non_alphanumeric': 0.12766814337494967, 'fraction_numerical': 0.0, 'mean_word_length': 3.443649373881932, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, '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': '7557624', 'n_tokens_mistral': 844, 'n_tokens_neox': 793, 'n_words': 185}