th: automod v2.2

main
kouhai dev 2024-02-21 01:25:07 -08:00
parent 57727c5f77
commit 21c56f461b
3 changed files with 39 additions and 15 deletions

View File

@ -89,7 +89,7 @@ module Treehouse
# minimal effort account, check mentions and account-known age # minimal effort account, check mentions and account-known age
has_mention_spam = status.mentions.size >= Rails.configuration.x.th_automod.mention_spam_threshold has_mention_spam = status.mentions.size >= Rails.configuration.x.th_automod.mention_spam_threshold
is_new_account = account.created_at > (Time.now - 1.day) is_new_account = account.created_at > (Time.now - Rails.configuration.x.th_automod.min_account_age_threshold)
has_mention_spam && is_new_account has_mention_spam && is_new_account
end end

View File

@ -1159,6 +1159,10 @@ RSpec.describe ActivityPub::Activity::Create do
context 'with automod active' do context 'with automod active' do
subject { described_class.new(json, sender, delivery: true) } subject { described_class.new(json, sender, delivery: true) }
let(:sender) { Fabricate(:account, followers_url: 'http://example.com/followers', domain: 'example.com', uri: 'https://example.com/actor', created_at: created_at) }
let(:created_at) { Time.now }
let(:min_account_age_threshold) { 1.day }
let(:recipient_a) { Fabricate(:account) } let(:recipient_a) { Fabricate(:account) }
let(:recipient_b) { Fabricate(:account) } let(:recipient_b) { Fabricate(:account) }
let(:staff_user) { Fabricate(:moderator_user) } let(:staff_user) { Fabricate(:moderator_user) }
@ -1182,14 +1186,13 @@ RSpec.describe ActivityPub::Activity::Create do
allow(Rails.configuration.x.th_automod).to receive(:automod_account_username).and_return(staff_user.account.username) allow(Rails.configuration.x.th_automod).to receive(:automod_account_username).and_return(staff_user.account.username)
allow(Rails.configuration.x.th_automod).to receive(:mention_spam_heuristic_auto_limit_active).and_return(true) allow(Rails.configuration.x.th_automod).to receive(:mention_spam_heuristic_auto_limit_active).and_return(true)
allow(Rails.configuration.x.th_automod).to receive(:mention_spam_threshold).and_return(2) allow(Rails.configuration.x.th_automod).to receive(:mention_spam_threshold).and_return(2)
allow(Rails.configuration.x.th_automod).to receive(:min_account_age_threshold).and_return(min_account_age_threshold)
allow(subject).to receive(:distribute) allow(subject).to receive(:distribute)
allow(sender).to receive(:silence!).and_call_original allow(sender).to receive(:silence!).and_call_original
subject.perform subject.perform
end end
context 'and spammy message' do shared_examples 'automod activates' do
let(:recipients) { [recipient_a, recipient_b] }
it 'silences the sender' do it 'silences the sender' do
expect(sender).to have_received(:silence!) expect(sender).to have_received(:silence!)
expect(sender.silenced?).to be_truthy expect(sender.silenced?).to be_truthy
@ -1204,9 +1207,7 @@ RSpec.describe ActivityPub::Activity::Create do
end end
end end
context 'and hammy message' do shared_examples 'automod does not activate' do
let(:recipients) { [recipient_a] }
it 'does not silence the sender' do it 'does not silence the sender' do
expect(sender.silenced?).to be_falsy expect(sender.silenced?).to be_falsy
end end
@ -1215,6 +1216,28 @@ RSpec.describe ActivityPub::Activity::Create do
expect(sender.reports.empty?).to be_truthy expect(sender.reports.empty?).to be_truthy
end end
end end
context 'and spammy message' do
let(:recipients) { [recipient_a, recipient_b] }
context 'and old account' do
let(:created_at) { Time.now - min_account_age_threshold - 1.hour }
include_examples 'automod does not activate'
end
context 'and new account' do
let(:created_at) { Time.now - min_account_age_threshold + 1.hour }
include_examples 'automod activates'
end
end
context 'and hammy message' do
let(:recipients) { [recipient_a] }
include_examples 'automod does not activate'
end
end end
end end
end end

View File

@ -206,7 +206,8 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do
let(:account_display_name) { 'evil display name' } let(:account_display_name) { 'evil display name' }
let(:account_payload_suspended) { false } let(:account_payload_suspended) { false }
let(:automod_account_username) { nil } let(:staff_user) { Fabricate(:moderator_user) }
let(:automod_account_username) { staff_user.account.username }
let(:payload) do let(:payload) do
{ {
@ -236,15 +237,15 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do
context 'new account' do context 'new account' do
context 'heuristic matching' do context 'heuristic matching' do
it 'suspends the user locally' do it 'suspends the user locally' do
expect(subject.suspended?).to be true expect(subject.suspended?).to be_truthy
expect(subject.suspension_origin_local?).to be true expect(subject.suspension_origin_local?).to be_truthy
end end
end end
context 'heuristic not matching' do context 'heuristic not matching' do
let(:account_display_name) { '' } let(:account_display_name) { '' }
it 'does nothing' do it 'does nothing' do
expect(subject.suspended?).to be false expect(subject.suspended?).to be_falsy
end end
end end
end end
@ -258,8 +259,8 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do
context 'heuristic matching' do context 'heuristic matching' do
it 'suspends the user locally' do it 'suspends the user locally' do
expect(subject.suspended?).to be true expect(subject.suspended?).to be_truthy
expect(subject.suspension_origin_local?).to be true expect(subject.suspension_origin_local?).to be_truthy
end end
end end
@ -267,7 +268,7 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do
let(:account_display_name) { 'not evil display name' } let(:account_display_name) { 'not evil display name' }
it 'does nothing' do it 'does nothing' do
expect(subject.suspended?).to be false expect(subject.suspended?).to be_falsy
end end
context 'suspended locally' do context 'suspended locally' do
@ -276,7 +277,7 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do
end end
it 'does nothing' do it 'does nothing' do
expect(subject.suspended?).to be true expect(subject.suspended?).to be_truthy
end end
end end
end end