]> git.openstreetmap.org Git - rails.git/blob - app/controllers/friendships_controller.rb
a983bec751442d36a31b26f79b8fca595ad024ea
[rails.git] / app / controllers / friendships_controller.rb
1 class FriendshipsController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :check_database_readable
7
8   authorize_resource
9
10   before_action :check_database_writable, :only => [:make_friend, :remove_friend]
11
12   def make_friend
13     @new_friend = User.find_by(:display_name => params[:display_name])
14
15     if @new_friend
16       if request.post?
17         friendship = Friendship.new
18         friendship.befriender = current_user
19         friendship.befriendee = @new_friend
20         if current_user.is_friends_with?(@new_friend)
21           flash[:warning] = t "friendships.make_friend.already_a_friend", :name => @new_friend.display_name
22         elsif friendship.save
23           flash[:notice] = t "friendships.make_friend.success", :name => @new_friend.display_name
24           Notifier.friendship_notification(friendship).deliver_later
25         else
26           friendship.add_error(t("friendships.make_friend.failed", :name => @new_friend.display_name))
27         end
28
29         if params[:referer]
30           redirect_to params[:referer]
31         else
32           redirect_to user_path
33         end
34       end
35     else
36       render_unknown_user params[:display_name]
37     end
38   end
39
40   def remove_friend
41     @friend = User.find_by(:display_name => params[:display_name])
42
43     if @friend
44       if request.post?
45         if current_user.is_friends_with?(@friend)
46           Friendship.where(:befriender => current_user, :befriendee => @friend).delete_all
47           flash[:notice] = t "friendships.remove_friend.success", :name => @friend.display_name
48         else
49           flash[:error] = t "friendships.remove_friend.not_a_friend", :name => @friend.display_name
50         end
51
52         if params[:referer]
53           redirect_to params[:referer]
54         else
55           redirect_to user_path
56         end
57       end
58     else
59       render_unknown_user params[:display_name]
60     end
61   end
62 end