/
user.rb
1057 lines (912 loc) · 37.1 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'digest/sha1'
class User < ActiveRecord::Base
extend ActiveSupport::Memoizable
require 'paperclip'
named_scope :active, :conditions => "users.status in ('pending','active')"
named_scope :at_least_one_endorsement, :conditions => "users.endorsements_count > 0"
named_scope :newsletter_subscribed, :conditions => "users.is_newsletter_subscribed = true and users.email is not null and users.email <> ''"
named_scope :comments_unsubscribed, :conditions => "users.is_comments_subscribed = false"
named_scope :twitterers, :conditions => "users.twitter_login is not null and users.twitter_login <> ''"
named_scope :authorized_twitterers, :conditions => "users.twitter_token is not null"
named_scope :uncrawled_twitterers, :conditions => "users.twitter_crawled_at is null"
named_scope :contributed, :conditions => "users.document_revisions_count > 0 or users.point_revisions_count > 0"
named_scope :no_recent_login, :conditions => "users.loggedin_at < '#{Time.now-90.days}'"
named_scope :admins, :conditions => "users.is_admin = true"
named_scope :suspended, :conditions => "users.status = 'suspended'"
named_scope :probation, :conditions => "users.status = 'probation'"
named_scope :deleted, :conditions => "users.status = 'deleted'"
named_scope :pending, :conditions => "users.status = 'pending'"
named_scope :warnings, :conditions => "warnings_count > 0"
named_scope :no_branch, :conditions => "branch_id is null"
named_scope :with_branch, :conditions => "branch_id is not null"
named_scope :by_capital, :order => "users.capitals_count desc, users.score desc"
named_scope :by_ranking, :conditions => "users.position > 0", :order => "users.position asc"
named_scope :by_talkative, :conditions => "users.comments_count > 0", :order => "users.comments_count desc"
named_scope :by_twitter_count, :order => "users.twitter_count desc"
named_scope :by_recently_created, :order => "users.created_at desc"
named_scope :by_revisions, :order => "users.document_revisions_count+users.point_revisions_count desc"
named_scope :by_invites_accepted, :conditions => "users.contacts_invited_count > 0", :order => "users.referrals_count desc"
named_scope :by_suspended_at, :order => "users.suspended_at desc"
named_scope :by_deleted_at, :order => "users.deleted_at desc"
named_scope :by_recently_loggedin, :order => "users.loggedin_at desc"
named_scope :by_probation_at, :order => "users.probation_at desc"
named_scope :by_oldest_updated_at, :order => "users.updated_at asc"
named_scope :by_twitter_crawled_at, :order => "users.twitter_crawled_at asc"
named_scope :by_24hr_gainers, :conditions => "users.endorsements_count > 4", :order => "users.index_24hr_change desc"
named_scope :by_24hr_losers, :conditions => "users.endorsements_count > 4", :order => "users.index_24hr_change asc"
named_scope :by_7days_gainers, :conditions => "users.endorsements_count > 4", :order => "users.index_7days_change desc"
named_scope :by_7days_losers, :conditions => "users.endorsements_count > 4", :order => "users.index_7days_change asc"
named_scope :by_30days_gainers, :conditions => "users.endorsements_count > 4", :order => "users.index_30days_change desc"
named_scope :by_30days_losers, :conditions => "users.endorsements_count > 4", :order => "users.index_30days_change asc"
belongs_to :picture
has_attached_file :buddy_icon, :styles => { :icon_24 => "24x24#", :icon_48 => "48x48#", :icon_96 => "96x96#" },
:storage => :s3, :s3_credentials => S3_CONFIG, :default_url => "/images/buddy_:style.png",
:path => ":class/:attachment/:id/:style.:extension", :bucket => S3_CONFIG["bucket"]
validates_attachment_size :buddy_icon, :less_than => 5.megabytes
validates_attachment_content_type :buddy_icon, :content_type => ['image/jpeg', 'image/png', 'image/gif']
belongs_to :partner
belongs_to :branch
belongs_to :referral, :class_name => "User", :foreign_key => "referral_id"
belongs_to :partner_referral, :class_name => "Partner", :foreign_key => "partner_referral_id"
belongs_to :top_endorsement, :class_name => "Endorsement", :foreign_key => "top_endorsement_id", :include => :priority
has_one :profile, :dependent => :destroy
has_many :unsubscribes, :dependent => :destroy
has_many :signups
has_many :partners, :through => :signups
has_many :endorsements, :dependent => :destroy
has_many :priorities, :conditions => "endorsements.status = 'active'", :through => :endorsements
has_many :finished_priorities, :conditions => "endorsements.status = 'finished'", :through => :endorsements, :source => :priority
has_many :created_priorities, :class_name => "Priority"
has_many :activities, :dependent => :destroy
has_many :points, :dependent => :destroy
has_many :point_revisions, :class_name => "Revision", :dependent => :destroy
has_many :documents, :dependent => :destroy
has_many :document_revisions, :class_name => "DocumentRevision", :dependent => :destroy
has_many :changes, :dependent => :nullify
has_many :rankings, :class_name => "UserRanking", :dependent => :destroy
has_many :constituents
has_many :legislators, :through => :constituents
has_many :point_qualities, :dependent => :destroy
has_many :document_qualities, :dependent => :destroy
has_many :votes, :dependent => :destroy
has_many :comments, :dependent => :destroy
has_many :blasts, :dependent => :destroy
has_many :ads, :dependent => :destroy
has_many :shown_ads, :dependent => :destroy
has_many :charts, :class_name => "UserChart", :dependent => :destroy
has_many :contacts, :class_name => "UserContact", :dependent => :destroy
has_many :sent_messages, :foreign_key => "sender_id", :class_name => "Message"
has_many :received_messages, :foreign_key => "recipient_id", :class_name => "Message"
has_many :sent_capitals, :foreign_key => "sender_id", :class_name => "Capital"
has_many :received_capitals, :foreign_key => "recipient_id", :class_name => "Capital"
has_many :capitals, :as => :capitalizable, :dependent => :nullify # this is for capitals about them, not capital they've given or received
has_many :sent_notifications, :foreign_key => "sender_id", :class_name => "Notification"
has_many :received_notifications, :foreign_key => "recipient_id", :class_name => "Notification"
has_many :notifications, :as => :notifiable, :dependent => :nullify # this is for notificiations about them, not notifications they've given or received
has_many :followings
has_many :followers, :foreign_key => "other_user_id", :class_name => "Following"
has_many :following_discussions, :dependent => :destroy
has_many :following_discussion_activities, :through => :following_discussions, :source => :activity
liquid_methods :first_name, :last_name, :id, :name, :login, :activation_code, :email, :root_url, :profile_url, :unsubscribe_url
validates_presence_of :login, :message => I18n.t('users.new.validation.login')
validates_length_of :login, :within => 3..40
validates_uniqueness_of :login, :case_sensitive => false
validates_presence_of :email, :unless => [:has_facebook?, :has_twitter?]
validates_length_of :email, :within => 3..100, :allow_nil => true, :allow_blank => true
validates_uniqueness_of :email, :case_sensitive => false, :allow_nil => true, :allow_blank => true
validates_uniqueness_of :facebook_uid, :allow_nil => true, :allow_blank => true
validates_format_of :email, :with => /^[-^!$#%&'*+\/=3D?`{|}~.\w]+@[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*(\.[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*)+$/x, :allow_nil => true, :allow_blank => true
validates_presence_of :password, :if => [:should_validate_password?]
validates_presence_of :password_confirmation, :if => [:should_validate_password?]
validates_length_of :password, :within => 4..40, :if => [:should_validate_password?]
validates_confirmation_of :password, :if => [:should_validate_password?]
before_save :encrypt_password
before_create :make_rss_code
before_create :check_branch
after_save :update_signups
after_create :check_contacts
after_create :give_partner_credit
after_create :give_user_credit
after_create :new_user_signedup
attr_protected :remember_token, :remember_token_expired_at, :activation_code, :salt, :crypted_password, :twitter_token, :twitter_secret
# Virtual attribute for the unencrypted password
attr_accessor :password, :partner_ids
def new_user_signedup
ActivityUserNew.create(:user => self, :partner => partner)
resend_activation if self.has_email? and self.is_pending?
end
def check_branch
return if has_branch? or not Government.current.is_branches?
self.branch = Government.current.default_branch
Government.current.default_branch.increment!(:users_count)
Branch.expire_cache
end
def check_contacts
if self.has_email?
existing_contacts = UserContact.find(:all, :conditions => ["email = ? and other_user_id is null",email], :order => "created_at asc")
for c in existing_contacts
if c.is_invited? # they were invited by this person
c.accept!
else # they're in the contacts, but not invited by this person
c.update_attribute(:other_user_id,self.id)
notifications << NotificationContactJoined.new(:sender => self, :recipient => c.user)
c.user.increment!(:contacts_members_count)
c.user.decrement!(:contacts_not_invited_count)
end
end
end
if self.has_facebook?
existing_contacts = UserContact.find(:all, :conditions => ["facebook_uid = ? and other_user_id is null",self.facebook_uid], :order => "created_at asc")
for c in existing_contacts
if c.is_invited? # they were invited by this person
c.accept!
else
c.update_attribute(:other_user_id,self.id)
end
end
end
end
def should_validate_password?
return false if has_twitter? or has_facebook? or not new_record?
return true
end
def give_partner_credit
return unless partner_referral
ActivityPartnerUserRecruited.create(:user => partner_referral.owner, :other_user => self, :partner => partner_referral)
ActivityCapitalPartnerUserRecruited.create(:user => partner_referral.owner, :other_user => self, :partner => partner_referral, :capital => CapitalPartnerUserRecruited.create(:recipient => partner_referral.owner, :amount => 2, :capitalizable => self))
partner_referral.owner.increment!(:referrals_count)
end
def give_user_credit
return unless referral
ActivityInvitationAccepted.create(:other_user => referral, :user => self)
ActivityUserRecruited.create(:user => referral, :other_user => self, :is_user_only => true)
referral.increment!(:referrals_count)
end
def update_signups
unless partner_ids.nil?
self.signups.each do |s|
s.destroy unless partner_ids.include?(s.partner_id.to_s)
partner_ids.delete(s.partner_id.to_s)
end
partner_ids.each do |p|
self.signups.create(:partner_id => p) unless p.blank?
end
reload
self.partner_ids = nil
end
end
# docs: http://www.vaporbase.com/postings/stateful_authentication
acts_as_state_machine :initial => :pending, :column => :status
state :passive
state :pending, :enter => :do_pending
state :active, :enter => :do_activate
state :suspended, :enter => :do_suspension
state :probation, :enter => :do_probation
state :deleted, :enter => :do_delete
event :register do
transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| !(u.crypted_password.blank? && u.password.blank?) }
end
event :activate do
transitions :from => [:pending, :passive], :to => :active
end
event :suspend do
transitions :from => [:passive, :pending, :active, :probation], :to => :suspended
end
event :delete do
transitions :from => [:passive, :pending, :active, :suspended, :probation], :to => :deleted
end
event :unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| !u.activated_at.blank? }
transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| !u.activation_code.blank? }
transitions :from => :suspended, :to => :passive
end
event :probation do
transitions :from => [:passive, :pending, :active], :to => :probation
end
event :unprobation do
transitions :from => :probation, :to => :active, :guard => Proc.new {|u| !u.activated_at.blank? }
transitions :from => :probation, :to => :pending, :guard => Proc.new {|u| !u.activation_code.blank? }
transitions :from => :probation, :to => :passive
end
def do_pending
self.probation_at = nil
self.suspended_at = nil
self.deleted_at = nil
end
# Activates the user in the database.
def do_activate
@activated = true
self.activated_at ||= Time.now.utc
self.activation_code = nil
self.probation_at = nil
self.suspended_at = nil
self.deleted_at = nil
for e in endorsements.suspended
e.unsuspend!
end
end
def do_delete
self.deleted_at = Time.now
for e in endorsements
e.destroy
end
for f in followings
f.destroy
end
for f in followers
f.destroy
end
for c in received_capitals
c.destroy
end
for c in sent_capitals
c.destroy
end
for c in constituents
c.destroy
end
self.facebook_uid = nil
end
def do_probation
self.probation_at = Time.now
ActivityUserProbation.create(:user => self)
end
def do_suspension
self.suspended_at = Time.now
for e in endorsements.active
e.suspend!
end
end
def resend_activation
make_activation_code
UserMailer.deliver_welcome(self)
end
def to_param
"#{id}-#{login.gsub(/[^a-z0-9]+/i, '-').downcase}"
end
cattr_reader :per_page
@@per_page = 25
def request=(request)
self.ip_address = request.remote_ip
self.user_agent = request.env['HTTP_USER_AGENT']
self.referrer = request.env['HTTP_REFERER']
end
def is_subscribed=(value)
if not value
self.is_newsletter_subscribed = false
self.is_comments_subscribed = false
self.is_votes_subscribed = false
self.is_point_changes_subscribed = false
self.is_followers_subscribed = false
self.is_finished_subscribed = false
self.is_messages_subscribed = false
self.is_votes_subscribed = false
self.is_admin_subscribed = false
else
self.is_newsletter_subscribed = true
self.is_comments_subscribed = true
self.is_votes_subscribed = true
self.is_point_changes_subscribed = true
self.is_followers_subscribed = true
self.is_finished_subscribed = true
self.is_messages_subscribed = true
self.is_votes_subscribed = true
self.is_admin_subscribed = true
end
end
def update_counts
self.endorsements_count = endorsements.active.size
self.up_endorsements_count = endorsements.active.endorsing.size
self.down_endorsements_count = endorsements.active.opposing.size
self.comments_count = comments.size
self.document_revisions_count = document_revisions.published.size
self.point_revisions_count = point_revisions.published.size
self.documents_count = documents.published.size
self.points_count = points.published.size
self.qualities_count = point_qualities.size + document_qualities.size
return true
end
def to_param_link
'<a href="http://' + Government.current.base_url + '/users/' + sender.to_param + '">' + sender.name + '</a>'
end
def has_top_priority?
attribute_present?("top_endorsement_id")
end
def most_recent_activity
activities.active.by_recently_created.find(:all, :limit => 1)[0]
end
memoize :most_recent_activity
def priority_list
s = "My top priorities for America:"
row = 0
for e in endorsements
row=row+1
s += "\r\n" + row.to_s + ". " + e.priority.name if row < 11
end
return s
end
memoize :priority_list
# ranking metrics
def up_issue_diversity
return 0 if up_endorsements_count < 5 or not Government.current.is_tags?
up_issues_count.to_f/up_endorsements_count.to_f
end
def recent_login?
return false if loggedin_at.nil?
loggedin_at > Time.now-30.days
end
def calculate_score
count = 0.1
count += 1 if active?
count += 3 if recent_login?
count += 0.5 if points_count > 0
count += up_issue_diversity
count += 0.6 if constituents_count > 1
count = count/6
count = 1 if count > 1
count = 0.1 if count < 0.1
return count
end
def activity_rank
(score*10).to_i
end
def quality_factor
return 1 if qualities_count < 10
rev_count = document_revisions_count+point_revisions_count
return 10/qualities_count.to_f if rev_count == 0
i = (rev_count*2).to_f/qualities_count.to_f
return 1 if i > 1
return i
end
memoize :quality_factor
def address_full
a = ""
a += address + ", " if attribute_present?("address")
a += city + ", " if attribute_present?("city")
a += state + ", " if attribute_present?("state")
a += zip if attribute_present?("zip")
a
end
def attach_legislators
return 0 unless attribute_present?("zip")
constituents.destroy_all
if attribute_present?("address")
begin
sun = Sunlight::Legislator.all_for(:address => address_full)
if sun and sun.size > 0
constituents << Constituent.new(:legislator => Legislator.find_by_govtrack_id(sun[:senior_senator].govtrack_id)) if sun[:senior_senator]
constituents << Constituent.new(:legislator => Legislator.find_by_govtrack_id(sun[:junior_senator].govtrack_id)) if sun[:junior_senator]
constituents << Constituent.new(:legislator => Legislator.find_by_govtrack_id(sun[:representative].govtrack_id)) if sun[:representative]
end
rescue
end
elsif zip.length == 10 and zip[4] == '-'
begin
sun = Sunlight::Legislator.all_for(:address => zip)
if sun and sun.size > 0
constituents << Constituent.new(:legislator => Legislator.find_by_govtrack_id(sun[:senior_senator].govtrack_id)) if sun[:senior_senator]
constituents << Constituent.new(:legislator => Legislator.find_by_govtrack_id(sun[:junior_senator].govtrack_id)) if sun[:junior_senator]
constituents << Constituent.new(:legislator => Legislator.find_by_govtrack_id(sun[:representative].govtrack_id)) if sun[:representative]
end
rescue
end
end
if constituents.empty? and zip.to_i > 0
begin
sun = Sunlight::Legislator.all_in_zipcode(zip[0..4])
if sun and sun.size > 3 # only pull in their senators, need more info to pick their rep
for s in sun
if s.title == 'Sen'
constituents << Constituent.new(:legislator => Legislator.find_by_govtrack_id(s.govtrack_id))
end
end
elsif sun and sun.size < 4
for s in sun
constituents << Constituent.new(:legislator => Legislator.find_by_govtrack_id(s.govtrack_id))
end
end
rescue
return 0
end
end
return constituents.size
end
def revisions_count
document_revisions_count+point_revisions_count-points_count-documents_count
end
memoize :revisions_count
def pick_ad(current_priority_ids)
shown = 0
for ad in Ad.active.most_paid.all
if shown == 0 and not current_priority_ids.include?(ad.priority_id)
shown_ad = ad.shown_ads.find_by_user_id(self.id)
if shown_ad and not shown_ad.has_response? and shown_ad.seen_count < 4
shown_ad.increment!(:seen_count)
return ad
elsif not shown_ad
shown_ad = ad.shown_ads.create(:user => self)
return ad
end
end
end
return nil
end
def following_user_ids
followings.collect{|f|f.other_user_id}
end
memoize :following_user_ids
def follower_user_ids
followers.collect{|f|f.user_id}
end
memoize :follower_user_ids
def calculate_contacts_count
self.contacts_members_count = contacts.active.members.not_following.size
self.contacts_invited_count = contacts.active.not_members.invited.size
self.contacts_not_invited_count = contacts.active.not_members.not_invited.size
self.contacts_count = contacts.active.size
end
def expire_charts
Rails.cache.delete("views/user_priority_chart_official-#{self.id.to_s}-#{self.endorsements_count.to_s}")
Rails.cache.delete("views/user_priority_chart-#{self.id.to_s}-#{self.endorsements_count.to_s}")
end
def recommend(limit=10)
return [] unless self.endorsements_count > 0
sql = "select relationships.percentage, priorities.id
from relationships,priorities
where relationships.other_priority_id = priorities.id and ("
if up_endorsements_count > 0
sql += "(relationships.priority_id in (#{endorsements.active_and_inactive.endorsing.collect{|e|e.priority_id}.join(',')}) and relationships.type = 'RelationshipEndorserEndorsed')"
end
if up_endorsements_count > 0 and down_endorsements_count > 0
sql += " or "
end
if down_endorsements_count > 0
sql += "(relationships.priority_id in (#{endorsements.active_and_inactive.opposing.collect{|e|e.priority_id}.join(',')}) and relationships.type = 'RelationshipOpposerEndorsed')"
end
sql += ") and relationships.other_priority_id not in (select priority_id from endorsements where user_id = " + self.id.to_s + ")
and priorities.position > 25
and priorities.status = 'published'
group by priorities.id, relationships.percentage
order by relationships.percentage desc"
sql += " limit " + limit.to_s
priority_ids = Priority.find_by_sql(sql).collect{|p|p.id}
Priority.find(priority_ids).paginate :per_page => limit, :page => 1
end
memoize :recommend
# Authenticates a user by their login name and unencrypted password. Returns the user or nil.
def self.authenticate(email, password)
u = find :first, :conditions => ["email = ? and status in ('active','pending')", email] # need to get the salt
if u && u.authenticated?(password)
#u.update_attribute(:loggedin_at,Time.now)
return u
else
return nil
end
end
# Encrypts some data with the salt.
def self.encrypt(password, salt)
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end
# Encrypts the password with the user salt
def encrypt(password)
self.class.encrypt(password, salt)
end
def authenticated?(password)
crypted_password == encrypt(password)
end
def remember_token?
remember_token_expires_at && Time.now.utc < remember_token_expires_at
end
# These create and unset the fields required for remembering users between browser closes
def remember_me
remember_me_for 4.weeks
end
def remember_me_for(time)
remember_me_until time.from_now.utc
end
def remember_me_until(time)
self.remember_token_expires_at = time
self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
save_with_validation(false)
end
def forget_me
self.remember_token_expires_at = nil
self.remember_token = nil
save_with_validation(false)
end
def name
return login
end
def real_name
return login if not attribute_present?("first_name") or not attribute_present?("last_name")
n = first_name + ' ' + last_name
n
end
def is_partner?
attribute_present?("partner_id")
end
def is_new?
created_at > Time.now-(86400*7)
end
def is_influential?
return false if position == 0
position < Endorsement.max_position
end
# Returns true if the user has just been activated.
def recently_activated?
@activated
end
def active?
# the existence of an activation code means they have not activated yet
activation_code.nil?
end
def activated?
active?
end
def is_active?
['pending','active'].include?(status)
end
def is_suspended?
['suspended'].include?(status)
end
def is_pending?
status == 'pending'
end
def is_ambassador?
contacts_invited_count > 0
end
def has_picture?
attribute_present?("picture_id")
end
def has_referral?
attribute_present?("referral_id")
end
def has_partner_referral?
attribute_present?("partner_referral_id") and partner_referral_id != 1
end
def has_twitter?
attribute_present?("twitter_token")
end
def has_website?
attribute_present?("website")
end
def has_zip?
attribute_present?("zip")
end
def website_link
return nil if self.website.nil?
wu = website
wu = 'http://' + wu if wu[0..3] != 'http'
return wu
end
def capital_received
Capital.sum(:amount, :conditions => ["recipient_id = ?",id])
end
def capital_spent
Capital.sum(:amount, :conditions => ["sender_id = ?",id])
end
def inactivity_capital_lost
Capital.sum(:amount, :conditions => ["recipient_id = ? and type='CapitalInactive'",id])
end
def has_capital?
capitals_count != 0
end
def has_google_token?
attribute_present?("google_token")
end
def update_capital
self.update_attribute(:capitals_count,capital_received-capital_spent)
end
def follow(u)
return nil if u.id == self.id
f = followings.find_by_other_user_id(u.id)
return f if f and f.value == 1
unignore(u) if f and f.value == -1
following = followings.create(:other_user => u, :value => 1)
contact_exists = contacts.find_by_other_user_id(u.id)
if contact_exists
contact_exists.update_attribute(:following_id, following.id)
self.decrement!(:contacts_members_count)
end
return following
end
def unfollow(u)
f = followings.find_by_other_user_id_and_value(u.id,1)
f.destroy if f
contact_exists = contacts.find_by_other_user_id(u.id)
if contact_exists
contact_exists.update_attribute(:following_id, nil)
self.increment!(:contacts_members_count)
end
end
def ignore(u)
f = followings.find_by_other_user_id(u.id)
return f if f and f.value == -1
unfollow(u) if f and f.value == 1
followings.create(:other_user => u, :value => -1)
end
def unignore(u)
f = followings.find_by_other_user_id_and_value(u.id,-1)
f.destroy if f
end
def reset_password
new_password = random_password
self.update_attribute(:password, new_password)
UserMailer.deliver_new_password(self, new_password)
end
def random_password( size = 4 )
c = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr lt)
v = %w(a e i o u y)
f, r = true, ''
(4 * 2).times do
r << (f ? c[rand * c.size] : v[rand * v.size])
f = !f
end
r
end
def index_charts(limit=30)
PriorityChart.find_by_sql(["select priority_charts.date_year,priority_charts.date_month,priority_charts.date_day,
sum(priority_charts.volume_count) as volume_count,
sum((priority_charts.down_count*(endorsements.value*-1))+(priority_charts.up_count*endorsements.value)) as down_count,
avg(endorsements.value*priority_charts.change_percent) as percentage
from priority_charts, endorsements
where endorsements.user_id = ? and endorsements.status = 'active'
and endorsements.priority_id = priority_charts.priority_id
group by endorsements.user_id, priority_charts.date_year, priority_charts.date_month, priority_charts.date_day
order by priority_charts.date_year desc, priority_charts.date_month desc, priority_charts.date_day desc limit ?",id,limit])
end
memoize :index_charts
# computes the change in percentage of all their priorities over the last [limit] days.
def index_change_percent(limit=7)
index_charts(limit-1).collect{|c|c.percentage.to_f}.reverse.sum
end
def index_chart_hash(limit=30)
h = Hash.new
h[:charts] = index_charts(limit)
h[:volume_counts] = h[:charts].collect{|c| c.volume_count.to_i}.reverse
h[:max_volume] = h[:volume_counts].max
h[:percentages] = h[:charts].collect{|c|c.percentage.to_f}.reverse
h[:percentages][0] = 0
for i in 1..h[:percentages].length-1
h[:percentages][i] = h[:percentages][i-1] + h[:percentages][i]
end
h[:max_percentage] = h[:percentages].max.abs
if h[:max_percentage] < h[:percentages].min.abs
h[:max_percentage] = h[:percentages].min.abs
end
h[:adjusted_percentages] = []
for i in 0..h[:percentages].length-1
h[:adjusted_percentages][i] = h[:percentages][i] + h[:max_percentage]
end
return h
end
def index_chart_with_obama_hash(limit=30)
h = Hash.new
h[:charts] = index_charts(limit)
h[:obama_charts] = Government.current.official_user.index_charts(limit)
h[:percentages] = h[:charts].collect{|c|c.percentage.to_f}.reverse
h[:percentages][0] = 0
for i in 1..h[:percentages].length-1
h[:percentages][i] = h[:percentages][i-1] + h[:percentages][i]
end
h[:obama_percentages] = h[:obama_charts].collect{|c|c.percentage.to_f}.reverse
h[:obama_percentages][0] = 0
for i in 1..h[:obama_percentages].length-1
h[:obama_percentages][i] = h[:obama_percentages][i-1] + h[:obama_percentages][i]
end
h[:max_percentage] = h[:percentages].max.abs
if h[:max_percentage] < h[:percentages].min.abs
h[:max_percentage] = h[:percentages].min.abs
end
if h[:max_percentage] < h[:obama_percentages].max.abs
h[:max_percentage] = h[:obama_percentages].max.abs
end
if h[:max_percentage] < h[:obama_percentages].min.abs
h[:max_percentage] = h[:obama_percentages].min.abs
end
h[:adjusted_percentages] = []
for i in 0..h[:percentages].length-1
h[:adjusted_percentages][i] = h[:percentages][i] + h[:max_percentage]
end
h[:obama_adjusted_percentages] = []
for i in 0..h[:obama_percentages].length-1
h[:obama_adjusted_percentages][i] = h[:obama_percentages][i] + h[:max_percentage]
end
return h
end
def has_facebook?
self.attribute_present?("facebook_uid")
end
def has_email?
self.attribute_present?("email")
end
def has_branch?
self.attribute_present?("branch_id")
end
def create_first_and_last_name_from_name(s)
names = s.split
self.last_name = names.pop
self.first_name = names.join(' ')
end
if TwitterAuth.oauth?
include TwitterAuth::OauthUser
else
include TwitterAuth::BasicUser
end
def twitter
if TwitterAuth.oauth?
TwitterAuth::Dispatcher::Oauth.new(self)
else
TwitterAuth::Dispatcher::Basic.new(self)
end
end
def twitter_followers_count
return 0 unless attribute_present?("twitter_token")
twitter.get('/users/'+twitter_id.to_s)['followers_count']
end
# this can be run on a regular basis
# it will look up all the people this person is following on twitter, and follow them here
# this only works for the first 5000 followers, need to support new cursor format to do more
def follow_twitter_friends
count = 0
friend_ids = twitter.get('/friends/ids.json?id='+twitter_id.to_s)
if friend_ids.any?
if following_user_ids.any?
users = User.active.find(:all, :conditions => ["twitter_id in (?) and id not in (?)",friend_ids, following_user_ids])
else
users = User.active.find(:all, :conditions => ["twitter_id in (?)",friend_ids])
end
for user in users
count += 1
follow(user)
end
end
return count
end
# this is for when someone adds twitter to their account for the first time
# it will look up all the people who are following this person on twitter and are already members
# and automatically follow this new person here.
def twitter_followers_follow
count = 0
followers_ids = twitter.get('/followers/ids.json?id='+twitter_id.to_s)
if follower_ids.any?
if follower_user_ids.any?
users = User.active.find(:all, :conditions => ["twitter_id in (?) and id not in (?)",follower_ids, follower_user_ids])
else
users = User.active.find(:all, :conditions => ["twitter_id in (?)",follower_ids])
end
for user in users
count += 1
user.follow(self)
end
end
return count
end
def User.create_from_twitter(twitter_info, token, secret, request)
name = twitter_info['name']
if User.find_by_login(name)
name = twitter_info['screen_name']
if User.find_by_login(name)
name = name + " TW"
end
end
u = User.new(:twitter_id => twitter_info['id'].to_i, :twitter_token => token, :twitter_secret => secret)
u.login = name
u.create_first_and_last_name_from_name(twitter_info['name'])
u.twitter_login = twitter_info['screen_name']
u.twitter_count = twitter_info['followers_count'].to_i
u.website = twitter_info['url']
u.request = request
if twitter_info['profile_image_url']
u.picture = Picture.create_from_url(twitter_info['profile_image_url'])
end
if u.save_with_validation(false)
u.activate!
return u
else
return nil
end
end
def update_with_twitter(twitter_info, token, secret, request)
self.twitter_id = twitter_info['id'].to_i
self.twitter_login = twitter_info['screen_name']
self.twitter_token = token
self.twitter_secret = secret
self.website = twitter_info['url'] if not self.has_website?
if twitter_info['profile_image_url'] and not self.has_picture?
self.picture = Picture.create_from_url(twitter_info['profile_image_url'])
end
self.twitter_count = twitter_info['followers_count'].to_i
self.save_with_validation(false)
self.activate! if not self.activated?
end
def User.create_from_facebook(fb_session,partner,request)
return if fb_session.expired?
name = fb_session.user.name
# check for existing account with this name
if User.find_by_login(name)
name = name + " FB"
end
u = User.new(
:login => name,
:first_name => fb_session.user.first_name,
:last_name => fb_session.user.last_name,
:facebook_uid => fb_session.user.uid,
:partner_referral => partner,
:request => request
)
if fb_session.user.current_location
u.zip = fb_session.user.current_location.zip if fb_session.user.current_location.zip and fb_session.user.current_location.zip.any?
u.city = fb_session.user.current_location.city if fb_session.user.current_location.city and fb_session.user.current_location.city.any?
u.state = fb_session.user.current_location.state if fb_session.user.current_location.state and fb_session.user.current_location.state.any?
end
if u.save
u.activate!
return u
else
puts "ERROR w/ user -- " + u.errors.full_messages.join(" | ")
return nil
end
end
def update_with_facebook(fb_session)
return if fb_session.expired?
self.facebook_uid = fb_session.user.uid
# need to do some checking on whether this facebook_uid is already attached to a diff account
check_existing_facebook = User.active.find(:all, :conditions => ["facebook_uid = ? and id <> ?",self.facebook_uid,self.id])
if check_existing_facebook.any?
for e in check_existing_facebook
e.remove_facebook
e.save_with_validation(false)
end
end
if fb_session.user.current_location
self.zip = fb_session.user.current_location.zip if fb_session.user.current_location.zip and fb_session.user.current_location.zip.any? and not self.attribute_present?("zip")
self.city = fb_session.user.current_location.city if fb_session.user.current_location.city and fb_session.user.current_location.city.any? and not self.attribute_present?("city")
self.state = fb_session.user.current_location.state if fb_session.user.current_location.state and fb_session.user.current_location.state.any? and not self.attribute_present?("state")
end