Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Diaspora::UserModules, Encryptor::Private
Defined in:
app/models/user.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Encryptor::Private

#aes_decrypt, #decrypt, #get_aes_key

Methods included from Diaspora::UserModules::Querying

#all_aspect_ids, #aspects_with_person, #aspects_with_post, #contact_for, #contact_for_person_id, #contacts_in_aspects, #find_visible_post_by_id, #people_in_aspects, #posts_from, #visible_photos, #visible_posts

Methods included from Diaspora::UserModules::Connecting

#disconnect, #disconnected_by, #remove_contact, #share_with

Class Method Details

+ (Object) build(opts = {})

Helpers############



272
273
274
275
276
# File 'app/models/user.rb', line 272

def self.build(opts = {})
  u = User.new(opts)
  u.setup(opts)
  u
end

+ (Object) find_for_database_authentication(conditions = {})



80
81
82
83
84
85
86
87
# File 'app/models/user.rb', line 80

def self.find_for_database_authentication(conditions={})
  conditions = conditions.dup
  conditions[:username] = conditions[:username].downcase
  if conditions[:username] =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i # email regex
    conditions[:email] = conditions.delete(:username)
  end
  where(conditions).first
end

+ (Object) generate_key



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

def self.generate_key
  key_size = (Rails.env == 'test' ? 512 : 4096)
  OpenSSL::PKey::RSA::generate key_size
end

Instance Method Details

- (Object) accept_invitation!(opts = {})



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/models/user.rb', line 247

def accept_invitation!(opts = {})
  log_string = "event=invitation_accepted username=#{opts[:username]} uid=#{self.id} "
  log_string << "inviter=#{invitations_to_me.first.sender.diaspora_handle} " if invitations_to_me.first
  begin
    if self.invited?
      self.setup(opts)
      self.invitation_token = nil
      self.password              = opts[:password]
      self.password_confirmation = opts[:password_confirmation]
      self.save!
      invitations_to_me.each{|invitation| invitation.}
      log_string << "success"
      Rails.logger.info log_string

      self.reload # Because to_request adds a request and saves elsewhere
      self
    end
  rescue Exception => e
    log_string << "failure"
    Rails.logger.info log_string
    raise e
  end
end

- (Object) add_contact_to_aspect(contact, aspect)



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

def add_contact_to_aspect(contact, aspect)
  return true if AspectMembership.exists?(:contact_id => contact.id, :aspect_id => aspect.id)
  contact.aspect_memberships.create!(:aspect => aspect)
end

- (Object) add_to_streams(post, aspects_to_insert)



138
139
140
141
142
143
# File 'app/models/user.rb', line 138

def add_to_streams(post, aspects_to_insert)
  post.socket_to_user(self, :aspect_ids => aspects_to_insert.map{|x| x.id}) if post.respond_to? :socket_to_user
  aspects_to_insert.each do |aspect|
    aspect.posts << post
  end
end

- (Boolean) admin?

Returns:

  • (Boolean)


317
318
319
# File 'app/models/user.rb', line 317

def admin?
  AppConfig[:admins].present? && AppConfig[:admins].include?(self.username)
end

- (Object) aspects_from_ids(aspect_ids)



145
146
147
148
149
150
151
# File 'app/models/user.rb', line 145

def aspects_from_ids(aspect_ids)
  if aspect_ids == "all" || aspect_ids == :all
    self.aspects
  else
    aspects.where(:id => aspect_ids)
  end
end

- (Object) build_comment(options = {})

Commenting ########



165
166
167
# File 'app/models/user.rb', line 165

def build_comment(options = {})
  build_relayable(Comment, options)
end

- (Object) build_like(options = {})

Liking ########



170
171
172
# File 'app/models/user.rb', line 170

def build_like(options = {})
  build_relayable(, options)
end

- (Object) build_post(class_name, opts = {})

Posting ########



112
113
114
115
116
117
118
# File 'app/models/user.rb', line 112

def build_post(class_name, opts = {})
  opts[:author] = self.person
  opts[:diaspora_handle] = opts[:author].diaspora_handle

  model_class = class_name.to_s.camelize.constantize
  model_class.diaspora_initialize(opts)
end

- (Object) build_relayable(model, options = {})



157
158
159
160
161
162
# File 'app/models/user.rb', line 157

def build_relayable(model, options = {})
  r = model.new(options.merge(:author_id => self.person.id))
  r.set_guid
  r.initialize_signatures
  r
end

- (Boolean) can_add?(person)

Returns:

  • (Boolean)


89
90
91
92
93
# File 'app/models/user.rb', line 89

def can_add?(person)
  return false if self.person == person
  return false if self.contact_for(person).present?
  true
end

- (Object) disconnect_everyone



331
332
333
334
335
336
337
338
339
340
341
# File 'app/models/user.rb', line 331

def disconnect_everyone
  self.contacts.each do |contact|
    if contact.person.remote?
      self.disconnect(contact)
    else
      contact.person.owner.disconnected_by(self.person)
      remove_contact(contact, :force => true)
    end
  end
  self.aspects.destroy_all
end

- (Object) dispatch_post(post, opts = {})



120
121
122
123
# File 'app/models/user.rb', line 120

def dispatch_post(post, opts = {})
  mailman = Postzord::Dispatch.new(self, post)
  mailman.post(opts)
end

- (Object) encryption_key



313
314
315
# File 'app/models/user.rb', line 313

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

- (Object) invite_user(aspect_id, service, identifier, invite_message = "")

Invitations############



234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/models/user.rb', line 234

