Avoid sending follow notifications from spammers (#20526)

This commit is contained in:
Anna Buianova 2024-01-15 19:50:21 +03:00 committed by GitHub
parent 08d34d5d34
commit 539844165f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

View file

@ -27,13 +27,14 @@ class Follow < ApplicationRecord
scope :follower_podcast, ->(id) { where(follower_id: id, followable_type: "Podcast") }
scope :follower_tag, ->(id) { where(follower_id: id, followable_type: "ActsAsTaggableOn::Tag") }
# Follows from users who don't have suspended or spam role
scope :non_suspended, lambda { |followable_type, followable_id|
joins("INNER JOIN users ON users.id = follows.follower_id")
.joins("LEFT JOIN users_roles ON users_roles.user_id = users.id")
.joins("LEFT JOIN roles ON roles.id = users_roles.role_id")
.where(followable_type: followable_type, followable_id: followable_id)
.where("follows.follower_type = 'User'")
.where("roles.name != 'suspended' OR roles.name IS NULL")
.where("roles.name NOT IN (?) OR roles.name IS NULL", %w[suspended spam])
}
counter_culture :follower, column_name: proc { |follow| COUNTER_CULTURE_COLUMN_NAME_BY_TYPE[follow.followable_type] },

View file

@ -4,11 +4,8 @@ RSpec.describe Follow do
let(:user) { create(:user) }
let(:tag) { create(:tag) }
let(:user_2) { create(:user) }
let(:suspended_user) { create(:user) }
before do
suspended_user.add_role(:suspended)
end
let(:suspended_user) { create(:user, :suspended) }
let(:spam_user) { create(:user, :spam) }
describe "validations" do
subject { user.follow(user_2) }
@ -77,15 +74,17 @@ RSpec.describe Follow do
user.follow(user_2)
user.follow(tag)
suspended_user.follow(user_2)
spam_user.follow(user_2)
end
it "excludes suspended users from the result" do
it "excludes suspended users from the result", :aggregate_failures do
result = described_class.non_suspended(user_2.class.name, user_2.id)
expect(result.map(&:follower)).to include(user)
expect(result.map(&:follower)).not_to include(suspended_user)
expect(result.map(&:follower)).not_to include(spam_user)
end
it "filters by followable type and id" do
it "filters by followable type and id", :aggregate_failures do
result = described_class.non_suspended("ActsAsTaggableOn::Tag", tag.id)
expect(result.map(&:follower)).to include(user)
expect(result.map(&:follower)).not_to include(user_2)