<commit_before>require 'digest/sha1'

class Invitation < RoleRecord
  belongs_to :group
  belongs_to :invited_user, :class_name => 'User'
  
  validate :check_invite
  
  attr_accessor :is_silent

  def target
    project || group
  end
  
  def check_invite
    if project.nil? and group.nil?
      @errors.add_to_base('Must belong to a project or group')
      return
    end
    @errors.add_to_base('Must belong to a valid user') if user.nil? or user.deleted? or !(target.admin?(user))
    
    # Check user
    check_user = invited_user
    unless check_user.nil?
      if group and group.has_member?(check_user)
        @errors.add :user_or_email, 'is already a member of the group'
        return
      elsif project and Person.exists?(:project_id => project_id, :user_id => check_user.id)
        @errors.add :user_or_email, 'is already a member of the project'
        return
      elsif Invitation.exists?(:project_id => project_id, :invited_user_id => check_user.id)
        @errors.add :user_or_email, 'already has a pending invitation'
        return
      end
    end
    
    # Check email (for non-existent users)
    if check_user.nil?
      if valid_email?(email)
        # One final check: do we have an invite for this email?
        if Invitation.exists?(:project_id => project_id, :email => email)
          @errors.add :user_or_email, 'already has a pending invitation'
        end
      else
        @errors.add :user_or_email, 'is not a valid username or email'
      end
    end
  end
  
  attr_reader :user_or_email
  
  def user_or_email=(value)
    self.invited_user = User.find_by_username_or_email(value)
    self.email = value unless self.invited_user
    @user_or_email = value
  end
  
  def accept(current_user)
    if target.is_a? Project
      person = project.people.new(
        :user => current_user,
        :role => role || 3,
        :source_user => user)
      person.save
    else
      target.add_user(current_user)
    end
  end
  
  def editable?(user)
    (project || group).admin?(user) or self.user_id == user.id or self.invited_user_id == user.id
  end

  before_create :generate_token
  after_create :send_email<commit_msg>Bugfix for invitations without a target<commit_after>require 'digest/sha1'

class Invitation < RoleRecord
  belongs_to :group
  belongs_to :invited_user, :class_name => 'User'
  
  validate :check_invite
  
  attr_accessor :is_silent

  def target
    project || group
  end
  
  def check_invite
    if project.nil? and group.nil?
      @errors.add_to_base('Must belong to a project or group')
      return
    end
    @errors.add_to_base('Must belong to a valid user') if user.nil? or user.deleted? or !(target.admin?(user))
    
    # Check user
    check_user = invited_user
    unless check_user.nil?
      if group and group.has_member?(check_user)
        @errors.add :user_or_email, 'is already a member of the group'
        return
      elsif project and Person.exists?(:project_id => project_id, :user_id => check_user.id)
        @errors.add :user_or_email, 'is already a member of the project'
        return
      elsif Invitation.exists?(:project_id => project_id, :invited_user_id => check_user.id)
        @errors.add :user_or_email, 'already has a pending invitation'
        return
      end
    end
    
    # Check email (for non-existent users)
    if check_user.nil?
      if valid_email?(email)
        # One final check: do we have an invite for this email?
        if Invitation.exists?(:project_id => project_id, :email => email)
          @errors.add :user_or_email, 'already has a pending invitation'
        end
      else
        @errors.add :user_or_email, 'is not a valid username or email'
      end
    end
  end
  
  attr_reader :user_or_email
  
  def user_or_email=(value)
    self.invited_user = User.find_by_username_or_email(value)
    self.email = value unless self.invited_user
    @user_or_email = value
  end
  
  def accept(current_user)
    if target.is_a? Project
      person = project.people.new(
        :user => current_user,
        :role => role || 3,
        :source_user => user)
      person.save
    elsif target
      target.add_user(current_user)
    end
  end
  
  def editable?(user)
    (project || group).admin?(user) or self.user_id == user.id or self.invited_user_id == user.id
  end

  before_create :generate_token
  after_create :send_email
{'fraction_non_alphanumeric': 0.08759124087591241, 'fraction_numerical': 0.0009124087591240876, 'mean_word_length': 2.776916451335056, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4976696', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1425, 'n_words': 389}