]> git.openstreetmap.org Git - rails.git/blob - app/models/user.rb
Improve handling of user to user messages.
[rails.git] / app / models / user.rb
1 class User < ActiveRecord::Base
2   require 'xml/libxml'
3   require 'digest/md5'
4
5   has_many :traces
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"
9   has_many :friends
10
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 => /^[^\/;.,?]*$/
18
19   before_save :encrypt_password
20
21   def set_defaults
22     self.creation_time = Time.now
23     self.timeout = Time.now
24     self.token = User.make_token()
25   end
26
27   def encrypt_password
28     self.pass_crypt = Digest::MD5.hexdigest(pass_crypt) unless pass_crypt_confirmation.nil?
29   end
30
31   def self.authenticate(email, passwd, active = true)
32     find(:first, :conditions => [ "email = ? AND pass_crypt = ? AND active = ?", email, Digest::MD5.hexdigest(passwd), active])
33   end 
34
35   def self.authenticate_token(token) 
36     find(:first, :conditions => [ "token = ? ", token])
37   end 
38
39   def self.make_token(length=30)
40     chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
41     confirmstring = ''
42
43     length.times do
44       confirmstring += chars[(rand * chars.length).to_i].chr
45     end
46
47     return confirmstring
48   end
49
50   def to_xml
51     doc = OSM::API.new.get_xml_doc
52     doc.root << to_xml_node()
53     return doc
54   end
55
56   def 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
60     return el1
61   end
62
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) }
70     else
71       nearby = []
72     end
73     return nearby
74   end
75
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)
78   end
79
80   def is_friends_with?(new_friend)
81     res = false
82     @new_friend = new_friend
83     self.friends.each do |friend|
84       if friend.user_id == @new_friend.user_id
85         return true
86       end
87     end
88     return false
89   end
90
91 end