diff --git a/app/controllers/diaspora_federation/receive_controller.rb b/app/controllers/diaspora_federation/receive_controller.rb
index 4a41ad2..db15008 100644
--- a/app/controllers/diaspora_federation/receive_controller.rb
+++ b/app/controllers/diaspora_federation/receive_controller.rb
@@ -11,8 +11,11 @@ module DiasporaFederation
# POST /receive/public
def public
logger.info "received a public message"
- logger.debug CGI.unescape(params[:xml])
- Receiver::Public.new(CGI.unescape(params[:xml])).receive!
+ xml = CGI.unescape(params[:xml])
+ logger.debug xml
+
+ DiasporaFederation.callbacks.trigger(:queue_public_receive, xml)
+
render nothing: true, status: :ok
end
@@ -21,13 +24,12 @@ module DiasporaFederation
# POST /receive/users/:guid
def private
logger.info "received a private message for #{params[:guid]}"
- logger.debug CGI.unescape(params[:xml])
- begin
- Receiver::Private.new(params[:guid], CGI.unescape(params[:xml])).receive!
- render nothing: true, status: :ok
- rescue RecipientNotFound
- render nothing: true, status: 404
- end
+ xml = CGI.unescape(params[:xml])
+ logger.debug xml
+
+ success = DiasporaFederation.callbacks.trigger(:queue_private_receive, params[:guid], xml)
+
+ render nothing: true, status: success ? :ok : 404
end
private
diff --git a/lib/diaspora_federation.rb b/lib/diaspora_federation.rb
index 73f9204..22b2d48 100644
--- a/lib/diaspora_federation.rb
+++ b/lib/diaspora_federation.rb
@@ -29,6 +29,8 @@ module DiasporaFederation
fetch_author_public_key_by_entity_guid
entity_author_is_local?
fetch_entity_author_id_by_guid
+ queue_public_receive
+ queue_private_receive
entity_persist
)
@@ -124,6 +126,16 @@ module DiasporaFederation
# @param [String] guid of the entity
# @return [String] Diaspora ID of the person
#
+ # queue_public_receive
+ # Queue a public salmon xml to process in background
+ # @param [String] xml salmon xml
+ #
+ # queue_private_receive
+ # Queue a private salmon xml to process in background
+ # @param [String] guid guid of the receiver person
+ # @param [String] xml salmon xml
+ # @return [Boolean] true if successful, false if the user was not found
+ #
# @see Callbacks#on
#
# @example
diff --git a/spec/controllers/diaspora_federation/receive_controller_spec.rb b/spec/controllers/diaspora_federation/receive_controller_spec.rb
index d1de84b..2ec50da 100644
--- a/spec/controllers/diaspora_federation/receive_controller_spec.rb
+++ b/spec/controllers/diaspora_federation/receive_controller_spec.rb
@@ -3,17 +3,14 @@ module DiasporaFederation
routes { DiasporaFederation::Engine.routes }
describe "POST #public" do
- it "raises on an empty object" do
- expect { post :public, xml: "" }.to raise_error(Salmon::MissingAuthor)
- end
-
it "returns a 422 if no xml is passed" do
post :public
expect(response.code).to eq("422")
end
- it "returns a 200 if receive! reports no errors" do
- expect_any_instance_of(Receiver::Public).to receive(:receive!)
+ it "returns a 200 if queued correctly" do
+ expect(DiasporaFederation.callbacks).to receive(:trigger)
+ .with(:queue_public_receive, "")
post :public, xml: ""
expect(response.code).to eq("200")
@@ -21,7 +18,11 @@ module DiasporaFederation
end
describe "POST #private" do
- it "return 404 for because of an unknown guid" do
+ it "return a 404 if not queued successfully (unknown user guid)" do
+ expect(DiasporaFederation.callbacks).to receive(:trigger)
+ .with(:queue_private_receive, "any-guid", "")
+ .and_return(false)
+
post :private, guid: "any-guid", xml: ""
expect(response.code).to eq("404")
end
@@ -33,10 +34,8 @@ module DiasporaFederation
it "returns a 200 if receive! reports no errors" do
expect(DiasporaFederation.callbacks).to receive(:trigger)
- .with(:fetch_private_key_by_user_guid, "any-guid")
- .once
+ .with(:queue_private_receive, "any-guid", "")
.and_return(true)
- expect_any_instance_of(Receiver::Private).to receive(:receive!)
post :private, guid: "any-guid", xml: ""
expect(response.code).to eq("200")
diff --git a/test/dummy/config/initializers/diaspora_federation.rb b/test/dummy/config/initializers/diaspora_federation.rb
index 39b315d..0a1ec1c 100644
--- a/test/dummy/config/initializers/diaspora_federation.rb
+++ b/test/dummy/config/initializers/diaspora_federation.rb
@@ -91,6 +91,13 @@ DiasporaFederation.configure do |config|
nil
end
+ on :queue_public_receive do
+ end
+
+ on :queue_private_receive do
+ true
+ end
+
on :entity_persist do
end
end