2
0

Add support for numeric-based URIs for local accounts (#32724)

This commit is contained in:
Claire
2025-09-29 14:05:48 +02:00
committed by GitHub
parent 4d7c208da3
commit 150f0fcba5
20 changed files with 324 additions and 52 deletions

View File

@@ -71,6 +71,10 @@ class AccountsController < ApplicationController
params[:username]
end
def account_id_param
params[:id]
end
def skip_temporary_suspension_response?
request.format == :json
end

View File

@@ -28,7 +28,7 @@ class ActivityPub::LikesController < ActivityPub::BaseController
def likes_collection_presenter
ActivityPub::CollectionPresenter.new(
id: account_status_likes_url(@account, @status),
id: ActivityPub::TagManager.instance.likes_uri_for(@status),
type: :unordered,
size: @status.favourites_count
)

View File

@@ -73,6 +73,8 @@ class ActivityPub::OutboxesController < ActivityPub::BaseController
end
def set_account
@account = params[:account_username].present? ? Account.find_local!(username_param) : Account.representative
return super if params[:account_username].present? || params[:account_id].present?
@account = Account.representative
end
end

View File

@@ -37,7 +37,7 @@ class ActivityPub::RepliesController < ActivityPub::BaseController
def replies_collection_presenter
page = ActivityPub::CollectionPresenter.new(
id: account_status_replies_url(@account, @status, page_params),
id: ActivityPub::TagManager.instance.replies_uri_for(@status, page_params),
type: :unordered,
part_of: account_status_replies_url(@account, @status),
next: next_page,
@@ -47,7 +47,7 @@ class ActivityPub::RepliesController < ActivityPub::BaseController
return page if page_requested?
ActivityPub::CollectionPresenter.new(
id: account_status_replies_url(@account, @status),
id: ActivityPub::TagManager.instance.replies_uri_for(@status),
type: :unordered,
first: page
)
@@ -66,8 +66,7 @@ class ActivityPub::RepliesController < ActivityPub::BaseController
# Only consider remote accounts
return nil if @replies.size < DESCENDANTS_LIMIT
account_status_replies_url(
@account,
ActivityPub::TagManager.instance.replies_uri_for(
@status,
page: true,
min_id: @replies&.last&.id,
@@ -77,8 +76,7 @@ class ActivityPub::RepliesController < ActivityPub::BaseController
# For now, we're serving only self-replies, but next page might be other accounts
next_only_other_accounts = @replies&.last&.account_id != @account.id || @replies.size < DESCENDANTS_LIMIT
account_status_replies_url(
@account,
ActivityPub::TagManager.instance.replies_uri_for(
@status,
page: true,
min_id: next_only_other_accounts ? nil : @replies&.last&.id,

View File

@@ -28,7 +28,7 @@ class ActivityPub::SharesController < ActivityPub::BaseController
def shares_collection_presenter
ActivityPub::CollectionPresenter.new(
id: account_status_shares_url(@account, @status),
id: ActivityPub::TagManager.instance.shares_uri_for(@status),
type: :unordered,
size: @status.reblogs_count
)

View File

@@ -18,7 +18,11 @@ module AccountOwnedConcern
end
def set_account
@account = Account.find_local!(username_param)
@account = username_param.present? ? Account.find_local!(username_param) : Account.local.find(account_id_param)
end
def account_id_param
params[:account_id]
end
def username_param

View File

@@ -60,17 +60,17 @@ class FollowerAccountsController < ApplicationController
def collection_presenter
if page_requested?
ActivityPub::CollectionPresenter.new(
id: account_followers_url(@account, page: params.fetch(:page, 1)),
id: page_url(params.fetch(:page, 1)),
type: :ordered,
size: @account.followers_count,
items: follows.map { |follow| ActivityPub::TagManager.instance.uri_for(follow.account) },
part_of: account_followers_url(@account),
part_of: ActivityPub::TagManager.instance.followers_uri_for(@account),
next: next_page_url,
prev: prev_page_url
)
else
ActivityPub::CollectionPresenter.new(
id: account_followers_url(@account),
id: ActivityPub::TagManager.instance.followers_uri_for(@account),
type: :ordered,
size: @account.followers_count,
first: page_url(1)

View File

@@ -49,7 +49,7 @@ class FollowingAccountsController < ApplicationController
end
def page_url(page)
account_following_index_url(@account, page: page) unless page.nil?
ActivityPub::TagManager.instance.following_uri_for(@account, page: page) unless page.nil?
end
def next_page_url
@@ -63,17 +63,17 @@ class FollowingAccountsController < ApplicationController
def collection_presenter
if page_requested?
ActivityPub::CollectionPresenter.new(
id: account_following_index_url(@account, page: params.fetch(:page, 1)),
id: page_url(params.fetch(:page, 1)),
type: :ordered,
size: @account.following_count,
items: follows.map { |follow| ActivityPub::TagManager.instance.uri_for(follow.target_account) },
part_of: account_following_index_url(@account),
part_of: ActivityPub::TagManager.instance.following_uri_for(@account),
next: next_page_url,
prev: prev_page_url
)
else
ActivityPub::CollectionPresenter.new(
id: account_following_index_url(@account),
id: ActivityPub::TagManager.instance.following_uri_for(@account),
type: :ordered,
size: @account.following_count,
first: page_url(1)

View File

@@ -39,13 +39,25 @@ class ActivityPub::TagManager
case target.object_type
when :person
target.instance_actor? ? instance_actor_url : account_url(target)
if target.instance_actor?
instance_actor_url
elsif target.numeric_ap_id?
ap_account_url(target.id)
else
account_url(target)
end
when :conversation
context_url(target) unless target.parent_account_id.nil? || target.parent_status_id.nil?
when :note, :comment, :activity
return activity_account_status_url(target.account, target) if target.reblog?
if target.account.numeric_ap_id?
return activity_ap_account_status_url(target.account, target) if target.reblog?
account_status_url(target.account, target)
ap_account_status_url(target.account.id, target)
else
return activity_account_status_url(target.account, target) if target.reblog?
account_status_url(target.account, target)
end
when :emoji
emoji_url(target)
when :flag
@@ -57,7 +69,7 @@ class ActivityPub::TagManager
return quote.approval_uri unless quote.quoted_account&.local?
return if check_approval && !quote.accepted?
account_quote_authorization_url(quote.quoted_account, quote)
quote.quoted_account.numeric_ap_id? ? ap_account_quote_authorization_url(quote.quoted_account_id, quote) : account_quote_authorization_url(quote.quoted_account, quote)
end
def key_uri_for(target)
@@ -68,6 +80,10 @@ class ActivityPub::TagManager
account_url(username: username)
end
def uri_for_account_id(id)
ap_account_url(id: id)
end
def generate_uri_for(_target)
URI.join(root_url, 'payloads', SecureRandom.uuid)
end
@@ -75,7 +91,7 @@ class ActivityPub::TagManager
def activity_uri_for(target)
raise ArgumentError, 'target must be a local activity' unless %i(note comment activity).include?(target.object_type) && target.local?
activity_account_status_url(target.account, target)
target.account.numeric_ap_id? ? activity_ap_account_status_url(target.account.id, target) : activity_account_status_url(target.account, target)
end
def context_uri_for(target, page_params = nil)
@@ -87,49 +103,61 @@ class ActivityPub::TagManager
def replies_uri_for(target, page_params = nil)
raise ArgumentError, 'target must be a local activity' unless %i(note comment activity).include?(target.object_type) && target.local?
account_status_replies_url(target.account, target, page_params)
target.account.numeric_ap_id? ? ap_account_status_replies_url(target.account.id, target, page_params) : account_status_replies_url(target.account, target, page_params)
end
def likes_uri_for(target)
raise ArgumentError, 'target must be a local activity' unless %i(note comment activity).include?(target.object_type) && target.local?
account_status_likes_url(target.account, target)
target.account.numeric_ap_id? ? ap_account_status_likes_url(target.account.id, target) : account_status_likes_url(target.account, target)
end
def shares_uri_for(target)
raise ArgumentError, 'target must be a local activity' unless %i(note comment activity).include?(target.object_type) && target.local?
account_status_shares_url(target.account, target)
target.account.numeric_ap_id? ? ap_account_status_shares_url(target.account.id, target) : account_status_shares_url(target.account, target)
end
def following_uri_for(target, ...)
raise ArgumentError, 'target must be a local account' unless target.local?
account_following_index_url(target, ...)
target.numeric_ap_id? ? ap_account_following_index_url(target.id, ...) : account_following_index_url(target, ...)
end
def followers_uri_for(target, ...)
return target.followers_url.presence unless target.local?
account_followers_url(target, ...)
target.numeric_ap_id? ? ap_account_followers_url(target.id, ...) : account_followers_url(target, ...)
end
def collection_uri_for(target, ...)
raise NotImplementedError unless target.local?
raise ArgumentError, 'target must be a local account' unless target.local?
account_collection_url(target, ...)
target.numeric_ap_id? ? ap_account_collection_url(target.id, ...) : account_collection_url(target, ...)
end
def inbox_uri_for(target)
raise NotImplementedError unless target.local?
raise ArgumentError, 'target must be a local account' unless target.local?
target.instance_actor? ? instance_actor_inbox_url : account_inbox_url(target)
if target.instance_actor?
instance_actor_inbox_url
elsif target.numeric_ap_id?
ap_account_inbox_url(target.id)
else
account_inbox_url(target)
end
end
def outbox_uri_for(target, ...)
raise NotImplementedError unless target.local?
raise ArgumentError, 'target must be a local account' unless target.local?
target.instance_actor? ? instance_actor_outbox_url(...) : account_outbox_url(target, ...)
if target.instance_actor?
instance_actor_outbox_url(...)
elsif target.numeric_ap_id?
ap_account_outbox_url(target.id, ...)
else
account_outbox_url(target, ...)
end
end
# Primary audience of a status
@@ -262,10 +290,9 @@ class ActivityPub::TagManager
path_params = Rails.application.routes.recognize_path(uri)
# TODO: handle numeric IDs
case path_params[:controller]
when 'accounts'
[:username, path_params[:username]]
path_params.key?(:username) ? [:username, path_params[:username]] : [:id, path_params[:id]]
when 'instance_actors'
[:id, -99]
end

View File

@@ -52,6 +52,7 @@
# requested_review_at :datetime
# indexable :boolean default(FALSE), not null
# attribution_domains :string default([]), is an Array
# id_scheme :integer default("username_ap_id")
#
class Account < ApplicationRecord
@@ -105,6 +106,7 @@ class Account < ApplicationRecord
enum :protocol, { ostatus: 0, activitypub: 1 }
enum :suspension_origin, { local: 0, remote: 1 }, prefix: true
enum :id_scheme, { username_ap_id: 0, numeric_ap_id: 1 }
validates :username, presence: true
validates_with UniqueUsernameValidator, if: -> { will_save_change_to_username? }

View File

@@ -215,8 +215,9 @@ module Account::Interactions
def local_followers_hash
Rails.cache.fetch("followers_hash:#{id}:local") do
digest = "\x00" * 32
followers.where(domain: nil).pluck_each(:username) do |username|
Xorcist.xor!(digest, Digest::SHA256.digest(ActivityPub::TagManager.instance.uri_for_username(username)))
followers.where(domain: nil).pluck_each(:id_scheme, :id, :username) do |id_scheme, id, username|
uri = id_scheme == 'numeric_ap_id' ? ActivityPub::TagManager.instance.uri_for_account_id(id) : ActivityPub::TagManager.instance.uri_for_username(username)
Xorcist.xor!(digest, Digest::SHA256.digest(uri))
end
digest.unpack1('H*')
end

View File

@@ -55,7 +55,7 @@ class ActivityPub::DeliveryWorker
end
def synchronization_header
"collectionId=\"#{account_followers_url(@source_account)}\", digest=\"#{@source_account.remote_followers_hash(@inbox_url)}\", url=\"#{account_followers_synchronization_url(@source_account)}\""
"collectionId=\"#{ActivityPub::TagManager.instance.followers_uri_for(@source_account)}\", digest=\"#{@source_account.remote_followers_hash(@inbox_url)}\", url=\"#{account_followers_synchronization_url(@source_account)}\""
end
def perform_request