From 208baf157ad0c8be470566d5d51d0214c229e6a5 Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 6 Jul 2020 11:38:40 +0200 Subject: [PATCH] ActivityPub: Add new 'capabilities' to user. --- lib/pleroma/web/activity_pub/activity_pub.ex | 3 ++- lib/pleroma/web/activity_pub/views/user_view.ex | 6 +++--- priv/static/schemas/litepub-0.1.jsonld | 2 +- test/fixtures/tesla_mock/admin@mastdon.example.org.json | 4 +++- test/web/activity_pub/views/user_view_test.exs | 13 ++++++++++--- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 86428b861..17c9d8f21 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1224,7 +1224,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end) locked = data["manuallyApprovesFollowers"] || false - accepts_chat_messages = data["acceptsChatMessages"] + capabilities = data["capabilities"] || %{} + accepts_chat_messages = capabilities["acceptsChatMessages"] data = Transmogrifier.maybe_fix_user_object(data) discoverable = data["discoverable"] || false invisible = data["invisible"] || false diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index d062d6230..3a4564912 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -81,7 +81,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do fields = Enum.map(user.fields, &Map.put(&1, "type", "PropertyValue")) - chat_message_acceptance = + capabilities = if is_boolean(user.accepts_chat_messages) do %{ "acceptsChatMessages" => user.accepts_chat_messages @@ -110,9 +110,9 @@ defmodule Pleroma.Web.ActivityPub.UserView do "endpoints" => endpoints, "attachment" => fields, "tag" => emoji_tags, - "discoverable" => user.discoverable + "discoverable" => user.discoverable, + "capabilities" => capabilities } - |> Map.merge(chat_message_acceptance) |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user)) |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user)) |> Map.merge(Utils.make_json_ld_header()) diff --git a/priv/static/schemas/litepub-0.1.jsonld b/priv/static/schemas/litepub-0.1.jsonld index c1bcad0f8..e7722cf72 100644 --- a/priv/static/schemas/litepub-0.1.jsonld +++ b/priv/static/schemas/litepub-0.1.jsonld @@ -13,7 +13,7 @@ }, "discoverable": "toot:discoverable", "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", - "acceptsChatMessages": "litepub:acceptsChatMessages", + "capabilities": "litepub:capabilities", "ostatus": "http://ostatus.org#", "schema": "http://schema.org#", "toot": "http://joinmastodon.org/ns#", diff --git a/test/fixtures/tesla_mock/admin@mastdon.example.org.json b/test/fixtures/tesla_mock/admin@mastdon.example.org.json index f5cf174be..a911b979a 100644 --- a/test/fixtures/tesla_mock/admin@mastdon.example.org.json +++ b/test/fixtures/tesla_mock/admin@mastdon.example.org.json @@ -26,7 +26,9 @@ "summary": "\u003cp\u003e\u003c/p\u003e", "url": "http://mastodon.example.org/@admin", "manuallyApprovesFollowers": false, - "acceptsChatMessages": true, + "capabilities": { + "acceptsChatMessages": true + }, "publicKey": { "id": "http://mastodon.example.org/users/admin#main-key", "owner": "http://mastodon.example.org/users/admin", diff --git a/test/web/activity_pub/views/user_view_test.exs b/test/web/activity_pub/views/user_view_test.exs index 3b4a1bcde..98c7c9d09 100644 --- a/test/web/activity_pub/views/user_view_test.exs +++ b/test/web/activity_pub/views/user_view_test.exs @@ -165,9 +165,16 @@ defmodule Pleroma.Web.ActivityPub.UserViewTest do false_user = insert(:user, accepts_chat_messages: false) nil_user = insert(:user, accepts_chat_messages: nil) - assert %{"acceptsChatMessages" => true} = UserView.render("user.json", user: true_user) - assert %{"acceptsChatMessages" => false} = UserView.render("user.json", user: false_user) - refute Map.has_key?(UserView.render("user.json", user: nil_user), "acceptsChatMessages") + assert %{"capabilities" => %{"acceptsChatMessages" => true}} = + UserView.render("user.json", user: true_user) + + assert %{"capabilities" => %{"acceptsChatMessages" => false}} = + UserView.render("user.json", user: false_user) + + refute Map.has_key?( + UserView.render("user.json", user: nil_user)["capabilities"], + "acceptsChatMessages" + ) end end end