Browse Source

Reject existing Follow in addition to sending a Block (#9811)

Mastodon expects remote servers to remove follow relationships upon receiving
a Block. However, the spec only evokes Block activities in a C2S context, never
in a S2S context.

This PR, in addition to federating the Block, explicitly sends a Reject for any
affected follow relationship, which makes a bit more sense with regards to the
spec.
ThibG 6 months ago
parent
commit
aeb124491d
2 changed files with 33 additions and 0 deletions
  1. 15
    0
      app/services/unfollow_service.rb
  2. 18
    0
      spec/services/unfollow_service_spec.rb

+ 15
- 0
app/services/unfollow_service.rb View File

@@ -20,6 +20,7 @@ class UnfollowService < BaseService
20 20
 
21 21
     follow.destroy!
22 22
     create_notification(follow) unless @target_account.local?
23
+    create_reject_notification(follow) if @target_account.local? && !@source_account.local?
23 24
     UnmergeWorker.perform_async(@target_account.id, @source_account.id)
24 25
     follow
25 26
   end
@@ -42,6 +43,12 @@ class UnfollowService < BaseService
42 43
     end
43 44
   end
44 45
 
46
+  def create_reject_notification(follow)
47
+    # Rejecting an already-existing follow request
48
+    return unless follow.account.activitypub?
49
+    ActivityPub::DeliveryWorker.perform_async(build_reject_json(follow), follow.target_account_id, follow.account.inbox_url)
50
+  end
51
+
45 52
   def build_json(follow)
46 53
     ActiveModelSerializers::SerializableResource.new(
47 54
       follow,
@@ -50,6 +57,14 @@ class UnfollowService < BaseService
50 57
     ).to_json
51 58
   end
52 59
 
60
+  def build_reject_json(follow)
61
+    ActiveModelSerializers::SerializableResource.new(
62
+      follow,
63
+      serializer: ActivityPub::RejectFollowSerializer,
64
+      adapter: ActivityPub::Adapter
65
+    ).to_json
66
+  end
67
+
53 68
   def build_xml(follow)
54 69
     OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.unfollow_salmon(follow))
55 70
   end

+ 18
- 0
spec/services/unfollow_service_spec.rb View File

@@ -56,4 +56,22 @@ RSpec.describe UnfollowService, type: :service do
56 56
       expect(a_request(:post, 'http://example.com/inbox')).to have_been_made.once
57 57
     end
58 58
   end
59
+
60
+  describe 'remote ActivityPub (reverse)' do
61
+    let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox')).account }
62
+
63
+    before do
64
+      bob.follow!(sender)
65
+      stub_request(:post, 'http://example.com/inbox').to_return(status: 200)
66
+      subject.call(bob, sender)
67
+    end
68
+
69
+    it 'destroys the following relation' do
70
+      expect(bob.following?(sender)).to be false
71
+    end
72
+
73
+    it 'sends a reject activity' do
74
+      expect(a_request(:post, 'http://example.com/inbox')).to have_been_made.once
75
+    end
76
+  end
59 77
 end