Class: Person

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

Overview

Copyright © 2010-2011, Diaspora Inc. This file is

licensed under the Affero General Public License version 3 or later.  See
the COPYRIGHT file.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Diaspora::Guid

included, #set_guid

Methods included from Encryptor::Public

#aes_encrypt, #encrypt, #encrypt_aes_key, #gen_aes_key

Constructor Details

- (Person) initialize(params = {})

Set a default of an empty profile when a new Person record is instantiated. Passing :profile => nil to Person.new will instantiate a person with no profile. Calling Person.new with a block:

Person.new do |p|
  p.profile = nil
end

will not work! The nil profile will be overriden with an empty one.



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

def initialize(params={})
  profile_set = params.has_key?(:profile) || params.has_key?("profile")
  params[:profile_attributes] = params.delete(:profile) if params.has_key?(:profile) && params[:profile].is_a?(Hash)
  super
  self.profile ||= Profile.new unless profile_set
end

Class Method Details

+ (Object) by_account_identifier(identifier)

database calls



237
238
239
240
# File 'app/models/person.rb', line 237

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

+ (Object) community_spotlight



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

def self.community_spotlight
  Person.joins(:roles).where(:roles => {:name => 'spotlight'})
end

+ (Object) create_from_webfinger(profile, hcard)



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/models/person.rb', line 247

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) find_from_guid_or_username(params)

Raises:

  • (ActiveRecord::RecordNotFound)


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

def self.find_from_guid_or_username(params)
  p = if params[:id].present?
        Person.where(:guid => params[:id]).first
      elsif params[:username].present? && u = User.find_by_username(params[:username])
        u.person
      else
        nil
      end
  raise ActiveRecord::RecordNotFound unless p.present?
  p
end

+ (Object) local_by_account_identifier(identifier)



242
243
244
245
# File 'app/models/person.rb', line 242

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

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



175
176
177
# File 'app/models/person.rb', line 175

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

+ (Object) search(query, user)



145
146
147
148
149
150
151
152
153
154
# File 'app/models/person.rb', line 145

def self.search(query, user)
  return self.where("1 = 0") if query.to_s.blank? || query.to_s.length < 2

  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(search_order)
end

+ (Array<String>) search_order

Returns postgreSQL and mysql deal with null values in orders differently, it seems.

Returns:

  • (Array<String>)

    postgreSQL and mysql deal with null values in orders differently, it seems.



157
158
159
160
161
162
163
164
165
166
# File 'app/models/person.rb', line 157

def self.search_order
  @search_order ||= Proc.new {
    order = if AppConfig.postgres?
      "ASC"
    else
      "DESC"
    end
    ["contacts.user_id #{order}", "profiles.last_name ASC", "profiles.first_name ASC"]
  }.call
end

+ (Object) search_query_string(query)



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/person.rb', line 128

def self.search_query_string(query)
  query = query.downcase
  like_operator = AppConfig.postgres? ? "ILIKE" : "LIKE"

  where_clause = <<-SQL
    profiles.full_name #{like_operator} ? OR
    people.diaspora_handle #{like_operator} ?
  SQL

  q_tokens = []
  q_tokens[0] = query.to_s.strip.gsub(/(\s|$|^)/) { "%#{$1}" }
  q_tokens[1] = q_tokens[0].gsub(/\s/,'').gsub('%','')
  q_tokens[1] << "%"

  [where_clause, q_tokens]
end

+ (Object) url_batch_update(people, url)

Update an array of people given a url, and set it as the new destination_url

Parameters:

  • people (Array<People>)
  • url (String)


301
302
303
304
305
# File 'app/models/person.rb', line 301

def self.url_batch_update(people, url)
  people.each do |person|
    person.update_url(url)
  end
end

Instance Method Details

- (Object) as_json(opts = {})



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'app/models/person.rb', line 284

def as_json( opts = {} )
  opts ||= {}
  json = {
    :id => self.id,
    :guid => self.guid,
    :name => self.name,
    :avatar => self.profile.image_url(:thumb_medium),
    :handle => self.diaspora_handle,
    :url => Rails.application.routes.url_helpers.person_path(self),
  }
  json.merge!(:tags => self.profile.tags.map{|t| "##{t.name}"}) if opts[:includes] == "tags"
  json
end

- (Object) assign_new_profile_from_hcard(hcard)



264
265
266
267
268
269
270
271
# File 'app/models/person.rb', line 264

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) clear_profile!



327
328
329
330
# File 'app/models/person.rb', line 327

def clear_profile!
  self.profile.tombstone!
  self
end

- (Object) downcase_diaspora_handle



39
40
41
# File 'app/models/person.rb', line 39

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

- (Object) exported_key



227
228
229
# File 'app/models/person.rb', line 227

def exported_key
  serialized_public_key
end

- (Object) exported_key=(new_key)



231
232
233
234
# File 'app/models/person.rb', line 231

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

- (Object) first_name



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

def first_name
  @first_name ||= if profile.nil? || profile.first_name.nil? || profile.first_name.blank?
              self.diaspora_handle.split('@').first
            else
              names = profile.first_name.to_s.split(/\s/)
              str = names[0...-1].join(' ')
              str = names[0] if str.blank?
              str
            end
end

- (Boolean) has_photos?

Returns:

  • (Boolean)


280
281
282
# File 'app/models/person.rb', line 280

def has_photos?
  self.photos.exists?
end

- (Boolean) local?

Returns:

  • (Boolean)


276
277
278
# File 'app/models/person.rb', line 276

def local?
  !remote?
end

- (Object) lock_access!



322
323
324
325
# File 'app/models/person.rb', line 322

def lock_access!
  self. = true
  self.save
end

- (Object) name(opts = {})



168
169
170
171
172
173
# File 'app/models/person.rb', line 168

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)


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

def owns?(obj)
  self.id == obj.author_id
end

- (Object) public_key



223
224
225
# File 'app/models/person.rb', line 223

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

- (Object) public_key_hash



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

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

- (Object) public_url



210
211
212
213
214
215
216
217
# File 'app/models/person.rb', line 210

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



206
207
208
# File 'app/models/person.rb', line 206

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

- (Boolean) remote?

Returns:

  • (Boolean)


273
274
275
# File 'app/models/person.rb', line 273

def remote?
  owner_id.nil?
end

- (Object) shares_with(user)

gross method pulled out from controller, not exactly sure how it should be used.



308
309
310
# File 'app/models/person.rb', line 308

def shares_with(user)
  user.contacts.receiving.where(:person_id => self.id).first if user
end

- (Object) to_param



124
125
126
# File 'app/models/person.rb', line 124

def to_param
  self.guid
end

- (Object) update_url(url)

Parameters:



314
315
316
317
318
319
320
# File 'app/models/person.rb', line 314

def update_url(url)
  location = URI.parse(url)
  newuri = "#{location.scheme}://#{location.host}"
  newuri += ":#{location.port}" unless ["80", "443"].include?(location.port.to_s)
  newuri += "/"
  self.update_attributes(:url => newuri)
end

- (Object) url



194
195
196
197
198
199
200
201
202
203
204
# File 'app/models/person.rb', line 194

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 => e
    url = @attributes['url']
  end
  url
end