fetch root posts for reshares

This commit is contained in:
Benjamin Neff 2016-06-26 21:22:01 +02:00
parent 79cbed4a3b
commit 9b090a3950
2 changed files with 25 additions and 1 deletions

View file

@ -27,6 +27,20 @@ module DiasporaFederation
def to_s def to_s
"#{super}:#{root_guid}" "#{super}:#{root_guid}"
end 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 end
end end

View file

@ -1,6 +1,7 @@
module DiasporaFederation module DiasporaFederation
describe Entities::Reshare do 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) { let(:xml) {
<<-XML <<-XML
@ -40,5 +41,14 @@ XML
expect(parsed_instance.provider_display_name).to be_nil expect(parsed_instance.provider_display_name).to be_nil
end end
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
end end