]> git.openstreetmap.org Git - rails.git/blob - app/controllers/follows_controller.rb
Rename friendship_notification to follow_notification
[rails.git] / app / controllers / follows_controller.rb
1 class FollowsController < ApplicationController
2   include UserMethods
3
4   layout "site"
5
6   before_action :authorize_web
7   before_action :set_locale
8   before_action :check_database_readable
9
10   authorize_resource
11
12   before_action :check_database_writable
13   before_action :lookup_friend
14
15   def show
16     @already_follows = current_user.follows?(@friend)
17   end
18
19   def create
20     follow = Follow.new
21     follow.follower = current_user
22     follow.following = @friend
23     if current_user.follows?(@friend)
24       flash[:warning] = t ".already_followed", :name => @friend.display_name
25     elsif current_user.follows.where(:created_at => Time.now.utc - 1.hour..).count >= current_user.max_friends_per_hour
26       flash[:error] = t ".limit_exceeded"
27     elsif follow.save
28       flash[:notice] = t ".success", :name => @friend.display_name
29       UserMailer.follow_notification(follow).deliver_later
30     else
31       follow.add_error(t(".failed", :name => @friend.display_name))
32     end
33
34     referer = safe_referer(params[:referer]) if params[:referer]
35
36     redirect_to referer || user_path
37   end
38
39   def destroy
40     if current_user.follows?(@friend)
41       Follow.where(:follower => current_user, :following => @friend).delete_all
42       flash[:notice] = t ".success", :name => @friend.display_name
43     else
44       flash[:error] = t ".not_followed", :name => @friend.display_name
45     end
46
47     referer = safe_referer(params[:referer]) if params[:referer]
48
49     redirect_to referer || user_path
50   end
51
52   private
53
54   ##
55   # ensure that there is a "friend" instance variable
56   def lookup_friend
57     @friend = User.active.find_by!(:display_name => params[:display_name])
58   rescue ActiveRecord::RecordNotFound
59     render_unknown_user params[:display_name]
60   end
61 end