create fetch_related_entity callback and cleanup unused callbacks

This commit is contained in:
Benjamin Neff 2016-03-29 04:20:52 +02:00
parent 800fa1786c
commit 3a5990c552
3 changed files with 63 additions and 85 deletions

View file

@ -63,32 +63,19 @@ DiasporaFederation.configure do |config|
person_entity.save! person_entity.save!
end end
on :fetch_private_key_by_diaspora_id do |diaspora_id| on :fetch_private_key do |diaspora_id|
key = Person.where(diaspora_handle: diaspora_id).joins(:owner).pluck(:serialized_private_key).first key = Person.where(diaspora_handle: diaspora_id).joins(:owner).pluck(:serialized_private_key).first
OpenSSL::PKey::RSA.new key unless key.nil? OpenSSL::PKey::RSA.new key unless key.nil?
end end
on :fetch_author_private_key_by_entity_guid do |entity_type, guid| on :fetch_public_key do |diaspora_id|
key = entity_type.constantize.where(guid: guid).joins(author: :owner).pluck(:serialized_private_key).first
OpenSSL::PKey::RSA.new key unless key.nil?
end
on :fetch_public_key_by_diaspora_id do |diaspora_id|
key = Person.find_or_fetch_by_identifier(diaspora_id).serialized_public_key key = Person.find_or_fetch_by_identifier(diaspora_id).serialized_public_key
OpenSSL::PKey::RSA.new key unless key.nil? OpenSSL::PKey::RSA.new key unless key.nil?
end end
on :fetch_author_public_key_by_entity_guid do |entity_type, guid| on :fetch_related_entity do |entity_type, guid|
key = entity_type.constantize.where(guid: guid).joins(:author).pluck(:serialized_public_key).first entity = entity_type.constantize.find_by(guid: guid)
OpenSSL::PKey::RSA.new key unless key.nil? Diaspora::Federation::Entities.related_entity(entity) if entity
end
on :entity_author_is_local? do |entity_type, guid|
entity_type.constantize.where(guid: guid).joins(author: :owner).exists?
end
on :fetch_entity_author_id_by_guid do |entity_type, guid|
entity_type.constantize.where(guid: guid).joins(:author).pluck(:diaspora_handle).first
end end
on :queue_public_receive do |xml| on :queue_public_receive do |xml|

View file

@ -128,6 +128,15 @@ module Diaspora
provider_display_name: status_message.provider_display_name provider_display_name: status_message.provider_display_name
) )
end end
def self.related_entity(entity)
DiasporaFederation::Entities::RelatedEntity.new(
author: entity.author.diaspora_handle,
local: entity.author.local?,
public: entity.respond_to?(:public?) && entity.public?,
parent: entity.respond_to?(:parent) ? related_entity(entity.parent) : nil
)
end
end end
end end
end end

View file

