Skip to content

Commit 000da19

Browse files
Gargronhiyuki2578
authored andcommitted
Refactor all ActivityPub deliveries to be serialized and signed through one concern (mastodon#10966)
1 parent 2a0ac54 commit 000da19

24 files changed

Lines changed: 79 additions & 153 deletions

app/lib/activitypub/activity/follow.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class ActivityPub::Activity::Follow < ActivityPub::Activity
4+
include Payloadable
5+
46
def perform
57
target_account = account_from_uri(object_uri)
68

@@ -28,7 +30,7 @@ def perform
2830
end
2931

3032
def reject_follow_request!(target_account)
31-
json = ActiveModelSerializers::SerializableResource.new(FollowRequest.new(account: @account, target_account: target_account, uri: @json['id']), serializer: ActivityPub::RejectFollowSerializer, adapter: ActivityPub::Adapter).to_json
33+
json = Oj.dump(serialize_payload(FollowRequest.new(account: @account, target_account: target_account, uri: @json['id']), ActivityPub::RejectFollowSerializer))
3234
ActivityPub::DeliveryWorker.perform_async(json, target_account.id, @account.inbox_url)
3335
end
3436
end

app/models/account.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ def memorialize!
205205
end
206206
end
207207

208+
def sign?
209+
true
210+
end
211+
208212
def keypair
209213
@keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
210214
end

app/models/form/account_batch.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class Form::AccountBatch
44
include ActiveModel::Model
55
include Authorization
6+
include Payloadable
67

78
attr_accessor :account_ids, :action, :current_account
89

@@ -54,13 +55,7 @@ def reject_follow!(follow)
5455

5556
return unless follow.account.activitypub?
5657

57-
json = ActiveModelSerializers::SerializableResource.new(
58-
follow,
59-
serializer: ActivityPub::RejectFollowSerializer,
60-
adapter: ActivityPub::Adapter
61-
).to_json
62-
63-
ActivityPub::DeliveryWorker.perform_async(json, current_account.id, follow.account.inbox_url)
58+
ActivityPub::DeliveryWorker.perform_async(Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), current_account.id, follow.account.inbox_url)
6459
end
6560

6661
def approve!

app/models/status.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ def distributable?
207207
public_visibility? || unlisted_visibility?
208208
end
209209

210+
alias sign? distributable?
211+
210212
def with_media?
211213
media_attachments.any?
212214
end

app/services/after_block_domain_from_account_service.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class AfterBlockDomainFromAccountService < BaseService
4+
include Payloadable
5+
46
# This service does not create an AccountDomainBlock record,
57
# it's meant to be called after such a record has been created
68
# synchronously, to "clean up"
@@ -31,12 +33,6 @@ def reject_follow!(follow)
3133

3234
return unless follow.account.activitypub?
3335

34-
json = ActiveModelSerializers::SerializableResource.new(
35-
follow,
36-
serializer: ActivityPub::RejectFollowSerializer,
37-
adapter: ActivityPub::Adapter
38-
).to_json
39-
40-
ActivityPub::DeliveryWorker.perform_async(json, @account.id, follow.account.inbox_url)
36+
ActivityPub::DeliveryWorker.perform_async(Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), @account.id, follow.account.inbox_url)
4137
end
4238
end

app/services/authorize_follow_service.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class AuthorizeFollowService < BaseService
4+
include Payloadable
5+
46
def call(source_account, target_account, **options)
57
if options[:skip_follow_request]
68
follow_request = FollowRequest.new(account: source_account, target_account: target_account, uri: options[:follow_request_uri])
@@ -24,11 +26,7 @@ def create_notification(follow_request)
2426
end
2527

2628
def build_json(follow_request)
27-
ActiveModelSerializers::SerializableResource.new(
28-
follow_request,
29-
serializer: ActivityPub::AcceptFollowSerializer,
30-
adapter: ActivityPub::Adapter
31-
).to_json
29+
Oj.dump(serialize_payload(follow_request, ActivityPub::AcceptFollowSerializer))
3230
end
3331

3432
def build_xml(follow_request)

app/services/block_service.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class BlockService < BaseService
4+
include Payloadable
5+
46
def call(account, target_account)
57
return if account.id == target_account.id
68

@@ -26,11 +28,7 @@ def create_notification(block)
2628
end
2729

2830
def build_json(block)
29-
ActiveModelSerializers::SerializableResource.new(
30-
block,
31-
serializer: ActivityPub::BlockSerializer,
32-
adapter: ActivityPub::Adapter
33-
).to_json
31+
Oj.dump(serialize_payload(block, ActivityPub::BlockSerializer))
3432
end
3533

3634
def build_xml(block)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module Payloadable
4+
def serialize_payload(record, serializer, options = {})
5+
signer = options.delete(:signer)
6+
sign_with = options.delete(:sign_with)
7+
payload = ActiveModelSerializers::SerializableResource.new(record, options.merge(serializer: serializer, adapter: ActivityPub::Adapter)).as_json
8+
9+
if (record.respond_to?(:sign?) && record.sign?) && signer && signing_enabled?
10+
ActivityPub::LinkedDataSignature.new(payload).sign!(signer, sign_with: sign_with)
11+
else
12+
payload
13+
end
14+
end
15+
16+
def signing_enabled?
17+
true
18+
end
19+
end

app/services/favourite_service.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
class FavouriteService < BaseService
44
include Authorization
5+
include Payloadable
56

67
# Favourite a status and notify remote user
78
# @param [Account] account
@@ -43,11 +44,7 @@ def bump_potential_friendship(account, status)
4344
end
4445

4546
def build_json(favourite)
46-
Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
47-
favourite,
48-
serializer: ActivityPub::LikeSerializer,
49-
adapter: ActivityPub::Adapter
50-
).as_json).sign!(favourite.account))
47+
Oj.dump(serialize_payload(favourite, ActivityPub::LikeSerializer))
5148
end
5249

5350
def build_xml(favourite)

app/services/follow_service.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
class FollowService < BaseService
44
include Redisable
5+
include Payloadable
56

67
# Follow a remote user, notify remote user about the follow
78
# @param [Account] source_account From which to follow
@@ -78,10 +79,6 @@ def build_follow_xml(follow)
7879
end
7980

8081
def build_json(follow_request)
81-
ActiveModelSerializers::SerializableResource.new(
82-
follow_request,
83-
serializer: ActivityPub::FollowSerializer,
84-
adapter: ActivityPub::Adapter
85-
).to_json
82+
Oj.dump(serialize_payload(follow_request, ActivityPub::FollowSerializer))
8683
end
8784
end

0 commit comments

Comments
 (0)