def invite_user(aspect_id, service, identifier, invite_message = "")
  aspect = aspects.find(aspect_id)
  if aspect
    Invitation.invite(:service => service,
                      :identifier => identifier,
                      :from => self,
                      :into => aspect,
                      :message => invite_message)
  else
    false
  end
end

- (Object) like_for(post)



182
183
184
185
186
187
188
189
# File 'app/models/user.rb', line 182

def (post)
  [post., post.dislikes].each do ||
    .each do ||
      return  if .author_id == self.person.id
    end
  end
  return nil
end

- (Boolean) liked?(post)

Returns:

  • (Boolean)


174
175
176
177
178
179
180
# File 'app/models/user.rb', line 174

def liked?(post)
  if self.(post)
    return true
  else
    return false
  end
end

- (Object) mail(job, *args)

Mailer #######################



192
193
194
195
196
197
# File 'app/models/user.rb', line 192

def mail(job, *args)
  pref = job.to_s.gsub('Job::Mail', '').underscore
  if(self.disable_mail == false && !self.user_preferences.exists?(:email_type => pref))
    Resque.enqueue(job, *args)
  end
end

- (Object) move_contact(person, to_aspect, from_aspect)

Aspects ######################



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

def move_contact(person, to_aspect, from_aspect)
  return true if to_aspect == from_aspect
  contact = contact_for(person)

  add_contact_to_aspect(contact, to_aspect)

  membership = contact ? AspectMembership.where(:contact_id => contact.id, :aspect_id => from_aspect.id).first : nil
  return(membership && membership.destroy)
end

- (Object) notify_if_mentioned(post)



132
133
134
135
136
# File 'app/models/user.rb', line 132

def notify_if_mentioned(post)
  return unless self.contact_for(post.author) && post.respond_to?(:mentions?)

  post.notify_person(self.person) if post.mentions? self.person
end

- (Object) remove_all_traces



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

def remove_all_traces
  disconnect_everyone
  remove_mentions
  remove_person
end

- (Object) remove_mentions



343
344
345
346
347
# File 'app/models/user.rb', line 343

def remove_mentions
  Mention.where( :person_id => self.person.id).each do |mentioned_person|
    mentioned_person.delete
  end
end

- (Object) remove_person



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

def remove_person
  self.person.destroy
end

- (Object) retract(post)

Posts and Such ###############



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/user.rb', line 200

def retract(post)
  if post.respond_to?(:relayable?) && post.relayable?
    aspects = post.parent.aspects
    retraction = RelayableRetraction.build(self, post)
  else
    aspects = post.aspects
    retraction = Retraction.for(post)
  end

  mailman = Postzord::Dispatch.new(self, retraction)
  mailman.post

  retraction.perform(self)

  retraction
end

- (Object) salmon(post)



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

def salmon(post)
  Salmon::SalmonSlap.create(self, post.to_diaspora_xml)
end

- (Object) seed_aspects



303
304
305
306
# File 'app/models/user.rb', line 303

def seed_aspects
  self.aspects.create(:name => I18n.t('aspects.seed.family'))
  self.aspects.create(:name => I18n.t('aspects.seed.work'))
end

- (Object) set_current_language



76
77
78
# File 'app/models/user.rb', line 76

def set_current_language
  self.language = I18n.locale.to_s if self.language.blank?
end

- (Object) setup(opts)



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'app/models/user.rb', line 278

def setup(opts)
  self.username = opts[:username]
  self. = opts[:email]
  self.valid?
  errors = self.errors
  errors.delete :person
  return if errors.size > 0

  opts[:person] ||= {}
  unless opts[:person][:profile].is_a?(Profile)
    opts[:person][:profile] ||= Profile.new
    opts[:person][:profile] = Profile.new(opts[:person][:profile])
  end

  self.person = Person.new(opts[:person])
  self.person.diaspora_handle = "#{opts[:username]}@#{AppConfig[:pod_uri].host}"
  self.person.url = AppConfig[:pod_url]


  self.serialized_private_key = User.generate_key if self.serialized_private_key.blank?
  self.person.serialized_public_key = OpenSSL::PKey::RSA.new(self.serialized_private_key).public_key

  self
end

- (Object) strip_and_downcase_username



69
70
71
72
73
74
# File 'app/models/user.rb', line 69

def strip_and_downcase_username
  if username.present?
    username.strip!
    username.downcase!
  end
end

- (Object) update_post(post, post_hash = {})



125
126
127
128
129
130
# File 'app/models/user.rb', line 125

def update_post(post, post_hash = {})
  if self.owns? post
    post.update_attributes(post_hash)
    Postzord::Dispatch.new(self, post).post
  end
end

- (Object) update_profile(params)

Profile ######################



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'app/models/user.rb', line 218

def update_profile(params)
  if photo = params.delete(:photo)
    photo.update_attributes(:pending => false) if photo.pending
    params[:image_url] = photo.url(:thumb_large)
    params[:image_url_medium] = photo.url(:thumb_medium)
    params[:image_url_small] = photo.url(:thumb_small)
  end
  if self.person.profile.update_attributes(params)
    Postzord::Dispatch.new(self, profile).post
    true
  else
    false
  end
end

- (Object) update_user_preferences(pref_hash)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/user.rb', line 50

def update_user_preferences(pref_hash)
  if self.disable_mail
    UserPreference::VALID_EMAIL_TYPES.each{|x| self.user_preferences.find_or_create_by_email_type(x)}
    self.disable_mail = false
    self.save
  end

  pref_hash.keys.each do |key|
    if pref_hash[key] == 'true'
      self.user_preferences.find_or_create_by_email_type(key)
    else
      block = self.user_preferences.where(:email_type => key).first
      if block
        block.destroy
      end
    end
  end
end