From da005d333296c626e29216f800e5ffd92e93dab0 Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Mon, 11 Dec 2017 18:19:46 +0100 Subject: [PATCH] ActivityPub: Add Objects View. --- .../web/activity_pub/views/object_view.ex | 26 +++++++++++++++++++ .../activity_pub/views/object_view_test.exs | 17 ++++++++++++ .../web/activity_pub/views/user_view_test.exs | 18 +++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 lib/pleroma/web/activity_pub/views/object_view.ex create mode 100644 test/web/activity_pub/views/object_view_test.exs create mode 100644 test/web/activity_pub/views/user_view_test.exs 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 index 000000000..403f8cb17 --- /dev/null +++ b/lib/pleroma/web/activity_pub/views/object_view.ex @@ -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 index 000000000..6a1311be7 --- /dev/null +++ b/test/web/activity_pub/views/object_view_test.exs @@ -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 index 000000000..0c64e62c3 --- /dev/null +++ b/test/web/activity_pub/views/user_view_test.exs @@ -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