Newer
Older
# licensed under the Affero General Public License version 3 or later. See
Daniel Vincent Grippi
a validé
require File.join(Rails.root, 'lib/diaspora/user')
maxwell
a validé
require File.join(Rails.root, 'lib/salmon/salmon')
include Diaspora::UserModules
include Encryptor::Private
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:timeoutable
# key :visible_post_ids, Array, :typecast => 'ObjectId'
# key :visible_person_ids, Array, :typecast => 'ObjectId'
#
before_validation :strip_and_downcase_username, :on => :create
before_validation :set_current_language, :on => :create
validates_presence_of :username
validates_uniqueness_of :username, :case_sensitive => false
validates_format_of :username, :with => /\A[A-Za-z0-9_]+\z/
validates_length_of :username, :maximum => 32
validates_inclusion_of :language, :in => AVAILABLE_LANGUAGE_CODES
validates_presence_of :person, :unless => proc {|user| user.invitation_token.present?}
validates_associated :person
has_one :person, :foreign_key => :owner_id
has_many :invitations_from_me, :class_name => 'Invitation', :foreign_key => :sender_id
has_many :invitations_to_me, :class_name => 'Invitation', :foreign_key => :recipient_id
has_many :aspects, :dependent => :destroy
has_many :aspect_memberships, :through => :aspects
# many :visible_people, :in => :visible_person_ids, :class => Person # One of these needs to go
# many :raw_visible_posts, :in => :visible_post_ids, :class => Post
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
before_destroy :disconnect_everyone, :remove_person
before_save do
person.save if person
end
attr_accessible :getting_started, :password, :password_confirmation, :language, :disable_mail
def strip_and_downcase_username
if username.present?
username.strip!
username.downcase!
end
end
def set_current_language
self.language = I18n.locale.to_s if self.language.blank?
end
def self.find_for_authentication(conditions={})
conditions[:username] = conditions[:username].downcase
if conditions[:username] =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i # email regex
conditions[:email] = conditions.delete(:username)
end
super
end
######### Aspects ######################
def drop_aspect(aspect)
if aspect.contacts.count == 0
aspect.destroy
else
raise "Aspect not empty"
end
end
def move_contact(person, to_aspect, from_aspect)
contact = contact_for(person)
if to_aspect == from_aspect
true
elsif add_contact_to_aspect(contact, to_aspect)
delete_person_from_aspect(person.id, from_aspect.id)
end
end
def add_contact_to_aspect(contact, aspect)
return true if contact.aspect_ids.include?(aspect.id)
contact.aspects << aspect
contact.save!
end
def delete_person_from_aspect(person_id, aspect_id, opts = {})
aspect = Aspect.find(aspect_id)
raise "Can not delete a person from an aspect you do not own" unless aspect.user == self
contact = contact_for Person.find(person_id)
if opts[:force] || contact.aspect_ids.count > 1
contact.aspect_ids.delete aspect.id
contact.save!
aspect.save!
else
raise "Can not delete a person from last aspect"
end
end
######## Posting ########
def build_post(class_name, opts = {})
opts[:person] = self.person
opts[:diaspora_handle] = opts[:person].diaspora_handle
model_class = class_name.to_s.camelize.constantize
model_class.instantiate(opts)
end
def dispatch_post(post, opts = {})
aspect_ids = opts.delete(:to)
Rails.logger.info("event=dispatch user=#{diaspora_handle} post=#{post.id.to_s}")
push_to_aspects(post, aspects_from_ids(aspect_ids))
Resque.enqueue(Jobs::PostToServices, self.id, post.id, opts[:url]) if post.public
end
def post_to_services(post, url)
if post.respond_to?(:message)
self.services.each do |service|
service.post(post, url)
end
end
end
def post_to_hub(post)
Rails.logger.debug("event=post_to_service type=pubsub sender_handle=#{self.diaspora_handle}")
EventMachine::PubSubHubbub.new(APP_CONFIG[:pubsub_server]).publish self.public_url
end
def update_post(post, post_hash = {})
if self.owns? post
post.update_attributes(post_hash)
aspects = aspects_with_post(post.id)
self.push_to_aspects(post, aspects)
end
end
def add_to_streams(post, aspect_ids)
self.raw_visible_posts << post
self.save
post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to? :socket_to_uid
target_aspects = aspects_from_ids(aspect_ids)
target_aspects.each do |aspect|
aspect.posts << post
aspect.save
end
end
def aspects_from_ids(aspect_ids)
if aspect_ids == "all" || aspect_ids == :all
self.aspects
else
if aspect_ids.respond_to? :to_id
aspect_ids = [aspect_ids]
end
aspect_ids.map!{ |x| x.to_id }
aspects.all(:id.in => aspect_ids)
end
end
def push_to_aspects(post, aspects)
#send to the aspects
target_aspect_ids = aspects.map {|a| a.id}
target_contacts = Contact.all(:aspect_ids.in => target_aspect_ids, :pending => false)
post_to_hub(post) if post.respond_to?(:public) && post.public
push_to_people(post, self.person_objects(target_contacts))
end
def push_to_people(post, people)
salmon = salmon(post)
people.each do |person|
push_to_person(salmon, post, person)
end
end
def push_to_person(salmon, post, person)
person.reload # Sadly, we need this for Ruby 1.9.
# person.owner will always return a ProxyObject.
# calling nil? performs a necessary evaluation.
if person.owner_id
Rails.logger.info("event=push_to_person route=local sender=#{self.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{post.class}")
if post.is_a?(Post) || post.is_a?(Comment)
Resque.enqueue(Jobs::ReceiveLocal, person.owner_id, self.person.id, post.class.to_s, post.id)
else
Resque.enqueue(Jobs::Receive, person.owner_id, post.to_diaspora_xml, self.person.id)
end
else
xml = salmon.xml_for person
Rails.logger.info("event=push_to_person route=remote sender=#{self.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{post.class}")
MessageHandler.add_post_request(person.receive_url, xml)
end
end
def salmon(post)
created_salmon = Salmon::SalmonSlap.create(self, post.to_diaspora_xml)
created_salmon
end
######## Commenting ########
def build_comment(text, options = {})
comment = Comment.new(:person_id => self.person.id,
:text => text,
:post => options[:on])
#sign comment as commenter
comment.creator_signature = comment.sign_with_key(self.encryption_key)
if !comment.post_id.blank? && person.owns?(comment.post)
#sign comment as post owner
comment.post_creator_signature = comment.sign_with_key(self.encryption_key)
end
comment
end
def dispatch_comment(comment)
if person.owns? comment.post
#push DOWNSTREAM (to original audience)
Rails.logger.info "event=dispatch_comment direction=downstream user=#{self.person.diaspora_handle} comment=#{comment.id}"
aspects = comment.post.aspects
#just socket to local users, as the comment has already
#been associated and saved by post owner
# (we'll push to all of their aspects for now, the comment won't
# show up via js where corresponding posts are not present)
people_in_aspects(aspects, :type => 'local').each do |person|
comment.socket_to_uid(person.owner_id, :aspect_ids => 'all')
end
#push to remote people
push_to_people(comment, people_in_aspects(aspects, :type => 'remote'))
elsif owns? comment
#push UPSTREAM (to poster)
Rails.logger.info "event=dispatch_comment direction=upstream user=#{self.diaspora_handle} comment=#{comment.id}"
push_to_people comment, [comment.post.person]
end
end
######### Mailer #######################
def mail(job, *args)
unless self.disable_mail
Resque.enqueue(job, *args)
end
end
######### Posts and Such ###############
def retract(post)
aspects = post.aspects
post.unsocket_from_uid(self.id, :aspect_ids => aspects.map { |a| a.id.to_s }) if post.respond_to? :unsocket_from_uid
retraction = Retraction.for(post)
push_to_people retraction, people_in_aspects(aspects)
retraction
end
########### Profile ######################
def update_profile(params)
if params[:photo]
params[:photo].update_attributes(:pending => false) if params[:photo].pending
params[:image_url] = params[:photo].url(:thumb_large)
params[:image_url_medium] = params[:photo].url(:thumb_medium)
params[:image_url_small] = params[:photo].url(:thumb_small)
end
if self.person.profile.update_attributes(params)
push_to_people profile, self.person_objects(contacts(:pending => false))
true
else
false
end
end
###Invitations############
def invite_user(email, aspect_id, invite_message = "")
aspect_object = Aspect.first(:user_id => self.id, :id => aspect_id)
if aspect_object
Invitation.invite(:email => email,
:from => self,
:into => aspect_object,
:message => invite_message)
else
false
end
end
def accept_invitation!(opts = {})
if self.invited?
log_string = "event=invitation_accepted username=#{opts[:username]} "
log_string << "inviter=#{invitations_to_me.first.from.diaspora_handle}" if invitations_to_me.first
Rails.logger.info log_string
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.to_request!}
self.reload # Because to_request adds a request and saves elsewhere
self
end
end
###Helpers############
def self.build(opts = {})
u = User.new(opts)
u.email = opts[:email]
u.setup(opts)
u
end
def setup(opts)
self.username = opts[:username]
self.valid?
errors = self.errors
errors.delete :person
return if errors.size > 0
opts[:person] ||= {}
opts[:person][:profile] ||= Profile.new
self.person = Person.new(opts[:person])
self.person.diaspora_handle = "#{opts[:username]}@#{APP_CONFIG[:pod_uri].host}"
self.person.url = APP_CONFIG[:pod_url]
self.serialized_private_key ||= User.generate_key
self.person.serialized_public_key = OpenSSL::PKey::RSA.new(self.serialized_private_key).public_key
self
end
def seed_aspects
self.aspects.create(:name => I18n.t('aspects.seed.family'))
self.aspects.create(:name => I18n.t('aspects.seed.work'))
end
def self.generate_key
key_size = (Rails.env == 'test' ? 512 : 4096)
OpenSSL::PKey::RSA::generate key_size
end
def encryption_key
OpenSSL::PKey::RSA.new(serialized_private_key)
end
protected
def remove_person
self.person.destroy
end
def disconnect_everyone
contacts.each { |contact|
if contact.person.owner?
contact.person.owner.disconnected_by self.person
else
self.disconnect contact
end
}
end