Create RelatedEntity.fetch method

This commit is contained in:
Benjamin Neff 2017-06-04 01:36:25 +02:00
parent 473450c34a
commit c92624d926
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
3 changed files with 33 additions and 7 deletions

View file

@ -24,6 +24,18 @@ module DiasporaFederation
# @return [RelatedEntity] parent entity
entity :parent, Entities::RelatedEntity, default: nil
# Get related entity from the backend or fetch it from remote if not available locally
# @return [RelatedEntity] fetched related entity
def self.fetch(author, type, guid)
# Try to fetch locally
entity = DiasporaFederation.callbacks.trigger(:fetch_related_entity, type, guid)
return entity if entity
# Fetch and receive entity from remote if not available locally
Federation::Fetcher.fetch_public(author, type, guid)
DiasporaFederation.callbacks.trigger(:fetch_related_entity, type, guid)
end
# never add {RelatedEntity} to xml
def to_xml
nil

View file

@ -193,13 +193,7 @@ module DiasporaFederation
raise DiasporaFederation::Entity::ValidationError, "invalid #{self}! missing 'parent_guid'."
}
data[:parent] = DiasporaFederation.callbacks.trigger(:fetch_related_entity, type, guid)
return if data[:parent]
# Fetch and receive parent from remote, if not available locally
Federation::Fetcher.fetch_public(data[:author], type, guid)
data[:parent] = DiasporaFederation.callbacks.trigger(:fetch_related_entity, type, guid)
data[:parent] = RelatedEntity.fetch(data[:author], type, guid)
end
def xml_parser_class

View file

@ -5,6 +5,26 @@ module DiasporaFederation
it_behaves_like "an Entity subclass"
describe ".fetch" do
let(:guid) { Fabricate.sequence(:guid) }
let(:entity) { Fabricate(:related_entity) }
it "fetches the entity from the backend" do
expect_callback(:fetch_related_entity, "Entity", guid).and_return(entity)
expect(Federation::Fetcher).not_to receive(:fetch_public)
expect(described_class.fetch(entity.author, "Entity", guid)).to eq(entity)
end
it "fetches the entity from remote if not found on backend" do
expect_callback(:fetch_related_entity, "Entity", guid).ordered.and_return(nil)
expect(Federation::Fetcher).to receive(:fetch_public).ordered.with(entity.author, "Entity", guid)
expect_callback(:fetch_related_entity, "Entity", guid).ordered.and_return(entity)
expect(described_class.fetch(entity.author, "Entity", guid)).to eq(entity)
end
end
describe "#to_xml" do
it "returns nil" do
expect(described_class.new(data).to_xml).to be_nil