1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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,
12 embed_relationships?: 1,
13 assign_account_by_id: 2
17 alias Pleroma.Web.ActivityPub.ActivityPub
18 alias Pleroma.Web.MastodonAPI.StatusView
19 alias Pleroma.Web.Plugs.OAuthScopesPlug
20 alias Pleroma.Web.Plugs.RateLimiter
22 require Pleroma.Constants
26 [pool: Pleroma.MajicPool] when action in [:update_avatar, :update_background, :update_banner]
30 OpenApiSpex.Plug.PutApiSpec,
31 [module: Pleroma.Web.ApiSpec] when action == :confirmation_resend
34 plug(Pleroma.Web.ApiSpec.CastAndValidate)
36 plug(:skip_auth when action == :confirmation_resend)
40 %{scopes: ["follow", "write:follows"]} when action in [:subscribe, :unsubscribe]
45 %{scopes: ["read:favourites"], fallback: :proceed_unauthenticated} when action == :favourites
50 %{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]}
51 when action == :endorsements
56 %{scopes: ["read:accounts"]} when action == :birthdays
59 plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend)
63 when action in [:favourites, :endorsements, :subscribe, :unsubscribe]
66 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaAccountOperation
68 @doc "POST /api/v1/pleroma/accounts/confirmation_resend"
69 def confirmation_resend(conn, params) do
70 nickname_or_email = params[:email] || params[:nickname]
72 with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email),
73 {:ok, _} <- User.maybe_send_confirmation_email(user) do
74 json_response(conn, :no_content, "")
78 @doc "GET /api/v1/pleroma/accounts/:id/favourites"
79 def favourites(%{assigns: %{account: %{hide_favorites: true}}} = conn, _params) do
80 render_error(conn, :forbidden, "Can't get favorites")
83 def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do
86 |> Map.put(:type, "Create")
87 |> Map.put(:favorited_by, user.ap_id)
88 |> Map.put(:blocking_user, for_user)
92 [Pleroma.Constants.as_public()] ++ [for_user.ap_id | User.following(for_user)]
94 [Pleroma.Constants.as_public()]
99 |> ActivityPub.fetch_activities(params)
103 |> add_link_headers(activities)
104 |> put_view(StatusView)
105 |> render("index.json",
106 activities: activities,
112 @doc "GET /api/v1/pleroma/accounts/:id/endorsements"
113 def endorsements(%{assigns: %{user: for_user, account: user}} = conn, params) do
116 |> User.endorsed_users_relation(_restrict_deactivated = true)
117 |> Pleroma.Repo.all()
120 |> render("index.json",
124 embed_relationships: embed_relationships?(params)
128 @doc "POST /api/v1/pleroma/accounts/:id/subscribe"
129 def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
130 with {:ok, _subscription} <- User.subscribe(user, subscription_target) do
131 render(conn, "relationship.json", user: user, target: subscription_target)
133 {:error, message} -> json_response(conn, :forbidden, %{error: message})
137 @doc "POST /api/v1/pleroma/accounts/:id/unsubscribe"
138 def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
139 with {:ok, _subscription} <- User.unsubscribe(user, subscription_target) do
140 render(conn, "relationship.json", user: user, target: subscription_target)
142 {:error, message} -> json_response(conn, :forbidden, %{error: message})
146 @doc "GET /api/v1/pleroma/birthdays"
147 def birthdays(%{assigns: %{user: %User{} = user}} = conn, %{day: day, month: month} = _params) do
149 User.get_friends_birthdays_query(user, day, month)
150 |> Pleroma.Repo.all()
153 |> render("index.json",