Class: Person
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Person
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)
included, #set_guid
#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.
104
105
106
107
108
109
|
# File 'app/models/person.rb', line 104
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)
236
237
238
239
|
# File 'app/models/person.rb', line 236
def self.by_account_identifier(identifier)
identifier = identifier.strip.downcase.gsub('acct:', '')
self.where(:diaspora_handle => identifier).first
end
|
93
94
95
|
# File 'app/models/person.rb', line 93
def self.
Person.joins(:roles).where(:roles => {:name => 'spotlight'})
end
|
+ (Object) create_from_webfinger(profile, hcard)
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'app/models/person.rb', line 246
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.account
new_person.url = profile.seed_location
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)
111
112
113
114
115
116
117
118
119
120
121
|
# File 'app/models/person.rb', line 111
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)
241
242
243
244
|
# File 'app/models/person.rb', line 241
def self.local_by_account_identifier(identifier)
person = self.by_account_identifier(identifier)
(person.nil? || person.remote?) ? nil : person
end
|
+ (Object) name_from_attrs(first_name, last_name, diaspora_handle)
174
175
176
|
# File 'app/models/person.rb', line 174
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)
144
145
146
147
148
149
150
151
152
153
|
# File 'app/models/person.rb', line 144
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
PostgreSQL and mysql deal with null values in orders differently, it seems.
156
157
158
159
160
161
162
163
164
165
|
# File 'app/models/person.rb', line 156
def self.search_order
@search_order ||= Proc.new {
order = if postgres?
"ASC"
else
"DESC"
end
["contacts.user_id #{order}", "profiles.last_name ASC", "profiles.first_name ASC"]
}.call
end
|
+ (Object) search_query_string(query)
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'app/models/person.rb', line 127
def self.search_query_string(query)
query = query.downcase
like_operator = 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
300
301
302
303
304
|
# File 'app/models/person.rb', line 300
def self.url_batch_update(people, url)
people.each do |person|
person.update_url(url)
end
end
|
Instance Method Details
- (Object) as_json(opts = {})
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# File 'app/models/person.rb', line 283
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)
263
264
265
266
267
268
269
270
|
# File 'app/models/person.rb', line 263
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!
326
327
328
329
|
# File 'app/models/person.rb', line 326
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
226
227
228
|
# File 'app/models/person.rb', line 226
def exported_key
serialized_public_key
end
|
- (Object) exported_key=(new_key)
230
231
232
233
|
# File 'app/models/person.rb', line 230
def exported_key= new_key
raise "Don't change a key" if serialized_public_key
serialized_public_key = new_key
end
|
- (Object) first_name
178
179
180
181
182
183
184
185
186
187
|
# File 'app/models/person.rb', line 178
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?
279
280
281
|
# File 'app/models/person.rb', line 279
def has_photos?
self.photos.exists?
end
|
- (Boolean) local?
275
276
277
|
# File 'app/models/person.rb', line 275
def local?
!remote?
end
|
- (Object) lock_access!
321
322
323
324
|
# File 'app/models/person.rb', line 321
def lock_access!
self.closed_account = true
self.save
end
|
- (Object) name(opts = {})
167
168
169
170
171
172
|
# File 'app/models/person.rb', line 167
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)
189
190
191
|
# File 'app/models/person.rb', line 189
def owns?(obj)
self.id == obj.author_id
end
|
- (Object) public_key
222
223
224
|
# File 'app/models/person.rb', line 222
def public_key
OpenSSL::PKey::RSA.new(serialized_public_key)
end
|
- (Object) public_key_hash
218
219
220
|
# File 'app/models/person.rb', line 218
def public_key_hash
Base64.encode64(OpenSSL::Digest::SHA256.new(self.exported_key).to_s)
end
|
- (Object) public_url
209
210
211
212
213
214
215
216
|
# File 'app/models/person.rb', line 209
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
205
206
207
|
# File 'app/models/person.rb', line 205
def receive_url
"#{url}receive/users/#{self.guid}/"
end
|
- (Boolean) remote?
272
273
274
|
# File 'app/models/person.rb', line 272
def remote?
owner_id.nil?
end
|
- (Object) shares_with(user)
gross method pulled out from controller, not exactly sure how it should be
used.
307
308
309
|
# File 'app/models/person.rb', line 307
def shares_with(user)
user.contacts.receiving.where(:person_id => self.id).first if user
end
|
- (Object) to_param
123
124
125
|
# File 'app/models/person.rb', line 123
def to_param
self.guid
end
|
- (Object) update_url(url)
313
314
315
316
317
318
319
|
# File 'app/models/person.rb', line 313
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
193
194
195
196
197
198
199
200
201
202
203
|
# File 'app/models/person.rb', line 193
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
|