diff --git a/lib/diaspora_federation/entities/reshare.rb b/lib/diaspora_federation/entities/reshare.rb index ec872b9..ca359c8 100644 --- a/lib/diaspora_federation/entities/reshare.rb +++ b/lib/diaspora_federation/entities/reshare.rb @@ -27,6 +27,20 @@ module DiasporaFederation def to_s "#{super}:#{root_guid}" end + + # fetch and receive root post from remote, if not available locally. + def fetch_root + root = DiasporaFederation.callbacks.trigger(:fetch_related_entity, "Post", root_guid) + Federation::Fetcher.fetch_public(root_author, "Post", root_guid) unless root + end + + # Fetch root post after parse. + # @see Entity.populate_entity + # @param [Nokogiri::XML::Element] root_node xml nodes + # @return [Entity] instance + private_class_method def self.populate_entity(root_node) + new(entity_data(root_node)).tap(&:fetch_root) + end end end end diff --git a/spec/lib/diaspora_federation/entities/reshare_spec.rb b/spec/lib/diaspora_federation/entities/reshare_spec.rb index 0b03bd9..8d0bf2b 100644 --- a/spec/lib/diaspora_federation/entities/reshare_spec.rb +++ b/spec/lib/diaspora_federation/entities/reshare_spec.rb @@ -1,6 +1,7 @@ module DiasporaFederation describe Entities::Reshare do - let(:data) { FactoryGirl.attributes_for(:reshare_entity) } + let(:root) { FactoryGirl.create(:post, author: bob) } + let(:data) { FactoryGirl.attributes_for(:reshare_entity, root_guid: root.guid, root_author: bob.diaspora_id) } let(:xml) { <<-XML @@ -40,5 +41,14 @@ XML expect(parsed_instance.provider_display_name).to be_nil end end + + context "fetch root" do + it "fetches the root post if it is not available already" do + expect_callback(:fetch_related_entity, "Post", data[:root_guid]).and_return(nil) + expect(Federation::Fetcher).to receive(:fetch_public).with(data[:root_author], "Post", data[:root_guid]) + + Entities::Reshare.from_xml(Nokogiri::XML::Document.parse(xml).root) + end + end end end