From 07ffb2e3bedf4fe93cf8a47719d01a0288a2a18a Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Thu, 8 Feb 2024 16:57:15 +0300 Subject: [PATCH] Removed spammers from followers list (#20603) * Remove spam and suspended users from followers list (api and dashboards) * Fixed the api followers endpoint * Fixed issue with follower_ids query --- app/controllers/concerns/api/followers_controller.rb | 2 +- app/controllers/dashboards_controller.rb | 2 +- app/models/follow.rb | 1 + app/models/user.rb | 4 ++++ app/services/notifications/new_follower/send.rb | 3 +-- app/views/api/v1/users/me.json.jbuilder | 2 +- app/views/dashboards/_actions.html.erb | 2 +- app/views/dashboards/_actions_mobile.html.erb | 2 +- .../notify_mailer/new_follower_email.html.erb | 2 +- spec/requests/api/v1/users_spec.rb | 8 ++++++++ spec/requests/dashboard_spec.rb | 12 +++++++++++- 11 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/controllers/concerns/api/followers_controller.rb b/app/controllers/concerns/api/followers_controller.rb index bbd7b9e16..28394a946 100644 --- a/app/controllers/concerns/api/followers_controller.rb +++ b/app/controllers/concerns/api/followers_controller.rb @@ -10,7 +10,7 @@ module Api private_constant :USERS_ATTRIBUTES_FOR_SERIALIZATION def users - @follows = Follow.followable_user(@user.id) + @follows = Follow.non_suspended("User", @user.id) .includes(:follower) .select(USERS_ATTRIBUTES_FOR_SERIALIZATION) .order(order_criteria) diff --git a/app/controllers/dashboards_controller.rb b/app/controllers/dashboards_controller.rb index a392080df..347054fe5 100644 --- a/app/controllers/dashboards_controller.rb +++ b/app/controllers/dashboards_controller.rb @@ -76,7 +76,7 @@ class DashboardsController < ApplicationController def followers fetch_and_authorize_user - @follows = Follow.followable_user(@user.id) + @follows = Follow.non_suspended("User", @user.id) .includes(:follower).order(created_at: :desc).limit(follows_limit) @collections_count = collections_count(@user) end diff --git a/app/models/follow.rb b/app/models/follow.rb index fc7281716..670b7de83 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -35,6 +35,7 @@ class Follow < ApplicationRecord .where(followable_type: followable_type, followable_id: followable_id) .where("follows.follower_type = 'User'") .where("roles.name NOT IN (?) OR roles.name IS NULL", %w[suspended spam]) + .distinct } counter_culture :follower, column_name: proc { |follow| COUNTER_CULTURE_COLUMN_NAME_BY_TYPE[follow.followable_type] }, diff --git a/app/models/user.rb b/app/models/user.rb index 85f7fd4f1..eca0764f5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -262,6 +262,10 @@ class User < ApplicationRecord find_by(id: Settings::General.mascot_user_id) end + def good_standing_followers_count + Follow.non_suspended("User", id).count + end + def tag_line profile.summary end diff --git a/app/services/notifications/new_follower/send.rb b/app/services/notifications/new_follower/send.rb index 095289228..4d109d99c 100644 --- a/app/services/notifications/new_follower/send.rb +++ b/app/services/notifications/new_follower/send.rb @@ -31,8 +31,7 @@ module Notifications when "Organization" notification_params[:organization_id] = followable_id end - - followers = User.where(id: recent_follows.select(:follower_id)) + followers = User.where(id: recent_follows.map(&:follower_id)) aggregated_siblings = followers.map { |follower| user_data(follower) } if aggregated_siblings.empty? notification = Notification.find_by(notification_params)&.destroy diff --git a/app/views/api/v1/users/me.json.jbuilder b/app/views/api/v1/users/me.json.jbuilder index 760073970..f754f1241 100644 --- a/app/views/api/v1/users/me.json.jbuilder +++ b/app/views/api/v1/users/me.json.jbuilder @@ -1,2 +1,2 @@ json.partial! "api/v1/shared/user_show_extended", user: @user -json.followers_count @user.followers_count +json.followers_count @user.good_standing_followers_count diff --git a/app/views/dashboards/_actions.html.erb b/app/views/dashboards/_actions.html.erb index f25d3f2a2..3d7cff51b 100644 --- a/app/views/dashboards/_actions.html.erb +++ b/app/views/dashboards/_actions.html.erb @@ -24,7 +24,7 @@ href="/dashboard/user_followers" <%= params[:action] == "followers" ? ' aria-current="page"'.html_safe : "" %>> <%= t("views.dashboard.actions.followers") %> - <%= @user.followers_count %> + <%= @user.good_standing_followers_count %> <% end %> diff --git a/app/views/dashboards/_actions_mobile.html.erb b/app/views/dashboards/_actions_mobile.html.erb index 273445452..0284c138c 100644 --- a/app/views/dashboards/_actions_mobile.html.erb +++ b/app/views/dashboards/_actions_mobile.html.erb @@ -1,7 +1,7 @@