layout 'site'
before_filter :authorize, :only => [:preferences, :api_details, :api_gpx_files]
- before_filter :authorize_web, :only => [:edit, :account, :go_public, :view, :diary]
- before_filter :require_user, :only => [:edit, :set_home, :account, :go_public]
+ before_filter :authorize_web, :only => [:edit, :account, :go_public, :view, :diary, :make_friend]
+ before_filter :require_user, :only => [:edit, :set_home, :account, :go_public, :make_friend]
def save
@user = User.new(params[:user])
end
def make_friend
- if params[:display_name]
+
+ if params[:display_name]
+ name = params[:display_name]
+ friend = Friend.new
+ friend.user_id = @user.id
+ friend.friend_user_id = User.find_by_display_name(name).id
+ unless @user.is_friends_with?(friend)
+ if friend.save
+ flash[:notice] = "#{name} is now your friend"
+ else
+ friend.add_error("adding a friend failed")
+ end
+ else
+ flash[:notice] = "Your are already friends"
+ end
+ redirect_to :controller => 'user', :action => 'view'
end
end
has_many :traces
has_many :diary_entries
has_many :messages, :foreign_key => :to_user_id
-
+ has_many :friends
+
validates_confirmation_of :pass_crypt, :message => 'Password must match the confirmation password'
validates_uniqueness_of :display_name, :allow_nil => true
validates_uniqueness_of :email
self.timeout = Time.now
self.token = User.make_token()
end
-
+
def pass_crypt=(str)
write_attribute("pass_crypt", Digest::MD5.hexdigest(str))
end
def pass_crypt_confirmation=(str)
write_attribute("pass_crypt_confirm", Digest::MD5.hexdigest(str))
end
-
+
def self.authenticate(email, passwd) \r
find(:first, :conditions => [ "email = ? AND pass_crypt = ?", email, Digest::MD5.hexdigest(passwd)])\r
end
def self.authenticate_token(token)
find(:first, :conditions => [ "token = ? ", token])
end
-
+
def self.make_token(length=30)
chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
confirmstring = ''
el1['account_created'] = self.creation_time.xmlschema
return el1
end
-
+
def nearby(lat_range=1, lon_range=1)
-
if self.home_lon and self.home_lat
nearby = User.find(:all, :conditions => "#{self.home_lon} > home_lon - #{lon_range} and #{self.home_lon} < home_lon + #{lon_range} and #{self.home_lon} > home_lon - #{lon_range} and #{self.home_lon} < home_lon + #{lon_range} and data_public = 1")
else
messages = Message.find(:all, :conditions => "message_read = 0")
return messages
end
-
+
def get_all_messages
messages = Message.find(:all, :conditions => "message_read = 0")
return messages
end
-
+ def is_friends_with?(new_friend)
+ res = false
+ @new_friend = new_friend
+ self.friends.each do |friend|
+ if friend.user_id == @new_friend.user_id
+ return true
+ end
+ end
+ return false
+ end
+
end
map.connect '/traces/tag/:tag/page/:page', :controller => 'trace', :action => 'list', :id => nil
# user pages
+ map.connect '/user/:display_name/make_friend', :controller => 'user', :action => 'make_friend'
map.connect '/user/:display_name', :controller => 'user', :action => 'view'
map.connect '/user/:display_name/diary', :controller => 'user', :action => 'diary'
map.connect '/user/:display_name/diary/newpost', :controller => 'diary_entry', :action => 'new'
alter table users add column within_lat double default 2;
create table messages (id bigint not null auto_increment, user_id bigint(20) not null, from_user_id bigint(20) not null, title varchar(255), body text, sent_on datetime, message_read boolean default 0, primary key(id));
+
+create table friends (id bigint not null auto_increment, user_id bigint(20) not null, friend_user_id(20) not null, primary key(id));
+create index user_id_idx on friends(friend_user_id);