1 class FriendshipsController < ApplicationController
4 before_action :authorize_web
5 before_action :set_locale
6 before_action :check_database_readable
10 before_action :check_database_writable, :only => [:make_friend, :remove_friend]
13 @new_friend = User.find_by(:display_name => params[:display_name])
17 friendship = Friendship.new
18 friendship.befriender = current_user
19 friendship.befriendee = @new_friend
20 if current_user.friends_with?(@new_friend)
21 flash[:warning] = t "friendships.make_friend.already_a_friend", :name => @new_friend.display_name
22 elsif current_user.friendships.where("created_at >= ?", Time.now.utc - 1.hour).count >= current_user.max_friends_per_hour
23 flash.now[:error] = t "friendships.make_friend.limit_exceeded"
25 flash[:notice] = t "friendships.make_friend.success", :name => @new_friend.display_name
26 UserMailer.friendship_notification(friendship).deliver_later
28 friendship.add_error(t("friendships.make_friend.failed", :name => @new_friend.display_name))
31 referer = safe_referer(params[:referer]) if params[:referer]
33 redirect_to referer || user_path
36 render_unknown_user params[:display_name]
41 @friend = User.find_by(:display_name => params[:display_name])
45 if current_user.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
49 flash[:error] = t "friendships.remove_friend.not_a_friend", :name => @friend.display_name
52 referer = safe_referer(params[:referer]) if params[:referer]
54 redirect_to referer || user_path
57 render_unknown_user params[:display_name]