Merge branch 'pull/5512'

This commit is contained in:
Anton Khorev 2025-01-18 18:23:51 +03:00
commit 2074e9dce2
13 changed files with 32 additions and 34 deletions

View file

@ -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'
@ -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'

View file

@ -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
@ -37,7 +37,7 @@ class FollowsController < ApplicationController
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

View file

@ -119,7 +119,7 @@ class UserMailer < ApplicationMailer
end
end
def friendship_notification(follow)
def follow_notification(follow)
with_recipient_locale follow.following do
@follow = follow
@viewurl = user_url(@follow.follower)

View file

@ -282,8 +282,8 @@ class User < ApplicationRecord
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
##
@ -411,12 +411,12 @@ class User < ApplicationRecord
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

View file

@ -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 %>

View file

@ -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'>
@ -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 %>

View file

@ -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 %>

View file

@ -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 -%>

View file

@ -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 %>

View file

@ -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."

View file

@ -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

View file

@ -136,18 +136,18 @@ class UserTest < ActiveSupport::TestCase
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

View file

@ -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"