Skip to content

Commit

Permalink
Refactor friendships controller and model
Browse files Browse the repository at this point in the history
  • Loading branch information
nertc committed Jan 14, 2025
1 parent 2b7a480 commit be11f20
Show file tree
Hide file tree
Showing 27 changed files with 318 additions and 339 deletions.
2 changes: 1 addition & 1 deletion app/abilities/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(user)
can [:create, :subscribe, :unsubscribe], DiaryEntry
can :update, DiaryEntry, :user => user
can [:create], DiaryComment
can [:make_friend, :remove_friend], Friendship
can [:show, :create, :destroy], Follow
can [:read, :create, :mark, :unmute, :destroy], Message
can [:close, :reopen], Note
can [:read, :update], :preference
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/changesets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def index
elsif @params[:bbox]
changesets = conditions_bbox(changesets, BoundingBox.from_bbox_params(params))
elsif @params[:friends] && current_user
changesets = changesets.where(:user => current_user.friends.identifiable)
changesets = changesets.where(:user => current_user.followings.identifiable)
elsif @params[:nearby] && current_user
changesets = changesets.where(:user => current_user.nearby)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/diary_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def index
elsif params[:friends]
if current_user
@title = t ".title_followed"
entries = DiaryEntry.where(:user => current_user.friends)
entries = DiaryEntry.where(:user => current_user.followings)
else
require_user
return
Expand Down
61 changes: 61 additions & 0 deletions app/controllers/follows_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class FollowsController < ApplicationController
include UserMethods

layout "site"

before_action :authorize_web
before_action :set_locale
before_action :check_database_readable

authorize_resource

before_action :check_database_writable
before_action :lookup_friend

def show
@already_follows = current_user.friends_with?(@friend)
end

def create
follow = Follow.new
follow.follower = current_user
follow.following = @friend
if current_user.friends_with?(@friend)
flash[:warning] = t ".already_followed", :name => @friend.display_name
elsif current_user.follows.where(:created_at => Time.now.utc - 1.hour..).count >= current_user.max_friends_per_hour
flash[:error] = t ".limit_exceeded"
elsif follow.save
flash[:notice] = t ".success", :name => @friend.display_name
UserMailer.friendship_notification(follow).deliver_later
else
follow.add_error(t(".failed", :name => @friend.display_name))
end

referer = safe_referer(params[:referer]) if params[:referer]

redirect_to referer || user_path
end

def destroy
if current_user.friends_with?(@friend)
Follow.where(:follower => current_user, :following => @friend).delete_all
flash[:notice] = t ".success", :name => @friend.display_name
else
flash[:error] = t ".not_followed", :name => @friend.display_name
end

referer = safe_referer(params[:referer]) if params[:referer]

redirect_to referer || user_path
end

private

##
# ensure that there is a "friend" instance variable
def lookup_friend
@friend = User.active.find_by!(:display_name => params[:display_name])
rescue ActiveRecord::RecordNotFound
render_unknown_user params[:display_name]
end
end
61 changes: 0 additions & 61 deletions app/controllers/friendships_controller.rb

This file was deleted.

20 changes: 10 additions & 10 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ def diary_comment_notification(comment, recipient)
end
end

def friendship_notification(friendship)
with_recipient_locale friendship.befriendee do
@friendship = friendship
@viewurl = user_url(@friendship.befriender)
@followurl = follow_url(@friendship.befriender)
@author = @friendship.befriender.display_name

attach_user_avatar(@friendship.befriender)
mail :to => friendship.befriendee.email,
:subject => t(".subject", :user => friendship.befriender.display_name)
def friendship_notification(follow)
with_recipient_locale follow.following do
@follow = follow
@viewurl = user_url(@follow.follower)
@followurl = follow_url(@follow.follower)
@author = @follow.follower.display_name

attach_user_avatar(@follow.follower)
mail :to => follow.following.email,
:subject => t(".subject", :user => follow.follower.display_name)
end
end

Expand Down
6 changes: 3 additions & 3 deletions app/models/friendship.rb → app/models/follow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
# friends_user_id_fkey (user_id => users.id)
#

class Friendship < ApplicationRecord
class Follow < ApplicationRecord
self.table_name = "friends"

