Merge remote-tracking branch 'upstream/develop' into aliases
[akkoma] / lib / pleroma / web / pleroma_api / controllers / account_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 OpenApiSpex.Plug.PutApiSpec,
22 [module: Pleroma.Web.ApiSpec] when action == :confirmation_resend
23 )
24
25 plug(Pleroma.Web.ApiSpec.CastAndValidate)
26
27 plug(
28 :skip_plug,
29 [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug] when action == :confirmation_resend
30 )
31
32 plug(
33 OAuthScopesPlug,
34 %{scopes: ["follow", "write:follows"]} when action in [:subscribe, :unsubscribe]
35 )
36
37 plug(
38 OAuthScopesPlug,
39 %{scopes: ["read:favourites"], fallback: :proceed_unauthenticated} when action == :favourites
40 )
41
42 plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend)
43
44 plug(:assign_account_by_id when action in [:favourites, :subscribe, :unsubscribe])
45 plug(:put_view, Pleroma.Web.MastodonAPI.AccountView)
46
47 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaAccountOperation
48
49 @doc "POST /api/v1/pleroma/accounts/confirmation_resend"
50 def confirmation_resend(conn, params) do
51 nickname_or_email = params[:email] || params[:nickname]
52
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, "")
56 end
57 end
58
59 @doc "GET /api/v1/pleroma/accounts/:id/favourites"
60 def favourites(%{assigns: %{account: %{hide_favorites: true}}} = conn, _params) do
61 render_error(conn, :forbidden, "Can't get favorites")
62 end
63
64 def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do
65 params =
66 params
67 |> Map.put(:type, "Create")
68 |> Map.put(:favorited_by, user.ap_id)
69 |> Map.put(:blocking_user, for_user)
70
71 recipients =
72 if for_user do
73 [Pleroma.Constants.as_public()] ++ [for_user.ap_id | User.following(for_user)]
74 else
75 [Pleroma.Constants.as_public()]
76 end
77
78 activities =
79 recipients
80 |> ActivityPub.fetch_activities(params)
81 |> Enum.reverse()
82
83 conn
84 |> add_link_headers(activities)
85 |> put_view(StatusView)
86 |> render("index.json",
87 activities: activities,
88 for: for_user,
89 as: :activity
90 )
91 end
92
93 @doc "POST /api/v1/pleroma/accounts/:id/subscribe"
94 def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
95 with {:ok, _subscription} <- User.subscribe(user, subscription_target) do
96 render(conn, "relationship.json", user: user, target: subscription_target)
97 else
98 {:error, message} -> json_response(conn, :forbidden, %{error: message})
99 end
100 end
101
102 @doc "POST /api/v1/pleroma/accounts/:id/unsubscribe"
103 def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
104 with {:ok, _subscription} <- User.unsubscribe(user, subscription_target) do
105 render(conn, "relationship.json", user: user, target: subscription_target)
106 else
107 {:error, message} -> json_response(conn, :forbidden, %{error: message})
108 end
109 end
110 end