Move incoming AP to Federator.
[akkoma] / lib / pleroma / web / activity_pub / activity_pub.ex
index fb33f2e3ef2eddfc6e637fab797673acc3d0a443..cc20197912485fababc2a7383b80d1920b6b797f 100644 (file)
@@ -2,6 +2,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
   alias Pleroma.Web.ActivityPub.Transmogrifier
   alias Pleroma.Web.WebFinger
+  alias Pleroma.Web.Federator
   import Ecto.Query
   import Pleroma.Web.ActivityPub.Utils
   require Logger
@@ -272,7 +273,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
         name: data["name"]
       }
 
-      User.insert_or_update_user(user_data)
+      if user = User.get_by_ap_id(ap_id) do
+        User.info_changeset(user, user_data)
+        |> Repo.update
+      else
+        User.insert_or_update_user(user_data)
+      end
     end
   end
 
@@ -283,7 +289,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   end
 
   def publish(actor, activity) do
-    {:ok, followers} = User.get_followers(actor)
+    followers = if actor.follower_address in activity.recipients do
+      {:ok, followers} = User.get_followers(actor)
+      followers
+    else
+      []
+    end
 
     remote_inboxes = (Pleroma.Web.Salmon.remote_users(activity) ++ followers)
     |> Enum.filter(fn (user) -> User.ap_enabled?(user) end)
@@ -295,13 +306,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
     json = Poison.encode!(data)
     Enum.each remote_inboxes, fn(inbox) ->
-      Logger.info("Federating #{activity.data["id"]} to #{inbox}")
-      host = URI.parse(inbox).host
-      signature = Pleroma.Web.HTTPSignatures.sign(actor, %{host: host, "content-length": byte_size(json)})
-      @httpoison.post(inbox, json, [{"Content-Type", "application/activity+json"}, {"signature", signature}])
+      Federator.enqueue(:publish_single_ap, %{inbox: inbox, json: json, actor: actor, id: activity.data["id"]})
     end
   end
 
+  def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do
+    Logger.info("Federating #{id} to #{inbox}")
+    host = URI.parse(inbox).host
+    signature = Pleroma.Web.HTTPSignatures.sign(actor, %{host: host, "content-length": byte_size(json)})
+    @httpoison.post(inbox, json, [{"Content-Type", "application/activity+json"}, {"signature", signature}])
+  end
+
   # TODO:
   # This will create a Create activity, which we need internally at the moment.
   def fetch_object_from_id(id) do