diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb index 563fa5aa07..4149fd9c4e 100644 --- a/app/services/activitypub/process_account_service.rb +++ b/app/services/activitypub/process_account_service.rb @@ -209,7 +209,7 @@ class ActivityPub::ProcessAccountService < BaseService end def fetch_instance_info - FetchInstanceInfoWorker.perform_async(@account.domain) unless InstanceInfo.exists?(domain: @account.domain) + ActivityPub::FetchInstanceInfoWorker.perform_async(@account.domain) unless InstanceInfo.exists?(domain: @account.domain) end def actor_type diff --git a/app/workers/fetch_instance_info_worker.rb b/app/workers/activitypub/fetch_instance_info_worker.rb similarity index 88% rename from app/workers/fetch_instance_info_worker.rb rename to app/workers/activitypub/fetch_instance_info_worker.rb index 2700f88e13..e982d4c086 100644 --- a/app/workers/fetch_instance_info_worker.rb +++ b/app/workers/activitypub/fetch_instance_info_worker.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class FetchInstanceInfoWorker +class ActivityPub::FetchInstanceInfoWorker include Sidekiq::Worker include JsonLdHelper include Redisable @@ -64,9 +64,9 @@ class FetchInstanceInfoWorker body_to_json(response.body_with_limit) elsif response.code == 410 - raise FetchInstanceInfoWorker::GoneError, "#{domain} is gone from the server" + raise ActivityPub::FetchInstanceInfoWorker::GoneError, "#{@instance.domain} is gone from the server" else - raise FetchInstanceInfoWorker::RequestError, "Request for #{domain} returned HTTP #{response.code}" + raise ActivityPub::FetchInstanceInfoWorker::RequestError, "Request for #{@instance.domain} returned HTTP #{response.code}" end end end diff --git a/app/workers/scheduler/update_instance_info_scheduler.rb b/app/workers/scheduler/update_instance_info_scheduler.rb index 0587146b32..f5b2852859 100644 --- a/app/workers/scheduler/update_instance_info_scheduler.rb +++ b/app/workers/scheduler/update_instance_info_scheduler.rb @@ -7,7 +7,7 @@ class Scheduler::UpdateInstanceInfoScheduler def perform Instance.select(:domain).reorder(nil).find_in_batches do |instances| - FetchInstanceInfoWorker.push_bulk(instances) do |instance| + ActivityPub::FetchInstanceInfoWorker.push_bulk(instances) do |instance| [instance.domain] end end diff --git a/spec/workers/activitypub/fetch_instance_info_worker_spec.rb b/spec/workers/activitypub/fetch_instance_info_worker_spec.rb new file mode 100644 index 0000000000..3561df72af --- /dev/null +++ b/spec/workers/activitypub/fetch_instance_info_worker_spec.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ActivityPub::FetchInstanceInfoWorker do + subject { described_class.new } + + let(:wellknown_nodeinfo) do + { + links: [ + { + rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', + href: 'https://example.com/nodeinfo/2.0', + }, + ], + } + end + + let(:nodeinfo) do + { + version: '2.0', + software: { + name: 'mastodon', + version: '4.2.0-beta1', + }, + protocols: ['activitypub'], + } + end + + let(:wellknown_nodeinfo_json) { Oj.dump(wellknown_nodeinfo) } + let(:nodeinfo_json) { Oj.dump(nodeinfo) } + + context 'when success' do + before do + stub_request(:get, 'https://example.com/.well-known/nodeinfo').to_return(status: 200, body: wellknown_nodeinfo_json) + stub_request(:get, 'https://example.com/nodeinfo/2.0').to_return(status: 200, body: nodeinfo_json) + Fabricate(:account, domain: 'example.com') + Instance.refresh + end + + it 'performs a mastodon instance' do + subject.perform('example.com') + + info = InstanceInfo.find_by(domain: 'example.com') + expect(info).to_not be_nil + expect(info.software).to eq 'mastodon' + expect(info.version).to eq '4.2.0-beta1' + end + end + + context 'when failed' do + before do + stub_request(:get, 'https://example.com/.well-known/nodeinfo').to_return(status: 404) + Fabricate(:account, domain: 'example.com') + Instance.refresh + end + + it 'performs a mastodon instance' do + expect { subject.perform('example.com') }.to raise_error(ActivityPub::FetchInstanceInfoWorker::RequestError, 'Request for example.com returned HTTP 404') + + info = InstanceInfo.find_by(domain: 'example.com') + expect(info).to be_nil + end + end +end