Skip to content

Commit

Permalink
Merge branch 'pull/5512'
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Jan 18, 2025
2 parents 09aa3cc + 42d622b commit 2074e9d
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ Rails/ActionControllerFlashBeforeRender:
Exclude:
- 'app/controllers/application_controller.rb'
- 'app/controllers/confirmations_controller.rb'
- 'app/controllers/friendships_controller.rb'
- 'app/controllers/issue_comments_controller.rb'
- 'app/controllers/messages_controller.rb'
- 'app/controllers/passwords_controller.rb'
Expand Down Expand Up @@ -142,7 +141,6 @@ Rails/InverseOf:
Exclude:
- 'app/models/changeset.rb'
- 'app/models/diary_entry.rb'
- 'app/models/friendship.rb'
- 'app/models/issue.rb'
- 'app/models/message.rb'
- 'app/models/note.rb'
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/follows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ class FollowsController < ApplicationController
before_action :lookup_friend

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

def create
follow = Follow.new
follow.follower = current_user
follow.following = @friend
if current_user.friends_with?(@friend)
if current_user.follows?(@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
elsif current_user.follows.where(:created_at => Time.now.utc - 1.hour..).count >= current_user.max_follows_per_hour
flash[:error] = t ".limit_exceeded"
elsif follow.save
flash[:notice] = t ".success", :name => @friend.display_name
UserMailer.friendship_notification(follow).deliver_later
UserMailer.follow_notification(follow).deliver_later
else
follow.add_error(t(".failed", :name => @friend.display_name))
end
Expand All @@ -37,7 +37,7 @@ def create
end

def destroy
if current_user.friends_with?(@friend)
if current_user.follows?(@friend)
Follow.where(:follower => current_user, :following => @friend).delete_all
flash[:notice] = t ".success", :name => @friend.display_name
else
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 @@ -119,7 +119,7 @@ def diary_comment_notification(comment, recipient)
end
end

def friendship_notification(follow)
def follow_notification(follow)
with_recipient_locale follow.following do
@follow = follow
@viewurl = user_url(@follow.follower)
Expand Down
12 changes: 6 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ def distance(nearby_user)
OSM::GreatCircle.new(home_lat, home_lon).distance(nearby_user.home_lat, nearby_user.home_lon)
end

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

##
Expand Down Expand Up @@ -411,12 +411,12 @@ def max_messages_per_hour
max_messages.clamp(0, Settings.max_messages_per_hour)
end

def max_friends_per_hour
def max_follows_per_hour
account_age_in_seconds = Time.now.utc - created_at
account_age_in_hours = account_age_in_seconds / 3600
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)
recent_follows = Follow.where(:following => self).where(:created_at => Time.now.utc - 3600..).count
max_follows = account_age_in_hours.ceil + recent_follows - (active_reports * 10)
max_follows.clamp(0, Settings.max_follows_per_hour)
end

def max_changeset_comments_per_hour
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboards/_contact.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<ul class='clearfix text-body-secondary'>
<li><%= link_to t("users.show.send message"), new_message_path(contact) %></li>
<li>
<% if current_user.friends_with?(contact) %>
<% if current_user.follows?(contact) %>
<%= link_to t("users.show.unfollow"), follow_path(:display_name => contact.display_name, :referer => request.fullpath), :method => :delete %>
<% else %>
<%= link_to t("users.show.follow"), follow_path(:display_name => contact.display_name, :referer => request.fullpath), :method => :post %>
Expand Down
8 changes: 4 additions & 4 deletions app/views/dashboards/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
<%= tag.div "", :id => "map", :class => "content_map border border-secondary-subtle rounded z-0", :data => { :user => user_data } %>
<% end %>

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

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

<% if friends.empty? %>
<% if followings.empty? %>
<%= t ".no followings" %>
<% else %>
<nav class='secondary-actions mb-3'>
Expand All @@ -39,7 +39,7 @@
</ul>
</nav>
<div>
<%= render :partial => "contact", :collection => friends, :locals => { :type => "following" } %>
<%= render :partial => "contact", :collection => followings, :locals => { :type => "following" } %>
</div>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= message_body do %>
<p><%= t ".see_their_profile_html", :userurl => link_to(@viewurl, @viewurl) %></p>

<% unless @follow.following.friends_with?(@follow.follower) -%>
<% unless @follow.following.follows?(@follow.follower) -%>
<p><%= t ".follow_them_html", :followurl => link_to(@followurl, @followurl) %></p>
<% end -%>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

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

<% unless @follow.following.friends_with?(@follow.follower) -%>
<% unless @follow.following.follows?(@follow.follower) -%>
<%= t '.follow_them', :followurl => @followurl %>
<% end -%>
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
</li>
<% if current_user %>
<li>
<% if current_user.friends_with?(@user) %>
<% if current_user.follows?(@user) %>
<%= link_to t(".unfollow"), follow_path(:display_name => @user.display_name), :method => :delete %>
<% else %>
<%= link_to t(".follow"), follow_path(:display_name => @user.display_name), :method => :post %>
Expand Down
4 changes: 2 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ en:
title: My Dashboard
no_home_location_html: "%{edit_profile_link} and set your home location to see nearby users."
edit_your_profile: Edit your profile
followings: Followings
followings: Followings
no followings: You have not followed any user yet.
nearby users: "Other nearby users"
no nearby users: "There are no other users who admit to mapping nearby yet."
Expand Down Expand Up @@ -1654,7 +1654,7 @@ en:
header_html: "%{from_user} has sent you a message through OpenStreetMap with the subject %{subject}:"
footer: "You can also read the message at %{readurl} and you can send a message to the author at %{replyurl}"
footer_html: "You can also read the message at %{readurl} and you can send a message to the author at %{replyurl}"
friendship_notification:
follow_notification:
hi: "Hi %{to_user},"
subject: "[OpenStreetMap] %{user} followed you"
followed_you: "%{user} is now following you on OpenStreetMap."
Expand Down
4 changes: 2 additions & 2 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ max_messages_per_hour: 60
default_message_query_limit: 100
# Maximum number of messages returned by inbox and outbox message api
max_message_query_limit: 100
# Rate limit for friending
max_friends_per_hour: 60
# Rate limit for following
max_follows_per_hour: 60
# Rate limit for changeset comments
min_changeset_comments_per_hour: 1
initial_changeset_comments_per_hour: 6
Expand Down
14 changes: 7 additions & 7 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,18 @@ def test_display_name_user_id_unchanged_is_valid
assert_predicate user, :valid?, "user_0 display_name is invalid but it hasn't been changed"
end

def test_friends_with
def test_follows
alice = create(:user, :active)
bob = create(:user, :active)
charlie = create(:user, :active)
create(:follow, :follower => alice, :following => bob)

assert alice.friends_with?(bob)
assert_not alice.friends_with?(charlie)
assert_not bob.friends_with?(alice)
assert_not bob.friends_with?(charlie)
assert_not charlie.friends_with?(bob)
assert_not charlie.friends_with?(alice)
assert alice.follows?(bob)
assert_not alice.follows?(charlie)
assert_not bob.follows?(alice)
assert_not bob.follows?(charlie)
assert_not charlie.follows?(bob)
assert_not charlie.follows?(alice)
end

def test_users_nearby
Expand Down
2 changes: 1 addition & 1 deletion test/system/follows_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FollowsTest < ApplicationSystemTestCase

sign_in_as create(:user)

with_settings(:max_friends_per_hour => 0) do
with_settings(:max_follows_per_hour => 0) do
visit user_path(following)
assert_link "Follow"

Expand Down

0 comments on commit 2074e9d

Please sign in to comment.