c13ff90968f6da314f3014fbb800725601c30412
[akkoma] / lib / pleroma / web / akkoma_api / controllers / frontend_settings_controller.ex
1 defmodule Pleroma.Web.AkkomaAPI.FrontendSettingsController do
2 use Pleroma.Web, :controller
3
4 alias Pleroma.Web.Plugs.OAuthScopesPlug
5 alias Pleroma.Akkoma.FrontendSettingsProfile
6
7 @unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
8 plug(
9 OAuthScopesPlug,
10 %{@unauthenticated_access | scopes: ["read:accounts"]}
11 when action in [
12 :list_profiles,
13 :get_profile
14 ]
15 )
16
17 plug(
18 OAuthScopesPlug,
19 %{@unauthenticated_access | scopes: ["write:accounts"]}
20 when action in [
21 :update_profile,
22 :delete_profile
23 ]
24 )
25
26 plug(Pleroma.Web.ApiSpec.CastAndValidate)
27 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.FrontendSettingsOperation
28
29 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
30
31 @doc "GET /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name"
32 def get_profile(conn, %{frontend_name: frontend_name, profile_name: profile_name}) do
33 with %FrontendSettingsProfile{} = profile <-
34 FrontendSettingsProfile.get_by_user_and_frontend_name_and_profile_name(
35 conn.assigns.user,
36 frontend_name,
37 profile_name
38 ) do
39 conn
40 |> json(%{
41 settings: profile.settings,
42 version: profile.version
43 })
44 else
45 nil -> {:error, :not_found}
46 end
47 end
48
49 @doc "GET /api/v1/akkoma/frontend_settings/:frontend_name"
50 def list_profiles(conn, %{frontend_name: frontend_name}) do
51 with profiles <-
52 FrontendSettingsProfile.get_all_by_user_and_frontend_name(
53 conn.assigns.user,
54 frontend_name
55 ),
56 data <-
57 Enum.map(profiles, fn profile ->
58 %{name: profile.profile_name, version: profile.version}
59 end) do
60 json(conn, data)
61 end
62 end
63
64 @doc "DELETE /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name"
65 def delete_profile(conn, %{frontend_name: frontend_name, profile_name: profile_name}) do
66 with %FrontendSettingsProfile{} = profile <-
67 FrontendSettingsProfile.get_by_user_and_frontend_name_and_profile_name(
68 conn.assigns.user,
69 frontend_name,
70 profile_name
71 ),
72 {:ok, _} <- FrontendSettingsProfile.delete_profile(profile) do
73 json(conn, %{deleted: "ok"})
74 else
75 nil -> {:error, :not_found}
76 end
77 end
78
79 @doc "PUT /api/v1/akkoma/frontend_settings/:frontend_name/:profile_name"
80 def update_profile(%{body_params: %{settings: settings, version: version}} = conn, %{
81 frontend_name: frontend_name,
82 profile_name: profile_name
83 }) do
84 with {:ok, profile} <-
85 FrontendSettingsProfile.create_or_update(
86 conn.assigns.user,
87 frontend_name,
88 profile_name,
89 settings,
90 version
91 ) do
92 conn
93 |> json(profile.settings)
94 end
95 end
96 end