4 changed files with 122 additions and 0 deletions
@ -0,0 +1,37 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class MentionResolveWorker |
||||||
|
include Sidekiq::Worker |
||||||
|
include ExponentialBackoff |
||||||
|
include JsonLdHelper |
||||||
|
|
||||||
|
sidekiq_options queue: 'pull', retry: 7 |
||||||
|
|
||||||
|
def perform(status_id, uri, options = {}) |
||||||
|
status = Status.find_by(id: status_id) |
||||||
|
return if status.nil? |
||||||
|
|
||||||
|
account = account_from_uri(uri) |
||||||
|
account = ActivityPub::FetchRemoteAccountService.new.call(uri, request_id: options[:request_id]) if account.nil? |
||||||
|
|
||||||
|
return if account.nil? |
||||||
|
|
||||||
|
status.mentions.create!(account: account, silent: false) |
||||||
|
rescue ActiveRecord::RecordNotFound |
||||||
|
# Do nothing |
||||||
|
rescue Mastodon::UnexpectedResponseError => e |
||||||
|
response = e.response |
||||||
|
|
||||||
|
if response_error_unsalvageable?(response) |
||||||
|
# Give up |
||||||
|
else |
||||||
|
raise e |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def account_from_uri(uri) |
||||||
|
ActivityPub::TagManager.instance.uri_to_resource(uri, Account) |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
require 'rails_helper' |
||||||
|
|
||||||
|
RSpec.describe MentionResolveWorker do |
||||||
|
let(:status_id) { -42 } |
||||||
|
let(:uri) { 'https://example.com/users/unknown' } |
||||||
|
|
||||||
|
describe '#perform' do |
||||||
|
subject { described_class.new.perform(status_id, uri, {}) } |
||||||
|
|
||||||
|
context 'with a non-existent status' do |
||||||
|
it 'returns nil' do |
||||||
|
expect(subject).to be_nil |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context 'with a valid user' do |
||||||
|
let(:status) { Fabricate(:status) } |
||||||
|
let(:status_id) { status.id } |
||||||
|
|
||||||
|
let(:service_double) { instance_double(ActivityPub::FetchRemoteAccountService) } |
||||||
|
|
||||||
|
before do |
||||||
|
allow(ActivityPub::FetchRemoteAccountService).to receive(:new).and_return(service_double) |
||||||
|
|
||||||
|
allow(service_double).to receive(:call).with(uri, anything) { Fabricate(:account, domain: 'example.com', uri: uri) } |
||||||
|
end |
||||||
|
|
||||||
|
it 'resolves the account and adds a new mention', :aggregate_failures do |
||||||
|
expect { subject } |
||||||
|
.to change { status.reload.mentions }.from([]).to(a_collection_including(having_attributes(account: having_attributes(uri: uri), silent: false))) |
||||||
|
|
||||||
|
expect(service_double).to have_received(:call).once |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
Loading…
Reference in new issue