From: Andy Allan Date: Wed, 3 Jun 2020 16:23:07 +0000 (+0200) Subject: Move make_friend and remove_friend to friendships controller X-Git-Tag: live~2697^2~1 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/8ebfdbc1ffacee695ca773f3b3c8af02bb41b8c1 Move make_friend and remove_friend to friendships controller --- diff --git a/app/abilities/ability.rb b/app/abilities/ability.rb index f0cebb380..5b1d7df68 100644 --- a/app/abilities/ability.rb +++ b/app/abilities/ability.rb @@ -37,11 +37,12 @@ class Ability if Settings.status != "database_offline" can [:index, :new, :create, :show, :edit, :update, :destroy], ClientApplication can [:new, :create, :edit, :update, :comment, :subscribe, :unsubscribe], DiaryEntry + can [:make_friend, :remove_friend], Friendship can [:new, :create, :reply, :show, :inbox, :outbox, :mark, :destroy], Message can [:close, :reopen], Note can [:new, :create], Report can [:mine, :new, :create, :edit, :update, :destroy], Trace - can [:account, :go_public, :make_friend, :remove_friend], User + can [:account, :go_public], User if user.moderator? can [:hide, :hidecomment], DiaryEntry diff --git a/app/controllers/friendships_controller.rb b/app/controllers/friendships_controller.rb new file mode 100644 index 000000000..034173e61 --- /dev/null +++ b/app/controllers/friendships_controller.rb @@ -0,0 +1,62 @@ +class FriendshipsController < ApplicationController + layout "site" + + before_action :authorize_web + before_action :set_locale + before_action :check_database_readable + + authorize_resource + + before_action :check_database_writable, :only => [:make_friend, :remove_friend] + + def make_friend + @new_friend = User.find_by(:display_name => params[:display_name]) + + if @new_friend + if request.post? + friendship = Friendship.new + friendship.befriender = current_user + friendship.befriendee = @new_friend + if current_user.is_friends_with?(@new_friend) + flash[:warning] = t "friendships.make_friend.already_a_friend", :name => @new_friend.display_name + elsif friendship.save + flash[:notice] = t "friendships.make_friend.success", :name => @new_friend.display_name + Notifier.friend_notification(friendship).deliver_later + else + friendship.add_error(t("friendships.make_friend.failed", :name => @new_friend.display_name)) + end + + if params[:referer] + redirect_to params[:referer] + else + redirect_to user_path + end + end + else + render_unknown_user params[:display_name] + end + end + + def remove_friend + @friend = User.find_by(:display_name => params[:display_name]) + + if @friend + if request.post? + if current_user.is_friends_with?(@friend) + Friendship.where(:befriender => current_user, :befriendee => @friend).delete_all + flash[:notice] = t "friendships.remove_friend.success", :name => @friend.display_name + else + flash[:error] = t "friendships.remove_friend.not_a_friend", :name => @friend.display_name + end + + if params[:referer] + redirect_to params[:referer] + else + redirect_to user_path + end + end + else + render_unknown_user params[:display_name] + end + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 514b3f8ee..4b949fe07 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -10,7 +10,7 @@ class UsersController < ApplicationController authorize_resource before_action :require_self, :only => [:account] - before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend] + before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public] before_action :require_cookies, :only => [:new, :login, :confirm] before_action :lookup_user_by_name, :only => [:set_status, :delete] before_action :allow_thirdparty_images, :only => [:show, :account] @@ -383,57 +383,6 @@ class UsersController < ApplicationController end end - def make_friend - @new_friend = User.find_by(:display_name => params[:display_name]) - - if @new_friend - if request.post? - friendship = Friendship.new - friendship.befriender = current_user - friendship.befriendee = @new_friend - if current_user.is_friends_with?(@new_friend) - flash[:warning] = t "users.make_friend.already_a_friend", :name => @new_friend.display_name - elsif friendship.save - flash[:notice] = t "users.make_friend.success", :name => @new_friend.display_name - Notifier.friend_notification(friendship).deliver_later - else - friendship.add_error(t("users.make_friend.failed", :name => @new_friend.display_name)) - end - - if params[:referer] - redirect_to params[:referer] - else - redirect_to :action => "show" - end - end - else - render_unknown_user params[:display_name] - end - end - - def remove_friend - @friend = User.find_by(:display_name => params[:display_name]) - - if @friend - if request.post? - if current_user.is_friends_with?(@friend) - Friendship.where(:befriender => current_user, :befriendee => @friend).delete_all - flash[:notice] = t "users.remove_friend.success", :name => @friend.display_name - else - flash[:error] = t "users.remove_friend.not_a_friend", :name => @friend.display_name - end - - if params[:referer] - redirect_to params[:referer] - else - redirect_to :action => "show" - end - end - else - render_unknown_user params[:display_name] - end - end - ## # sets a user's status def set_status diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index cebb46939..558339889 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -110,8 +110,7 @@ class Notifier < ApplicationMailer with_recipient_locale friend.befriendee do @friend = friend @viewurl = user_url(@friend.befriender) - @friendurl = url_for(:controller => "users", :action => "make_friend", - :display_name => @friend.befriender.display_name) + @friendurl = make_friend_url(@friend.befriender) @author = @friend.befriender.display_name attach_user_avatar(@friend.befriender) diff --git a/app/views/users/make_friend.html.erb b/app/views/friendships/make_friend.html.erb similarity index 100% rename from app/views/users/make_friend.html.erb rename to app/views/friendships/make_friend.html.erb diff --git a/app/views/users/remove_friend.html.erb b/app/views/friendships/remove_friend.html.erb similarity index 100% rename from app/views/users/remove_friend.html.erb rename to app/views/friendships/remove_friend.html.erb diff --git a/config/locales/en.yml b/config/locales/en.yml index f9ea78ef3..042d998b3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -430,6 +430,18 @@ en: comment: Comment newer_comments: "Newer Comments" older_comments: "Older Comments" + friendships: + make_friend: + heading: "Add %{user} as a friend?" + button: "Add as friend" + success: "%{name} is now your friend!" + failed: "Sorry, failed to add %{name} as a friend." + already_a_friend: "You are already friends with %{name}." + remove_friend: + heading: "Unfriend %{user}?" + button: "Unfriend" + success: "%{name} was removed from your friends." + not_a_friend: "%{name} is not one of your friends." geocoder: search: title: @@ -2271,17 +2283,6 @@ en: flash success: "Home location saved successfully" go_public: flash success: "All your edits are now public, and you are now allowed to edit." - make_friend: - heading: "Add %{user} as a friend?" - button: "Add as friend" - success: "%{name} is now your friend!" - failed: "Sorry, failed to add %{name} as a friend." - already_a_friend: "You are already friends with %{name}." - remove_friend: - heading: "Unfriend %{user}?" - button: "Unfriend" - success: "%{name} was removed from your friends." - not_a_friend: "%{name} is not one of your friends." index: title: Users heading: Users diff --git a/config/routes.rb b/config/routes.rb index b48dad733..2744b1323 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -236,12 +236,14 @@ OpenStreetMap::Application.routes.draw do # user pages get "/user/:display_name" => "users#show", :as => "user" - match "/user/:display_name/make_friend" => "users#make_friend", :via => [:get, :post], :as => "make_friend" - match "/user/:display_name/remove_friend" => "users#remove_friend", :via => [:get, :post], :as => "remove_friend" match "/user/:display_name/account" => "users#account", :via => [:get, :post], :as => "user_account" get "/user/:display_name/set_status" => "users#set_status", :as => :set_status_user get "/user/:display_name/delete" => "users#delete", :as => :delete_user + # friendships + match "/user/:display_name/make_friend" => "friendships#make_friend", :via => [:get, :post], :as => "make_friend" + match "/user/:display_name/remove_friend" => "friendships#remove_friend", :via => [:get, :post], :as => "remove_friend" + # user lists match "/users" => "users#index", :via => [:get, :post] match "/users/:status" => "users#index", :via => [:get, :post] diff --git a/test/controllers/friendships_controller_test.rb b/test/controllers/friendships_controller_test.rb index 906756850..b25986d84 100644 --- a/test/controllers/friendships_controller_test.rb +++ b/test/controllers/friendships_controller_test.rb @@ -6,19 +6,19 @@ class FriendshipsControllerTest < ActionDispatch::IntegrationTest def test_routes assert_routing( { :path => "/user/username/make_friend", :method => :get }, - { :controller => "users", :action => "make_friend", :display_name => "username" } + { :controller => "friendships", :action => "make_friend", :display_name => "username" } ) assert_routing( { :path => "/user/username/make_friend", :method => :post }, - { :controller => "users", :action => "make_friend", :display_name => "username" } + { :controller => "friendships", :action => "make_friend", :display_name => "username" } ) assert_routing( { :path => "/user/username/remove_friend", :method => :get }, - { :controller => "users", :action => "remove_friend", :display_name => "username" } + { :controller => "friendships", :action => "remove_friend", :display_name => "username" } ) assert_routing( { :path => "/user/username/remove_friend", :method => :post }, - { :controller => "users", :action => "remove_friend", :display_name => "username" } + { :controller => "friendships", :action => "remove_friend", :display_name => "username" } ) end @@ -32,7 +32,7 @@ class FriendshipsControllerTest < ActionDispatch::IntegrationTest # When not logged in a GET should ask us to login get make_friend_path(friend) - assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name) + assert_redirected_to login_path(:referer => make_friend_path(:display_name => friend.display_name)) # When not logged in a POST should error post make_friend_path(friend) @@ -129,7 +129,7 @@ class FriendshipsControllerTest < ActionDispatch::IntegrationTest # When not logged in a GET should ask us to login get remove_friend_path(friend) - assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name) + assert_redirected_to login_path(:referer => remove_friend_path(:display_name => friend.display_name)) # When not logged in a POST should error post remove_friend_path, :params => { :display_name => friend.display_name }