<commit_before>require 'lib/hcard'

class Person
  include MongoMapper::Document
  include ROXML
  include Encryptor::Public

  xml_accessor :_id
  xml_accessor :email
  xml_accessor :url
  xml_accessor :profile, :as => Profile
  xml_reader :exported_key
  
  key :url,            String
  key :email,          String, :unique => true
  key :serialized_key, String 

  key :owner_id,  ObjectId
  key :user_refs, Integer, :default => 0 

  one :profile, :class_name => 'Profile'
  many :albums, :class_name => 'Album', :foreign_key => :person_id
  belongs_to :owner, :class_name => 'User'

  timestamps!

  before_destroy :remove_all_traces
  before_validation :clean_url
  validates_presence_of :email, :url, :profile, :serialized_key 
  validates_format_of :url, :with =>
     /^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix
  
  
  def self.search(query)
    Person.all('$where' => "function() { return this.email.match(/^#{query}/i) ||
               this.profile.first_name.match(/^#{query}/i) ||
               this.profile.last_name.match(/^#{query}/i); }")
  end

  def real_name
    "#{profile.first_name.to_s} #{profile.last_name.to_s}"
  end
  def owns?(post)
    self.id == post.person.id
  end

  def receive_url
    "#{self.url}receive/users/#{self.id}/"
  end

  def encryption_key
    OpenSSL::PKey::RSA.new( serialized_key )
  end

  def encryption_key= new_key
    raise TypeError unless new_key.class == OpenSSL::PKey::RSA
    serialized_key = new_key.export
  end

  def public_key_hash
    Base64.encode64 OpenSSL::Digest::SHA256.new(self.exported_key).to_s
  end

  def public_key
    encryption_key.public_key
  end

  def exported_key
    encryption_key.public_key.export
  end

  def exported_key= new_key
    raise "Don't change a key" if serialized_key
    @serialized_key = new_key
  end

  def self.by_webfinger( identifier )
     local_person = Person.first(:email => identifier.gsub('acct:', ''))
     if local_person
       local_person
     elsif  !identifier.include?("localhost")
       begin
        f = Redfinger.finger(identifier)
       rescue SocketError => e
         raise "Diaspora server for #{identifier} not found" if e.message =~ /Name or service not known/
       end
       #raise "No diaspora user found at #{identifier}"
       Person.from_webfinger_profile(identifier, f )
     end
  end

  def self.from_webfinger_profile( identifier, profile)
    new_person = Person.new

    public_key = profile.links.select{|x| x.rel == 'diaspora-public-key'}.first.href
    new_person.exported_key = Base64.decode64 public_key

    guid = profile.links.select{|x| x.rel == 'diaspora-public-key'}.first.href
    new_person.id = guid
    
    new_person.email = identifier
    
    hcard = HCard.find profile.hcard.first[:href]

    new_person.url = hcard[:url]
    new_person.profile = Profile.new(:first_name => hcard[:given_name], :last_name => hcard[:family_name])
    if new_person.save
      new_person
    else
      nil
    end
  end
  <commit_msg>RS IZ added a logger statement and a guid in the webfinger profile<commit_after>require 'lib/hcard'

class Person
  include MongoMapper::Document
  include ROXML
  include Encryptor::Public

  xml_accessor :_id
  xml_accessor :email
  xml_accessor :url
  xml_accessor :profile, :as => Profile
  xml_reader :exported_key
  
  key :url,            String
  key :email,          String, :unique => true
  key :serialized_key, String 

  key :owner_id,  ObjectId
  key :user_refs, Integer, :default => 0 

  one :profile, :class_name => 'Profile'
  many :albums, :class_name => 'Album', :foreign_key => :person_id
  belongs_to :owner, :class_name => 'User'

  timestamps!

  before_destroy :remove_all_traces
  before_validation :clean_url
  validates_presence_of :email, :url, :profile, :serialized_key 
  validates_format_of :url, :with =>
     /^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix
  
  
  def self.search(query)
    Person.all('$where' => "function() { return this.email.match(/^#{query}/i) ||
               this.profile.first_name.match(/^#{query}/i) ||
               this.profile.last_name.match(/^#{query}/i); }")
  end

  def real_name
    "#{profile.first_name.to_s} #{profile.last_name.to_s}"
  end
  def owns?(post)
    self.id == post.person.id
  end

  def receive_url
    "#{self.url}receive/users/#{self.id}/"
  end

  def encryption_key
    OpenSSL::PKey::RSA.new( serialized_key )
  end

  def encryption_key= new_key
    raise TypeError unless new_key.class == OpenSSL::PKey::RSA
    serialized_key = new_key.export
  end

  def public_key_hash
    Base64.encode64 OpenSSL::Digest::SHA256.new(self.exported_key).to_s
  end

  def public_key
    encryption_key.public_key
  end

  def exported_key
    encryption_key.public_key.export
  end

  def exported_key= new_key
    raise "Don't change a key" if serialized_key
    @serialized_key = new_key
  end

  def self.by_webfinger( identifier )
     local_person = Person.first(:email => identifier.gsub('acct:', ''))
     if local_person
       local_person
     elsif  !identifier.include?("localhost")
       begin
        f = Redfinger.finger(identifier)
       rescue SocketError => e
         raise "Diaspora server for #{identifier} not found" if e.message =~ /Name or service not known/
       end
       #raise "No diaspora user found at #{identifier}"
       Person.from_webfinger_profile(identifier, f )
     end
  end

  def self.from_webfinger_profile( identifier, profile)
    new_person = Person.new

    public_key = profile.links.select{|x| x.rel == 'diaspora-public-key'}.first.href
    new_person.exported_key = Base64.decode64 public_key

    guid = profile.links.select{|x| x.rel == 'http://joindiaspora.com/guid'}.first.href
    new_person.id = guid
    
    new_person.email = identifier
    
    hcard = HCard.find profile.hcard.first[:href]

    new_person.url = hcard[:url]
    new_person.profile = Profile.new(:first_name => hcard[:given_name], :last_name => hcard[:family_name])
    if new_person.save
      new_person
    else
      nil
    end
  end
  
{'fraction_non_alphanumeric': 0.1278158667972576, 'fraction_numerical': 0.0075089781260202415, 'mean_word_length': 3.1708645336963923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '1416264', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2124, 'n_words': 442}