ActivityPub: Add Objects View.
authorLain Iwakura <lain@soykaf.club>
Mon, 11 Dec 2017 17:19:46 +0000 (18:19 +0100)
committerLain Iwakura <lain@soykaf.club>
Mon, 11 Dec 2017 17:19:46 +0000 (18:19 +0100)
lib/pleroma/web/activity_pub/views/object_view.ex [new file with mode: 0644]
test/web/activity_pub/views/object_view_test.exs [new file with mode: 0644]
test/web/activity_pub/views/user_view_test.exs [new file with mode: 0644]

diff --git a/lib/pleroma/web/activity_pub/views/object_view.ex b/lib/pleroma/web/activity_pub/views/object_view.ex
new file mode 100644 (file)
index 0000000..403f8cb
--- /dev/null
@@ -0,0 +1,26 @@
+defmodule Pleroma.Web.ActivityPub.ObjectView do
+  use Pleroma.Web, :view
+
+  def render("object.json", %{object: object}) do
+    base = %{
+      "@context" => [
+        "https://www.w3.org/ns/activitystreams",
+        "https://w3id.org/security/v1",
+        %{
+          "manuallyApprovesFollowers" => "as:manuallyApprovesFollowers",
+          "sensitive" => "as:sensitive",
+          "Hashtag" => "as:Hashtag",
+          "ostatus" => "http://ostatus.org#",
+          "atomUri" => "ostatus:atomUri",
+          "inReplyToAtomUri" => "ostatus:inReplyToAtomUri",
+          "conversation" => "ostatus:conversation",
+          "toot" => "http://joinmastodon.org/ns#",
+          "Emoji" => "toot:Emoji"
+        }
+      ]
+    }
+
+    additional = Map.take(object.data, ["id", "to", "cc", "actor", "content", "summary", "type"])
+    Map.merge(base, additional)
+  end
+end
diff --git a/test/web/activity_pub/views/object_view_test.exs b/test/web/activity_pub/views/object_view_test.exs
new file mode 100644 (file)
index 0000000..6a1311b
--- /dev/null
@@ -0,0 +1,17 @@
+defmodule Pleroma.Web.ActivityPub.ObjectViewTest do
+  use Pleroma.DataCase
+  import Pleroma.Factory
+
+  alias Pleroma.Web.ActivityPub.ObjectView
+
+  test "renders a note object" do
+    note = insert(:note)
+
+    result = ObjectView.render("object.json", %{object: note})
+
+    assert result["id"] == note.data["id"]
+    assert result["to"] == note.data["to"]
+    assert result["content"] == note.data["content"]
+    assert result["type"] == "Note"
+  end
+end
diff --git a/test/web/activity_pub/views/user_view_test.exs b/test/web/activity_pub/views/user_view_test.exs
new file mode 100644 (file)
index 0000000..0c64e62
--- /dev/null
@@ -0,0 +1,18 @@
+defmodule Pleroma.Web.ActivityPub.UserViewTest do
+  use Pleroma.DataCase
+  import Pleroma.Factory
+
+  alias Pleroma.Web.ActivityPub.UserView
+
+  test "Renders a user, including the public key" do
+    user = insert(:user)
+    {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
+
+    result = UserView.render("user.json", %{user: user})
+
+    assert result["id"] == user.ap_id
+    assert result["preferredUsername"] == user.nickname
+
+    assert String.contains?(result["publicKey"]["publicKeyPem"], "BEGIN RSA PUBLIC KEY")
+  end
+end