From b52672294e8cbc6f1ff53b9c7193e55781db84a6 Mon Sep 17 00:00:00 2001
From: lain <lain@soykaf.club>
Date: Wed, 21 Feb 2018 08:51:03 +0100
Subject: [PATCH] Move incoming AP to Federator.

---
 .../activity_pub/activity_pub_controller.ex   | 28 ++-----------------
 lib/pleroma/web/federator/federator.ex        | 24 ++++++++++++++++
 2 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
index 835e8bd9d..513758176 100644
--- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
@@ -3,6 +3,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
   alias Pleroma.{User, Repo, Object, Activity}
   alias Pleroma.Web.ActivityPub.{ObjectView, UserView, Transmogrifier}
   alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Web.Federator
 
   require Logger
 
@@ -24,22 +25,8 @@ def object(conn, %{"uuid" => uuid}) do
 
   # TODO: Ensure that this inbox is a recipient of the message
   def inbox(%{assigns: %{valid_signature: true}} = conn, params) do
-    # File.write("/tmp/incoming.json", Poison.encode!(params))
-    # Logger.info(Poison.encode!(params, [pretty: 2]))
-    with {:ok, _user} <- ap_enabled_actor(params["actor"]),
-         nil <- Activity.get_by_ap_id(params["id"]),
-         {:ok, activity} <- Transmogrifier.handle_incoming(params) do
-      json(conn, "ok")
-    else
-      %Activity{} ->
-        Logger.info("Already had #{params["id"]}")
-        json(conn, "ok")
-      e ->
-        # Just drop those for now
-        Logger.info("Unhandled activity")
-        Logger.info(Poison.encode!(params, [pretty: 2]))
-        json(conn, "ok")
-    end
+    Federator.enqeue(:incoming_ap_doc, params)
+    json(conn, "ok")
   end
 
   def inbox(conn, params) do
@@ -48,15 +35,6 @@ def inbox(conn, params) do
     json(conn, "ok")
   end
 
-  def ap_enabled_actor(id) do
-    user = User.get_by_ap_id(id)
-    if User.ap_enabled?(user) do
-      {:ok, user}
-    else
-      ActivityPub.make_user_from_ap_id(id)
-    end
-  end
-
   def errors(conn, _e) do
     conn
     |> put_status(500)
diff --git a/lib/pleroma/web/federator/federator.ex b/lib/pleroma/web/federator/federator.ex
index b64b43c56..9f81264e4 100644
--- a/lib/pleroma/web/federator/federator.ex
+++ b/lib/pleroma/web/federator/federator.ex
@@ -3,6 +3,7 @@ defmodule Pleroma.Web.Federator do
   alias Pleroma.User
   alias Pleroma.Web.{WebFinger, Websub}
   alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Web.ActivityPub.Transmogrifier
   require Logger
 
   @websub Application.get_env(:pleroma, :websub)
@@ -66,6 +67,20 @@ def handle(:incoming_doc, doc) do
     @ostatus.handle_incoming(doc)
   end
 
+  def handle(:incoming_ap_doc, params) do
+    with {:ok, _user} <- ap_enabled_actor(params["actor"]),
+         nil <- Activity.get_by_ap_id(params["id"]),
+         {:ok, activity} <- Transmogrifier.handle_incoming(params) do
+    else
+      %Activity{} ->
+        Logger.info("Already had #{params["id"]}")
+      e ->
+        # Just drop those for now
+        Logger.info("Unhandled activity")
+        Logger.info(Poison.encode!(params, [pretty: 2]))
+    end
+  end
+
   def handle(:publish_single_ap, params) do
     ActivityPub.publish_one(params)
   end
@@ -145,4 +160,13 @@ def enqueue_sorted(queue, element, priority) do
   def queue_pop([%{item: element} | queue]) do
     {element, queue}
   end
+
+  def ap_enabled_actor(id) do
+    user = User.get_by_ap_id(id)
+    if User.ap_enabled?(user) do
+      {:ok, user}
+    else
+      ActivityPub.make_user_from_ap_id(id)
+    end
+  end
 end