Merge branch 'instance-contact-account' into 'develop'

Add contact account to InstanceView

See merge request soapbox-pub/rebased!201
This commit is contained in:
marcin mikołajczak 2022-11-05 15:31:56 +00:00
commit fc93cce682
3 changed files with 38 additions and 3 deletions

View file

@ -566,6 +566,12 @@
"Cool instance"
]
},
%{
key: :contact_username,
type: :string,
description: "Instance owner username",
suggestions: ["admin"]
},
%{
key: :limit,
type: :integer,

View file

@ -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

View file

@ -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