@ -178,55 +178,30 @@ describe "diaspora federation callbacks" do
let(:local_person) { FactoryGirl.create(:user).person } let(:local_person) { FactoryGirl.create(:user).person }
let(:remote_person) { FactoryGirl.create(:person) } let(:remote_person) { FactoryGirl.create(:person) }
let(:post_by_a_local_person) { FactoryGirl.create(:status_message, author: local_person) }
let(:post_by_a_remote_person) { FactoryGirl.create(:status_message, author: remote_person) }
describe ":fetch_private_key_by_diaspora_id" do describe ":fetch_private_key" do
it "returns a private key for a local user" do it "returns a private key for a local user" do
key = DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, local_person.diaspora_handle) key = DiasporaFederation.callbacks.trigger(:fetch_private_key, local_person.diaspora_handle)
expect(key).to be_a(OpenSSL::PKey::RSA) expect(key).to be_a(OpenSSL::PKey::RSA)
expect(key.to_s).to eq(local_person.owner.serialized_private_key) expect(key.to_s).to eq(local_person.owner.serialized_private_key)
end end
it "returns nil for a remote user" do it "returns nil for a remote user" do
expect( expect(
DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, remote_person.diaspora_handle) DiasporaFederation.callbacks.trigger(:fetch_private_key, remote_person.diaspora_handle)
).to be_nil ).to be_nil
end end
it "returns nil for an unknown id" do it "returns nil for an unknown id" do
expect( expect(
DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, FactoryGirl.generate(:diaspora_id)) DiasporaFederation.callbacks.trigger(:fetch_private_key, FactoryGirl.generate(:diaspora_id))
).to be_nil ).to be_nil
end end
end end
describe ":fetch_author_private_key_by_entity_guid" do describe ":fetch_public_key" do
it "returns a private key for a post by a local user" do
key = DiasporaFederation.callbacks.trigger(:fetch_author_private_key_by_entity_guid,
"Post", post_by_a_local_person.guid)
expect(key).to be_a(OpenSSL::PKey::RSA)
expect(key.to_s).to eq(post_by_a_local_person.author.owner.serialized_private_key)
end
it "returns nil for a post by a remote user" do
expect(
DiasporaFederation.callbacks.trigger(:fetch_author_private_key_by_entity_guid,
"Post", post_by_a_remote_person.guid)
).to be_nil
end
it "returns nil for an unknown post" do
expect(
DiasporaFederation.callbacks.trigger(:fetch_author_private_key_by_entity_guid,
"Post", FactoryGirl.generate(:guid))
).to be_nil
end
end
describe ":fetch_public_key_by_diaspora_id" do
it "returns a public key for a person" do it "returns a public key for a person" do
key = DiasporaFederation.callbacks.trigger(:fetch_public_key_by_diaspora_id, remote_person.diaspora_handle) key = DiasporaFederation.callbacks.trigger(:fetch_public_key, remote_person.diaspora_handle)
expect(key).to be_a(OpenSSL::PKey::RSA) expect(key).to be_a(OpenSSL::PKey::RSA)
expect(key.to_s).to eq(remote_person.serialized_public_key) expect(key.to_s).to eq(remote_person.serialized_public_key)
end end
@ -235,7 +210,7 @@ describe "diaspora federation callbacks" do
person = FactoryGirl.build(:person) person = FactoryGirl.build(:person)
expect(Person).to receive(:find_or_fetch_by_identifier).with(person.diaspora_handle).and_return(person) expect(Person).to receive(:find_or_fetch_by_identifier).with(person.diaspora_handle).and_return(person)
key = DiasporaFederation.callbacks.trigger(:fetch_public_key_by_diaspora_id, person.diaspora_handle) key = DiasporaFederation.callbacks.trigger(:fetch_public_key, person.diaspora_handle)
expect(key).to be_a(OpenSSL::PKey::RSA) expect(key).to be_a(OpenSSL::PKey::RSA)
expect(key.to_s).to eq(person.serialized_public_key) expect(key.to_s).to eq(person.serialized_public_key)
end end
@ -246,57 +221,64 @@ describe "diaspora federation callbacks" do
.and_raise(DiasporaFederation::Discovery::DiscoveryError) .and_raise(DiasporaFederation::Discovery::DiscoveryError)
expect { expect {
DiasporaFederation.callbacks.trigger(:fetch_public_key_by_diaspora_id, diaspora_id) DiasporaFederation.callbacks.trigger(:fetch_public_key, diaspora_id)
}.to raise_error DiasporaFederation::Discovery::DiscoveryError }.to raise_error DiasporaFederation::Discovery::DiscoveryError
end end
end end
describe ":fetch_author_public_key_by_entity_guid" do describe ":fetch_related_entity" do
it "returns a public key for a known post" do it "returns related entity for a existing local post" do
key = DiasporaFederation.callbacks.trigger(:fetch_author_public_key_by_entity_guid, post = FactoryGirl.create(:status_message, author: local_person)
"Post", post_by_a_remote_person.guid) entity = DiasporaFederation.callbacks.trigger(:fetch_related_entity, "Post", post.guid)
expect(key).to be_a(OpenSSL::PKey::RSA) expect(entity.author).to eq(post.diaspora_handle)
expect(key.to_s).to eq(post_by_a_remote_person.author.serialized_public_key) expect(entity.local).to be_truthy
expect(entity.public).to be_falsey
expect(entity.parent).to be_nil
end end
it "returns nil for an unknown post" do it "returns related entity for a existing remote post" do
expect( post = FactoryGirl.create(:status_message, author: remote_person)
DiasporaFederation.callbacks.trigger(:fetch_author_public_key_by_entity_guid, entity = DiasporaFederation.callbacks.trigger(:fetch_related_entity, "Post", post.guid)
"Post", FactoryGirl.generate(:guid)) expect(entity.author).to eq(post.diaspora_handle)
).to be_nil expect(entity.local).to be_falsey
end expect(entity.public).to be_falsey
end expect(entity.parent).to be_nil
describe ":entity_author_is_local?" do
it "returns true for a post by a local user" do
expect(
DiasporaFederation.callbacks.trigger(:entity_author_is_local?, "Post", post_by_a_local_person.guid)
).to be_truthy
end end
it "returns false for a post by a remote user" do it "returns related entity for a existing public post" do
expect( post = FactoryGirl.create(:status_message, author: local_person, public: true)
DiasporaFederation.callbacks.trigger(:entity_author_is_local?, "Post", post_by_a_remote_person.guid) entity = DiasporaFederation.callbacks.trigger(:fetch_related_entity, "Post", post.guid)
).to be_falsey expect(entity.author).to eq(post.diaspora_handle)
expect(entity.local).to be_truthy
expect(entity.public).to be_truthy
expect(entity.parent).to be_nil
end end
it "returns false for a unknown post" do it "returns related entity for a existing comment" do
expect( post = FactoryGirl.create(:status_message, author: local_person, public: true)
DiasporaFederation.callbacks.trigger(:entity_author_is_local?, "Post", FactoryGirl.generate(:diaspora_id)) comment = FactoryGirl.create(:comment, author: remote_person, parent: post)
).to be_falsey entity = DiasporaFederation.callbacks.trigger(:fetch_related_entity, "Comment", comment.guid)
expect(entity.author).to eq(comment.diaspora_handle)
expect(entity.local).to be_falsey
expect(entity.public).to be_truthy
expect(entity.parent.author).to eq(post.diaspora_handle)
expect(entity.parent.local).to be_truthy
expect(entity.parent.public).to be_truthy
expect(entity.parent.parent).to be_nil
end end
end
describe ":fetch_entity_author_id_by_guid" do it "returns related entity for a existing conversation" do
it "returns id for a existing guid" do conversation = FactoryGirl.create(:conversation, author: local_person)
expect( entity = DiasporaFederation.callbacks.trigger(:fetch_related_entity, "Conversation", conversation.guid)
DiasporaFederation.callbacks.trigger(:fetch_entity_author_id_by_guid, "Post", post_by_a_remote_person.guid) expect(entity.author).to eq(local_person.diaspora_handle)
).not_to eq(post_by_a_remote_person.author_id) expect(entity.local).to be_truthy
expect(entity.public).to be_falsey
expect(entity.parent).to be_nil
end end
it "returns nil for a non-existing guid" do it "returns nil for a non-existing guid" do
expect( expect(
DiasporaFederation.callbacks.trigger(:fetch_entity_author_id_by_guid, "Post", FactoryGirl.generate(:guid)) DiasporaFederation.callbacks.trigger(:fetch_related_entity, "Post", FactoryGirl.generate(:guid))
).to be_nil ).to be_nil
end end
end end