belongs_to :befriender, :class_name => "User", :foreign_key => :user_id
belongs_to :befriendee, :class_name => "User", :foreign_key => :friend_user_id
belongs_to :follower, :class_name => "User", :foreign_key => :user_id, :inverse_of => :follows
belongs_to :following, :class_name => "User", :foreign_key => :friend_user_id, :inverse_of => :follows
end
8 changes: 4 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class User < ApplicationRecord
has_many :new_messages, -> { where(:to_user_visible => true, :muted => false, :message_read => false).order(:sent_on => :desc) }, :class_name => "Message", :foreign_key => :to_user_id
has_many :sent_messages, -> { where(:from_user_visible => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :class_name => "Message", :foreign_key => :from_user_id
has_many :muted_messages, -> { where(:to_user_visible => true, :muted => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :class_name => "Message", :foreign_key => :to_user_id
has_many :friendships, -> { joins(:befriendee).where(:users => { :status => %w[active confirmed] }) }
has_many :friends, :through => :friendships, :source => :befriendee
has_many :follows, -> { joins(:following).where(:users => { :status => %w[active confirmed] }) }
has_many :followings, :through => :follows, :source => :following
has_many :preferences, :class_name => "UserPreference"
has_many :changesets, -> { order(:created_at => :desc) }, :inverse_of => :user
has_many :changeset_comments, :foreign_key => :author_id, :inverse_of => :author
Expand Down Expand Up @@ -283,7 +283,7 @@ def distance(nearby_user)
end

def friends_with?(new_friend)
friendships.exists?(:befriendee => new_friend)
follows.exists?(:following => new_friend)
end

##
Expand Down Expand Up @@ -414,7 +414,7 @@ def max_messages_per_hour
def max_friends_per_hour
account_age_in_seconds = Time.now.utc - created_at
account_age_in_hours = account_age_in_seconds / 3600
recent_friends = Friendship.where(:befriendee => self).where(:created_at => Time.now.utc - 3600..).count
recent_friends = Follow.where(:following => self).where(:created_at => Time.now.utc - 3600..).count
max_friends = account_age_in_hours.ceil + recent_friends - (active_reports * 10)
max_friends.clamp(0, Settings.max_friends_per_hour)
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/dashboards/_contact.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
<li><%= link_to t("users.show.send message"), new_message_path(contact) %></li>
<li>
<% if current_user.friends_with?(contact) %>
<%= link_to t("users.show.unfollow"), remove_friend_path(:display_name => contact.display_name, :referer => request.fullpath), :method => :post %>
<%= link_to t("users.show.unfollow"), follow_path(:display_name => contact.display_name, :referer => request.fullpath), :method => :delete %>
<% else %>
<%= link_to t("users.follow"), make_friend_path(:display_name => contact.display_name, :referer => request.fullpath), :method => :post %>
<%= link_to t("users.follow"), follow_path(:display_name => contact.display_name, :referer => request.fullpath), :method => :post %>
<% end %>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboards/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<%= tag.div "", :id => "map", :class => "content_map border border-secondary-subtle rounded z-0", :data => { :user => user_data } %>
<% end %>

<% friends = @user.friends %>
<% friends = @user.followings %>
<% nearby = @user.nearby - friends %>
</div>

Expand Down
8 changes: 8 additions & 0 deletions app/views/follows/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% content_for :heading do %>
<h1><%= t(@already_follows ? ".unfollow.heading" : ".follow.heading", :user => @friend.display_name) %></h1>
<% end %>

<%= link_to t(@already_follows ? ".unfollow.button" : ".follow.button"),
follow_path(:display_name => @friend.display_name, :referer => params[:referer]),
:method => (@already_follows ? :delete : :post),
:class => "btn btn-sm btn-primary" %>
10 changes: 0 additions & 10 deletions app/views/friendships/make_friend.html.erb

This file was deleted.

10 changes: 0 additions & 10 deletions app/views/friendships/remove_friend.html.erb

This file was deleted.

6 changes: 3 additions & 3 deletions app/views/user_mailer/friendship_notification.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<p><%= t ".hi", :to_user => @friendship.befriendee.display_name %></p>
<p><%= t ".hi", :to_user => @follow.following.display_name %></p>

<p><%= t ".followed_you", :user => @friendship.befriender.display_name %></p>
<p><%= t ".followed_you", :user => @follow.follower.display_name %></p>

<%= message_body do %>
<p><%= t ".see_their_profile_html", :userurl => link_to(@viewurl, @viewurl) %></p>

<% unless @friendship.befriendee.friends_with?(@friendship.befriender) -%>
<% unless @follow.following.friends_with?(@follow.follower) -%>
<p><%= t ".follow_them_html", :followurl => link_to(@followurl, @followurl) %></p>
<% end -%>
<% end %>
6 changes: 3 additions & 3 deletions app/views/user_mailer/friendship_notification.text.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<%= t ".hi", :to_user => @friendship.befriendee.display_name %>
<%= t ".hi", :to_user => @follow.following.display_name %>

<%= t '.followed_you', :user => @friendship.befriender.display_name %>
<%= t '.followed_you', :user => @follow.follower.display_name %>

<%= t '.see_their_profile', :userurl => @viewurl %>

<% unless @friendship.befriendee.friends_with?(@friendship.befriender) -%>
<% unless @follow.following.friends_with?(@follow.follower) -%>
<%= t '.follow_them', :followurl => @followurl %>
<% end -%>
4 changes: 2 additions & 2 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@
<% if current_user %>
<li>
<% if current_user.friends_with?(@user) %>
<%= link_to t(".unfollow"), remove_friend_path(:display_name => @user.display_name), :method => :post %>
<%= link_to t(".unfollow"), follow_path(:display_name => @user.display_name), :method => :delete %>
<% else %>
<%= link_to t(".follow"), make_friend_path(:display_name => @user.display_name), :method => :post %>
<%= link_to t(".follow"), follow_path(:display_name => @user.display_name), :method => :post %>
<% end %>
</li>
<% end %>
Expand Down
17 changes: 10 additions & 7 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -686,17 +686,20 @@ en:
not_found:
title: File not found
description: Couldn't find a file/directory/API operation by that name on the OpenStreetMap server (HTTP 404)
friendships:
make_friend:
heading: "Do you want to follow %{user}?"
button: "Follow User"
follows:
show:
follow:
heading: "Do you want to follow %{user}?"
button: "Follow User"
unfollow:
heading: "Do you want to unfollow %{user}?"
button: "Unfollow User"
create:
success: "You are now following %{name}!"
failed: "Sorry, your request to follow %{name} has failed."
already_followed: "You already follow %{name}."
limit_exceeded: "You have followed a lot of users recently. Please wait a while before trying to follow any more."
remove_friend:
heading: "Do you want to unfollow %{user}?"
button: "Unfollow"
destroy:
success: "You successfully unfollowed %{name}."
not_followed: "You are not following %{name}."
geocoder:
Expand Down
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,12 @@
resource :profile, :only => [:edit, :update]

# 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"
scope "/user/:display_name" do
resource :follow, :only => [:create, :destroy, :show], :path => "follow"

get "make_friend", :to => redirect("/user/%{display_name}/follow")
get "remove_friend", :to => redirect("/user/%{display_name}/follow")
end

# user lists
namespace :users do
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/changesets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ def test_index_user_not_found
# Checks the display of the friends changesets listing
def test_index_friends
private_user = create(:user, :data_public => true)
friendship = create(:friendship, :befriender => private_user)
changeset = create(:changeset, :user => friendship.befriendee, :num_changes => 1)
follow = create(:follow, :follower => private_user)
changeset = create(:changeset, :user => follow.following, :num_changes => 1)
_changeset2 = create(:changeset, :user => create(:user), :num_changes => 1)

get friend_changesets_path
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/diary_entries_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ def test_index_user
def test_index_friends
user = create(:user)
other_user = create(:user)
friendship = create(:friendship, :befriender => user)
diary_entry = create(:diary_entry, :user => friendship.befriendee)
follow = create(:follow, :follower => user)
diary_entry = create(:diary_entry, :user => follow.following)
_other_entry = create(:diary_entry, :user => other_user)

# Try a list of diary entries for your friends when not logged in
Expand Down
Loading

0 comments on commit be11f20

Please sign in to comment.