Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the way "Friends" are added #5261

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [:index, :create, :destroy], Follow
can [:create, :reply, :show, :inbox, :outbox, :muted, :mark, :unmute, :destroy], Message
can [:close, :reopen], Note
can [:show, :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
4 changes: 2 additions & 2 deletions app/controllers/diary_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def index
end
elsif params[:friends]
if current_user
@title = t ".title_friends"
entries = DiaryEntry.where(:user => current_user.friends)
@title = t ".title_followed"
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, :only => [:create, :destroy, :index]
before_action :lookup_friend, :only => [:create, :destroy, :index]

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

def create
follow = Follow.new
follow.befriender = current_user
follow.befriendee = @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(:befriender => current_user, :befriendee => @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.

2 changes: 1 addition & 1 deletion app/helpers/changesets_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def changeset_details(changeset)

def changeset_index_title(params, user)
if params[:friends] && user
t "changesets.index.title_friend"
t "changesets.index.title_followed"
elsif params[:nearby] && user
t "changesets.index.title_nearby"
elsif params[:display_name]
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def friendship_notification(friendship)
with_recipient_locale friendship.befriendee do
@friendship = friendship
@viewurl = user_url(@friendship.befriender)
@friendurl = make_friend_url(@friendship.befriender)
@followurl = follow_url(@friendship.befriender)
@author = @friendship.befriender.display_name

attach_user_avatar(@friendship.befriender)
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 :befriender, :class_name => "User", :foreign_key => :user_id, :inverse_of => :follows
belongs_to :befriendee, :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(:befriendee).where(:users => { :status => %w[active confirmed] }) }
has_many :followings, :through => :follows, :source => :befriendee
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 @@ -281,7 +281,7 @@ def distance(nearby_user)
end

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

##
Expand Down Expand Up @@ -412,7 +412,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(:befriendee => 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
6 changes: 3 additions & 3 deletions app/views/dashboards/_contact.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% user_data = {
:lon => contact.home_lon,
:lat => contact.home_lat,
:icon => image_path(type == "friend" ? "marker-blue.png" : "marker-green.png"),
:icon => image_path(type == "following" ? "marker-blue.png" : "marker-green.png"),
:description => render(:partial => "popup", :object => contact, :locals => { :type => type })
} %>
<%= tag.div :class => "clearfix row", :data => { :user => user_data } do %>
Expand Down 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.remove as friend"), 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.show.add as friend"), 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
12 changes: 6 additions & 6 deletions app/views/dashboards/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@
<%= 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>

<div class="col-md">
<h2><%= t ".my friends" %></h2>
<h2><%= t ".followings" %></h2>

<% if friends.empty? %>
<%= t ".no friends" %>
<%= t ".no followings" %>
<% else %>
<nav class='secondary-actions mb-3'>
<ul class='clearfix'>
<li><%= link_to t(".friends_changesets"), friend_changesets_path %></li>
<li><%= link_to t(".friends_diaries"), friends_diary_entries_path %></li>
<li><%= link_to t(".followed_changesets"), friend_changesets_path %></li>
<li><%= link_to t(".followed_diaries"), friends_diary_entries_path %></li>
</ul>
</nav>
<div>
<%= render :partial => "contact", :collection => friends, :locals => { :type => "friend" } %>
<%= render :partial => "contact", :collection => friends, :locals => { :type => "following" } %>
</div>
<% end %>

Expand Down
8 changes: 8 additions & 0 deletions app/views/follows/index.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.

4 changes: 2 additions & 2 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 ".had_added_you", :user => @friendship.befriender.display_name %></p>
<p><%= t ".followed_you", :user => @friendship.befriender.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) -%>
<p><%= t ".befriend_them_html", :befriendurl => link_to(@friendurl, @friendurl) %></p>
<p><%= t ".follow_them_html", :followurl => link_to(@followurl, @followurl) %></p>
<% end -%>
<% end %>
4 changes: 2 additions & 2 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 '.had_added_you', :user => @friendship.befriender.display_name %>
<%= t '.followed_you', :user => @friendship.befriender.display_name %>

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

<% unless @friendship.befriendee.friends_with?(@friendship.befriender) -%>
<%= t '.befriend_them', :befriendurl => @friendurl %>
<%= 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(".remove as friend"), 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(".add as friend"), 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
Loading