1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.SettingsController do
6 use Pleroma.Web, :controller
8 alias Pleroma.Web.Plugs.OAuthScopesPlug
10 plug(Pleroma.Web.ApiSpec.CastAndValidate)
14 %{scopes: ["write:accounts"]} when action in [:update]
19 %{scopes: ["read:accounts"]} when action in [:show]
22 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaSettingsOperation
24 @doc "GET /api/v1/pleroma/settings/:app"
25 def show(%{assigns: %{user: user}} = conn, %{app: app} = _params) do
27 |> json(get_settings(user, app))
30 @doc "PATCH /api/v1/pleroma/settings/:app"
31 def update(%{assigns: %{user: user}, body_params: body_params} = conn, %{app: app} = _params) do
33 get_settings(user, app)
34 |> merge_recursively(body_params)
37 Pleroma.User.update_changeset(
39 %{pleroma_settings_store: %{app => settings}}
41 {:ok, _} <- Pleroma.Repo.update(changeset) do
47 defp merge_recursively(old, %{} = new) do
48 old = ensure_object(old)
57 {k, %{} = new_child}, acc ->
58 Map.put(acc, k, merge_recursively(acc[k], new_child))
66 defp get_settings(user, app) do
67 user.pleroma_settings_store
72 defp ensure_object(%{} = object) do
76 defp ensure_object(_) do