1 class User < ActiveRecord::Base
6 has_many :diary_entries, :order => 'created_at DESC'
7 has_many :messages, :foreign_key => :to_user_id
8 has_many :new_messages, :class_name => "Message", :foreign_key => :to_user_id, :conditions => "message_read = 0"
11 validates_confirmation_of :pass_crypt, :message => 'Password must match the confirmation password'
12 validates_uniqueness_of :display_name, :allow_nil => true
13 validates_uniqueness_of :email
14 validates_length_of :pass_crypt, :minimum => 8
15 validates_length_of :display_name, :minimum => 3, :allow_nil => true
16 validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
17 validates_format_of :display_name, :with => /^[^\/;.,?]*$/
19 before_save :encrypt_password
22 self.creation_time = Time.now
23 self.timeout = Time.now
24 self.token = User.make_token()
28 self.pass_crypt = Digest::MD5.hexdigest(pass_crypt) unless pass_crypt_confirmation.nil?
31 def self.authenticate(email, passwd, active = true)
32 find(:first, :conditions => [ "email = ? AND pass_crypt = ? AND active = ?", email, Digest::MD5.hexdigest(passwd), active])
35 def self.authenticate_token(token)
36 find(:first, :conditions => [ "token = ? ", token])
39 def self.make_token(length=30)
40 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
44 confirmstring += chars[(rand * chars.length).to_i].chr
51 doc = OSM::API.new.get_xml_doc
52 doc.root << to_xml_node()
57 el1 = XML::Node.new 'user'
58 el1['display_name'] = self.display_name.to_s
59 el1['account_created'] = self.creation_time.xmlschema
63 def nearby(radius = 50)
64 if self.home_lon and self.home_lat
65 gc = OSM::GreatCircle.new(self.home_lat, self.home_lon)
66 bounds = gc.bounds(radius)
67 nearby = User.find(:all, :conditions => "home_lat between #{bounds[:minlat]} and #{bounds[:maxlat]} and home_lon between #{bounds[:minlon]} and #{bounds[:maxlon]} and data_public = 1 and id != #{self.id}")
68 nearby.delete_if { |u| gc.distance(u.home_lat, u.home_lon) > radius }
69 nearby.sort! { |u1,u2| gc.distance(u1.home_lat, u1.home_lon) <=> gc.distance(u2.home_lat, u2.home_lon) }
76 def distance(nearby_user)
77 return OSM::GreatCircle.new(self.home_lat, self.home_lon).distance(nearby_user.home_lat, nearby_user.home_lon)
80 def is_friends_with?(new_friend)
82 @new_friend = new_friend
83 self.friends.each do |friend|
84 if friend.user_id == @new_friend.user_id