From c4ad63d10671c05625e8e1af29eedf20b21d5e49 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sat, 26 Mar 2016 00:19:30 +0100 Subject: [PATCH 1/4] bump diaspora_federation and bump typhoeus --- Gemfile | 6 +++--- Gemfile.lock | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index a5716e277..e12c3319a 100644 --- a/Gemfile +++ b/Gemfile @@ -12,7 +12,7 @@ gem "unicorn", "5.0.1", require: false # Federation -gem "diaspora_federation-rails", "0.0.12" +gem "diaspora_federation-rails", "0.0.13" # API and JSON @@ -160,7 +160,7 @@ gem "addressable", "2.3.8", require: "addressable/uri" gem "faraday", "0.9.2" gem "faraday_middleware", "0.10.0" gem "faraday-cookie_jar", "0.0.6" -gem "typhoeus", "0.8.0" +gem "typhoeus", "1.0.1" # Views @@ -272,7 +272,7 @@ group :test do gem "webmock", "1.22.6", require: false gem "shoulda-matchers", "3.1.1" - gem "diaspora_federation-test", "0.0.12" + gem "diaspora_federation-test", "0.0.13" end group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index 6f422d1ff..b1f8b8311 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -155,17 +155,17 @@ GEM eventmachine (~> 1.0.8) http_parser.rb (~> 0.6) nokogiri (~> 1.6) - diaspora_federation (0.0.12) + diaspora_federation (0.0.13) faraday (~> 0.9.0) faraday_middleware (~> 0.10.0) - nokogiri (~> 1.6, >= 1.6.7.1) - typhoeus (~> 0.7) + nokogiri (~> 1.6, >= 1.6.7.2) + typhoeus (~> 1.0) valid (~> 1.0) - diaspora_federation-rails (0.0.12) - diaspora_federation (= 0.0.12) + diaspora_federation-rails (0.0.13) + diaspora_federation (= 0.0.13) rails (~> 4.2) - diaspora_federation-test (0.0.12) - diaspora_federation (= 0.0.12) + diaspora_federation-test (0.0.13) + diaspora_federation (= 0.0.13) factory_girl (~> 4.5, >= 4.5.0) diff-lcs (1.2.5) docile (1.1.5) @@ -745,7 +745,7 @@ GEM simple_oauth (~> 0.3.0) twitter-text (1.13.3) unf (~> 0.1.0) - typhoeus (0.8.0) + typhoeus (1.0.1) ethon (>= 0.8.0) tzinfo (1.2.2) thread_safe (~> 0.1) @@ -797,8 +797,8 @@ DEPENDENCIES devise-token_authenticatable (~> 0.4.0) devise_lastseenable (= 0.0.6) diaspora-vines (~> 0.2.0.develop) - diaspora_federation-rails (= 0.0.12) - diaspora_federation-test (= 0.0.12) + diaspora_federation-rails (= 0.0.13) + diaspora_federation-test (= 0.0.13) entypo-rails (= 2.2.3) eye (= 0.7) facebox-rails (= 0.2.0) @@ -900,7 +900,7 @@ DEPENDENCIES turbo_dev_assets (= 0.0.2) twitter (= 5.16.0) twitter-text (= 1.13.3) - typhoeus (= 0.8.0) + typhoeus (= 1.0.1) uglifier (= 2.7.2) unicorn (= 5.0.1) uuid (= 2.3.8) From a3ed9da2109adc229d0a0b4498e3cd0f483a44fd Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sat, 16 Jan 2016 00:14:01 +0100 Subject: [PATCH 2/4] changes for new federation-gem version --- config/initializers/diaspora_federation.rb | 6 +++++- spec/integration/federation/federation_helper.rb | 12 +++++------- .../federation/receive_federation_messages_spec.rb | 14 +++++++------- .../federation/shared_receive_retraction.rb | 2 +- .../federation/shared_receive_stream_items.rb | 4 ++-- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/config/initializers/diaspora_federation.rb b/config/initializers/diaspora_federation.rb index 4d63aebd2..27d458917 100644 --- a/config/initializers/diaspora_federation.rb +++ b/config/initializers/diaspora_federation.rb @@ -106,7 +106,11 @@ DiasporaFederation.configure do |config| end end - on :save_entity_after_receive do + on :receive_entity do + # TODO + end + + on :update_pod do # TODO end end diff --git a/spec/integration/federation/federation_helper.rb b/spec/integration/federation/federation_helper.rb index dd3609b71..f9340517b 100644 --- a/spec/integration/federation/federation_helper.rb +++ b/spec/integration/federation/federation_helper.rb @@ -20,11 +20,10 @@ def create_remote_user(pod) end def create_relayable_entity(entity_name, target, diaspora_id, parent_author_key) - target_entity_type = FactoryGirl.factory_by_name(entity_name).build_class.get_target_entity_type(@entity.to_h) expect(DiasporaFederation.callbacks).to receive(:trigger) .with( :fetch_author_private_key_by_entity_guid, - target_entity_type, + FactoryGirl.build(entity_name).parent_type, target.guid ) .and_return(parent_author_key) @@ -33,19 +32,18 @@ def create_relayable_entity(entity_name, target, diaspora_id, parent_author_key) entity_name, conversation_guid: target.guid, parent_guid: target.guid, - diaspora_id: diaspora_id, + author: diaspora_id, poll_answer_guid: target.respond_to?(:poll_answers) ? target.poll_answers.first.guid : nil ) end def generate_xml(entity, remote_user, recipient=nil) if recipient - DiasporaFederation::Salmon::EncryptedSlap.generate_xml( + DiasporaFederation::Salmon::EncryptedSlap.prepare( remote_user.diaspora_handle, OpenSSL::PKey::RSA.new(remote_user.encryption_key), - entity, - OpenSSL::PKey::RSA.new(recipient.encryption_key) - ) + entity + ).generate_xml(OpenSSL::PKey::RSA.new(recipient.encryption_key)) else DiasporaFederation::Salmon::Slap.generate_xml( remote_user.diaspora_handle, diff --git a/spec/integration/federation/receive_federation_messages_spec.rb b/spec/integration/federation/receive_federation_messages_spec.rb index 2c24a5aa2..3c5bc1605 100644 --- a/spec/integration/federation/receive_federation_messages_spec.rb +++ b/spec/integration/federation/receive_federation_messages_spec.rb @@ -36,7 +36,7 @@ describe "Receive federation messages feature" do it "reshare of public post passes" do post = FactoryGirl.create(:status_message, author: alice.person, public: true) reshare = FactoryGirl.build( - :reshare_entity, root_diaspora_id: alice.diaspora_handle, root_guid: post.guid, diaspora_id: sender_id) + :reshare_entity, root_author: alice.diaspora_handle, root_guid: post.guid, author: sender_id) post_message(generate_xml(reshare, sender)) expect(Reshare.exists?(root_guid: post.guid, diaspora_handle: sender_id)).to be_truthy @@ -45,7 +45,7 @@ describe "Receive federation messages feature" do it "reshare of private post fails" do post = FactoryGirl.create(:status_message, author: alice.person, public: false) reshare = FactoryGirl.build( - :reshare_entity, root_diaspora_id: alice.diaspora_handle, root_guid: post.guid, diaspora_id: sender_id) + :reshare_entity, root_author: alice.diaspora_handle, root_guid: post.guid, author: sender_id) expect { post_message(generate_xml(reshare, sender)) }.to raise_error ActiveRecord::RecordInvalid, "Validation failed: Only posts which are public may be reshared." @@ -72,7 +72,7 @@ describe "Receive federation messages feature" do let(:recipient) { alice } it "treats sharing request recive correctly" do - entity = FactoryGirl.build(:request_entity, recipient_id: alice.diaspora_handle) + entity = FactoryGirl.build(:request_entity, recipient: alice.diaspora_handle) expect(Diaspora::Fetcher::Public).to receive(:queue_for).exactly(1).times @@ -94,7 +94,7 @@ describe "Receive federation messages feature" do end it "doesn't save the private status message if there is no sharing" do - entity = FactoryGirl.build(:status_message_entity, diaspora_id: sender_id, public: false) + entity = FactoryGirl.build(:status_message_entity, author: sender_id, public: false) post_message(generate_xml(entity, sender, alice), alice) expect(StatusMessage.exists?(guid: entity.guid)).to be_falsey @@ -111,7 +111,7 @@ describe "Receive federation messages feature" do it_behaves_like "messages which can't be send without sharing" it "treats profile receive correctly" do - entity = FactoryGirl.build(:profile_entity, diaspora_id: sender_id) + entity = FactoryGirl.build(:profile_entity, author: sender_id) post_message(generate_xml(entity, sender, alice), alice) expect(Profile.exists?(diaspora_handle: entity.diaspora_id)).to be_truthy @@ -120,8 +120,8 @@ describe "Receive federation messages feature" do it "receives conversation correctly" do entity = FactoryGirl.build( :conversation_entity, - diaspora_id: sender_id, - participant_ids: "#{sender_id};#{alice.diaspora_handle}" + author: sender_id, + participants: "#{sender_id};#{alice.diaspora_handle}" ) post_message(generate_xml(entity, sender, alice), alice) diff --git a/spec/integration/federation/shared_receive_retraction.rb b/spec/integration/federation/shared_receive_retraction.rb index c9182e539..e4e2612a7 100644 --- a/spec/integration/federation/shared_receive_retraction.rb +++ b/spec/integration/federation/shared_receive_retraction.rb @@ -9,7 +9,7 @@ def retraction_entity(entity_name, target_object, sender) FactoryGirl.build( entity_name, - diaspora_id: sender.diaspora_handle, + author: sender.diaspora_handle, target_guid: target_object.guid, target_type: target_object.class.to_s ) diff --git a/spec/integration/federation/shared_receive_stream_items.rb b/spec/integration/federation/shared_receive_stream_items.rb index 93e876481..6d7456836 100644 --- a/spec/integration/federation/shared_receive_stream_items.rb +++ b/spec/integration/federation/shared_receive_stream_items.rb @@ -4,7 +4,7 @@ shared_examples_for "messages which are indifferent about sharing fact" do let(:public) { recipient.nil? } it "treats status message receive correctly" do - entity = FactoryGirl.build(:status_message_entity, diaspora_id: sender_id, public: public) + entity = FactoryGirl.build(:status_message_entity, author: sender_id, public: public) post_message(generate_xml(entity, sender, recipient), recipient) @@ -13,7 +13,7 @@ shared_examples_for "messages which are indifferent about sharing fact" do it "doesn't accept status message with wrong signature" do allow(sender).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024)) - entity = FactoryGirl.build(:status_message_entity, diaspora_id: sender_id, public: public) + entity = FactoryGirl.build(:status_message_entity, author: sender_id, public: public) post_message(generate_xml(entity, sender, recipient), recipient) From 625eedf05d3ac479c4e54a0ffe4d2e1712db7e6a Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 25 Mar 2016 19:30:36 +0100 Subject: [PATCH 3/4] add "diaspora to federation entity" methods for posts --- lib/diaspora.rb | 19 +++++----- lib/diaspora/federation.rb | 78 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 lib/diaspora/federation.rb diff --git a/lib/diaspora.rb b/lib/diaspora.rb index 6f6d2002c..2cf2579c2 100644 --- a/lib/diaspora.rb +++ b/lib/diaspora.rb @@ -3,13 +3,14 @@ # the COPYRIGHT file. module Diaspora - require 'diaspora/camo' - require 'diaspora/exceptions' - require 'diaspora/exporter' - require 'diaspora/federated' - require 'diaspora/fetcher' - require 'diaspora/markdownify' - require 'diaspora/mentionable' - require 'diaspora/message_renderer' - require 'diaspora/parser' + require "diaspora/camo" + require "diaspora/exceptions" + require "diaspora/exporter" + require "diaspora/federated" + require "diaspora/federation" + require "diaspora/fetcher" + require "diaspora/markdownify" + require "diaspora/mentionable" + require "diaspora/message_renderer" + require "diaspora/parser" end diff --git a/lib/diaspora/federation.rb b/lib/diaspora/federation.rb new file mode 100644 index 000000000..710e3349e --- /dev/null +++ b/lib/diaspora/federation.rb @@ -0,0 +1,78 @@ +module Diaspora + module Federation + def self.post(post) + case post + when StatusMessage + status_message(post) + when Reshare + reshare(post) + else + raise ArgumentError, "unknown post-class: #{post.class}" + end + end + + def self.location(location) + DiasporaFederation::Entities::Location.new( + address: location.address, + lat: location.lat, + lng: location.lng + ) + end + + def self.photo(photo) + DiasporaFederation::Entities::Photo.new( + author: photo.diaspora_handle, + guid: photo.guid, + public: photo.public, + created_at: photo.created_at, + remote_photo_path: photo.remote_photo_path, + remote_photo_name: photo.remote_photo_name, + text: photo.text, + status_message_guid: photo.status_message_guid, + height: photo.height, + width: photo.width + ) + end + + def self.poll(poll) + DiasporaFederation::Entities::Poll.new( + guid: poll.guid, + question: poll.question, + poll_answers: poll.poll_answers.map {|answer| poll_answer(answer) } + ) + end + + def self.poll_answer(poll_answer) + DiasporaFederation::Entities::PollAnswer.new( + guid: poll_answer.guid, + answer: poll_answer.answer + ) + end + + def self.reshare(reshare) + DiasporaFederation::Entities::Reshare.new( + root_author: reshare.root_diaspora_id, + root_guid: reshare.root_guid, + author: reshare.diaspora_handle, + guid: reshare.guid, + public: reshare.public, + created_at: reshare.created_at, + provider_display_name: reshare.provider_display_name + ) + end + + def self.status_message(status_message) + DiasporaFederation::Entities::StatusMessage.new( + author: status_message.diaspora_handle, + guid: status_message.guid, + raw_message: status_message.raw_message, + photos: status_message.photos.map {|photo| photo(photo) }, + location: status_message.location ? location(status_message.location) : nil, + poll: status_message.poll ? poll(status_message.poll) : nil, + public: status_message.public, + created_at: status_message.created_at, + provider_display_name: status_message.provider_display_name + ) + end + end +end From 69f08a4a7eb09ee7f12398ff201ec5abbd0dd425 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Tue, 15 Mar 2016 02:57:19 +0100 Subject: [PATCH 4/4] add fetch callbacks --- config/initializers/diaspora_federation.rb | 9 ++++ spec/federation_callbacks_spec.rb | 53 ++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/config/initializers/diaspora_federation.rb b/config/initializers/diaspora_federation.rb index 27d458917..19a01937b 100644 --- a/config/initializers/diaspora_federation.rb +++ b/config/initializers/diaspora_federation.rb @@ -110,6 +110,15 @@ DiasporaFederation.configure do |config| # TODO end + on :fetch_public_entity do |entity_type, guid| + entity = entity_type.constantize.find_by(guid: guid, public: true) + Diaspora::Federation.post(entity) if entity.is_a? Post + end + + on :fetch_person_url_to do |diaspora_id, path| + Person.find_by(diaspora_handle: diaspora_id).send(:url_to, path) + end + on :update_pod do # TODO end diff --git a/spec/federation_callbacks_spec.rb b/spec/federation_callbacks_spec.rb index a775d5ecc..febfcfc68 100644 --- a/spec/federation_callbacks_spec.rb +++ b/spec/federation_callbacks_spec.rb @@ -296,4 +296,57 @@ describe "diaspora federation callbacks" do expect(result).to be_falsey end end + + describe ":fetch_public_entity" do + it "fetches a Post" do + post = FactoryGirl.create(:status_message, author: alice.person, public: true) + entity = DiasporaFederation.callbacks.trigger(:fetch_public_entity, "Post", post.guid) + + expect(entity.guid).to eq(post.guid) + expect(entity.author).to eq(alice.diaspora_handle) + expect(entity.public).to be_truthy + end + + it "fetches a StatusMessage" do + post = FactoryGirl.create(:status_message, author: alice.person, public: true) + entity = DiasporaFederation.callbacks.trigger(:fetch_public_entity, "StatusMessage", post.guid) + + expect(entity.guid).to eq(post.guid) + expect(entity.author).to eq(alice.diaspora_handle) + expect(entity.public).to be_truthy + end + + it "fetches a Reshare" do + post = FactoryGirl.create(:reshare, author: alice.person, public: true) + entity = DiasporaFederation.callbacks.trigger(:fetch_public_entity, "Reshare", post.guid) + + expect(entity.guid).to eq(post.guid) + expect(entity.author).to eq(alice.diaspora_handle) + expect(entity.public).to be_truthy + end + + it "does not fetch a private post" do + post = FactoryGirl.create(:status_message, author: alice.person, public: false) + + expect( + DiasporaFederation.callbacks.trigger(:fetch_public_entity, "StatusMessage", post.guid) + ).to be_nil + end + + it "returns nil, if the post is unknown" do + expect( + DiasporaFederation.callbacks.trigger(:fetch_public_entity, "Post", "unknown-guid") + ).to be_nil + end + end + + describe ":fetch_person_url_to" do + it "returns the url with with the pod of the person" do + person = FactoryGirl.create(:person, url: "https://example.org") + + expect( + DiasporaFederation.callbacks.trigger(:fetch_person_url_to, person.diaspora_handle, "/path/on/pod") + ).to eq("https://example.org/path/on/pod") + end + end end