From 0c1eaf26341b1615689149f46c424d15fb098c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sat, 5 Nov 2022 13:05:14 +0100 Subject: [PATCH] Add contact account to InstanceView MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- config/description.exs | 6 ++++++ .../web/mastodon_api/views/instance_view.ex | 21 ++++++++++++++++++- .../controllers/instance_controller_test.exs | 14 +++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/config/description.exs b/config/description.exs index bdf57dd633..b038051063 100644 --- a/config/description.exs +++ b/config/description.exs @@ -566,6 +566,12 @@ "Cool instance" ] }, + %{ + key: :contact_username, + type: :string, + description: "Instance owner username", + suggestions: ["admin"] + }, %{ key: :limit, type: :integer, diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index f8852e9a45..7fe150ccb4 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -6,7 +6,9 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do use Pleroma.Web, :view alias Pleroma.Config + alias Pleroma.User alias Pleroma.Web.ActivityPub.MRF + alias Pleroma.Web.MastodonAPI @mastodon_api_level "2.7.2" @@ -31,6 +33,7 @@ def render("show.json", _) do registrations: Keyword.get(instance, :registrations_open), approval_required: Keyword.get(instance, :account_approval_required), configuration: configuration(), + contact_account: contact_account(Keyword.get(instance, :contact_username)), rules: render(__MODULE__, "rules.json"), # Extra (not present in Mastodon): max_toot_chars: Keyword.get(instance, :limit), @@ -74,7 +77,7 @@ def render("show2.json", _) do }, contact: %{ email: Keyword.get(instance, :email), - account: nil + account: contact_account(Keyword.get(instance, :contact_username)) }, rules: render(__MODULE__, "rules.json"), # Extra (not present in Mastodon): @@ -250,4 +253,20 @@ defp pleroma_configuration2(instance) do }) }) end + + defp contact_account(nil), do: nil + + defp contact_account("@" <> username) do + contact_account(username) + end + + defp contact_account(username) do + user = User.get_cached_by_nickname(username) + + if user do + MastodonAPI.AccountView.render("show.json", %{user: user, for: nil}) + else + nil + end + end end diff --git a/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs index 953e6008df..4db9b4da8f 100644 --- a/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs @@ -139,9 +139,19 @@ test "get oauth_consumer_strategies", %{conn: conn} do assert result["pleroma"]["oauth_consumer_strategies"] == ["keycloak"] end - test "get instance information v2", %{conn: conn} do - clear_config([:auth, :oauth_consumer_strategies], []) + test "get instance contact information", %{conn: conn} do + user = insert(:user, %{local: true}) + clear_config([:instance, :contact_username], user.nickname) + + conn = get(conn, "/api/v1/instance") + + assert result = json_response_and_validate_schema(conn, 200) + + assert result["contact_account"]["id"] == user.id + end + + test "get instance information v2", %{conn: conn} do assert get(conn, "/api/v2/instance") |> json_response_and_validate_schema(200) end