From 21c56f461b6d1391919c9ac06532bb7e206539b7 Mon Sep 17 00:00:00 2001 From: Kouhai Date: Wed, 21 Feb 2024 01:25:07 -0800 Subject: [PATCH] th: automod v2.2 --- lib/treehouse/automod.rb | 2 +- spec/lib/activitypub/activity/create_spec.rb | 35 +++++++++++++++---- .../process_account_service_spec.rb | 17 ++++----- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/treehouse/automod.rb b/lib/treehouse/automod.rb index 98c68489a5..f2cbb132ba 100644 --- a/lib/treehouse/automod.rb +++ b/lib/treehouse/automod.rb @@ -89,7 +89,7 @@ module Treehouse # minimal effort account, check mentions and account-known age 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 end diff --git a/spec/lib/activitypub/activity/create_spec.rb b/spec/lib/activitypub/activity/create_spec.rb index c48ee9dd72..ff897b6cf6 100644 --- a/spec/lib/activitypub/activity/create_spec.rb +++ b/spec/lib/activitypub/activity/create_spec.rb @@ -1159,6 +1159,10 @@ RSpec.describe ActivityPub::Activity::Create do context 'with automod active' do 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_b) { Fabricate(:account) } 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(: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(:min_account_age_threshold).and_return(min_account_age_threshold) allow(subject).to receive(:distribute) allow(sender).to receive(:silence!).and_call_original subject.perform end - context 'and spammy message' do - let(:recipients) { [recipient_a, recipient_b] } - + shared_examples 'automod activates' do it 'silences the sender' do expect(sender).to have_received(:silence!) expect(sender.silenced?).to be_truthy @@ -1204,9 +1207,7 @@ RSpec.describe ActivityPub::Activity::Create do end end - context 'and hammy message' do - let(:recipients) { [recipient_a] } - + shared_examples 'automod does not activate' do it 'does not silence the sender' do expect(sender.silenced?).to be_falsy end @@ -1215,6 +1216,28 @@ RSpec.describe ActivityPub::Activity::Create do expect(sender.reports.empty?).to be_truthy 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 diff --git a/spec/services/activitypub/process_account_service_spec.rb b/spec/services/activitypub/process_account_service_spec.rb index 638a53df1a..1c6b737b13 100644 --- a/spec/services/activitypub/process_account_service_spec.rb +++ b/spec/services/activitypub/process_account_service_spec.rb @@ -206,7 +206,8 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do let(:account_display_name) { 'evil display name' } 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 { @@ -236,15 +237,15 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do context 'new account' do context 'heuristic matching' do it 'suspends the user locally' do - expect(subject.suspended?).to be true - expect(subject.suspension_origin_local?).to be true + expect(subject.suspended?).to be_truthy + expect(subject.suspension_origin_local?).to be_truthy end end context 'heuristic not matching' do let(:account_display_name) { '' } it 'does nothing' do - expect(subject.suspended?).to be false + expect(subject.suspended?).to be_falsy end end end @@ -258,8 +259,8 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do context 'heuristic matching' do it 'suspends the user locally' do - expect(subject.suspended?).to be true - expect(subject.suspension_origin_local?).to be true + expect(subject.suspended?).to be_truthy + expect(subject.suspension_origin_local?).to be_truthy end end @@ -267,7 +268,7 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do let(:account_display_name) { 'not evil display name' } it 'does nothing' do - expect(subject.suspended?).to be false + expect(subject.suspended?).to be_falsy end context 'suspended locally' do @@ -276,7 +277,7 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do end it 'does nothing' do - expect(subject.suspended?).to be true + expect(subject.suspended?).to be_truthy end end end