1 class FriendshipsController < ApplicationController
6 before_action :authorize_web
7 before_action :set_locale
8 before_action :check_database_readable
12 before_action :check_database_writable, :only => [:make_friend, :remove_friend]
15 @new_friend = User.find_by(:display_name => params[:display_name])
19 friendship = Friendship.new
20 friendship.befriender = current_user
21 friendship.befriendee = @new_friend
22 if current_user.friends_with?(@new_friend)
23 flash[:warning] = t ".already_a_friend", :name => @new_friend.display_name
24 elsif current_user.friendships.where("created_at >= ?", Time.now.utc - 1.hour).count >= current_user.max_friends_per_hour
25 flash.now[:error] = t ".limit_exceeded"
27 flash[:notice] = t ".success", :name => @new_friend.display_name
28 UserMailer.friendship_notification(friendship).deliver_later
30 friendship.add_error(t(".failed", :name => @new_friend.display_name))
33 referer = safe_referer(params[:referer]) if params[:referer]
35 redirect_to referer || user_path
38 render_unknown_user params[:display_name]
43 @friend = User.find_by(:display_name => params[:display_name])
47 if current_user.friends_with?(@friend)
48 Friendship.where(:befriender => current_user, :befriendee => @friend).delete_all
49 flash[:notice] = t ".success", :name => @friend.display_name
51 flash[:error] = t ".not_a_friend", :name => @friend.display_name
54 referer = safe_referer(params[:referer]) if params[:referer]
56 redirect_to referer || user_path
59 render_unknown_user params[:display_name]