forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from mashirozx/dev
merge upstream (bump version to 3.2.1)
- Loading branch information
Showing
28 changed files
with
995 additions
and
517 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
app/controllers/activitypub/followers_synchronizations_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActivityPub::FollowersSynchronizationsController < ActivityPub::BaseController | ||
include SignatureVerification | ||
include AccountOwnedConcern | ||
|
||
before_action :require_signature! | ||
before_action :set_items | ||
before_action :set_cache_headers | ||
|
||
def show | ||
expires_in 0, public: false | ||
render json: collection_presenter, | ||
serializer: ActivityPub::CollectionSerializer, | ||
adapter: ActivityPub::Adapter, | ||
content_type: 'application/activity+json' | ||
end | ||
|
||
private | ||
|
||
def uri_prefix | ||
signed_request_account.uri[/http(s?):\/\/[^\/]+\//] | ||
end | ||
|
||
def set_items | ||
@items = @account.followers.where(Account.arel_table[:uri].matches(uri_prefix + '%', false, true)).pluck(:uri) | ||
end | ||
|
||
def collection_presenter | ||
ActivityPub::CollectionPresenter.new( | ||
id: account_followers_synchronization_url(@account), | ||
type: :ordered, | ||
items: @items | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ module Config | |
gopher | ||
xmpp | ||
magnet | ||
gemini | ||
).freeze | ||
|
||
CLASS_WHITELIST_TRANSFORMER = lambda do |env| | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/services/activitypub/prepare_followers_synchronization_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActivityPub::PrepareFollowersSynchronizationService < BaseService | ||
include JsonLdHelper | ||
|
||
def call(account, params) | ||
@account = account | ||
|
||
return if params['collectionId'] != @account.followers_url || invalid_origin?(params['url']) || @account.local_followers_hash == params['digest'] | ||
|
||
ActivityPub::FollowersSynchronizationWorker.perform_async(@account.id, params['url']) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActivityPub::SynchronizeFollowersService < BaseService | ||
include JsonLdHelper | ||
include Payloadable | ||
|
||
def call(account, partial_collection_url) | ||
@account = account | ||
|
||
items = collection_items(partial_collection_url) | ||
return if items.nil? | ||
|
||
# There could be unresolved accounts (hence the call to .compact) but this | ||
# should never happen in practice, since in almost all cases we keep an | ||
# Account record, and should we not do that, we should have sent a Delete. | ||
# In any case there is not much we can do if that occurs. | ||
@expected_followers = items.map { |uri| ActivityPub::TagManager.instance.uri_to_resource(uri, Account) }.compact | ||
|
||
remove_unexpected_local_followers! | ||
handle_unexpected_outgoing_follows! | ||
end | ||
|
||
private | ||
|
||
def remove_unexpected_local_followers! | ||
@account.followers.local.where.not(id: @expected_followers.map(&:id)).each do |unexpected_follower| | ||
UnfollowService.new.call(unexpected_follower, @account) | ||
end | ||
end | ||
|
||
def handle_unexpected_outgoing_follows! | ||
@expected_followers.each do |expected_follower| | ||
next if expected_follower.following?(@account) | ||
|
||
if expected_follower.requested?(@account) | ||
# For some reason the follow request went through but we missed it | ||
expected_follower.follow_requests.find_by(target_account: @account)&.authorize! | ||
else | ||
# Since we were not aware of the follow from our side, we do not have an | ||
# ID for it that we can include in the Undo activity. For this reason, | ||
# the Undo may not work with software that relies exclusively on | ||
# matching activity IDs and not the actor and target | ||
follow = Follow.new(account: expected_follower, target_account: @account) | ||
ActivityPub::DeliveryWorker.perform_async(build_undo_follow_json(follow), follow.account_id, follow.target_account.inbox_url) | ||
end | ||
end | ||
end | ||
|
||
def build_undo_follow_json(follow) | ||
Oj.dump(serialize_payload(follow, ActivityPub::UndoFollowSerializer)) | ||
end | ||
|
||
def collection_items(collection_or_uri) | ||
collection = fetch_collection(collection_or_uri) | ||
return unless collection.is_a?(Hash) | ||
|
||
collection = fetch_collection(collection['first']) if collection['first'].present? | ||
return unless collection.is_a?(Hash) | ||
|
||
case collection['type'] | ||
when 'Collection', 'CollectionPage' | ||
collection['items'] | ||
when 'OrderedCollection', 'OrderedCollectionPage' | ||
collection['orderedItems'] | ||
end | ||
end | ||
|
||
def fetch_collection(collection_or_uri) | ||
return collection_or_uri if collection_or_uri.is_a?(Hash) | ||
return if invalid_origin?(collection_or_uri) | ||
|
||
fetch_resource_without_id_validation(collection_or_uri, nil, true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/workers/activitypub/followers_synchronization_worker.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActivityPub::FollowersSynchronizationWorker | ||
include Sidekiq::Worker | ||
|
||
sidekiq_options queue: 'push', lock: :until_executed | ||
|
||
def perform(account_id, url) | ||
@account = Account.find_by(id: account_id) | ||
return true if @account.nil? | ||
|
||
ActivityPub::SynchronizeFollowersService.new.call(@account, url) | ||
end | ||
end |
Oops, something went wrong.