<commit_before>class User
  include MongoMapper::Document

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
         
  key :friend_ids, Array
  key :pending_request_ids, Array

  one :person, :class_name => 'Person', :foreign_key => :owner_id

  many :friends, :in => :friend_ids, :class_name => 'Person'
  many :pending_requests, :in => :pending_request_ids, :class_name => 'Request'

  many :groups, :class_name => 'Group'

  before_validation_on_create :assign_key
  before_validation :do_bad_things
  
  ######## Making things work ########

  key :email, String

  def method_missing(method, *args)
    self.person.send(method, *args)
  end


  def real_name
    "#{person.profile.first_name.to_s} #{person.profile.last_name.to_s}"
  end
  
  ######### Groups ######################

  def group( opts = {} )
    opts[:user] = self
    Group.create(opts)
  end

  ######### Friend Requesting ###########
  def send_friend_request_to(friend_url, group_id)
    unless self.friends.detect{ |x| x.receive_url == friend_url}
      request = Request.instantiate(:to => friend_url, :from => self.person, :into => group_id)
      if request.save
        self.pending_requests << request
        self.save

        group = self.groups.first(:id => group_id)

        group.requests << request
        group.save
        
        request.push_to_url friend_url
      end
      request
    end
  end 

  def accept_friend_request(friend_request_id, group_id)
    request = Request.where(:id => friend_request_id).first
    n = pending_requests.delete(request)
    
    friends << request.person
    save

    group = self.groups.first(:id => group_id)
    group.people << request.person
    group.save

    request.reverse self

    request.push_to_url(request.callback_url)
    
    request.destroy
  end

  def ignore_friend_request(friend_request_id)
    request = Request.first(:id => friend_request_id)
    person = request.person
    person.user_refs -= 1
    pending_requests.delete(request)
    save
    (person.user_refs > 0 || person.owner.nil? == false) ?  person.save : person.destroy
    request.destroy
  end

  def receive_friend_request(friend_request)
    Rails.logger.debug("receiving friend request #{friend_request.to_json}")
    if request_from_me?(friend_request)
      activate_friend(friend_request.person, friend_request.group_id)

      Rails.logger.debug("#{self.real_name}'s friend request has been accepted")
      friend_request.destroy
    else
      friend_request.person.user_refs += 1
      friend_request.person.save
      pending_requests << friend_request
      save
      Rails.logger.debug("#{self.real_name} has received a friend request")
      friend_request.save
    end
  end

  def unfriend(friend_id)
    bad_friend = Person.first(:_id => friend_id)
    self.friend_ids.delete( friend_id )

    if bad_friend 
      Retraction.for(self).push_to_url(bad_friend.receive_url) 
      bad_friend.user_refs -= 1

      (bad_friend.user_refs > 0 || bad_friend.owner.nil? == false) ?  bad_friend.save : bad_friend.destroy
    end
    self.save
  end

  def unfriended_by friend_id
    bad_friend = Person.first(:_id => friend_id)

    self.friend_ids.delete( friend_id )
    bad_friend.user_refs -= 1
<commit_msg>Turning up logging on friend request receive<commit_after>class User
  include MongoMapper::Document

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
         
  key :friend_ids, Array
  key :pending_request_ids, Array

  one :person, :class_name => 'Person', :foreign_key => :owner_id

  many :friends, :in => :friend_ids, :class_name => 'Person'
  many :pending_requests, :in => :pending_request_ids, :class_name => 'Request'

  many :groups, :class_name => 'Group'

  before_validation_on_create :assign_key
  before_validation :do_bad_things
  
  ######## Making things work ########

  key :email, String

  def method_missing(method, *args)
    self.person.send(method, *args)
  end


  def real_name
    "#{person.profile.first_name.to_s} #{person.profile.last_name.to_s}"
  end
  
  ######### Groups ######################

  def group( opts = {} )
    opts[:user] = self
    Group.create(opts)
  end

  ######### Friend Requesting ###########
  def send_friend_request_to(friend_url, group_id)
    unless self.friends.detect{ |x| x.receive_url == friend_url}
      request = Request.instantiate(:to => friend_url, :from => self.person, :into => group_id)
      if request.save
        self.pending_requests << request
        self.save

        group = self.groups.first(:id => group_id)

        group.requests << request
        group.save
        
        request.push_to_url friend_url
      end
      request
    end
  end 

  def accept_friend_request(friend_request_id, group_id)
    request = Request.where(:id => friend_request_id).first
    n = pending_requests.delete(request)
    
    friends << request.person
    save

    group = self.groups.first(:id => group_id)
    group.people << request.person
    group.save

    request.reverse self

    request.push_to_url(request.callback_url)
    
    request.destroy
  end

  def ignore_friend_request(friend_request_id)
    request = Request.first(:id => friend_request_id)
    person = request.person
    person.user_refs -= 1
    pending_requests.delete(request)
    save
    (person.user_refs > 0 || person.owner.nil? == false) ?  person.save : person.destroy
    request.destroy
  end

  def receive_friend_request(friend_request)
    Rails.logger.debug("receiving friend request #{friend_request.to_json}")
    if request_from_me?(friend_request)
      activate_friend(friend_request.person, friend_request.group_id)

      Rails.logger.info("#{self.real_name}'s friend request has been accepted")
      friend_request.destroy
    else
      friend_request.person.user_refs += 1
      friend_request.person.save
      pending_requests << friend_request
      save
      Rails.logger.info("#{self.real_name} has received a friend request")
      friend_request.save
    end
  end

  def unfriend(friend_id)
    bad_friend = Person.first(:_id => friend_id)
    self.friend_ids.delete( friend_id )

    if bad_friend 
      Retraction.for(self).push_to_url(bad_friend.receive_url) 
      bad_friend.user_refs -= 1

      (bad_friend.user_refs > 0 || bad_friend.owner.nil? == false) ?  bad_friend.save : bad_friend.destroy
    end
    self.save
  end

  def unfriended_by friend_id
    bad_friend = Person.first(:_id => friend_id)

    self.friend_ids.delete( friend_id )
    bad_friend.user_refs -= 1

{'fraction_non_alphanumeric': 0.11727559765448804, 'fraction_numerical': 0.0018042399639152007, 'mean_word_length': 3.322287199480182, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 39, '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': '7416450', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2067, 'n_words': 420}