Merge branch 'openapi/update-admin-api/status' into 'develop'
[akkoma] / lib / pleroma / web / mastodon_api / controllers / account_controller.ex
index 2dd0252ccb9c77ddf1c2a6f25962dbfba9e349f0..97295a52fb5a0322d3a5671af521ad00eceab2b6 100644 (file)
@@ -10,7 +10,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
       add_link_headers: 2,
       truthy_param?: 1,
       assign_account_by_id: 2,
-      with_relationships?: 1,
+      embed_relationships?: 1,
       json_response: 3
     ]
 
@@ -81,7 +81,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
 
   plug(
     RateLimiter,
-    [name: :relation_id_action, params: ["id", "uri"]] when action in @relationship_actions
+    [name: :relation_id_action, params: [:id, :uri]] when action in @relationship_actions
   )
 
   plug(RateLimiter, [name: :relations_actions] when action in @relationship_actions)
@@ -139,9 +139,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
   end
 
   @doc "PATCH /api/v1/accounts/update_credentials"
-  def update_credentials(%{assigns: %{user: original_user}, body_params: params} = conn, _params) do
-    user = original_user
-
+  def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _params) do
     params =
       params
       |> Enum.filter(fn {_, value} -> not is_nil(value) end)
@@ -177,19 +175,40 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
       )
       |> add_if_present(params, :pleroma_settings_store, :pleroma_settings_store)
       |> add_if_present(params, :default_scope, :default_scope)
+      |> add_if_present(params["source"], "privacy", :default_scope)
       |> add_if_present(params, :actor_type, :actor_type)
 
     changeset = User.update_changeset(user, user_params)
 
     with {:ok, user} <- User.update_and_set_cache(changeset) do
+      user
+      |> build_update_activity_params()
+      |> ActivityPub.update()
+
       render(conn, "show.json", user: user, for: user, with_pleroma_settings: true)
     else
       _e -> render_error(conn, :forbidden, "Invalid request")
     end
   end
 
+  # Hotfix, handling will be redone with the pipeline
+  defp build_update_activity_params(user) do
+    object =
+      Pleroma.Web.ActivityPub.UserView.render("user.json", user: user)
+      |> Map.delete("@context")
+
+    %{
+      local: true,
+      to: [user.follower_address],
+      cc: [],
+      object: object,
+      actor: user.ap_id
+    }
+  end
+
   defp add_if_present(map, params, params_field, map_field, value_function \\ &{:ok, &1}) do
-    with true <- Map.has_key?(params, params_field),
+    with true <- is_map(params),
+         true <- Map.has_key?(params, params_field),
          {:ok, new_value} <- value_function.(Map.get(params, params_field)) do
       Map.put(map, map_field, new_value)
     else
@@ -275,7 +294,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
       for: for_user,
       users: followers,
       as: :user,
-      embed_relationships: with_relationships?(params)
+      embed_relationships: embed_relationships?(params)
     )
   end
 
@@ -300,7 +319,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
       for: for_user,
       users: followers,
       as: :user,
-      embed_relationships: with_relationships?(params)
+      embed_relationships: embed_relationships?(params)
     )
   end