From: Dan Karran Date: Mon, 8 Oct 2007 22:21:15 +0000 (+0000) Subject: Improving friend capabilities so you can now add and remove friends as you wish.... X-Git-Tag: live~8084 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/d320673cc7bc8d5e79cea3c5ea5f9005129a63e6 Improving friend capabilities so you can now add and remove friends as you wish. Friends will be notified when you add them. You can view a list of friends on your profile page (which has been reorganised a little). --- diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index e69ce3137..a9b91098f 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -2,8 +2,8 @@ class UserController < ApplicationController layout 'site' before_filter :authorize, :only => [:api_details, :api_gpx_files] - before_filter :authorize_web, :only => [:account, :go_public, :view, :diary, :make_friend] - before_filter :require_user, :only => [:set_home, :account, :go_public, :make_friend] + before_filter :authorize_web, :only => [:account, :go_public, :view, :diary, :make_friend, :remove_friend] + before_filter :require_user, :only => [:set_home, :account, :go_public, :make_friend, :remove_friend] filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation @@ -178,15 +178,16 @@ class UserController < ApplicationController end def make_friend - if params[:display_name] name = params[:display_name] + new_friend = User.find_by_display_name(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) + friend.friend_user_id = new_friend.id + unless @user.is_friends_with?(new_friend) if friend.save flash[:notice] = "#{name} is now your friend." + Notifier::deliver_friend_notification(friend) else friend.add_error("Sorry, failed to add #{name} as a friend.") end @@ -197,5 +198,19 @@ class UserController < ApplicationController end end + def remove_friend + if params[:display_name] + name = params[:display_name] + friend = User.find_by_display_name(name) + if @user.is_friends_with?(friend) + Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}" + flash[:notice] = "#{friend.display_name} was removed from your friends." + else + flash[:notice] = "#{friend.display_name} was not already one of your friends." + end + redirect_to :controller => 'user', :action => 'view' + end + end + end diff --git a/app/models/notifier.rb b/app/models/notifier.rb index 9eff2fb92..e471730c1 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -49,4 +49,14 @@ class Notifier < ActionMailer::Base @body['readurl'] = "http://#{SERVER_URL}/message/read/#{message.id}" @body['replyurl'] = "http://#{SERVER_URL}/message/new/#{message.from_user_id}" end + + def friend_notification(friend) + @friend = User.find_by_id(friend.user_id) + @new_friend = User.find_by_id(friend.friend_user_id) + @recipients = @new_friend.email + @from = 'abuse@openstreetmap.org' + @subject = "[OpenStreetMap] #{@friend.display_name} added you as a friend" + @body['user'] = @friend.display_name + @body['userurl'] = "http://#{SERVER_URL}/user/#{@friend.display_name}" + end end diff --git a/app/models/user.rb b/app/models/user.rb index 83a8194dd..c4b81e7ab 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -93,7 +93,7 @@ class User < ActiveRecord::Base res = false @new_friend = new_friend self.friends.each do |friend| - if friend.user_id == @new_friend.user_id + if friend.friend_user_id == @new_friend.id return true end end diff --git a/app/views/notifier/friend_notification.rhtml b/app/views/notifier/friend_notification.rhtml new file mode 100644 index 000000000..4103a763d --- /dev/null +++ b/app/views/notifier/friend_notification.rhtml @@ -0,0 +1,3 @@ +<%= @user %> has added you as a friend on OpenStreetMap. + +You can see their profile at <%= @userurl %> and add them as a friend too if you wish. \ No newline at end of file diff --git a/app/views/user/view.rhtml b/app/views/user/view.rhtml index 97a90ed61..d0600bb8d 100644 --- a/app/views/user/view.rhtml +++ b/app/views/user/view.rhtml @@ -1,30 +1,35 @@ +<% @this_user = User.find_by_display_name(@this_user.display_name) %>

<%= @this_user.display_name %>

-
<%= simple_format(@this_user.description) %>
- +
<% if @user and @this_user.id == @user.id %> -

