1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AccountController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper,
12 assign_account_by_id: 2,
14 skip_relationships?: 1
17 alias Pleroma.Plugs.OAuthScopesPlug
18 alias Pleroma.Plugs.RateLimiter
20 alias Pleroma.Web.ActivityPub.ActivityPub
21 alias Pleroma.Web.CommonAPI
22 alias Pleroma.Web.MastodonAPI.ListView
23 alias Pleroma.Web.MastodonAPI.MastodonAPI
24 alias Pleroma.Web.MastodonAPI.StatusView
25 alias Pleroma.Web.OAuth.Token
26 alias Pleroma.Web.TwitterAPI.TwitterAPI
30 %{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]}
36 %{scopes: ["read:accounts"]}
37 when action in [:endorsements, :verify_credentials, :followers, :following]
40 plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action == :update_credentials)
42 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :lists)
46 %{scopes: ["follow", "read:blocks"]} when action == :blocks
51 %{scopes: ["follow", "write:blocks"]} when action in [:block, :unblock]
54 plug(OAuthScopesPlug, %{scopes: ["read:follows"]} when action == :relationships)
56 # Note: :follows (POST /api/v1/follows) is the same as :follow, consider removing :follows
59 %{scopes: ["follow", "write:follows"]} when action in [:follows, :follow, :unfollow]
62 plug(OAuthScopesPlug, %{scopes: ["follow", "read:mutes"]} when action == :mutes)
64 plug(OAuthScopesPlug, %{scopes: ["follow", "write:mutes"]} when action in [:mute, :unmute])
67 Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
68 when action not in [:create, :show, :statuses]
71 @relationship_actions [:follow, :unfollow]
72 @needs_account ~W(followers following lists follow unfollow mute unmute block unblock)a
76 [name: :relation_id_action, params: ["id", "uri"]] when action in @relationship_actions
79 plug(RateLimiter, [name: :relations_actions] when action in @relationship_actions)
80 plug(RateLimiter, [name: :app_account_creation] when action == :create)
81 plug(:assign_account_by_id when action in @needs_account)
84 OpenApiSpex.Plug.CastAndValidate,
85 [render_error: Pleroma.Web.ApiSpec.RenderError]
86 when action in [:create, :verify_credentials, :update_credentials, :relationships, :show]
89 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
91 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AccountOperation
93 @doc "POST /api/v1/accounts"
94 def create(%{assigns: %{app: app}, body_params: params} = conn, _params) do
102 :captcha_answer_data,
107 |> Map.put(:nickname, params.username)
108 |> Map.put(:fullname, params.fullname || params.username)
109 |> Map.put(:bio, params.bio || "")
110 |> Map.put(:confirm, params.password)
112 with :ok <- validate_email_param(params),
113 {:ok, user} <- TwitterAPI.register_user(params, need_confirmation: true),
114 {:ok, token} <- Token.create_token(app, user, %{scopes: app.scopes}) do
116 token_type: "Bearer",
117 access_token: token.token,
119 created_at: Token.Utils.format_created_at(token)
122 {:error, errors} -> json_response(conn, :bad_request, errors)
126 def create(%{assigns: %{app: _app}} = conn, _) do
127 render_error(conn, :bad_request, "Missing parameters")
130 def create(conn, _) do
131 render_error(conn, :forbidden, "Invalid credentials")
134 defp validate_email_param(%{:email => email}) when not is_nil(email), do: :ok
136 defp validate_email_param(_) do
137 case Pleroma.Config.get([:instance, :account_activation_required]) do
138 true -> {:error, %{"error" => "Missing parameters"}}
143 @doc "GET /api/v1/accounts/verify_credentials"
144 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
145 chat_token = Phoenix.Token.sign(conn, "user socket", user.id)
147 render(conn, "show.json",
150 with_pleroma_settings: true,
151 with_chat_token: chat_token
155 @doc "PATCH /api/v1/accounts/update_credentials"
156 def update_credentials(%{assigns: %{user: original_user}, body_params: params} = conn, _params) do
162 |> Enum.filter(fn {_, value} -> not is_nil(value) end)
169 :hide_followers_count,
175 :skip_thread_containment,
176 :allow_following_move,
179 |> Enum.reduce(%{}, fn key, acc ->
180 add_if_present(acc, params, key, key, &{:ok, truthy_param?(&1)})
182 |> add_if_present(params, :display_name, :name)
183 |> add_if_present(params, :note, :bio)
184 |> add_if_present(params, :avatar, :avatar)
185 |> add_if_present(params, :header, :banner)
186 |> add_if_present(params, :pleroma_background_image, :background)
191 &{:ok, normalize_fields_attributes(&1)}
193 |> add_if_present(params, :pleroma_settings_store, :pleroma_settings_store)
194 |> add_if_present(params, :default_scope, :default_scope)
195 |> add_if_present(params, :actor_type, :actor_type)
197 changeset = User.update_changeset(user, user_params)
199 with {:ok, user} <- User.update_and_set_cache(changeset) do
200 if original_user != user, do: CommonAPI.update(user)
202 render(conn, "show.json", user: user, for: user, with_pleroma_settings: true)
204 _e -> render_error(conn, :forbidden, "Invalid request")
208 defp add_if_present(map, params, params_field, map_field, value_function \\ &{:ok, &1}) do
209 with true <- Map.has_key?(params, params_field),
210 {:ok, new_value} <- value_function.(Map.get(params, params_field)) do
211 Map.put(map, map_field, new_value)
217 defp normalize_fields_attributes(fields) do
218 if Enum.all?(fields, &is_tuple/1) do
219 Enum.map(fields, fn {_, v} -> v end)
222 %Pleroma.Web.ApiSpec.Schemas.AccountAttributeField{} = field ->
223 %{"name" => field.name, "value" => field.value}
231 @doc "GET /api/v1/accounts/relationships"
232 def relationships(%{assigns: %{user: user}} = conn, %{id: id}) do
233 targets = User.get_all_by_ids(List.wrap(id))
235 render(conn, "relationships.json", user: user, targets: targets)
238 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
239 def relationships(%{assigns: %{user: _user}} = conn, _), do: json(conn, [])
241 @doc "GET /api/v1/accounts/:id"
242 def show(%{assigns: %{user: for_user}} = conn, %{id: nickname_or_id}) do
243 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname_or_id, for: for_user),
244 true <- User.visible_for?(user, for_user) do
245 render(conn, "show.json", user: user, for: for_user)
247 _e -> render_error(conn, :not_found, "Can't find user")
251 @doc "GET /api/v1/accounts/:id/statuses"
252 def statuses(%{assigns: %{user: reading_user}} = conn, params) do
253 with %User{} = user <- User.get_cached_by_nickname_or_id(params["id"], for: reading_user),
254 true <- User.visible_for?(user, reading_user) do
257 |> Map.put("tag", params["tagged"])
258 |> Map.delete("godmode")
260 activities = ActivityPub.fetch_user_activities(user, reading_user, params)
263 |> add_link_headers(activities)
264 |> put_view(StatusView)
265 |> render("index.json",
266 activities: activities,
269 skip_relationships: skip_relationships?(params)
272 _e -> render_error(conn, :not_found, "Can't find user")
276 @doc "GET /api/v1/accounts/:id/followers"
277 def followers(%{assigns: %{user: for_user, account: user}} = conn, params) do
280 for_user && user.id == for_user.id -> MastodonAPI.get_followers(user, params)
281 user.hide_followers -> []
282 true -> MastodonAPI.get_followers(user, params)
286 |> add_link_headers(followers)
287 |> render("index.json", for: for_user, users: followers, as: :user)
290 @doc "GET /api/v1/accounts/:id/following"
291 def following(%{assigns: %{user: for_user, account: user}} = conn, params) do
294 for_user && user.id == for_user.id -> MastodonAPI.get_friends(user, params)
295 user.hide_follows -> []
296 true -> MastodonAPI.get_friends(user, params)
300 |> add_link_headers(followers)
301 |> render("index.json", for: for_user, users: followers, as: :user)
304 @doc "GET /api/v1/accounts/:id/lists"
305 def lists(%{assigns: %{user: user, account: account}} = conn, _params) do
306 lists = Pleroma.List.get_lists_account_belongs(user, account)
309 |> put_view(ListView)
310 |> render("index.json", lists: lists)
313 @doc "POST /api/v1/accounts/:id/follow"
314 def follow(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
318 def follow(%{assigns: %{user: follower, account: followed}} = conn, _params) do
319 with {:ok, follower} <- MastodonAPI.follow(follower, followed, conn.params) do
320 render(conn, "relationship.json", user: follower, target: followed)
322 {:error, message} -> json_response(conn, :forbidden, %{error: message})
326 @doc "POST /api/v1/accounts/:id/unfollow"
327 def unfollow(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
331 def unfollow(%{assigns: %{user: follower, account: followed}} = conn, _params) do
332 with {:ok, follower} <- CommonAPI.unfollow(follower, followed) do
333 render(conn, "relationship.json", user: follower, target: followed)
337 @doc "POST /api/v1/accounts/:id/mute"
338 def mute(%{assigns: %{user: muter, account: muted}} = conn, params) do
339 notifications? = params |> Map.get("notifications", true) |> truthy_param?()
341 with {:ok, _user_relationships} <- User.mute(muter, muted, notifications?) do
342 render(conn, "relationship.json", user: muter, target: muted)
344 {:error, message} -> json_response(conn, :forbidden, %{error: message})
348 @doc "POST /api/v1/accounts/:id/unmute"
349 def unmute(%{assigns: %{user: muter, account: muted}} = conn, _params) do
350 with {:ok, _user_relationships} <- User.unmute(muter, muted) do
351 render(conn, "relationship.json", user: muter, target: muted)
353 {:error, message} -> json_response(conn, :forbidden, %{error: message})
357 @doc "POST /api/v1/accounts/:id/block"
358 def block(%{assigns: %{user: blocker, account: blocked}} = conn, _params) do
359 with {:ok, _user_block} <- User.block(blocker, blocked),
360 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
361 render(conn, "relationship.json", user: blocker, target: blocked)
363 {:error, message} -> json_response(conn, :forbidden, %{error: message})
367 @doc "POST /api/v1/accounts/:id/unblock"
368 def unblock(%{assigns: %{user: blocker, account: blocked}} = conn, _params) do
369 with {:ok, _user_block} <- User.unblock(blocker, blocked),
370 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
371 render(conn, "relationship.json", user: blocker, target: blocked)
373 {:error, message} -> json_response(conn, :forbidden, %{error: message})
377 @doc "POST /api/v1/follows"
378 def follows(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
379 with {_, %User{} = followed} <- {:followed, User.get_cached_by_nickname(uri)},
380 {_, true} <- {:followed, follower.id != followed.id},
381 {:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do
382 render(conn, "show.json", user: followed, for: follower)
384 {:followed, _} -> {:error, :not_found}
385 {:error, message} -> json_response(conn, :forbidden, %{error: message})
389 @doc "GET /api/v1/mutes"
390 def mutes(%{assigns: %{user: user}} = conn, _) do
391 users = User.muted_users(user, _restrict_deactivated = true)
392 render(conn, "index.json", users: users, for: user, as: :user)
395 @doc "GET /api/v1/blocks"
396 def blocks(%{assigns: %{user: user}} = conn, _) do
397 users = User.blocked_users(user, _restrict_deactivated = true)
398 render(conn, "index.json", users: users, for: user, as: :user)
401 @doc "GET /api/v1/endorsements"
402 def endorsements(conn, params),
403 do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params)