Add author to the diaspora:// URL parser

This commit is contained in:
Benjamin Neff 2017-09-12 23:08:13 +02:00
parent e663a65c7e
commit b6ec405e55
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
5 changed files with 33 additions and 28 deletions

View file

@ -5,22 +5,27 @@ module DiasporaFederation
include Logging include Logging
# Regex to find diaspora:// URLs # 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 # Parses all diaspora:// URLs from the text and fetches the entities from
# the remote server if needed. # the remote server if needed.
# @param [String] sender the diaspora* ID of the sender of the entity # @param [String] sender the diaspora* ID of the sender of the entity
# @param [String] text text with diaspora:// URLs to fetch # @param [String] text text with diaspora:// URLs to fetch
def self.fetch_linked_entities(sender, text) def self.fetch_linked_entities(text)
text.scan(DIASPORA_URL_REGEX).each do |type, guid| text.scan(DIASPORA_URL_REGEX).each do |author, type, guid|
fetch_entity(sender, type, guid) fetch_entity(author, type, guid)
end end
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 class_name = Entity.entity_class(type).to_s.rpartition("::").last
return if DiasporaFederation.callbacks.trigger(:fetch_related_entity, class_name, guid) 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 rescue => e
logger.error "Failed to fetch linked entity #{type}:#{guid}: #{e.class}: #{e.message}" logger.error "Failed to fetch linked entity #{type}:#{guid}: #{e.class}: #{e.message}"
end end

View file

@ -46,7 +46,7 @@ module DiasporaFederation
end end
def fetch_linked_entities_from_text 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 end
end end

View file

@ -1,6 +1,6 @@
module DiasporaFederation module DiasporaFederation
describe Federation::DiasporaUrlParser do describe Federation::DiasporaUrlParser do
let(:sender) { Fabricate.sequence(:diaspora_id) } let(:author) { Fabricate.sequence(:diaspora_id) }
let(:guid) { Fabricate.sequence(:guid) } let(:guid) { Fabricate.sequence(:guid) }
describe ".fetch_linked_entities" do 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", guid2).and_return(double)
expect_callback(:fetch_related_entity, "Post", guid3).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 " \ text = "This is a [link to a post with markdown](diaspora://#{author}/post/#{guid}) and one without " \
"diaspora://post/#{guid2} and finally a last one diaspora://post/#{guid3}." "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 end
it "ignores invalid diaspora:// urls" do it "ignores invalid diaspora:// urls" do
expect(DiasporaFederation.callbacks).not_to receive(:trigger) expect(DiasporaFederation.callbacks).not_to receive(:trigger)
text = "This is an invalid link diaspora://Post/#{guid}) and another one: " \ text = "This is an invalid link diaspora://#{author}/Post/#{guid} and another one " \
"diaspora://post/abcd." "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 end
it "allows to link other entities" do it "allows to link other entities" do
expect_callback(:fetch_related_entity, "Event", guid).and_return(double) 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 end
it "handles unknown entities gracefully" do it "handles unknown entities gracefully" do
expect(DiasporaFederation.callbacks).not_to receive(:trigger) 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 end
it "fetches entities from sender when not found locally" do it "fetches entities from sender when not found locally" do
expect_callback(:fetch_related_entity, "Post", guid).and_return(nil) 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 end
it "handles fetch errors gracefully" do it "handles fetch errors gracefully" do
expect_callback(:fetch_related_entity, "Post", guid).and_return(nil) expect_callback(:fetch_related_entity, "Post", guid).and_return(nil)
expect(Federation::Fetcher).to receive(:fetch_public).with( expect(Federation::Fetcher).to receive(:fetch_public).with(
sender, "post", guid author, "post", guid
).and_raise(Federation::Fetcher::NotFetchable, "Something went wrong!") ).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 { expect {
Federation::DiasporaUrlParser.fetch_linked_entities(sender, text) Federation::DiasporaUrlParser.fetch_linked_entities(text)
}.not_to raise_error }.not_to raise_error
end end
end end

View file

@ -119,7 +119,7 @@ module DiasporaFederation
end end
it "fetches linked entities when the received entity has a text property" do 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 described_class.new(magic_env, recipient).receive
end end
@ -128,7 +128,7 @@ module DiasporaFederation
profile = Fabricate(:profile_entity) profile = Fabricate(:profile_entity)
magic_env = Salmon::MagicEnvelope.new(profile, profile.author) 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 described_class.new(magic_env, recipient).receive
end end

View file

@ -141,7 +141,7 @@ module DiasporaFederation
end end
it "fetches linked entities when the received entity has a text property" do 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 described_class.new(magic_env).receive
end end
@ -150,7 +150,7 @@ module DiasporaFederation
profile = Fabricate(:profile_entity, public: true) profile = Fabricate(:profile_entity, public: true)
magic_env = Salmon::MagicEnvelope.new(profile, profile.author) 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 described_class.new(magic_env).receive
end end