Add account 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.Plugs.EnsurePublicOrAuthenticatedPlug
12 alias Pleroma.Plugs.OAuthScopesPlug
13 alias Pleroma.Plugs.RateLimiter
14 alias Pleroma.User
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.MastodonAPI.StatusView
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(
43 OAuthScopesPlug,
44 %{scopes: ["write:accounts"]} when action in [:add_aliases, :delete_aliases]
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 plug(:put_view, Pleroma.Web.MastodonAPI.AccountView)
51
52 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaAccountOperation
53
54 @doc "POST /api/v1/pleroma/accounts/confirmation_resend"
55 def confirmation_resend(conn, params) do
56 nickname_or_email = params[:email] || params[:nickname]
57
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, "")
61 end
62 end
63
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")
67 end
68
69 def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do
70 params =
71 params
72 |> Map.put(:type, "Create")
73 |> Map.put(:favorited_by, user.ap_id)
74 |> Map.put(:blocking_user, for_user)
75
76 recipients =
77 if for_user do
78 [Pleroma.Constants.as_public()] ++ [for_user.ap_id | User.following(for_user)]
79 else
80 [Pleroma.Constants.as_public()]
81 end
82
83 activities =
84 recipients
85 |> ActivityPub.fetch_activities(params)
86 |> Enum.reverse()
87
88 conn
89 |> add_link_headers(activities)
90 |> put_view(StatusView)
91 |> render("index.json",
92 activities: activities,
93 for: for_user,
94 as: :activity
95 )
96 end
97
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)
102 else
103 {:error, message} -> json_response(conn, :forbidden, %{error: message})
104 end
105 end
106
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)
111 else
112 {:error, message} -> json_response(conn, :forbidden, %{error: message})
113 end
114 end
115
116 @doc "POST /api/v1/pleroma/accounts/ap_aliases"
117 def add_aliases(%{assigns: %{user: user}, body_params: %{aliases: aliases}} = conn, _params)
118 when is_list(aliases) do
119 with {:ok, user} <- User.add_aliases(user, aliases) do
120 render(conn, "show.json", user: user)
121 else
122 {:error, message} -> json_response(conn, :forbidden, %{error: message})
123 end
124 end
125
126 @doc "DELETE /api/v1/pleroma/accounts/ap_aliases"
127 def delete_aliases(%{assigns: %{user: user}, body_params: %{aliases: aliases}} = conn, _params)
128 when is_list(aliases) do
129 with {:ok, user} <- User.delete_aliases(user, aliases) do
130 render(conn, "show.json", user: user)
131 else
132 {:error, message} -> json_response(conn, :forbidden, %{error: message})
133 end
134 end
135 end