Class: Person

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Diaspora::Guid, Diaspora::Socketable, Encryptor::Public, ROXML
Defined in:
app/models/person.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Diaspora::Guid

included, #set_guid

Methods included from Diaspora::Socketable

#socket_to_user, #unsocket_from_user

Methods included from Encryptor::Public

#aes_encrypt, #encrypt, #encrypt_aes_key, #gen_aes_key

Class Method Details

+ (Object) by_account_identifier(identifier)

database calls



153
154
155
156
# File 'app/models/person.rb', line 153

def self.(identifier)
  identifier = identifier.strip.downcase.gsub('acct:', '')
  self.where(:diaspora_handle => identifier).first
end

+ (Object) create_from_webfinger(profile, hcard)



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/models/person.rb', line 163

def self.create_from_webfinger(profile, hcard)
  return nil if profile.nil? || !profile.valid_diaspora_profile?
  new_person = Person.new
  new_person.serialized_public_key = profile.public_key
  new_person.guid = profile.guid
  new_person.diaspora_handle = profile.
  new_person.url = profile.seed_location

  #hcard_profile = HCard.find profile.hcard.first[:href]
  Rails.logger.info("event=webfinger_marshal valid=#{new_person.valid?} target=#{new_person.diaspora_handle}")
  new_person.url = hcard[:url]
  new_person.assign_new_profile_from_hcard(hcard)
  new_person.save!
  new_person.profile.save!
  new_person
end

+ (Object) local_by_account_identifier(identifier)



158
159
160
161
# File 'app/models/person.rb', line 158

def self.(identifier)
  person = self.(identifier)
 (person.nil? || person.remote?) ? nil : person
end

+ (Object) name_from_attrs(first_name, last_name, diaspora_handle)



94
95
96
# File 'app/models/person.rb', line 94

def self.name_from_attrs(first_name, last_name, diaspora_handle)
  first_name.blank? ? diaspora_handle : "#{first_name.to_s} #{last_name.to_s}"
end

+ (Object) public_search(query, opts = {})



81
82
83
84
85
# File 'app/models/person.rb', line 81

def self.public_search(query, opts={})
  return [] if query.to_s.blank? || query.to_s.length < 3
  sql, tokens = self.search_query_string(query)
  Person.searchable.where(sql, *tokens)
end

+ (Object) search(query, user)



70
71
72
73
74
75
76
77
78
# File 'app/models/person.rb', line 70

def self.search(query, user)
  return [] if query.to_s.blank? || query.to_s.length < 3

  sql, tokens = self.search_query_string(query)
  Person.searchable.where(sql, *tokens).joins(
    "LEFT OUTER JOIN `contacts` ON `contacts`.user_id = #{user.id} AND `contacts`.person_id = `people`.id"
  ).includes(:profile
  ).order("contacts.user_id DESC", "profiles.last_name ASC", "profiles.first_name ASC")
end

+ (Object) search_query_string(query)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/person.rb', line 46

def self.search_query_string(query)
  where_clause = "profiles.first_name LIKE ? OR\nprofiles.last_name LIKE ? OR\npeople.diaspora_handle LIKE ? OR\nprofiles.first_name LIKE ? OR\nprofiles.last_name LIKE ?\n"
  sql = ""
  tokens = []

  query_tokens = query.to_s.strip.split(" ")
  query_tokens.each_with_index do |raw_token, i|
    token = "#{raw_token}%"
    up_token = "#{raw_token.titleize}%"
    sql << " OR " unless i == 0
    sql << where_clause
    tokens.concat([token, token, token])
    tokens.concat([up_token, up_token])
  end
  [sql, tokens]
end

Instance Method Details

- (Object) as_json(opts = {})



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/person.rb', line 196

def as_json(opts={})
  opts ||= {}
  if(opts[:format] == :twitter)
    {
      :id => self.guid,
      :screen_name => self.diaspora_handle,
      :name => self.name,
      :created_at => self.created_at,
      :profile_image_url => self.profile.image_url(:thumb_small),
      :profile => self.profile.as_json(opts)
    }
  else
    {:id => self.guid,
     :name => self.name,
     :avatar => self.profile.image_url(:thumb_small),
     :handle => self.diaspora_handle,
     :url => "/people/#{self.id}"}
  end
end

- (Object) assign_new_profile_from_hcard(hcard)



180
181
182
183
184
185
186
187
# File 'app/models/person.rb', line 180

def assign_new_profile_from_hcard(hcard)
  self.profile = Profile.new(:first_name => hcard[:given_name],
                            :last_name  => hcard[:family_name],
                            :image_url  => hcard[:photo],
                            :image_url_medium  => hcard[:photo_medium],
                            :image_url_small  => hcard[:photo_small],
                            :searchable => hcard[:searchable])
end

- (Object) downcase_diaspora_handle



24
25
26
# File 'app/models/person.rb', line 24

def downcase_diaspora_handle
  diaspora_handle.downcase! unless diaspora_handle.blank?
end

- (Object) exported_key



143
144
145
# File 'app/models/person.rb', line 143

def exported_key
  serialized_public_key
end

- (Object) exported_key=(new_key)



147
148
149
150
# File 'app/models/person.rb', line 147

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

- (Object) first_name



98
99
100
101
102
103
104
# File 'app/models/person.rb', line 98

def first_name
  @first_name ||= if profile.nil? || profile.first_name.nil? || profile.first_name.blank?
              self.diaspora_handle.split('@').first
            else
              profile.first_name.to_s
            end
end

- (Boolean) has_photos?

Returns:

  • (Boolean)


219
220
221
# File 'app/models/person.rb', line 219

def has_photos?
  self.posts.where(:type => "Photo").exists?
end

- (Boolean) local?

Returns:

  • (Boolean)


192
193
194
# File 'app/models/person.rb', line 192

def local?
  !remote?
end

- (Object) name(opts = {})



87
88
89
90
91
92
# File 'app/models/person.rb', line 87

def name(opts = {})
  if self.profile.nil?
    fix_profile
  end
  @name ||= Person.name_from_attrs(self.profile.first_name, self.profile.last_name, self.diaspora_handle)
end

- (Boolean) owns?(obj)

Returns:

  • (Boolean)


106
107
108
# File 'app/models/person.rb', line 106

def owns?(obj)
  self == obj.author
end

- (Object) public_key



139
140
141
# File 'app/models/person.rb', line 139

def public_key
  OpenSSL::PKey::RSA.new(serialized_public_key)
end

- (Object) public_key_hash



135
136
137
# File 'app/models/person.rb', line 135

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

- (Object) public_url



126
127
128
129
130
131
132
133
# File 'app/models/person.rb', line 126

def public_url
  if self.owner
    username = self.owner.username
  else
    username = self.diaspora_handle.split("@")[0]
  end
  "#{url}public/#{username}"
end

- (Object) receive_url



122
123
124
# File 'app/models/person.rb', line 122

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

- (Boolean) remote?

Returns:

  • (Boolean)


189
190
191
# File 'app/models/person.rb', line 189

def remote?
  owner_id.nil?
end

- (Object) to_twitter(format = :json)



216
217
# File 'app/models/person.rb', line 216

def to_twitter(format=:json)
end

- (Object) url



110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/person.rb', line 110

def url
  begin
    uri = URI.parse(@attributes['url'])
    url = "#{uri.scheme}://#{uri.host}"
    url += ":#{uri.port}" unless ["80", "443"].include?(uri.port.to_s)
    url += "/"
  rescue Exception => e
    url = @attributes['url']
  end
  url
end