1 class User < ActiveRecord::Base
4 has_many :traces, :conditions => { :visible => true }
5 has_many :diary_entries, :order => 'created_at DESC'
6 has_many :diary_comments, :order => 'created_at DESC'
7 has_many :messages, :foreign_key => :to_user_id, :conditions => { :to_user_visible => true }, :order => 'sent_on DESC'
8 has_many :new_messages, :class_name => "Message", :foreign_key => :to_user_id, :conditions => { :to_user_visible => true, :message_read => false }, :order => 'sent_on DESC'
9 has_many :sent_messages, :class_name => "Message", :foreign_key => :from_user_id, :conditions => { :from_user_visible => true }, :order => 'sent_on DESC'
10 has_many :friends, :include => :befriendee, :conditions => "users.status IN ('active', 'confirmed')"
11 has_many :tokens, :class_name => "UserToken"
12 has_many :preferences, :class_name => "UserPreference"
13 has_many :changesets, :order => 'created_at DESC'
15 has_many :client_applications
16 has_many :oauth_tokens, :class_name => "OauthToken", :order => "authorized_at desc", :include => [:client_application]
18 has_many :active_blocks, :class_name => "UserBlock", :conditions => ['user_blocks.ends_at > \'#{Time.now.getutc.xmlschema(5)}\' or user_blocks.needs_view']
19 has_many :roles, :class_name => "UserRole"
21 validates_presence_of :email, :display_name
22 validates_confirmation_of :email#, :message => ' addresses must match'
23 validates_confirmation_of :pass_crypt#, :message => ' must match the confirmation password'
24 validates_uniqueness_of :display_name, :allow_nil => true
25 validates_uniqueness_of :email
26 validates_uniqueness_of :openid_url, :allow_nil => true
27 validates_length_of :pass_crypt, :within => 8..255
28 validates_length_of :display_name, :within => 3..255, :allow_nil => true
29 validates_email_format_of :email, :if => Proc.new { |u| u.email_changed? }
30 validates_email_format_of :new_email, :allow_blank => true, :if => Proc.new { |u| u.new_email_changed? }
31 validates_format_of :display_name, :with => /^[^\/;.,?]*$/, :if => Proc.new { |u| u.display_name_changed? }
32 validates_format_of :display_name, :with => /^\S/, :message => "has leading whitespace", :if => Proc.new { |u| u.display_name_changed? }
33 validates_format_of :display_name, :with => /\S$/, :message => "has trailing whitespace", :if => Proc.new { |u| u.display_name_changed? }
34 validates_numericality_of :home_lat, :allow_nil => true
35 validates_numericality_of :home_lon, :allow_nil => true
36 validates_numericality_of :home_zoom, :only_integer => true, :allow_nil => true
37 validates_inclusion_of :preferred_editor, :in => Editors::ALL_EDITORS, :allow_nil => true
39 before_save :encrypt_password
41 file_column :image, :magick => { :geometry => "100x100>" }
44 self.creation_time = Time.now.getutc unless self.attribute_present?(:creation_time)
48 if pass_crypt_confirmation
49 self.pass_salt = OSM::make_token(8)
50 self.pass_crypt = OSM::encrypt_password(pass_crypt, pass_salt)
54 def self.authenticate(options)
55 if options[:username] and options[:password]
56 user = find(:first, :conditions => ["email = ? OR display_name = ?", options[:username], options[:username]])
57 user = nil if user and user.pass_crypt != OSM::encrypt_password(options[:password], user.pass_salt)
59 token = UserToken.find(:first, :include => :user, :conditions => ["user_tokens.token = ?", options[:token]])
60 user = token.user if token
64 ( user.status == "deleted" or
65 ( user.status == "pending" and not options[:pending] ) or
66 ( user.status == "suspended" and not options[:suspended] ) )
70 token.update_attribute(:expiry, 1.week.from_now) if token and user
76 doc = OSM::API.new.get_xml_doc
77 doc.root << to_xml_node()
82 el1 = XML::Node.new 'user'
83 el1['display_name'] = self.display_name.to_s
84 el1['account_created'] = self.creation_time.xmlschema
85 if self.home_lat and self.home_lon
86 home = XML::Node.new 'home'
87 home['lat'] = self.home_lat.to_s
88 home['lon'] = self.home_lon.to_s
89 home['zoom'] = self.home_zoom.to_s
96 attribute_present?(:languages) ? read_attribute(:languages).split(/ *, */) : []
99 def languages=(languages)
100 write_attribute(:languages, languages.join(","))
103 def preferred_language
104 languages.find { |l| Language.find(:first, :conditions => { :code => l }) }
107 def preferred_language_from(array)
108 (languages & array.collect { |i| i.to_s }).first
111 def nearby(radius = NEARBY_RADIUS, num = NEARBY_USERS)
112 if self.home_lon and self.home_lat
113 gc = OSM::GreatCircle.new(self.home_lat, self.home_lon)
114 bounds = gc.bounds(radius)
115 sql_for_distance = gc.sql_for_distance("home_lat", "home_lon")
116 nearby = User.find(:all,
117 :conditions => ["id != ? AND status IN (\'active\', \'confirmed\') AND data_public = ? AND #{sql_for_distance} <= ?", id, true, radius],
118 :order => sql_for_distance, :limit => num)
125 def distance(nearby_user)
126 return OSM::GreatCircle.new(self.home_lat, self.home_lon).distance(nearby_user.home_lat, nearby_user.home_lon)
129 def is_friends_with?(new_friend)
131 @new_friend = new_friend
132 self.friends.each do |friend|
133 if friend.friend_user_id == @new_friend.id
141 # returns true if a user is visible
143 ["pending","active","confirmed"].include? self.status
147 # returns true if a user is active
149 ["active","confirmed"].include? self.status
153 # returns true if the user has the moderator role, false otherwise
155 has_role? 'moderator'
159 # returns true if the user has the administrator role, false otherwise
161 has_role? 'administrator'
165 # returns true if the user has the requested role
167 roles.any? { |r| r.role == role }
171 # returns the first active block which would require users to view
172 # a message, or nil if there are none.
174 active_blocks.detect { |b| b.needs_view? }
178 # delete a user - leave the account but purge most personal data
180 self.display_name = "user_#{self.id}"
181 self.description = ""
185 self.email_valid = false
187 self.status = "deleted"
192 # return a spam score for a user
194 changeset_score = self.changesets.find(:all, :limit => 10).length * 50
195 trace_score = self.traces.find(:all, :limit => 10).length * 50
196 diary_entry_score = self.diary_entries.inject(0) { |s,e| s += OSM.spam_score(e.body) }
197 diary_comment_score = self.diary_comments.inject(0) { |s,e| s += OSM.spam_score(e.body) }
199 score = OSM.spam_score(self.description) * 2
200 score += diary_entry_score / self.diary_entries.length if self.diary_entries.length > 0
201 score += diary_comment_score / self.diary_comments.length if self.diary_comments.length > 0
202 score -= changeset_score
209 # return an oauth access token for a specified application
210 def access_token(application_key)
211 return ClientApplication.find_by_key(application_key).access_token_for_user(self)