Do some transmogrifying for the output.
[akkoma] / lib / pleroma / web / activity_pub / activity_pub.ex
index 7b85770b71b9f62d55740172f7c5384b21c47dfa..8e15fde4a810cf1373b92e255ceb271c1ee3103e 100644 (file)
@@ -1,10 +1,12 @@
 defmodule Pleroma.Web.ActivityPub.ActivityPub do
   alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
-  alias Pleroma.Web.OStatus
+  alias Pleroma.Web.ActivityPub.Transmogrifier
   import Ecto.Query
   import Pleroma.Web.ActivityPub.Utils
   require Logger
 
+  @httpoison Application.get_env(:pleroma, :httpoison)
+
   def get_recipients(data) do
     (data["to"] || []) ++ (data["cc"] || [])
   end
@@ -35,7 +37,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     end
   end
 
-  def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
+  def create(%{to: to, actor: actor, context: context, object: object} = params) do
+    additional = params[:additional] || %{}
+    local = !(params[:local] == false) # only accept false as false value
+    published = params[:published]
+
     with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
          {:ok, activity} <- insert(create_data, local),
          :ok <- maybe_federate(activity) do
@@ -134,12 +140,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   end
   defp restrict_tag(query, _), do: query
 
+  defp restrict_recipients(query, []), do: query
   defp restrict_recipients(query, recipients) do
-    Enum.reduce(recipients, query, fn (recipient, q) ->
-      map = %{ to: [recipient] }
-      from activity in q,
-      or_where: fragment(~s(? @> ?), activity.data, ^map)
-    end)
+    from activity in query,
+     where: fragment("? && ?", ^recipients, activity.recipients)
   end
 
   defp restrict_local(query, %{"local_only" => true}) do
@@ -221,15 +225,35 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     Repo.insert(%Object{data: data})
   end
 
-  def prepare_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
-    with {:ok, user} <- OStatus.find_or_make_user(data["actor"]) do
-      data
-    else
-      _e -> :error
+  def make_user_from_ap_id(ap_id) do
+    with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]),
+    {:ok, data} <- Poison.decode(body)
+      do
+      user_data = %{
+        ap_id: data["id"],
+        info: %{
+          "ap_enabled" => true,
+          "source_data" => data
+        },
+        nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}",
+        name: data["name"]
+      }
+
+      User.insert_or_update_user(user_data)
     end
   end
 
-  def prepare_incoming(_) do
-    :error
+  def publish(actor, activity) do
+    remote_users = Pleroma.Web.Salmon.remote_users(activity)
+    {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
+    Enum.each remote_users, fn(user) ->
+      if user.info["ap_enabled"] do
+        inbox = user.info["source_data"]["inbox"]
+        Logger.info("Federating #{activity.data["id"]} to #{inbox}")
+        host = URI.parse(inbox).host
+        signature = Pleroma.Web.HTTPSignatures.sign(actor, %{host: host})
+        @httpoison.post(inbox, Poison.encode!(data), [{"Content-Type", "application/activity+json"}, {"signature", signature}])
+      end
+    end
   end
 end