Add: #600 NGルール (#602)

* Wip

* Wip

* Wip: History

* Wip: テストコード作成

* Fix test

* Wip

* Wip

* Wip

* Fix test

* Wip

* Wip

* Wip

* Wip

* なんとか完成、これから動作確認

* spell miss

* Change ng rule timings

* Fix test

* Wip

* Fix test

* Wip

* Fix form

* 表示まわりの改善
This commit is contained in:
KMY(雪あすか) 2024-02-26 17:45:41 +09:00 committed by GitHub
parent 0779c748a6
commit 7d96d5828e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
56 changed files with 2062 additions and 42 deletions

View file

@ -681,5 +681,31 @@ RSpec.describe ActivityPub::ProcessStatusUpdateService, type: :service do
end
end
end
context 'when ng rule is existing' do
context 'when ng rule is match' do
before do
Fabricate(:ng_rule, account_domain: 'example.com', status_text: 'universe')
subject.call(status, json, json)
end
it 'does not update text' do
expect(status.reload.text).to eq 'Hello world'
expect(status.edits.reload.map(&:text)).to eq []
end
end
context 'when ng rule is not match' do
before do
Fabricate(:ng_rule, account_domain: 'foo.bar', status_text: 'universe')
subject.call(status, json, json)
end
it 'updates text' do
expect(status.reload.text).to eq 'Hello universe'
expect(status.edits.reload.map(&:text)).to eq ['Hello world', 'Hello universe']
end
end
end
end
end

View file

