0
0
Fork 0

Add notification policies and notification requests (#29366)

This commit is contained in:
Eugen Rochko 2024-03-07 15:53:37 +01:00 committed by GitHub
parent 653ce43abe
commit 50b17f7e10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
104 changed files with 1096 additions and 247 deletions

View file

@ -24,14 +24,13 @@ describe Settings::Preferences::NotificationsController do
describe 'PUT #update' do
it 'updates notifications settings' do
user.settings.update('notification_emails.follow': false, 'interactions.must_be_follower': true)
user.settings.update('notification_emails.follow': false)
user.save
put :update, params: {
user: {
settings_attributes: {
'notification_emails.follow': '1',
'interactions.must_be_follower': '0',
},
},
}
@ -39,7 +38,6 @@ describe Settings::Preferences::NotificationsController do
expect(response).to redirect_to(settings_preferences_notifications_path)
user.reload
expect(user.settings['notification_emails.follow']).to be true
expect(user.settings['interactions.must_be_follower']).to be false
end
end
end

View file

@ -0,0 +1,6 @@
# frozen_string_literal: true
Fabricator(:notification_permission) do
account
from_account { Fabricate.build(:account) }
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
Fabricator(:notification_policy) do
account
filter_not_following false
filter_not_followers false
filter_new_accounts false
filter_private_mentions true
end

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
Fabricator(:notification_request) do
account
from_account { Fabricate.build(:account) }
last_status { Fabricate.build(:status) }
dismissed false
end

View file

@ -0,0 +1,25 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe NotificationPolicy do
describe '#summarize!' do
subject { Fabricate(:notification_policy) }
let(:sender) { Fabricate(:account) }
before do
Fabricate.times(2, :notification, account: subject.account, activity: Fabricate(:status, account: sender))
Fabricate(:notification_request, account: subject.account, from_account: sender)
subject.summarize!
end
it 'sets pending_requests_count' do
expect(subject.pending_requests_count).to eq 1
end
it 'sets pending_notifications_count' do
expect(subject.pending_notifications_count).to eq 2
end
end
end

View file

@ -0,0 +1,44 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe NotificationRequest do
describe '#reconsider_existence!' do
subject { Fabricate(:notification_request, dismissed: dismissed) }
let(:dismissed) { false }
context 'when there are remaining notifications' do
before do
Fabricate(:notification, account: subject.account, activity: Fabricate(:status, account: subject.from_account))
subject.reconsider_existence!
end
it 'leaves request intact' do
expect(subject.destroyed?).to be false
end
it 'updates notifications_count' do
expect(subject.notifications_count).to eq 1
end
end
context 'when there are no notifications' do
before do
subject.reconsider_existence!
end
context 'when dismissed' do
let(:dismissed) { true }
it 'leaves request intact' do
expect(subject.destroyed?).to be false
end
end
it 'removes the request' do
expect(subject.destroyed?).to be true
end
end
end
end

View file

@ -12,6 +12,7 @@ RSpec.describe 'API V1 Conversations' do
describe 'GET /api/v1/conversations', :sidekiq_inline do
before do
user.account.follow!(other.account)
PostStatusService.new.call(other.account, text: 'Hey @alice', visibility: 'direct')
PostStatusService.new.call(user.account, text: 'Hey, nobody here', visibility: 'direct')
end

View file

@ -0,0 +1,48 @@
# 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/v1/notifications/policy', :sidekiq_inline do
subject do
get '/api/v1/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 http success', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
end
end
describe 'PUT /api/v1/notifications/policy' do
subject do
put '/api/v1/notifications/policy', headers: headers, params: params
end
let(:params) { {} }
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
end
end

View file

@ -0,0 +1,107 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Requests' 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/v1/notifications/requests', :sidekiq_inline do
subject do
get '/api/v1/notifications/requests', headers: headers, params: params
end
let(:params) { {} }
before do
Fabricate(:notification_request, account: user.account)
Fabricate(:notification_request, account: user.account, dismissed: true)
end
it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
context 'with no options' do
it 'returns http success', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
end
context 'with dismissed' do
let(:params) { { dismissed: '1' } }
it 'returns http success', :aggregate_failures do
subject
expect(response).to have_http_status(200)
end
end
end
describe 'POST /api/v1/notifications/requests/:id/accept' do
subject do
post "/api/v1/notifications/requests/#{notification_request.id}/accept", headers: headers
end
let(:notification_request) { Fabricate(:notification_request, account: user.account) }
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'creates notification permission' do
subject
expect(NotificationPermission.find_by(account: notification_request.account, from_account: notification_request.from_account)).to_not be_nil
end
context 'when notification request belongs to someone else' do
let(:notification_request) { Fabricate(:notification_request) }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v1/notifications/requests/:id/dismiss' do
subject do
post "/api/v1/notifications/requests/#{notification_request.id}/dismiss", headers: headers
end
let(:notification_request) { Fabricate(:notification_request, account: user.account) }
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
it 'dismisses the notification request' do
subject
expect(notification_request.reload.dismissed?).to be true
end
context 'when notification request belongs to someone else' do
let(:notification_request) { Fabricate(:notification_request) }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
end
end

View file

@ -41,7 +41,8 @@ RSpec.describe NotifyService, type: :service do
it 'does not notify when sender is silenced and not followed' do
sender.silence!
expect { subject }.to_not change(Notification, :count)
subject
expect(Notification.find_by(activity: activity).filtered?).to be true
end
it 'does not notify when recipient is suspended' do
@ -49,66 +50,6 @@ RSpec.describe NotifyService, type: :service do
expect { subject }.to_not change(Notification, :count)
end
context 'with direct messages' do
let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct)) }
let(:type) { :mention }
before do
user.settings.update('interactions.must_be_following_dm': enabled)
user.save
end
context 'when recipient is supposed to be following sender' do
let(:enabled) { true }
it 'does not notify' do
expect { subject }.to_not change(Notification, :count)
end
context 'when the message chain is initiated by recipient, but is not direct message' do
let(:reply_to) { Fabricate(:status, account: recipient) }
let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) }
before { Fabricate(:mention, account: sender, status: reply_to) }
it 'does not notify' do
expect { subject }.to_not change(Notification, :count)
end
end
context 'when the message chain is initiated by recipient, but without a mention to the sender, even if the sender sends multiple messages in a row' do
let(:reply_to) { Fabricate(:status, account: recipient) }
let(:dummy_reply) { Fabricate(:status, account: sender, visibility: :direct, thread: reply_to) }
let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: dummy_reply)) }
before { Fabricate(:mention, account: sender, status: reply_to) }
it 'does not notify' do
expect { subject }.to_not change(Notification, :count)
end
end
context 'when the message chain is initiated by the recipient with a mention to the sender' do
let(:reply_to) { Fabricate(:status, account: recipient, visibility: :direct) }
let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) }
before { Fabricate(:mention, account: sender, status: reply_to) }
it 'does notify' do
expect { subject }.to change(Notification, :count)
end
end
end
context 'when recipient is NOT supposed to be following sender' do
let(:enabled) { false }
it 'does notify' do
expect { subject }.to change(Notification, :count)
end
end
end
describe 'reblogs' do
let(:status) { Fabricate(:status, account: Fabricate(:account)) }
let(:activity) { Fabricate(:status, account: sender, reblog: status) }
@ -187,4 +128,189 @@ RSpec.describe NotifyService, type: :service do
end
end
end
describe NotifyService::FilterCondition 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 '#filter?' do
context 'when sender is silenced' do
before do
notification.from_account.silence!
end
it 'returns true' do
expect(subject.filter?).to be true
end
context 'when recipient follows sender' do
before do
notification.account.follow!(notification.from_account)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
end
context 'when recipient is filtering not-followed senders' do
before do
Fabricate(:notification_policy, account: notification.account, filter_not_following: true)
end
it 'returns true' do
expect(subject.filter?).to be true
end
context 'when sender has permission' do
before do
Fabricate(:notification_permission, account: notification.account, from_account: notification.from_account)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
context 'when sender is followed by recipient' do
before do
notification.account.follow!(notification.from_account)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
end
context 'when recipient is filtering not-followers' do
before do
Fabricate(:notification_policy, account: notification.account, filter_not_followers: true)
end
it 'returns true' do
expect(subject.filter?).to be true
end
context 'when sender has permission' do
before do
Fabricate(:notification_permission, account: notification.account, from_account: notification.from_account)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
context 'when sender follows recipient' do
before do
notification.from_account.follow!(notification.account)
end
it 'returns true' do
expect(subject.filter?).to be true
end
end
context 'when sender follows recipient for longer than 3 days' do
before do
follow = notification.from_account.follow!(notification.account)
follow.update(created_at: 4.days.ago)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
end
context 'when recipient is filtering new accounts' do
before do
Fabricate(:notification_policy, account: notification.account, filter_new_accounts: true)
end
it 'returns true' do
expect(subject.filter?).to be true
end
context 'when sender has permission' do
before do
Fabricate(:notification_permission, account: notification.account, from_account: notification.from_account)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
context 'when sender is older than 30 days' do
before do
notification.from_account.update(created_at: 31.days.ago)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
end
context 'when recipient is not filtering anyone' do
before do
Fabricate(:notification_policy, account: notification.account)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
context 'when recipient is filtering unsolicited private mentions' do
before do
Fabricate(:notification_policy, account: notification.account, filter_private_mentions: true)
end
context 'when notification is not a private mention' do
it 'returns false' do
expect(subject.filter?).to be false
end
end
context 'when notification is a private mention' do
before do
notification.target_status.update(visibility: :direct)
end
it 'returns true' do
expect(subject.filter?).to be true
end
context 'when the message chain is initiated by recipient, but sender is not mentioned' do
before do
original_status = Fabricate(:status, account: notification.account, visibility: :direct)
notification.target_status.update(thread: original_status)
end
it 'returns true' do
expect(subject.filter?).to be true
end
end
context 'when the message chain is initiated by recipient, and sender is mentioned' do
before do
original_status = Fabricate(:status, account: notification.account, visibility: :direct)
notification.target_status.update(thread: original_status)
Fabricate(:mention, status: original_status, account: notification.from_account)
end
it 'returns false' do
expect(subject.filter?).to be false
end
end
end
end
end
end
end