Add frontend preference route

This commit is contained in:
FloatingGhost 2023-03-12 23:24:07 +00:00
parent 58f75ac062
commit 3d964a9970
4 changed files with 102 additions and 4 deletions

View file

@ -745,6 +745,9 @@
primary: %{"name" => "pleroma-fe", "ref" => "stable"}, primary: %{"name" => "pleroma-fe", "ref" => "stable"},
admin: %{"name" => "admin-fe", "ref" => "stable"}, admin: %{"name" => "admin-fe", "ref" => "stable"},
mastodon: %{"name" => "mastodon-fe", "ref" => "akkoma"}, mastodon: %{"name" => "mastodon-fe", "ref" => "akkoma"},
pickable: [
"pleroma-fe/stable"
],
swagger: %{ swagger: %{
"name" => "swagger-ui", "name" => "swagger-ui",
"ref" => "stable", "ref" => "stable",

View file

@ -5,6 +5,16 @@ defmodule Pleroma.Web.AkkomaAPI.FrontendSettingsController do
alias Pleroma.Akkoma.FrontendSettingsProfile alias Pleroma.Akkoma.FrontendSettingsProfile
@unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []} @unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
plug(
OAuthScopesPlug,
@unauthenticated_access
when action in [
:available_frontends,
:update_preferred_frontend
]
)
plug( plug(
OAuthScopesPlug, OAuthScopesPlug,
%{@unauthenticated_access | scopes: ["read:accounts"]} %{@unauthenticated_access | scopes: ["read:accounts"]}
@ -93,4 +103,22 @@ def update_profile(%{body_params: %{settings: settings, version: version}} = con
|> json(profile.settings) |> json(profile.settings)
end end
end end
@doc "GET /api/v1/akkoma/preferred_frontend/available"
def available_frontends(conn, _params) do
available = Pleroma.Config.get([:frontends, :pickable])
conn
|> json(available)
end
@doc "PUT /api/v1/akkoma/preferred_frontend"
def update_preferred_frontend(
%{body_params: %{frontend_name: preferred_frontend}} = conn,
_params
) do
conn
|> put_resp_cookie("preferred_frontend", preferred_frontend)
|> json(%{frontend_name: preferred_frontend})
end
end end

View file

@ -12,7 +12,7 @@ def open_api_operation(action) do
@spec list_profiles_operation() :: Operation.t() @spec list_profiles_operation() :: Operation.t()
def list_profiles_operation() do def list_profiles_operation() do
%Operation{ %Operation{
tags: ["Retrieve frontend setting profiles"], tags: ["Frontends"],
summary: "Frontend Settings Profiles", summary: "Frontend Settings Profiles",
description: "List frontend setting profiles", description: "List frontend setting profiles",
operationId: "AkkomaAPI.FrontendSettingsController.list_profiles", operationId: "AkkomaAPI.FrontendSettingsController.list_profiles",
@ -37,7 +37,7 @@ def list_profiles_operation() do
@spec get_profile_operation() :: Operation.t() @spec get_profile_operation() :: Operation.t()
def get_profile_operation() do def get_profile_operation() do
%Operation{ %Operation{
tags: ["Retrieve frontend setting profile"], tags: ["Frontends"],
summary: "Frontend Settings Profile", summary: "Frontend Settings Profile",
description: "Get frontend setting profile", description: "Get frontend setting profile",
operationId: "AkkomaAPI.FrontendSettingsController.get_profile", operationId: "AkkomaAPI.FrontendSettingsController.get_profile",
@ -60,7 +60,7 @@ def get_profile_operation() do
@spec delete_profile_operation() :: Operation.t() @spec delete_profile_operation() :: Operation.t()
def delete_profile_operation() do def delete_profile_operation() do
%Operation{ %Operation{
tags: ["Delete frontend setting profile"], tags: ["Frontends"],
summary: "Delete frontend Settings Profile", summary: "Delete frontend Settings Profile",
description: "Delete frontend setting profile", description: "Delete frontend setting profile",
operationId: "AkkomaAPI.FrontendSettingsController.delete_profile", operationId: "AkkomaAPI.FrontendSettingsController.delete_profile",
@ -76,7 +76,7 @@ def delete_profile_operation() do
@spec update_profile_operation() :: Operation.t() @spec update_profile_operation() :: Operation.t()
def update_profile_operation() do def update_profile_operation() do
%Operation{ %Operation{
tags: ["Update frontend setting profile"], tags: ["Frontends"],
summary: "Frontend Settings Profile", summary: "Frontend Settings Profile",
description: "Update frontend setting profile", description: "Update frontend setting profile",
operationId: "AkkomaAPI.FrontendSettingsController.update_profile_operation", operationId: "AkkomaAPI.FrontendSettingsController.update_profile_operation",
@ -90,6 +90,57 @@ def update_profile_operation() do
} }
end end
def available_frontends_operation() do
%Operation{
tags: ["Frontends"],
summary: "Frontend Settings Profiles",
description: "List frontend setting profiles",
operationId: "AkkomaAPI.FrontendSettingsController.available_frontends",
responses: %{
200 =>
Operation.response("Frontends", "application/json", %Schema{
type: :array,
items: %Schema{
type: :string
}
})
}
}
end
def update_preferred_frontend_operation() do
%Operation{
tags: ["Frontends"],
summary: "Frontend Settings Profiles",
description: "List frontend setting profiles",
operationId: "AkkomaAPI.FrontendSettingsController.available_frontends",
requestBody:
request_body(
"Frontend",
%Schema{
type: :object,
required: [:frontend_name],
properties: %{
frontend_name: %Schema{
type: :string,
description: "Frontend name"
}
}
},
required: true
),
responses: %{
200 =>
Operation.response("Frontends", "application/json", %Schema{
type: :array,
items: %Schema{
type: :string
}
})
}
}
end
def frontend_name_param do def frontend_name_param do
Operation.parameter(:frontend_name, :path, :string, "Frontend name", Operation.parameter(:frontend_name, :path, :string, "Frontend name",
example: "pleroma-fe", example: "pleroma-fe",

View file

@ -466,6 +466,22 @@ defmodule Pleroma.Web.Router do
put("/statuses/:id/emoji_reactions/:emoji", EmojiReactionController, :create) put("/statuses/:id/emoji_reactions/:emoji", EmojiReactionController, :create)
end end
scope "/api/v1/akkoma", Pleroma.Web.AkkomaAPI do
pipe_through(:api)
get(
"/api/v1/akkoma/preferred_frontend/available",
FrontendSettingsController,
:available_frontends
)
put(
"/api/v1/akkoma/preferred_frontend",
FrontendSettingsController,
:update_preferred_frontend
)
end
scope "/api/v1/akkoma", Pleroma.Web.AkkomaAPI do scope "/api/v1/akkoma", Pleroma.Web.AkkomaAPI do
pipe_through(:authenticated_api) pipe_through(:authenticated_api)
get("/metrics", MetricsController, :show) get("/metrics", MetricsController, :show)