Transmogrifier: Handle basic notice creation.
authorlain <lain@soykaf.club>
Thu, 15 Feb 2018 19:00:06 +0000 (20:00 +0100)
committerlain <lain@soykaf.club>
Thu, 15 Feb 2018 19:00:06 +0000 (20:00 +0100)
lib/pleroma/web/activity_pub/transmogrifier.ex [new file with mode: 0644]
test/web/activity_pub/transmogrifier_test.exs [new file with mode: 0644]

diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex
new file mode 100644 (file)
index 0000000..3e302f5
--- /dev/null
@@ -0,0 +1,44 @@
+defmodule Pleroma.Web.ActivityPub.Transmogrifier do
+  @moduledoc """
+  A module to handle coding from internal to wire ActivityPub and back.
+  """
+  alias Pleroma.User
+  alias Pleroma.Web.ActivityPub.ActivityPub
+
+  @doc """
+  Modifies an incoming AP object (mastodon format) to our internal format.
+  """
+  def fix_object(object) do
+    object
+    |> Map.put("actor", object["attributedTo"])
+  end
+
+  # TODO: validate those with a Ecto scheme
+  # - tags
+  # - emoji
+  def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
+    with %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
+      object = fix_object(data["object"])
+      params = %{
+        to: data["to"],
+        object: object,
+        actor: user,
+        context: data["object"]["conversation"],
+        local: false,
+        published: data["published"],
+        additional: Map.take(data, [
+              "cc",
+              "id"
+            ])
+      }
+
+      ActivityPub.create(params)
+    else
+      _e -> :error
+    end
+  end
+
+  def prepare_incoming(_) do
+    :error
+  end
+end
diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs
new file mode 100644 (file)
index 0000000..2694293
--- /dev/null
@@ -0,0 +1,32 @@
+defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
+  use Pleroma.DataCase
+  alias Pleroma.Web.ActivityPub.Transmogrifier
+  alias Pleroma.Activity
+
+  describe "handle_incoming" do
+    test "it works for incoming notices" do
+      data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!
+
+      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
+      assert data["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
+      assert data["context"] == "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
+      assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
+      assert data["cc"] == [
+        "http://mastodon.example.org/users/admin/followers",
+        "http://localtesting.pleroma.lol/users/lain"
+      ]
+      assert data["actor"] == "http://mastodon.example.org/users/admin"
+
+      object = data["object"]
+      assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
+
+      assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
+      assert object["cc"] == [
+        "http://mastodon.example.org/users/admin/followers",
+        "http://localtesting.pleroma.lol/users/lain"
+      ]
+      assert object["actor"] == "http://mastodon.example.org/users/admin"
+      assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
+    end
+  end
+end