<commit_before>class Paper < ActiveRecord::Base
  include AASM

  belongs_to :user
  has_many :annotations
  has_many :assignments

  has_many :reviewers, -> { where('assignments.role = ?', 'reviewer') }, :through => :assignments, :source => :user
  has_many :collaborators, -> { where('assignments.role = ?', 'collaborator') }, :through => :assignments, :source => :user

  # Which User is this currently for the attention of?
  belongs_to :fao, :class_name => "User", :foreign_key => "fao_id"

  scope :active, -> { all }


  before_create :set_iniital_values, :get_arxiv_details


  aasm column: :state do
    state :submitted,          initial:true
    state :under_review
    state :accepted
    state :rejected

    event :accept, before: :resolve_all_issues do
      transitions from: :under_review,
                  to:   :accepted
    end
    event :reject do
      transitions from: :under_review,
                  to:   :rejected
    end

    event :start_review, guard: :has_reviewers? do
      transitions from: :submitted,
                  to:   :under_review
    end

  end

  def self.with_state(state = nil)
    if state
      where(state:state)
    else
      all
    end
  end

  def issues
    annotations.root_annotations
  end

  def outstanding_issues
    issues.where.not(state:'resolved')
  end

  def resolve_all_issues
    issues.each(&:resolve!)
  end

  def editors
    User.editors
  end

  def pretty_submission_date
    submitted_at.strftime("%-d %B %Y")
  end

  def draft?
    submitted?
  end

  def self.for_user(user)
    user.papers
  end

  def to_param
    sha
  end

  def assign_reviewer(user)
    # Change this to actually be username later on. Also this is a mess tidy up later

    return true if user.reviewer_of? self

    if assignments.create(user: user, role:"reviewer")
      true
    else
      errors.add(:assignments, 'Unable to assign user')
      false
    end
  end

  def remove_reviewer(user)
    assignments.where(user_id: user.id).where(role: "reviewer").first.destroy
  end

  # FIXME if the UI needs it then we should add "submittor" and "editor" in here.
  def permissions_for_user(user)
    assigned = assignments.where(:user_id => user.id).pluck(:role)
    if user.author_of?(self)
      assigned << "submittor"
    end

    if user.editor?
      assigned << "editor"
    end

    assigned
  end

  private

  def set_iniital_values
    self.sha = SecureRandom.hex
    self.submitted_at = Time.now
  end

  def get_arxiv_details
    details          = Arxiv.get(self.arxiv_id.to_s)

    location         = details.links.select{|link| link.content_type=="application/pdf"}.first.url<commit_msg>Prioritize permissions from highest to lowest<commit_after>class Paper < ActiveRecord::Base
  include AASM

  belongs_to :user
  has_many :annotations
  has_many :assignments

  has_many :reviewers, -> { where('assignments.role = ?', 'reviewer') }, :through => :assignments, :source => :user
  has_many :collaborators, -> { where('assignments.role = ?', 'collaborator') }, :through => :assignments, :source => :user

  # Which User is this currently for the attention of?
  belongs_to :fao, :class_name => "User", :foreign_key => "fao_id"

  scope :active, -> { all }


  before_create :set_iniital_values, :get_arxiv_details


  aasm column: :state do
    state :submitted,          initial:true
    state :under_review
    state :accepted
    state :rejected

    event :accept, before: :resolve_all_issues do
      transitions from: :under_review,
                  to:   :accepted
    end
    event :reject do
      transitions from: :under_review,
                  to:   :rejected
    end

    event :start_review, guard: :has_reviewers? do
      transitions from: :submitted,
                  to:   :under_review
    end

  end

  def self.with_state(state = nil)
    if state
      where(state:state)
    else
      all
    end
  end

  def issues
    annotations.root_annotations
  end

  def outstanding_issues
    issues.where.not(state:'resolved')
  end

  def resolve_all_issues
    issues.each(&:resolve!)
  end

  def editors
    User.editors
  end

  def pretty_submission_date
    submitted_at.strftime("%-d %B %Y")
  end

  def draft?
    submitted?
  end

  def self.for_user(user)
    user.papers
  end

  def to_param
    sha
  end

  def assign_reviewer(user)
    # Change this to actually be username later on. Also this is a mess tidy up later

    return true if user.reviewer_of? self

    if assignments.create(user: user, role:"reviewer")
      true
    else
      errors.add(:assignments, 'Unable to assign user')
      false
    end
  end

  def remove_reviewer(user)
    assignments.where(user_id: user.id).where(role: "reviewer").first.destroy
  end

  # FIXME if the UI needs it then we should add "submittor" and "editor" in here.
  def permissions_for_user(user)
    assigned = []

    if user.editor?
      assigned << "editor"
    end

    assigned += assignments.where(:user_id => user.id).pluck(:role)

    if user.author_of?(self)
      assigned << "submittor"
    end

    assigned
  end

  private

  def set_iniital_values
    self.sha = SecureRandom.hex
    self.submitted_at = Time.now
  end

  def get_arxiv_details
    details          = Arxiv.get(self.arxiv_id.to_s)

    location         = details.links.select{|link| link.content_type=="application/pdf"}.first.url
{'fraction_non_alphanumeric': 0.09447647387018784, 'fraction_numerical': 0.0, 'mean_word_length': 2.758211041229909, 'pattern_counts': {'":': 0, '<': 13, '<?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': '3174576', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1699, 'n_words': 459}