mastodon_api: Add /api/v1/preferences endpoint

Implements the preferences endpoint in the Mastodon API, but returns
default values for most of the preferences right now. The only supported
preference we can access is default post visibility, and a relevant test
is added as well.
This commit is contained in:
Joshua Goins 2023-08-12 08:05:48 -04:00
parent 7bd4ae5412
commit c22ecac567
5 changed files with 61 additions and 1 deletions

View file

@ -451,6 +451,20 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
}
end
def preferences_operation do
%Operation{
tags: ["Account Preferences"],
description: "Preferences defined by the user in their account settings.",
summary: "Preferred common behaviors to be shared across clients.",
operationId: "AccountController.preferences",
security: [%{"oAuth" => ["read:accounts"]}],
responses: %{
200 => Operation.response("Preferences", "application/json", Account),
401 => Operation.response("Error", "application/json", ApiError)
}
}
end
def identity_proofs_operation do
%Operation{
tags: ["Retrieve account information"],

View file

@ -51,7 +51,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
plug(
OAuthScopesPlug,
%{scopes: ["read:accounts"]}
when action in [:verify_credentials, :endorsements, :identity_proofs]
when action in [:verify_credentials, :endorsements, :identity_proofs, :preferences]
)
plug(
@ -544,4 +544,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
@doc "GET /api/v1/identity_proofs"
def identity_proofs(conn, params), do: MastodonAPIController.empty_array(conn, params)
@doc "GET /api/v1/preferences"
def preferences(%{assigns: %{user: user}} = conn, params) do
render(conn, "preferences.json", user: user)
end
end

View file

@ -315,6 +315,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|> maybe_put_email_address(user, opts[:for])
end
def render("preferences.json", %{user: user} = opts) do
# TODO: Do we expose more settings that make sense to plug in here?
%{
"posting:default:visibility": user.default_scope,
"posting:default:sensitive": false,
"posting:default:language": nil,
"reading:expand:media": "default",
"reading:expand:spoilers": false
}
end
defp username_from_nickname(string) when is_binary(string) do
hd(String.split(string, "@"))
end

View file

@ -629,6 +629,8 @@ defmodule Pleroma.Web.Router do
post("/tags/:id/follow", TagController, :follow)
post("/tags/:id/unfollow", TagController, :unfollow)
get("/followed_tags", TagController, :show_followed)
get("/preferences", AccountController, :preferences)
end
scope "/api/web", Pleroma.Web do

View file

@ -2060,4 +2060,32 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
end
end
describe "preferences" do
test "get account preferences" do
user = insert(:user, default_scope: "public")
%{conn: conn} = oauth_access(["read:accounts"], user: user)
conn = get(conn, "/api/v1/preferences")
response = json_response_and_validate_schema(conn, 200)
assert %{
"posting:default:language" => nil,
"posting:default:sensitive" => false,
"posting:default:visibility" => "public",
"reading:expand:media" => "default",
"reading:expand:spoilers" => false
} = response
end
test "test changing account preferences" do
user = insert(:user, default_scope: "unlisted")
%{conn: conn} = oauth_access(["read:accounts"], user: user)
conn = get(conn, "/api/v1/preferences")
response = json_response_and_validate_schema(conn, 200)
assert response["posting:default:visibility"] == "unlisted"
end
end
end