1
0
Fork 0
forked from gitea/nas

Revamp notification policy options (#31343)

This commit is contained in:
Claire 2024-08-09 15:30:55 +02:00 committed by GitHub
parent e29c401f77
commit cbdd8edf68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 442 additions and 109 deletions

View file

@ -51,7 +51,7 @@ RSpec.describe 'Policies' do
it 'changes notification policy and returns an updated json object', :aggregate_failures do
expect { subject }
.to change { NotificationPolicy.find_or_initialize_by(account: user.account).filter_not_following }.from(false).to(true)
.to change { NotificationPolicy.find_or_initialize_by(account: user.account).for_not_following.to_sym }.from(:accept).to(:filter)
expect(response).to have_http_status(200)
expect(body_as_json).to include(

View file

@ -0,0 +1,72 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Policies' do
let(:user) { Fabricate(:user, account_attributes: { username: 'alice' }) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:scopes) { 'read:notifications write:notifications' }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v2/notifications/policy', :inline_jobs do
subject do
get '/api/v2/notifications/policy', headers: headers, params: params
end
let(:params) { {} }
before do
Fabricate(:notification_request, account: user.account)
end
it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
context 'with no options' do
it 'returns json with expected attributes', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(body_as_json).to include(
for_not_following: 'accept',
for_not_followers: 'accept',
for_new_accounts: 'accept',
for_private_mentions: 'filter',
for_limited_accounts: 'filter',
summary: a_hash_including(
pending_requests_count: 1,
pending_notifications_count: 0
)
)
end
end
end
describe 'PUT /api/v2/notifications/policy' do
subject do
put '/api/v2/notifications/policy', headers: headers, params: params
end
let(:params) { { for_not_following: 'filter', for_limited_accounts: 'drop' } }
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
it 'changes notification policy and returns an updated json object', :aggregate_failures do
expect { subject }
.to change { NotificationPolicy.find_or_initialize_by(account: user.account).for_not_following.to_sym }.from(:accept).to(:filter)
.and change { NotificationPolicy.find_or_initialize_by(account: user.account).for_limited_accounts.to_sym }.from(:filter).to(:drop)
expect(response).to have_http_status(200)
expect(body_as_json).to include(
for_not_following: 'filter',
for_not_followers: 'accept',
for_new_accounts: 'accept',
for_private_mentions: 'filter',
for_limited_accounts: 'drop',
summary: a_hash_including(
pending_requests_count: 0,
pending_notifications_count: 0
)
)
end
end
end

View file

@ -196,20 +196,58 @@ RSpec.describe NotifyService do
end
end
describe NotifyService::DismissCondition do
describe NotifyService::DropCondition do
subject { described_class.new(notification) }
let(:activity) { Fabricate(:mention, status: Fabricate(:status)) }
let(:notification) { Fabricate(:notification, type: :mention, activity: activity, from_account: activity.status.account, account: activity.account) }
describe '#dismiss?' do
context 'when sender is silenced' do
describe '#drop' do
context 'when sender is silenced and recipient has a default policy' do
before do
notification.from_account.silence!
end
it 'returns false' do
expect(subject.dismiss?).to be false
expect(subject.drop?).to be false
end
end
context 'when sender is silenced and recipient has a policy to ignore silenced accounts' do
before do
notification.from_account.silence!
notification.account.create_notification_policy!(for_limited_accounts: :drop)
end
it 'returns true' do
expect(subject.drop?).to be true
end
end
context 'when sender is new and recipient has a default policy' do
it 'returns false' do
expect(subject.drop?).to be false
end
end
context 'when sender is new and recipient has a policy to ignore silenced accounts' do
before do
notification.account.create_notification_policy!(for_new_accounts: :drop)
end
it 'returns true' do
expect(subject.drop?).to be true
end
end
context 'when sender is new and followed and recipient has a policy to ignore silenced accounts' do
before do
notification.account.create_notification_policy!(for_new_accounts: :drop)
notification.account.follow!(notification.from_account)
end
it 'returns false' do
expect(subject.drop?).to be false
end
end
@ -219,7 +257,7 @@ RSpec.describe NotifyService do
end
it 'returns true' do
expect(subject.dismiss?).to be true
expect(subject.drop?).to be true
end
end
end
@ -250,6 +288,16 @@ RSpec.describe NotifyService do
expect(subject.filter?).to be false
end
end
context 'when recipient is allowing limited accounts' do
before do
notification.account.create_notification_policy!(for_limited_accounts: :accept)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
end
context 'when recipient is filtering not-followed senders' do