Fetch statuses/following/followers numbers from ActivityPub collections (#4840)

This commit is contained in:
Eugen Rochko 2017-09-08 12:00:17 +02:00 committed by GitHub
parent 7c2d84910c
commit a4caa7eb62
3 changed files with 39 additions and 8 deletions

View file

@ -13,6 +13,7 @@ class ActivityPub::ProcessAccountService < BaseService
@username = username
@domain = domain
@account = Account.find_by(uri: @uri)
@collections = {}
create_account if @account.nil?
upgrade_account if @account.ostatus?
@ -51,6 +52,9 @@ class ActivityPub::ProcessAccountService < BaseService
@account.header_remote_url = image_url('image')
@account.public_key = public_key || ''
@account.locked = @json['manuallyApprovesFollowers'] || false
@account.statuses_count = outbox_total_items if outbox_total_items.present?
@account.following_count = following_total_items if following_total_items.present?
@account.followers_count = followers_total_items if followers_total_items.present?
@account.save!
end
@ -88,6 +92,29 @@ class ActivityPub::ProcessAccountService < BaseService
value['href']
end
def outbox_total_items
collection_total_items('outbox')
end
def following_total_items
collection_total_items('following')
end
def followers_total_items
collection_total_items('followers')
end
def collection_total_items(type)
return if @json[type].blank?
return @collections[type] if @collections.key?(type)
collection = fetch_resource(@json[type])
@collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
rescue HTTP::Error, OpenSSL::SSL::SSLError
@collections[type] = nil
end
def auto_suspend?
domain_block && domain_block.suspend?
end

View file

@ -4,6 +4,10 @@ RSpec.describe ActivityPub::Activity::Update do
let!(:sender) { Fabricate(:account) }
before do
stub_request(:get, actor_json[:outbox]).to_return(status: 404)
stub_request(:get, actor_json[:followers]).to_return(status: 404)
stub_request(:get, actor_json[:following]).to_return(status: 404)
sender.update!(uri: ActivityPub::TagManager.instance.uri_for(sender))
end