akkoma/lib/pleroma/web/akkoma_api/controllers/frontend_settings_controller.ex

81 lines
2.3 KiB
Elixir
Raw Normal View History

2022-09-12 15:04:19 +00:00
defmodule Pleroma.Web.AkkomaAPI.FrontendSettingsController do
use Pleroma.Web, :controller
alias Pleroma.Web.Plugs.OAuthScopesPlug
2022-09-19 20:09:13 +00:00
alias Pleroma.Akkoma.FrontendSettingsProfile
2022-09-12 15:04:19 +00:00
@unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
plug(
OAuthScopesPlug,
2022-09-13 12:37:57 +00:00
%{@unauthenticated_access | scopes: ["read:accounts"]}
2022-09-12 15:04:19 +00:00
when action in [
2022-09-19 20:09:13 +00:00
:list_profiles,
:get_profile
]
2022-09-13 12:37:57 +00:00
)
2022-09-19 20:09:13 +00:00
2022-09-13 12:37:57 +00:00
plug(
OAuthScopesPlug,
%{@unauthenticated_access | scopes: ["write:accounts"]}
when action in [
2022-09-19 20:09:13 +00:00
:update_profile
]
2022-09-12 15:04:19 +00:00
)
2022-09-13 12:37:57 +00:00
plug(Pleroma.Web.ApiSpec.CastAndValidate)
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.FrontendSettingsOperation
2022-09-12 15:04:19 +00:00
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
@doc "GET /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name"
2022-09-13 12:37:57 +00:00
def get_profile(conn, %{frontend_name: frontend_name, profile_name: profile_name}) do
2022-09-19 20:09:13 +00:00
with %FrontendSettingsProfile{} = profile <-
FrontendSettingsProfile.get_by_user_and_frontend_name_and_profile_name(
conn.assigns.user,
frontend_name,
profile_name
) do
2022-09-12 15:04:19 +00:00
conn
2022-09-19 20:09:13 +00:00
|> json(%{
settings: profile.settings,
version: profile.version
})
2022-09-12 15:04:19 +00:00
else
nil -> {:error, :not_found}
end
end
@doc "GET /api/v1/akkoma/frontend_settings/:frontend_name"
2022-09-13 12:37:57 +00:00
def list_profiles(conn, %{frontend_name: frontend_name}) do
2022-09-19 20:09:13 +00:00
with profiles <-
FrontendSettingsProfile.get_all_by_user_and_frontend_name(
conn.assigns.user,
frontend_name
),
data <-
Enum.map(profiles, fn profile ->
%{name: profile.profile_name, version: profile.version}
end) do
2022-09-13 12:37:57 +00:00
json(conn, data)
2022-09-12 15:04:19 +00:00
end
end
@doc "PUT /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name"
2022-09-19 20:09:13 +00:00
def update_profile(%{body_params: %{settings: settings, version: version}} = conn, %{
frontend_name: frontend_name,
profile_name: profile_name
}) do
with {:ok, profile} <-
FrontendSettingsProfile.create_or_update(
conn.assigns.user,
frontend_name,
profile_name,
settings,
version
) do
2022-09-12 15:04:19 +00:00
conn
|> json(profile.settings)
end
end
2022-09-19 20:09:13 +00:00
end