Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
class ArchiveImporter | ||
class BlockImporter | ||
include Diaspora::Logging | ||
attr_reader :json, :user | ||
|
||
def initialize(json, user) | ||
@json = json | ||
@user = user | ||
end | ||
|
||
def import | ||
p = Person.find_or_fetch_by_identifier(json) | ||
migrant_person = handle_migrant_person(p) | ||
user.blocks.create(person_id: migrant_person.id) | ||
rescue ActiveRecord::RecordInvalid, | ||
DiasporaFederation::Discovery::DiscoveryError => e | ||
logger.warn "#{self}: #{e}" | ||
end | ||
|
||
private | ||
|
||
def handle_migrant_person(person) | ||
return person if person.account_migration.nil? | ||
|
||
person.account_migration.newest_person | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
describe ArchiveImporter::BlockImporter do | ||
let(:target) { FactoryBot.create(:user) } | ||
|
||
describe "#import" do | ||
it "adds a block entry to the user" do | ||
person_to_block = FactoryBot.create(:person) | ||
block_importer = ArchiveImporter::BlockImporter.new(person_to_block.diaspora_handle, target) | ||
block_importer.import | ||
|
||
expect(target.blocks.count).to eq(1) | ||
expect(target.blocks.first.person_id).to eq(person_to_block.id) | ||
end | ||
|
||
it "handles unfetchable person to block" do | ||
expect_any_instance_of(DiasporaFederation::Discovery::Discovery).to receive(:fetch_and_save).and_raise( | ||
DiasporaFederation::Discovery::DiscoveryError, "discovery error reasons" | ||
) | ||
|
||
block_importer = ArchiveImporter::BlockImporter.new("unknown_person@bad_pod.tld", target) | ||
block_importer.import | ||
|
||
expect(target.blocks).to be_empty | ||
end | ||
end | ||
end |