diff --git a/lib/diaspora_federation/federation/diaspora_url_parser.rb b/lib/diaspora_federation/federation/diaspora_url_parser.rb index 3fb4300..dad4b41 100644 --- a/lib/diaspora_federation/federation/diaspora_url_parser.rb +++ b/lib/diaspora_federation/federation/diaspora_url_parser.rb @@ -5,22 +5,27 @@ module DiasporaFederation include Logging # Regex to find diaspora:// URLs - DIASPORA_URL_REGEX = %r{diaspora://(#{Entity::ENTITY_NAME_REGEX})/(#{Validation::Rule::Guid::VALID_CHARS})} + DIASPORA_URL_REGEX = %r{ + diaspora:// + (#{Validation::Rule::DiasporaId::DIASPORA_ID_REGEX})/ + (#{Entity::ENTITY_NAME_REGEX})/ + (#{Validation::Rule::Guid::VALID_CHARS}) + }ux # Parses all diaspora:// URLs from the text and fetches the entities from # the remote server if needed. # @param [String] sender the diaspora* ID of the sender of the entity # @param [String] text text with diaspora:// URLs to fetch - def self.fetch_linked_entities(sender, text) - text.scan(DIASPORA_URL_REGEX).each do |type, guid| - fetch_entity(sender, type, guid) + def self.fetch_linked_entities(text) + text.scan(DIASPORA_URL_REGEX).each do |author, type, guid| + fetch_entity(author, type, guid) end end - private_class_method def self.fetch_entity(sender, type, guid) + private_class_method def self.fetch_entity(author, type, guid) class_name = Entity.entity_class(type).to_s.rpartition("::").last return if DiasporaFederation.callbacks.trigger(:fetch_related_entity, class_name, guid) - Fetcher.fetch_public(sender, type, guid) + Fetcher.fetch_public(author, type, guid) rescue => e logger.error "Failed to fetch linked entity #{type}:#{guid}: #{e.class}: #{e.message}" end diff --git a/lib/diaspora_federation/federation/receiver/abstract_receiver.rb b/lib/diaspora_federation/federation/receiver/abstract_receiver.rb index d63ba6e..30a203e 100644 --- a/lib/diaspora_federation/federation/receiver/abstract_receiver.rb +++ b/lib/diaspora_federation/federation/receiver/abstract_receiver.rb @@ -46,7 +46,7 @@ module DiasporaFederation end def fetch_linked_entities_from_text - DiasporaUrlParser.fetch_linked_entities(sender, entity.text) if entity.respond_to?(:text) && entity.text + DiasporaUrlParser.fetch_linked_entities(entity.text) if entity.respond_to?(:text) && entity.text end end end diff --git a/spec/lib/diaspora_federation/federation/diaspora_url_parser_spec.rb b/spec/lib/diaspora_federation/federation/diaspora_url_parser_spec.rb index bce2b2d..0bd5628 100644 --- a/spec/lib/diaspora_federation/federation/diaspora_url_parser_spec.rb +++ b/spec/lib/diaspora_federation/federation/diaspora_url_parser_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Federation::DiasporaUrlParser do - let(:sender) { Fabricate.sequence(:diaspora_id) } + let(:author) { Fabricate.sequence(:diaspora_id) } let(:guid) { Fabricate.sequence(:guid) } describe ".fetch_linked_entities" do @@ -11,56 +11,56 @@ module DiasporaFederation expect_callback(:fetch_related_entity, "Post", guid2).and_return(double) expect_callback(:fetch_related_entity, "Post", guid3).and_return(double) - text = "This is a [link to a post with markdown](diaspora://post/#{guid}) and one without " \ - "diaspora://post/#{guid2} and finally a last one diaspora://post/#{guid3}." + text = "This is a [link to a post with markdown](diaspora://#{author}/post/#{guid}) and one without " \ + "diaspora://#{author}/post/#{guid2} and finally a last one diaspora://#{author}/post/#{guid3}." - Federation::DiasporaUrlParser.fetch_linked_entities(sender, text) + Federation::DiasporaUrlParser.fetch_linked_entities(text) end it "ignores invalid diaspora:// urls" do expect(DiasporaFederation.callbacks).not_to receive(:trigger) - text = "This is an invalid link diaspora://Post/#{guid}) and another one: " \ - "diaspora://post/abcd." + text = "This is an invalid link diaspora://#{author}/Post/#{guid} and another one " \ + "diaspora://#{author}/post/abcd and last one: diaspora://example.org/post/#{guid}." - Federation::DiasporaUrlParser.fetch_linked_entities(sender, text) + Federation::DiasporaUrlParser.fetch_linked_entities(text) end it "allows to link other entities" do expect_callback(:fetch_related_entity, "Event", guid).and_return(double) - text = "This is a link to an event diaspora://event/#{guid}." + text = "This is a link to an event diaspora://#{author}/event/#{guid}." - Federation::DiasporaUrlParser.fetch_linked_entities(sender, text) + Federation::DiasporaUrlParser.fetch_linked_entities(text) end it "handles unknown entities gracefully" do expect(DiasporaFederation.callbacks).not_to receive(:trigger) - text = "This is a link to an event diaspora://unknown/#{guid}." + text = "This is a link to an event diaspora://#{author}/unknown/#{guid}." - Federation::DiasporaUrlParser.fetch_linked_entities(sender, text) + Federation::DiasporaUrlParser.fetch_linked_entities(text) end it "fetches entities from sender when not found locally" do expect_callback(:fetch_related_entity, "Post", guid).and_return(nil) - expect(Federation::Fetcher).to receive(:fetch_public).with(sender, "post", guid) + expect(Federation::Fetcher).to receive(:fetch_public).with(author, "post", guid) - text = "This is a link to a post: diaspora://post/#{guid}." + text = "This is a link to a post: diaspora://#{author}/post/#{guid}." - Federation::DiasporaUrlParser.fetch_linked_entities(sender, text) + Federation::DiasporaUrlParser.fetch_linked_entities(text) end it "handles fetch errors gracefully" do expect_callback(:fetch_related_entity, "Post", guid).and_return(nil) expect(Federation::Fetcher).to receive(:fetch_public).with( - sender, "post", guid + author, "post", guid ).and_raise(Federation::Fetcher::NotFetchable, "Something went wrong!") - text = "This is a link to a post: diaspora://post/#{guid}." + text = "This is a link to a post: diaspora://#{author}/post/#{guid}." expect { - Federation::DiasporaUrlParser.fetch_linked_entities(sender, text) + Federation::DiasporaUrlParser.fetch_linked_entities(text) }.not_to raise_error end end diff --git a/spec/lib/diaspora_federation/federation/receiver/private_spec.rb b/spec/lib/diaspora_federation/federation/receiver/private_spec.rb index 5635e91..e0fbb37 100644 --- a/spec/lib/diaspora_federation/federation/receiver/private_spec.rb +++ b/spec/lib/diaspora_federation/federation/receiver/private_spec.rb @@ -119,7 +119,7 @@ module DiasporaFederation end it "fetches linked entities when the received entity has a text property" do - expect(Federation::DiasporaUrlParser).to receive(:fetch_linked_entities).with(post.author, post.text) + expect(Federation::DiasporaUrlParser).to receive(:fetch_linked_entities).with(post.text) described_class.new(magic_env, recipient).receive end @@ -128,7 +128,7 @@ module DiasporaFederation profile = Fabricate(:profile_entity) magic_env = Salmon::MagicEnvelope.new(profile, profile.author) - expect(Federation::DiasporaUrlParser).to receive(:fetch_linked_entities).with(profile.author, profile.bio) + expect(Federation::DiasporaUrlParser).to receive(:fetch_linked_entities).with(profile.bio) described_class.new(magic_env, recipient).receive end diff --git a/spec/lib/diaspora_federation/federation/receiver/public_spec.rb b/spec/lib/diaspora_federation/federation/receiver/public_spec.rb index 04f2108..0096120 100644 --- a/spec/lib/diaspora_federation/federation/receiver/public_spec.rb +++ b/spec/lib/diaspora_federation/federation/receiver/public_spec.rb @@ -141,7 +141,7 @@ module DiasporaFederation end it "fetches linked entities when the received entity has a text property" do - expect(Federation::DiasporaUrlParser).to receive(:fetch_linked_entities).with(post.author, post.text) + expect(Federation::DiasporaUrlParser).to receive(:fetch_linked_entities).with(post.text) described_class.new(magic_env).receive end @@ -150,7 +150,7 @@ module DiasporaFederation profile = Fabricate(:profile_entity, public: true) magic_env = Salmon::MagicEnvelope.new(profile, profile.author) - expect(Federation::DiasporaUrlParser).to receive(:fetch_linked_entities).with(profile.author, profile.bio) + expect(Federation::DiasporaUrlParser).to receive(:fetch_linked_entities).with(profile.bio) described_class.new(magic_env).receive end