Merge commit '07fed0fda2473fc4e1e3b01e863217391fd2902f'; commit 'e3173a279dad89dfce6e...
[akkoma] / lib / pleroma / web / pleroma_api / controllers / account_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.AccountController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper,
9 only: [json_response: 3, add_link_headers: 2, assign_account_by_id: 2]
10
11 alias Pleroma.User
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
17
18 require Pleroma.Constants
19
20 plug(
21 Majic.Plug,
22 [pool: Pleroma.MajicPool] when action in [:update_avatar, :update_background, :update_banner]
23 )
24
25 plug(
26 OpenApiSpex.Plug.PutApiSpec,
27 [module: Pleroma.Web.ApiSpec] when action == :confirmation_resend
28 )
29
30 plug(Pleroma.Web.ApiSpec.CastAndValidate)
31
32 plug(
33 :skip_plug,
34 [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug] when action == :confirmation_resend
35 )
36
37 plug(
38 OAuthScopesPlug,
39 %{scopes: ["follow", "write:follows"]} when action in [:subscribe, :unsubscribe]
40 )
41
42 plug(
43 OAuthScopesPlug,
44 %{scopes: ["read:favourites"], fallback: :proceed_unauthenticated} when action == :favourites
45 )
46
47 plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend)
48
49 plug(:assign_account_by_id when action in [:favourites, :subscribe, :unsubscribe])
50
51 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaAccountOperation
52
53 @doc "POST /api/v1/pleroma/accounts/confirmation_resend"
54 def confirmation_resend(conn, params) do
55 nickname_or_email = params[:email] || params[:nickname]
56
57 with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email),
58 {:ok, _} <- User.maybe_send_confirmation_email(user) do
59 json_response(conn, :no_content, "")
60 end
61 end
62
63 @doc "GET /api/v1/pleroma/accounts/:id/favourites"
64 def favourites(%{assigns: %{account: %{hide_favorites: true}}} = conn, _params) do
65 render_error(conn, :forbidden, "Can't get favorites")
66 end
67
68 def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do
69 params =
70 params
71 |> Map.put(:type, "Create")
72 |> Map.put(:favorited_by, user.ap_id)
73 |> Map.put(:blocking_user, for_user)
74
75 recipients =
76 if for_user do
77 [Pleroma.Constants.as_public()] ++ [for_user.ap_id | User.following(for_user)]
78 else
79 [Pleroma.Constants.as_public()]
80 end
81
82 activities =
83 recipients
84 |> ActivityPub.fetch_activities(params)
85 |> Enum.reverse()
86
87 conn
88 |> add_link_headers(activities)
89 |> put_view(StatusView)
90 |> render("index.json",
91 activities: activities,
92 for: for_user,
93 as: :activity
94 )
95 end
96
97 @doc "POST /api/v1/pleroma/accounts/:id/subscribe"
98 def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
99 with {:ok, _subscription} <- User.subscribe(user, subscription_target) do
100 render(conn, "relationship.json", user: user, target: subscription_target)
101 else
102 {:error, message} -> json_response(conn, :forbidden, %{error: message})
103 end
104 end
105
106 @doc "POST /api/v1/pleroma/accounts/:id/unsubscribe"
107 def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
108 with {:ok, _subscription} <- User.unsubscribe(user, subscription_target) do
109 render(conn, "relationship.json", user: user, target: subscription_target)
110 else
111 {:error, message} -> json_response(conn, :forbidden, %{error: message})
112 end
113 end
114 end