total rebase
[anni] / lib / pleroma / web / admin_api / controllers / invite_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.InviteController do
6   use Pleroma.Web, :controller
7
8   import Pleroma.Web.ControllerHelper, only: [json_response: 3]
9
10   alias Pleroma.Config
11   alias Pleroma.UserInviteToken
12   alias Pleroma.Web.Plugs.OAuthScopesPlug
13
14   require Logger
15
16   plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
17   plug(OAuthScopesPlug, %{scopes: ["admin:read:invites"]} when action == :index)
18
19   plug(
20     OAuthScopesPlug,
21     %{scopes: ["admin:write:invites"]} when action in [:create, :revoke, :email]
22   )
23
24   action_fallback(Pleroma.Web.AdminAPI.FallbackController)
25
26   defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InviteOperation
27
28   @doc "Get list of created invites"
29   def index(conn, _params) do
30     invites = UserInviteToken.list_invites()
31
32     render(conn, "index.json", invites: invites)
33   end
34
35   @doc "Create an account registration invite token"
36   def create(%{private: %{open_api_spex: %{body_params: params}}} = conn, _) do
37     {:ok, invite} = UserInviteToken.create_invite(params)
38
39     render(conn, "show.json", invite: invite)
40   end
41
42   @doc "Revokes invite by token"
43   def revoke(%{private: %{open_api_spex: %{body_params: %{token: token}}}} = conn, _) do
44     with {:ok, invite} <- UserInviteToken.find_by_token(token),
45          {:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do
46       render(conn, "show.json", invite: updated_invite)
47     else
48       nil -> {:error, :not_found}
49       error -> error
50     end
51   end
52
53   @doc "Sends registration invite via email"
54   def email(
55         %{
56           assigns: %{user: user},
57           private: %{open_api_spex: %{body_params: %{email: email} = params}}
58         } = conn,
59         _
60       ) do
61     with {_, false} <- {:registrations_open, Config.get([:instance, :registrations_open])},
62          {_, true} <- {:invites_enabled, Config.get([:instance, :invites_enabled])},
63          {:ok, invite_token} <- UserInviteToken.create_invite(),
64          {:ok, _} <-
65            user
66            |> Pleroma.Emails.UserEmail.user_invitation_email(
67              invite_token,
68              email,
69              params[:name]
70            )
71            |> Pleroma.Emails.Mailer.deliver() do
72       json_response(conn, :no_content, "")
73     else
74       {:registrations_open, _} ->
75         {:error, "To send invites you need to set the `registrations_open` option to false."}
76
77       {:invites_enabled, _} ->
78         {:error, "To send invites you need to set the `invites_enabled` option to true."}
79
80       {:error, error} ->
81         {:error, error}
82     end
83   end
84 end