From 5c8f0c16714363f1a5967394728224277efefd32 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 18 Dec 2015 02:10:07 +0100 Subject: [PATCH] create queue callbacks and remove receive routes --- app/controllers/publics_controller.rb | 33 ----------- config/initializers/diaspora_federation.rb | 19 ++++++ config/routes.rb | 4 +- spec/controllers/publics_controller_spec.rb | 65 --------------------- spec/federation_callbacks_spec.rb | 35 +++++++++++ 5 files changed, 55 insertions(+), 101 deletions(-) diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb index cca482bbd2e..5d7d0e0eda5 100644 --- a/app/controllers/publics_controller.rb +++ b/app/controllers/publics_controller.rb @@ -7,7 +7,6 @@ class PublicsController < ApplicationController skip_before_action :set_header_data skip_before_action :set_grammatical_gender - before_action :check_for_xml, :only => [:receive, :receive_public] before_action :authenticate_user!, :only => [:index] respond_to :html @@ -18,36 +17,4 @@ class PublicsController < ApplicationController def hub render :text => params['hub.challenge'], :status => 202, :layout => false end - - def receive_public - logger.info "received a public message" - Workers::ReceiveUnencryptedSalmon.perform_async(CGI::unescape(params[:xml])) - render :nothing => true, :status => :ok - end - - def receive - person = Person.find_by_guid(params[:guid]) - - if person.nil? || person.owner_id.nil? - logger.error "Received post for nonexistent person #{params[:guid]}" - render :nothing => true, :status => 404 - return - end - - @user = person.owner - - logger.info "received a private message for user: #{@user.id}" - Workers::ReceiveEncryptedSalmon.perform_async(@user.id, CGI::unescape(params[:xml])) - - render :nothing => true, :status => 202 - end - - private - - def check_for_xml - if params[:xml].nil? - render :nothing => true, :status => 422 - return - end - end end diff --git a/config/initializers/diaspora_federation.rb b/config/initializers/diaspora_federation.rb index 3cbb9e1e1a4..58285b2922b 100644 --- a/config/initializers/diaspora_federation.rb +++ b/config/initializers/diaspora_federation.rb @@ -89,5 +89,24 @@ on :fetch_entity_author_id_by_guid do |entity_type, guid| entity_type.constantize.where(guid: guid).joins(:author).pluck(:diaspora_handle).first end + + on :queue_public_receive do |xml| + Workers::ReceiveUnencryptedSalmon.perform_async(xml) + end + + on :queue_private_receive do |guid, xml| + person = Person.find_by_guid(guid) + + if person.nil? || person.owner_id.nil? + false + else + Workers::ReceiveEncryptedSalmon.perform_async(person.owner.id, xml) + true + end + end + + on :save_entity_after_receive do + # TODO + end end end diff --git a/config/routes.rb b/config/routes.rb index fe9c1e1cc39..33e5847a187 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -186,9 +186,7 @@ # Federation controller :publics do - post 'receive/users/:guid' => :receive - post 'receive/public' => :receive_public - get 'hub' => :hub + get "hub" => :hub end diff --git a/spec/controllers/publics_controller_spec.rb b/spec/controllers/publics_controller_spec.rb index 5b105688bae..3f3d089ba5b 100644 --- a/spec/controllers/publics_controller_spec.rb +++ b/spec/controllers/publics_controller_spec.rb @@ -5,71 +5,6 @@ require 'spec_helper' describe PublicsController, :type => :controller do - let(:fixture_path) { Rails.root.join('spec', 'fixtures') } - before do - @user = alice - @person = FactoryGirl.create(:person) - end - - describe '#receive_public' do - it 'succeeds' do - post :receive_public, :xml => "" - expect(response).to be_success - end - - it 'returns a 422 if no xml is passed' do - post :receive_public - expect(response.code).to eq('422') - end - - it 'enqueues a ReceiveUnencryptedSalmon job' do - xml = "stuff" - expect(Workers::ReceiveUnencryptedSalmon).to receive(:perform_async).with(xml) - post :receive_public, :xml => xml - end - end - - describe '#receive' do - let(:xml) { "" } - - it 'succeeds' do - post :receive, "guid" => @user.person.guid.to_s, "xml" => xml - expect(response).to be_success - end - - it 'enqueues a receive job' do - expect(Workers::ReceiveEncryptedSalmon).to receive(:perform_async).with(@user.id, xml).once - post :receive, "guid" => @user.person.guid.to_s, "xml" => xml - end - - it 'unescapes the xml before sending it to receive_salmon' do - aspect = @user.aspects.create(:name => 'foo') - post1 = @user.post(:status_message, :text => 'moms', :to => [aspect.id]) - xml2 = post1.to_diaspora_xml - user2 = FactoryGirl.create(:user) - - salmon_factory = Salmon::EncryptedSlap.create_by_user_and_activity(@user, xml2) - enc_xml = salmon_factory.xml_for(user2.person) - - expect(Workers::ReceiveEncryptedSalmon).to receive(:perform_async).with(@user.id, enc_xml).once - post :receive, "guid" => @user.person.guid.to_s, "xml" => CGI::escape(enc_xml) - end - - it 'returns a 422 if no xml is passed' do - post :receive, "guid" => @person.guid.to_s - expect(response.code).to eq('422') - end - - it 'returns a 404 if no user is found' do - post :receive, "guid" => @person.guid.to_s, "xml" => xml - expect(response).to be_not_found - end - it 'returns a 404 if no person is found' do - post :receive, :guid => '2398rq3948yftn', :xml => xml - expect(response).to be_not_found - end - end - describe '#hub' do it 'succeeds' do get :hub diff --git a/spec/federation_callbacks_spec.rb b/spec/federation_callbacks_spec.rb index 7c5cd9640d1..dc763b05c0b 100644 --- a/spec/federation_callbacks_spec.rb +++ b/spec/federation_callbacks_spec.rb @@ -266,4 +266,39 @@ def create_post_by_a_remote_person ).to be_nil end end + + describe ":queue_public_receive" do + it "enqueues a ReceiveUnencryptedSalmon job" do + xml = "" + expect(Workers::ReceiveUnencryptedSalmon).to receive(:perform_async).with(xml) + + DiasporaFederation.callbacks.trigger(:queue_public_receive, xml) + end + end + + describe ":queue_private_receive" do + let(:xml) { "" } + + it "returns true if the user is found" do + result = DiasporaFederation.callbacks.trigger(:queue_private_receive, alice.person.guid, xml) + expect(result).to be_truthy + end + + it "enqueues a ReceiveEncryptedSalmon job" do + expect(Workers::ReceiveEncryptedSalmon).to receive(:perform_async).with(alice.id, xml) + + DiasporaFederation.callbacks.trigger(:queue_private_receive, alice.person.guid, xml) + end + + it "returns false if the no user is found" do + person = FactoryGirl.create(:person) + result = DiasporaFederation.callbacks.trigger(:queue_private_receive, person.guid, xml) + expect(result).to be_falsey + end + + it "returns false if the no person is found" do + result = DiasporaFederation.callbacks.trigger(:queue_private_receive, "2398rq3948yftn", xml) + expect(result).to be_falsey + end + end end