From 4940a49a50e14f79a019bd544b271133f42e78c9 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Sun, 2 Oct 2022 17:00:26 +0100 Subject: [PATCH] allow deleting profiles --- .../akkoma/frontend_setting_profile.ex | 4 +++ .../frontend_settings_controller.ex | 17 +++++++++++- .../operations/frontend_settings_operation.ex | 26 +++++++++++++++++-- lib/pleroma/web/router.ex | 6 +++++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/akkoma/frontend_setting_profile.ex b/lib/pleroma/akkoma/frontend_setting_profile.ex index 6649a7943..18208a7dd 100644 --- a/lib/pleroma/akkoma/frontend_setting_profile.ex +++ b/lib/pleroma/akkoma/frontend_setting_profile.ex @@ -69,6 +69,10 @@ defmodule Pleroma.Akkoma.FrontendSettingsProfile do ) end + def delete_profile(profile) do + Repo.delete(profile) + end + defp validate_settings_length( %Ecto.Changeset{changes: %{settings: settings}} = changeset, max_length diff --git a/lib/pleroma/web/akkoma_api/controllers/frontend_settings_controller.ex b/lib/pleroma/web/akkoma_api/controllers/frontend_settings_controller.ex index 8738e3636..71f7f195d 100644 --- a/lib/pleroma/web/akkoma_api/controllers/frontend_settings_controller.ex +++ b/lib/pleroma/web/akkoma_api/controllers/frontend_settings_controller.ex @@ -18,7 +18,7 @@ defmodule Pleroma.Web.AkkomaAPI.FrontendSettingsController do OAuthScopesPlug, %{@unauthenticated_access | scopes: ["write:accounts"]} when action in [ - :update_profile + :update_profile, :delete_profile ] ) @@ -60,6 +60,21 @@ defmodule Pleroma.Web.AkkomaAPI.FrontendSettingsController do end end + @doc "DELETE /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name" + def delete_profile(conn, %{frontend_name: frontend_name, profile_name: profile_name}) do + with %FrontendSettingsProfile{} = profile <- + FrontendSettingsProfile.get_by_user_and_frontend_name_and_profile_name( + conn.assigns.user, + frontend_name, + profile_name + ), + {:ok, _} <- FrontendSettingsProfile.delete_profile(profile) do + json(conn, %{}) + else + nil -> {:error, :not_found} + end + end + @doc "PUT /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name" def update_profile(%{body_params: %{settings: settings, version: version}} = conn, %{ frontend_name: frontend_name, diff --git a/lib/pleroma/web/api_spec/operations/frontend_settings_operation.ex b/lib/pleroma/web/api_spec/operations/frontend_settings_operation.ex index 82177d8ff..8608086d5 100644 --- a/lib/pleroma/web/api_spec/operations/frontend_settings_operation.ex +++ b/lib/pleroma/web/api_spec/operations/frontend_settings_operation.ex @@ -22,7 +22,10 @@ defmodule Pleroma.Web.ApiSpec.FrontendSettingsOperation do 200 => Operation.response("Profiles", "application/json", %Schema{ type: :array, - items: %Schema{type: :string} + items: %Schema{type: :object, properties: %{ + name: %Schema{type: :string}, + version: %Schema{type: :integer} + }} }) } } @@ -38,7 +41,26 @@ defmodule Pleroma.Web.ApiSpec.FrontendSettingsOperation do security: [%{"oAuth" => ["read:accounts"]}], parameters: [frontend_name_param(), profile_name_param()], responses: %{ - 200 => Operation.response("Translation", "application/json", %Schema{type: :object}), + 200 => Operation.response("Profile", "application/json", %Schema{type: :object, properties: %{ + "version" => %Schema{type: :integer}, + "settings" => %Schema{type: :object, additionalProperties: true} + }}), + 404 => Operation.response("Not Found", "application/json", %Schema{type: :object}) + } + } + end + + @spec delete_profile_operation() :: Operation.t() + def delete_profile_operation() do + %Operation{ + tags: ["Delete frontend setting profile"], + summary: "Delete frontend Settings Profile", + description: "Delete frontend setting profile", + operationId: "AkkomaAPI.FrontendSettingsController.delete_profile", + security: [%{"oAuth" => ["write:accounts"]}], + parameters: [frontend_name_param(), profile_name_param()], + responses: %{ + 200 => Operation.response("Empty", "application/json", %Schema{type: :string}), 404 => Operation.response("Not Found", "application/json", %Schema{type: :object}) } } diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index ab348255d..838599c4d 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -480,6 +480,12 @@ defmodule Pleroma.Web.Router do FrontendSettingsController, :update_profile ) + + delete( + "/frontend_settings/:frontend_name/:profile_name", + FrontendSettingsController, + :delete_profile + ) end scope "/api/v1", Pleroma.Web.MastodonAPI do