Your diary

-<%= link_to 'View your diary', :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name %>
-<%= link_to 'New diary post', :controller => 'diary_entry', :action => 'new', :display_name => @user.display_name %> -

Your traces

-<%= link_to 'View your traces', :controller => 'trace', :action=>'mine' %> -

Your account

-<%= link_to 'Edit your settings', :controller => 'user', :action => 'account', :display_name => @user.display_name %> +<%= link_to 'my diary', :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name %> +| <%= link_to 'new diary post', :controller => 'diary_entry', :action => 'new', :display_name => @user.display_name %> +| <%= link_to 'my traces', :controller => 'trace', :action=>'mine' %> +| <%= link_to 'my settings', :controller => 'user', :action => 'account', :display_name => @user.display_name %> +<% else %> +<%= link_to 'send message', :controller => 'message', :action => 'new', :user_id => @this_user.id %> +| <%= link_to 'diary', :controller => 'diary_entry', :action => 'list', :display_name => @this_user.display_name %> +| <%= link_to 'traces', :controller => 'trace', :action => 'view', :display_name => @this_user.display_name %> +| <% if @user and @user.is_friends_with?(@this_user) %> + <%= link_to 'remove as friend', :controller => 'user', :action => 'remove_friend', :display_name => @this_user.display_name %> <% else %> -<%= link_to 'Send message', :controller => 'message', :action => 'new', :user_id => @this_user.id %>
-<%= link_to 'Add as friend', :controller => 'user', :action => 'make_friend', :display_name => @this_user.display_name %>
-<%= link_to 'View diary', :controller => 'diary_entry', :action => 'list', :display_name => @this_user.display_name %>
-<%= link_to 'View traces', :controller => 'trace', :action => 'view', :display_name => @this_user.display_name %> + <%= link_to 'add as friend', :controller => 'user', :action => 'make_friend', :display_name => @this_user.display_name %> <% end %> +<% end %> +
+ +
<%= simple_format(@this_user.description) %>
-

Nearby users

+

User location

<% if @this_user.home_lat.nil? or @this_user.home_lon.nil? %> No home location has been set. <% if @user and @this_user.id == @user.id %> You can set your home location on your <%= link_to 'settings', :controller => 'user', :action => 'account', :display_name => @user.display_name %> page. <% end %> <% else %> + Nearby users:
<% if @this_user.nearby.empty? %> - There are no users who admit to mapping nearby. + There are no users who admit to mapping nearby yet. <% else %> <% @this_user.nearby.each do |nearby| %> @@ -36,4 +41,23 @@ <%end%>
<%end%> + + <% if @user and @this_user.id == @user.id %> +
+ Your friends:
+ <% if @this_user.friends.empty? %> + You have not added any friends yet. + <% else %> + + <% @this_user.friends.each do |friend| %> + <% @friend = User.find_by_id(friend.friend_user_id) %> + + + + + + <%end%> +
<%= link_to @friend.display_name, :controller => 'user', :action => 'view', :display_name => @friend.display_name %><%= @this_user.distance(@friend).round %>km away(<%= link_to 'send message', :controller => 'message', :action => 'new', :user_id => @friend.id %>)
+ <%end%> + <%end%> <% end %> diff --git a/config/routes.rb b/config/routes.rb index bf1f2a20f..81bcb52a6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -91,8 +91,9 @@ ActionController::Routing::Routes.draw do |map| map.connect '/user/:display_name/traces/:id/icon', :controller => 'trace', :action => 'icon' # 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/make_friend', :controller => 'user', :action => 'make_friend' + map.connect '/user/:display_name/remove_friend', :controller => 'user', :action => 'remove_friend' map.connect '/user/:display_name/diary', :controller => 'diary_entry', :action => 'list' map.connect '/user/:display_name/diary/:id', :controller => 'diary_entry', :action => 'list', :id => /\d+/ map.connect '/user/:display_name/diary/rss', :controller => 'diary_entry', :action => 'rss'