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.PleromaAPI.AccountController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper,
9 only: [json_response: 3, add_link_headers: 2, assign_account_by_id: 2]
12 alias Pleroma.Plugs.OAuthScopesPlug
13 alias Pleroma.Plugs.RateLimiter
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.MastodonAPI.StatusView
19 require Pleroma.Constants
23 %{scopes: ["follow", "write:follows"]} when action in [:subscribe, :unsubscribe]
28 %{scopes: ["write:accounts"]}
29 # Note: the following actions are not permission-secured in Mastodon:
37 plug(OAuthScopesPlug, %{scopes: ["read:favourites"]} when action == :favourites)
39 # An extra safety measure for possible actions not guarded by OAuth permissions specification
41 Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
42 when action != :confirmation_resend
45 plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend)
46 plug(:assign_account_by_id when action in [:favourites, :subscribe, :unsubscribe])
47 plug(:put_view, Pleroma.Web.MastodonAPI.AccountView)
49 @doc "POST /api/v1/pleroma/accounts/confirmation_resend"
50 def confirmation_resend(conn, params) do
51 nickname_or_email = params["email"] || params["nickname"]
53 with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email),
54 {:ok, _} <- User.try_send_confirmation_email(user) do
55 json_response(conn, :no_content, "")
59 @doc "PATCH /api/v1/pleroma/accounts/update_avatar"
60 def update_avatar(%{assigns: %{user: user}} = conn, %{"img" => ""}) do
63 |> Changeset.change(%{avatar: nil})
64 |> User.update_and_set_cache()
66 CommonAPI.update(user)
68 json(conn, %{url: nil})
71 def update_avatar(%{assigns: %{user: user}} = conn, params) do
72 {:ok, %{data: data}} = ActivityPub.upload(params, type: :avatar)
73 {:ok, user} = user |> Changeset.change(%{avatar: data}) |> User.update_and_set_cache()
74 %{"url" => [%{"href" => href} | _]} = data
76 CommonAPI.update(user)
78 json(conn, %{url: href})
81 @doc "PATCH /api/v1/pleroma/accounts/update_banner"
82 def update_banner(%{assigns: %{user: user}} = conn, %{"banner" => ""}) do
83 with {:ok, user} <- User.update_banner(user, %{}) do
84 CommonAPI.update(user)
85 json(conn, %{url: nil})
89 def update_banner(%{assigns: %{user: user}} = conn, params) do
90 with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}, type: :banner),
91 {:ok, user} <- User.update_banner(user, object.data) do
92 CommonAPI.update(user)
93 %{"url" => [%{"href" => href} | _]} = object.data
95 json(conn, %{url: href})
99 @doc "PATCH /api/v1/pleroma/accounts/update_background"
100 def update_background(%{assigns: %{user: user}} = conn, %{"img" => ""}) do
101 with {:ok, _user} <- User.update_background(user, %{}) do
102 json(conn, %{url: nil})
106 def update_background(%{assigns: %{user: user}} = conn, params) do
107 with {:ok, object} <- ActivityPub.upload(params, type: :background),
108 {:ok, _user} <- User.update_background(user, object.data) do
109 %{"url" => [%{"href" => href} | _]} = object.data
111 json(conn, %{url: href})
115 @doc "GET /api/v1/pleroma/accounts/:id/favourites"
116 def favourites(%{assigns: %{account: %{hide_favorites: true}}} = conn, _params) do
117 render_error(conn, :forbidden, "Can't get favorites")
120 def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do
123 |> Map.put("type", "Create")
124 |> Map.put("favorited_by", user.ap_id)
125 |> Map.put("blocking_user", for_user)
129 [Pleroma.Constants.as_public()] ++ [for_user.ap_id | User.following(for_user)]
131 [Pleroma.Constants.as_public()]
136 |> ActivityPub.fetch_activities(params)
140 |> add_link_headers(activities)
141 |> put_view(StatusView)
142 |> render("index.json", activities: activities, for: for_user, as: :activity)
145 @doc "POST /api/v1/pleroma/accounts/:id/subscribe"
146 def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
147 with {:ok, subscription_target} <- User.subscribe(user, subscription_target) do
148 render(conn, "relationship.json", user: user, target: subscription_target)
150 {:error, message} -> json_response(conn, :forbidden, %{error: message})
154 @doc "POST /api/v1/pleroma/accounts/:id/unsubscribe"
155 def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
156 with {:ok, subscription_target} <- User.unsubscribe(user, subscription_target) do
157 render(conn, "relationship.json", user: user, target: subscription_target)
159 {:error, message} -> json_response(conn, :forbidden, %{error: message})