Add: フレンドサーバー (#61)

* Fix mastodon version

* テーブル作成

* Wip: フレンドサーバーフォローの承認を受信

* Wip: フレンド申請拒否を受信

* Wip: フォローリクエストを受理

* Wip: 相手からのフォロー・アンフォローを受理

* 普通のフォローとフレンドサーバーのフォローを区別するテストを追加

* ドメインブロックによるフォロー拒否

* ドメインブロックしたあと、申請中のフォロリクを取り下げる処理

* スタブに条件を追加

* Wip: 相手からのDelete信号に対応

* DB定義が消えていたので修正

* Wip: ローカル公開投稿をフレンドに送信する処理など

* Wip: 未収載+誰でもの投稿をフレンドに送る設定

* Wip: ローカル公開をそのまま送信する設定を考慮

* Fix test

* Wip: 他サーバーからのローカル公開投稿の受け入れ

* Wip: Web画面作成

* Fix test

* Wip: ローカル公開を連合TLに流す

* Wip: フレンドサーバーの削除ボタン

* Wip: メール通知や設定のテストなど

* Wip: 翻訳を作成

* Fix: 却下されたあとフォローボタンが表示されない問題

* Wip: 編集できない問題

* 有効にしていないフレンドサーバーをリストで無効表示
This commit is contained in:
KMY(雪あすか) 2023-10-09 11:51:15 +09:00 committed by GitHub
parent acb29e5b11
commit 87e858a202
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
66 changed files with 1638 additions and 51 deletions

View file

@ -43,6 +43,35 @@ RSpec.describe ActivityPub::Activity::Accept do
end
end
context 'when sender is from friend server' do
subject { described_class.new(json, sender) }
let(:sender) { Fabricate(:account, domain: 'abc.com', url: 'https://abc.com/#actor') }
let!(:friend) { Fabricate(:friend_domain, domain: 'abc.com', active_state: :pending, active_follow_activity_id: 'https://abc-123/456') }
before do
allow(RemoteAccountRefreshWorker).to receive(:perform_async)
Fabricate(:follow_request, account: recipient, target_account: sender)
subject.perform
end
it 'creates a follow relationship' do
expect(recipient.following?(sender)).to be true
end
it 'removes the follow request' do
expect(recipient.requested?(sender)).to be false
end
it 'queues a refresh' do
expect(RemoteAccountRefreshWorker).to have_received(:perform_async).with(sender.id)
end
it 'friend server is not changed' do
expect(friend.reload.i_am_pending?).to be true
end
end
context 'when given a relay' do
subject { described_class.new(json, sender) }
@ -68,4 +97,26 @@ RSpec.describe ActivityPub::Activity::Accept do
expect(relay.reload.accepted?).to be true
end
end
context 'when given a friend server' do
subject { described_class.new(json, sender) }
let(:sender) { Fabricate(:account, domain: 'abc.com', url: 'https://abc.com/#actor') }
let!(:friend) { Fabricate(:friend_domain, domain: 'abc.com', active_state: :pending, active_follow_activity_id: 'https://abc-123/456') }
let(:json) do
{
'@context': 'https://www.w3.org/ns/activitystreams',
id: 'foo',
type: 'Accept',
actor: ActivityPub::TagManager.instance.uri_for(sender),
object: 'https://abc-123/456',
}.with_indifferent_access
end
it 'marks the friend as accepted' do
subject.perform
expect(friend.reload.i_am_accepted?).to be true
end
end
end

View file

@ -234,6 +234,25 @@ RSpec.describe ActivityPub::Activity::Create do
end
end
context 'when public_unlisted with LocalPublic' do
let(:object_json) do
{
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
type: 'Note',
content: 'Lorem ipsum',
to: ['http://example.com/followers', 'LocalPublic'],
cc: 'https://www.w3.org/ns/activitystreams#Public',
}
end
it 'creates status' do
status = sender.statuses.first
expect(status).to_not be_nil
expect(status.visibility).to eq 'public_unlisted'
end
end
context 'when private' do
let(:object_json) do
{
@ -411,6 +430,17 @@ RSpec.describe ActivityPub::Activity::Create do
end
end
context 'with public_unlisted with LocalPublic' do
let(:searchable_by) { ['http://example.com/followers', 'LocalPublic'] }
it 'create status' do
status = sender.statuses.first
expect(status).to_not be_nil
expect(status.searchability).to eq 'public_unlisted'
end
end
context 'with private' do
let(:searchable_by) { 'http://example.com/followers' }
@ -1462,6 +1492,30 @@ RSpec.describe ActivityPub::Activity::Create do
end
end
context 'when sender is in friend server' do
subject { described_class.new(json, sender, delivery: true) }
before do
Fabricate(:friend_domain, domain: sender.domain, active_state: :accepted, passive_state: :accepted)
subject.perform
end
let(:object_json) do
{
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
type: 'Note',
content: 'Lorem ipsum',
}
end
it 'creates status' do
status = sender.statuses.first
expect(status).to_not be_nil
expect(status.text).to eq 'Lorem ipsum'
end
end
context 'when the sender has no relevance to local activity' do
subject { described_class.new(json, sender, delivery: true) }

View file

@ -73,4 +73,30 @@ RSpec.describe ActivityPub::Activity::Delete do
end
end
end
context 'when given a friend server' do
subject { described_class.new(json, sender) }
before do
Fabricate(:friend_domain, domain: 'abc.com', inbox_url: 'https://abc.com/inbox', passive_state: :accepted)
stub_request(:post, 'https://abc.com/inbox')
end
let(:sender) { Fabricate(:account, domain: 'abc.com', url: 'https://abc.com/#actor') }
let(:json) do
{
'@context': 'https://www.w3.org/ns/activitystreams',
id: 'foo',
type: 'Delete',
actor: ActivityPub::TagManager.instance.uri_for(sender),
object: 'https://www.w3.org/ns/activitystreams#Public',
}.with_indifferent_access
end
it 'marks the friend as deleted' do
subject.perform
expect(FriendDomain.find_by(domain: 'abc.com')).to be_nil
end
end
end

View file

@ -37,6 +37,23 @@ RSpec.describe ActivityPub::Activity::Follow do
end
end
context 'with an unlocked account from friend server' do
let!(:friend) { Fabricate(:friend_domain, domain: sender.domain, passive_state: :idle) }
before do
subject.perform
end
it 'creates a follow from sender to recipient' do
expect(sender.following?(recipient)).to be true
expect(sender.active_relationships.find_by(target_account: recipient).uri).to eq 'foo'
end
it 'does not change friend server passive status' do
expect(friend.they_are_idle?).to be true
end
end
context 'when silenced account following an unlocked account' do
before do
sender.touch(:silenced_at)
@ -285,4 +302,148 @@ RSpec.describe ActivityPub::Activity::Follow do
end
end
end
context 'when given a friend server' do
subject { described_class.new(json, sender) }
let(:sender) { Fabricate(:account, domain: 'abc.com', url: 'https://abc.com/#actor') }
let!(:friend) { Fabricate(:friend_domain, domain: 'abc.com', passive_state: :idle) }
let!(:owner_user) { Fabricate(:user, role: UserRole.find_by(name: 'Owner')) }
let!(:patch_user) { Fabricate(:user, role: Fabricate(:user_role, name: 'OhagiOps', permissions: UserRole::FLAGS[:manage_federation])) }
let(:json) do
{
'@context': 'https://www.w3.org/ns/activitystreams',
id: 'foo',
type: 'Follow',
actor: ActivityPub::TagManager.instance.uri_for(sender),
object: 'https://www.w3.org/ns/activitystreams#Public',
}.with_indifferent_access
end
it 'marks the friend as pending' do
subject.perform
expect(friend.reload.they_are_pending?).to be true
expect(friend.passive_follow_activity_id).to eq 'foo'
end
context 'when no record' do
before do
friend.update(domain: 'def.com')
end
it 'marks the friend as pending' do
subject.perform
friend = FriendDomain.find_by(domain: 'abc.com')
expect(friend).to_not be_nil
expect(friend.they_are_pending?).to be true
expect(friend.passive_follow_activity_id).to eq 'foo'
end
end
context 'with sending email' do
around do |example|
queue_adapter = ActiveJob::Base.queue_adapter
ActiveJob::Base.queue_adapter = :test
example.run
ActiveJob::Base.queue_adapter = queue_adapter
end
it 'perform' do
expect { subject.perform }.to have_enqueued_mail(AdminMailer, :new_pending_friend_server)
.with(hash_including(params: { recipient: owner_user.account })).once
.and(have_enqueued_mail(AdminMailer, :new_pending_friend_server).with(hash_including(params: { recipient: patch_user.account })).once)
.and(have_enqueued_mail.at_most(2))
end
end
context 'when after rejected' do
before do
friend.update(passive_state: :rejected)
end
it 'marks the friend as pending' do
subject.perform
expect(friend.reload.they_are_pending?).to be true
expect(friend.passive_follow_activity_id).to eq 'foo'
end
end
context 'when unlocked' do
before do
friend.update(unlocked: true)
stub_request(:post, 'https://example.com/inbox')
end
it 'marks the friend as accepted' do
subject.perform
friend = FriendDomain.find_by(domain: 'abc.com')
expect(friend).to_not be_nil
expect(friend.they_are_accepted?).to be true
expect(a_request(:post, 'https://example.com/inbox').with(body: hash_including({
id: 'foo#accepts/friends',
type: 'Accept',
object: 'foo',
}))).to have_been_made.once
end
end
context 'when unlocked on admin settings' do
before do
Form::AdminSettings.new(unlocked_friend: '1').save
stub_request(:post, 'https://example.com/inbox')
end
it 'marks the friend as accepted' do
subject.perform
friend = FriendDomain.find_by(domain: 'abc.com')
expect(friend).to_not be_nil
expect(friend.they_are_accepted?).to be true
expect(a_request(:post, 'https://example.com/inbox').with(body: hash_including({
id: 'foo#accepts/friends',
type: 'Accept',
object: 'foo',
}))).to have_been_made.once
end
end
context 'when already accepted' do
before do
friend.update(passive_state: :accepted)
stub_request(:post, 'https://example.com/inbox')
end
it 'marks the friend as accepted' do
subject.perform
friend = FriendDomain.find_by(domain: 'abc.com')
expect(friend).to_not be_nil
expect(friend.they_are_accepted?).to be true
expect(a_request(:post, 'https://example.com/inbox').with(body: hash_including({
id: 'foo#accepts/friends',
type: 'Accept',
object: 'foo',
}))).to have_been_made.once
end
end
context 'when domain blocked' do
before do
friend.update(domain: 'def.com')
end
it 'marks the friend rejected' do
Fabricate(:domain_block, domain: 'abc.com', reject_friend: true)
subject.perform
friend = FriendDomain.find_by(domain: 'abc.com')
expect(friend).to be_nil
end
end
end
end

View file

@ -122,6 +122,30 @@ RSpec.describe ActivityPub::Activity::Reject do
end
end
context 'when sender is from friend server' do
subject { described_class.new(json, sender) }
let(:sender) { Fabricate(:account, domain: 'abc.com', url: 'https://abc.com/#actor') }
let!(:friend) { Fabricate(:friend_domain, domain: 'abc.com', active_state: :pending, active_follow_activity_id: 'https://abc-123/456') }
before do
Fabricate(:follow_request, account: recipient, target_account: sender)
subject.perform
end
it 'does not create a follow relationship' do
expect(recipient.following?(sender)).to be false
end
it 'removes the follow request' do
expect(recipient.requested?(sender)).to be false
end
it 'friend server is not changed' do
expect(friend.reload.i_am_pending?).to be true
end
end
context 'when given a relay' do
subject { described_class.new(json, sender) }
@ -147,4 +171,26 @@ RSpec.describe ActivityPub::Activity::Reject do
expect(relay.reload.rejected?).to be true
end
end
context 'when given a friend' do
subject { described_class.new(json, sender) }
let(:sender) { Fabricate(:account, domain: 'abc.com', url: 'https://abc.com/#actor') }
let!(:friend) { Fabricate(:friend_domain, domain: 'abc.com', active_state: :pending, active_follow_activity_id: 'https://abc-123/456') }
let(:json) do
{
'@context': 'https://www.w3.org/ns/activitystreams',
id: 'foo',
type: 'Reject',
actor: ActivityPub::TagManager.instance.uri_for(sender),
object: 'https://abc-123/456',
}.with_indifferent_access
end
it 'marks the friend as rejected' do
subject.perform
expect(friend.reload.i_am_rejected?).to be true
end
end
end

View file

@ -145,6 +145,13 @@ RSpec.describe ActivityPub::Activity::Undo do
expect(sender.following?(recipient)).to be false
end
it 'deletes follow from sender to recipient when has friend' do
friend = Fabricate(:friend_domain, domain: sender.domain, passive_state: :accepted)
subject.perform
expect(sender.following?(recipient)).to be false
expect(friend.they_are_accepted?).to be true
end
context 'with only object uri' do
let(:object_json) { 'bar' }
@ -153,6 +160,25 @@ RSpec.describe ActivityPub::Activity::Undo do
expect(sender.following?(recipient)).to be false
end
end
context 'when for a friend' do
let(:sender) { Fabricate(:account, domain: 'abc.com', url: 'https://abc.com/#actor') }
let!(:friend) { Fabricate(:friend_domain, domain: 'abc.com', passive_state: :accepted, passive_follow_activity_id: 'bar') }
let(:object_json) do
{
id: 'bar',
type: 'Follow',
actor: ActivityPub::TagManager.instance.uri_for(sender),
object: 'https://www.w3.org/ns/activitystreams#Public',
}
end
it 'deletes follow from this server to friend' do
subject.perform
expect(friend.reload.they_are_idle?).to be true
expect(friend.passive_follow_activity_id).to be_nil
end
end
end
context 'with Like' do

View file

@ -27,6 +27,11 @@ RSpec.describe ActivityPub::TagManager do
expect(subject.to(status)).to eq ['https://www.w3.org/ns/activitystreams#Public']
end
it 'returns followers collection for public_unlisted status' do
status = Fabricate(:status, visibility: :public_unlisted)
expect(subject.to(status)).to eq [account_followers_url(status.account)]
end
it 'returns followers collection for unlisted status' do
status = Fabricate(:status, visibility: :unlisted)
expect(subject.to(status)).to eq [account_followers_url(status.account)]
@ -69,12 +74,34 @@ RSpec.describe ActivityPub::TagManager do
end
end
describe '#to_for_friend' do
it 'returns followers collection for public_unlisted status' do
status = Fabricate(:status, visibility: :public_unlisted)
expect(subject.to_for_friend(status)).to eq [account_followers_url(status.account), 'LocalPublic']
end
it 'returns followers collection for unlisted status' do
status = Fabricate(:status, visibility: :unlisted)
expect(subject.to_for_friend(status)).to eq [account_followers_url(status.account)]
end
it 'returns followers collection for private status' do
status = Fabricate(:status, visibility: :private)
expect(subject.to_for_friend(status)).to eq [account_followers_url(status.account)]
end
end
describe '#cc' do
it 'returns followers collection for public status' do
status = Fabricate(:status, visibility: :public)
expect(subject.cc(status)).to eq [account_followers_url(status.account)]
end
it 'returns public collection for public_unlisted status' do
status = Fabricate(:status, visibility: :public_unlisted)
expect(subject.cc(status)).to eq ['https://www.w3.org/ns/activitystreams#Public']
end
it 'returns public collection for unlisted status' do
status = Fabricate(:status, visibility: :unlisted)
expect(subject.cc(status)).to eq ['https://www.w3.org/ns/activitystreams#Public']
@ -114,6 +141,74 @@ RSpec.describe ActivityPub::TagManager do
end
end
describe '#cc_for_misskey' do
let(:user) { Fabricate(:user) }
before do
user.settings.update(reject_unlisted_subscription: true, reject_public_unlisted_subscription: true)
user.save
end
it 'returns public collection for public status' do
status = Fabricate(:status, visibility: :public)
expect(subject.cc_for_misskey(status)).to eq [account_followers_url(status.account)]
end
it 'returns empty array for public_unlisted status' do
status = Fabricate(:status, account: user.account, visibility: :public_unlisted)
expect(subject.cc_for_misskey(status)).to eq []
end
it 'returns empty array for unlisted status' do
status = Fabricate(:status, account: user.account, visibility: :unlisted)
expect(subject.cc_for_misskey(status)).to eq []
end
end
describe '#searchable_by' do
it 'returns public collection for public status' do
status = Fabricate(:status, searchability: :public)
expect(subject.searchable_by(status)).to eq ['https://www.w3.org/ns/activitystreams#Public']
end
it 'returns followers collection for public_unlisted status' do
status = Fabricate(:status, searchability: :public_unlisted)
expect(subject.searchable_by(status)).to eq [account_followers_url(status.account)]
end
it 'returns followers collection for private status' do
status = Fabricate(:status, searchability: :private)
expect(subject.searchable_by(status)).to eq [account_followers_url(status.account)]
end
it 'returns empty array for direct status' do
status = Fabricate(:status, searchability: :direct)
expect(subject.searchable_by(status)).to eq []
end
it 'returns as:Limited array for limited status' do
status = Fabricate(:status, searchability: :limited)
expect(subject.searchable_by(status)).to eq ['as:Limited']
end
end
describe '#searchable_by_for_friend' do
it 'returns public collection for public status' do
status = Fabricate(:status, account: Fabricate(:account, searchability: :public), searchability: :public)
expect(subject.searchable_by_for_friend(status)).to eq ['https://www.w3.org/ns/activitystreams#Public']
end
it 'returns public collection for public_unlisted status' do
status = Fabricate(:status, account: Fabricate(:account, searchability: :public), searchability: :public_unlisted)
expect(subject.searchable_by_for_friend(status)).to eq [account_followers_url(status.account), 'LocalPublic']
end
it 'returns followers collection for private status' do
status = Fabricate(:status, account: Fabricate(:account, searchability: :public), searchability: :private)
expect(subject.searchable_by_for_friend(status)).to eq [account_followers_url(status.account)]
end
end
describe '#local_uri?' do
it 'returns false for non-local URI' do
expect(subject.local_uri?('http://example.com/123')).to be false