1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.MastodonAPI.StatusView
14 alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
15 alias Pleroma.Web.Plugs.OAuthScopesPlug
16 alias Pleroma.Web.Plugs.RateLimiter
18 require Pleroma.Constants
22 [pool: Pleroma.MajicPool] when action in [:update_avatar, :update_background, :update_banner]
26 OpenApiSpex.Plug.PutApiSpec,
27 [module: Pleroma.Web.ApiSpec] when action == :confirmation_resend
30 plug(Pleroma.Web.ApiSpec.CastAndValidate)
34 [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug] when action == :confirmation_resend
39 %{scopes: ["follow", "write:follows"]} when action in [:subscribe, :unsubscribe]
44 %{scopes: ["read:favourites"], fallback: :proceed_unauthenticated} when action == :favourites
47 plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend)
49 plug(:assign_account_by_id when action in [:favourites, :subscribe, :unsubscribe])
50 plug(:put_view, Pleroma.Web.MastodonAPI.AccountView)
52 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaAccountOperation
54 @doc "POST /api/v1/pleroma/accounts/confirmation_resend"
55 def confirmation_resend(conn, params) do
56 nickname_or_email = params[:email] || params[:nickname]
58 with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email),
59 {:ok, _} <- User.try_send_confirmation_email(user) do
60 json_response(conn, :no_content, "")
64 @doc "GET /api/v1/pleroma/accounts/:id/favourites"
65 def favourites(%{assigns: %{account: %{hide_favorites: true}}} = conn, _params) do
66 render_error(conn, :forbidden, "Can't get favorites")
69 def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do
72 |> Map.put(:type, "Create")
73 |> Map.put(:favorited_by, user.ap_id)
74 |> Map.put(:blocking_user, for_user)
78 [Pleroma.Constants.as_public()] ++ [for_user.ap_id | User.following(for_user)]
80 [Pleroma.Constants.as_public()]
85 |> ActivityPub.fetch_activities(params)
89 |> add_link_headers(activities)
90 |> put_view(StatusView)
91 |> render("index.json",
92 activities: activities,
98 @doc "POST /api/v1/pleroma/accounts/:id/subscribe"
99 def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
100 with {:ok, _subscription} <- User.subscribe(user, subscription_target) do
101 render(conn, "relationship.json", user: user, target: subscription_target)
103 {:error, message} -> json_response(conn, :forbidden, %{error: message})
107 @doc "POST /api/v1/pleroma/accounts/:id/unsubscribe"
108 def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
109 with {:ok, _subscription} <- User.unsubscribe(user, subscription_target) do
110 render(conn, "relationship.json", user: user, target: subscription_target)
112 {:error, message} -> json_response(conn, :forbidden, %{error: message})