@ -47,6 +47,8 @@ RSpec.describe DeleteAccountService, type: :service do
let!(:account_note) { Fabricate(:account_note, account: account) }
let!(:ng_rule_history) { Fabricate(:ng_rule_history, account: account) }
it 'deletes associated owned and target records and target notifications' do
subject
@ -68,6 +70,7 @@ RSpec.describe DeleteAccountService, type: :service do
expect { bookmark_category_status.status.reload }.to_not raise_error
expect { antenna_account.account.reload }.to_not raise_error
expect { circle_account.account.reload }.to_not raise_error
expect { ng_rule_history.reload }.to_not raise_error
expect { list.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { list_account.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { antenna_account.reload }.to raise_error(ActiveRecord::RecordNotFound)

View file

@ -113,6 +113,33 @@ RSpec.describe EmojiReactService, type: :service do
end
end
context 'with ng rule' do
let(:name) { 'ohagi' }
context 'when rule hits' do
before do
Fabricate(:custom_emoji, shortcode: 'ohagi')
Fabricate(:ng_rule, reaction_type: ['emoji_reaction'])
end
it 'react with emoji' do
expect { subject }.to raise_error Mastodon::ValidationError
end
end
context 'when rule does not hit' do
before do
Fabricate(:custom_emoji, shortcode: 'ohagi')
Fabricate(:ng_rule, reaction_type: ['emoji_reaction'], emoji_reaction_name: 'aaa')
end
it 'react with emoji' do
expect { subject }.to_not raise_error
expect(subject.count).to eq 1
end
end
end
context 'with custom emoji of remote' do
let(:name) { 'ohagi@foo.bar' }
let!(:custom_emoji) { Fabricate(:custom_emoji, shortcode: 'ohagi', domain: 'foo.bar', uri: 'https://foo.bar/emoji/ohagi') }

View file

@ -37,4 +37,31 @@ RSpec.describe FavouriteService, type: :service do
expect(a_request(:post, 'http://example.com/inbox')).to have_been_made.once
end
end
context 'with ng rule' do
let(:status) { Fabricate(:status) }
let(:sender) { Fabricate(:account) }
context 'when rule matches' do
before do
Fabricate(:ng_rule, reaction_type: ['favourite'])
end
it 'does not favourite' do
expect { subject.call(sender, status) }.to raise_error Mastodon::ValidationError
expect(sender.favourited?(status)).to be false
end
end
context 'when rule does not match' do
before do
Fabricate(:ng_rule, account_display_name: 'else', reaction_type: ['favourite'])
end
it 'favourites' do
expect { subject.call(sender, status) }.to_not raise_error
expect(sender.favourited?(status)).to be true
end
end
end
end

View file

@ -154,4 +154,30 @@ RSpec.describe FollowService, type: :service do
expect(a_request(:post, 'http://example.com/inbox')).to have_been_made.once
end
end
context 'with ng rule' do
let(:bob) { Fabricate(:account) }
context 'when rule matches' do
before do
Fabricate(:ng_rule, reaction_type: ['follow'])
end
it 'does not favourite' do
expect { subject.call(sender, bob) }.to raise_error Mastodon::ValidationError
expect(sender.following?(bob)).to be false
end
end
context 'when rule does not match' do
before do
Fabricate(:ng_rule, account_display_name: 'else', reaction_type: ['follow'])
end
it 'favourites' do
expect { subject.call(sender, bob) }.to_not raise_error
expect(sender.following?(bob)).to be true
end
end
end
end

View file

@ -820,6 +820,27 @@ RSpec.describe PostStatusService, type: :service do
end
end
describe 'ng rule is set' do
it 'creates a new status when no rule matches' do
Fabricate(:ng_rule, account_username: 'ohagi', status_allow_follower_mention: false)
account = Fabricate(:account)
text = 'test status update'
status = subject.call(account, text: text)
expect(status).to be_persisted
expect(status.text).to eq text
end
it 'does not create a new status when a rule matches' do
Fabricate(:ng_rule, status_text: 'test', status_allow_follower_mention: false)
account = Fabricate(:account)
text = 'test status update'
expect { subject.call(account, text: text) }.to raise_error Mastodon::ValidationError
end
end
def create_status_with_options(**options)
subject.call(Fabricate(:account), options.merge(text: 'test'))
end

View file

@ -68,6 +68,35 @@ RSpec.describe ReblogService, type: :service do
end
end
context 'with ng rule' do
subject { described_class.new }
let(:status) { Fabricate(:status, account: alice, visibility: :public) }
let(:account) { Fabricate(:account) }
context 'when rule matches' do
before do
Fabricate(:ng_rule, reaction_type: ['reblog'])
end
it 'does not reblog' do
expect { subject.call(account, status) }.to raise_error Mastodon::ValidationError
expect(account.reblogged?(status)).to be false
end
end
context 'when rule does not match' do
before do
Fabricate(:ng_rule, account_display_name: 'else', reaction_type: ['reblog'])
end
it 'reblogs' do
expect { subject.call(account, status) }.to_not raise_error
expect(account.reblogged?(status)).to be true
end
end
end
context 'when the reblogged status is discarded in the meantime' do
let(:status) { Fabricate(:status, account: alice, visibility: :public, text: 'discard-status-text') }

View file

@ -401,4 +401,32 @@ RSpec.describe UpdateStatusService, type: :service do
expect(status.text).to_not eq text
end
end
describe 'ng rule is set' do
let(:status) { Fabricate(:status, text: 'Foo') }
context 'when rule hits' do
before do
Fabricate(:ng_rule, status_text: 'Bar', status_allow_follower_mention: false)
end
it 'does not update text' do
expect { subject.call(status, status.account_id, text: 'Bar') }.to raise_error Mastodon::ValidationError
expect(status.reload.text).to_not eq 'Bar'
expect(status.edits.pluck(:text)).to eq %w()
end
end
context 'when rule does not hit' do
before do
Fabricate(:ng_rule, status_text: 'aar', status_allow_follower_mention: false)
end
it 'does not update text' do
expect { subject.call(status, status.account_id, text: 'Bar') }.to_not raise_error
expect(status.reload.text).to eq 'Bar'
expect(status.edits.pluck(:text)).to eq %w(Foo Bar)
end
end
end
end