1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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,
9 only: [add_link_headers: 2, truthy_param?: 1, assign_account_by_id: 2, json_response: 3]
12 alias Pleroma.Plugs.RateLimiter
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.CommonAPI
16 alias Pleroma.Web.MastodonAPI.ListView
17 alias Pleroma.Web.MastodonAPI.MastodonAPI
18 alias Pleroma.Web.MastodonAPI.StatusView
19 alias Pleroma.Web.OAuth.Token
20 alias Pleroma.Web.TwitterAPI.TwitterAPI
22 @relations [:follow, :unfollow]
23 @needs_account ~W(followers following lists follow unfollow mute unmute block unblock)a
25 plug(RateLimiter, {:relations_id_action, params: ["id", "uri"]} when action in @relations)
26 plug(RateLimiter, :relations_actions when action in @relations)
27 plug(RateLimiter, :app_account_creation when action == :create)
28 plug(:assign_account_by_id when action in @needs_account)
30 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
32 @doc "POST /api/v1/accounts"
34 %{assigns: %{app: app}} = conn,
35 %{"username" => nickname, "email" => _, "password" => _, "agreement" => true} = params
43 "captcha_answer_data",
47 |> Map.put("nickname", nickname)
48 |> Map.put("fullname", params["fullname"] || nickname)
49 |> Map.put("bio", params["bio"] || "")
50 |> Map.put("confirm", params["password"])
52 with {:ok, user} <- TwitterAPI.register_user(params, need_confirmation: true),
53 {:ok, token} <- Token.create_token(app, user, %{scopes: app.scopes}) do
56 access_token: token.token,
58 created_at: Token.Utils.format_created_at(token)
61 {:error, errors} -> json_response(conn, :bad_request, errors)
65 def create(%{assigns: %{app: _app}} = conn, _) do
66 render_error(conn, :bad_request, "Missing parameters")
69 def create(conn, _) do
70 render_error(conn, :forbidden, "Invalid credentials")
73 @doc "GET /api/v1/accounts/verify_credentials"
74 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
75 chat_token = Phoenix.Token.sign(conn, "user socket", user.id)
77 render(conn, "show.json",
80 with_pleroma_settings: true,
81 with_chat_token: chat_token
85 @doc "PATCH /api/v1/accounts/update_credentials"
86 def update_credentials(%{assigns: %{user: original_user}} = conn, params) do
91 |> add_if_present(params, "display_name", :name)
92 |> add_if_present(params, "note", :bio, fn value -> {:ok, User.parse_bio(value, user)} end)
93 |> add_if_present(params, "avatar", :avatar, fn value ->
94 with %Plug.Upload{} <- value,
95 {:ok, object} <- ActivityPub.upload(value, type: :avatar) do
100 emojis_text = (user_params["display_name"] || "") <> (user_params["note"] || "")
104 |> Map.get(:emoji, [])
105 |> Enum.concat(Emoji.Formatter.get_emoji_map(emojis_text))
112 :hide_followers_count,
118 :skip_thread_containment,
121 |> Enum.reduce(%{}, fn key, acc ->
122 add_if_present(acc, params, to_string(key), key, &{:ok, truthy_param?(&1)})
124 |> add_if_present(params, "default_scope", :default_scope)
125 |> add_if_present(params, "fields", :fields, fn fields ->
126 fields = Enum.map(fields, fn f -> Map.update!(f, "value", &AutoLinker.link(&1)) end)
130 |> add_if_present(params, "fields", :raw_fields)
131 |> add_if_present(params, "pleroma_settings_store", :pleroma_settings_store, fn value ->
132 {:ok, Map.merge(user.info.pleroma_settings_store, value)}
134 |> add_if_present(params, "header", :banner, fn value ->
135 with %Plug.Upload{} <- value,
136 {:ok, object} <- ActivityPub.upload(value, type: :banner) do
140 |> add_if_present(params, "pleroma_background_image", :background, fn value ->
141 with %Plug.Upload{} <- value,
142 {:ok, object} <- ActivityPub.upload(value, type: :background) do
146 |> Map.put(:emoji, user_info_emojis)
150 |> User.update_changeset(user_params)
151 |> User.change_info(&User.Info.profile_update(&1, info_params))
153 with {:ok, user} <- User.update_and_set_cache(changeset) do
154 if original_user != user, do: CommonAPI.update(user)
156 render(conn, "show.json", user: user, for: user, with_pleroma_settings: true)
158 _e -> render_error(conn, :forbidden, "Invalid request")
162 defp add_if_present(map, params, params_field, map_field, value_function \\ &{:ok, &1}) do
163 with true <- Map.has_key?(params, params_field),
164 {:ok, new_value} <- value_function.(params[params_field]) do
165 Map.put(map, map_field, new_value)
171 @doc "GET /api/v1/accounts/relationships"
172 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
173 targets = User.get_all_by_ids(List.wrap(id))
175 render(conn, "relationships.json", user: user, targets: targets)
178 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
179 def relationships(%{assigns: %{user: _user}} = conn, _), do: json(conn, [])
181 @doc "GET /api/v1/accounts/:id"
182 def show(%{assigns: %{user: for_user}} = conn, %{"id" => nickname_or_id}) do
183 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname_or_id, for: for_user),
184 true <- User.auth_active?(user) || user.id == for_user.id || User.superuser?(for_user) do
185 render(conn, "show.json", user: user, for: for_user)
187 _e -> render_error(conn, :not_found, "Can't find user")
191 @doc "GET /api/v1/accounts/:id/statuses"
192 def statuses(%{assigns: %{user: reading_user}} = conn, params) do
193 with %User{} = user <- User.get_cached_by_nickname_or_id(params["id"], for: reading_user) do
194 params = Map.put(params, "tag", params["tagged"])
195 activities = ActivityPub.fetch_user_activities(user, reading_user, params)
198 |> add_link_headers(activities)
199 |> put_view(StatusView)
200 |> render("index.json", activities: activities, for: reading_user, as: :activity)
204 @doc "GET /api/v1/accounts/:id/followers"
205 def followers(%{assigns: %{user: for_user, account: user}} = conn, params) do
208 for_user && user.id == for_user.id -> MastodonAPI.get_followers(user, params)
209 user.info.hide_followers -> []
210 true -> MastodonAPI.get_followers(user, params)
214 |> add_link_headers(followers)
215 |> render("index.json", for: for_user, users: followers, as: :user)
218 @doc "GET /api/v1/accounts/:id/following"
219 def following(%{assigns: %{user: for_user, account: user}} = conn, params) do
222 for_user && user.id == for_user.id -> MastodonAPI.get_friends(user, params)
223 user.info.hide_follows -> []
224 true -> MastodonAPI.get_friends(user, params)
228 |> add_link_headers(followers)
229 |> render("index.json", for: for_user, users: followers, as: :user)
232 @doc "GET /api/v1/accounts/:id/lists"
233 def lists(%{assigns: %{user: user, account: account}} = conn, _params) do
234 lists = Pleroma.List.get_lists_account_belongs(user, account)
237 |> put_view(ListView)
238 |> render("index.json", lists: lists)
241 @doc "POST /api/v1/accounts/:id/follow"
242 def follow(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
246 def follow(%{assigns: %{user: follower, account: followed}} = conn, _params) do
247 with {:ok, follower} <- MastodonAPI.follow(follower, followed, conn.params) do
248 render(conn, "relationship.json", user: follower, target: followed)
250 {:error, message} -> json_response(conn, :forbidden, %{error: message})
254 @doc "POST /api/v1/accounts/:id/unfollow"
255 def unfollow(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
259 def unfollow(%{assigns: %{user: follower, account: followed}} = conn, _params) do
260 with {:ok, follower} <- CommonAPI.unfollow(follower, followed) do
261 render(conn, "relationship.json", user: follower, target: followed)
265 @doc "POST /api/v1/accounts/:id/mute"
266 def mute(%{assigns: %{user: muter, account: muted}} = conn, params) do
267 notifications? = params |> Map.get("notifications", true) |> truthy_param?()
269 with {:ok, muter} <- User.mute(muter, muted, notifications?) do
270 render(conn, "relationship.json", user: muter, target: muted)
272 {:error, message} -> json_response(conn, :forbidden, %{error: message})
276 @doc "POST /api/v1/accounts/:id/unmute"
277 def unmute(%{assigns: %{user: muter, account: muted}} = conn, _params) do
278 with {:ok, muter} <- User.unmute(muter, muted) do
279 render(conn, "relationship.json", user: muter, target: muted)
281 {:error, message} -> json_response(conn, :forbidden, %{error: message})
285 @doc "POST /api/v1/accounts/:id/block"
286 def block(%{assigns: %{user: blocker, account: blocked}} = conn, _params) do
287 with {:ok, blocker} <- User.block(blocker, blocked),
288 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
289 render(conn, "relationship.json", user: blocker, target: blocked)
291 {:error, message} -> json_response(conn, :forbidden, %{error: message})
295 @doc "POST /api/v1/accounts/:id/unblock"
296 def unblock(%{assigns: %{user: blocker, account: blocked}} = conn, _params) do
297 with {:ok, blocker} <- User.unblock(blocker, blocked),
298 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
299 render(conn, "relationship.json", user: blocker, target: blocked)
301 {:error, message} -> json_response(conn, :forbidden, %{error: message})