Add AdminAPI.InviteView

This commit is contained in:
Egor Kislitsyn 2020-05-26 15:21:33 +04:00
parent 2a4f965191
commit fca48154a2
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
6 changed files with 49 additions and 43 deletions

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.InviteTokenController do defmodule Pleroma.Web.AdminAPI.InviteController do
use Pleroma.Web, :controller use Pleroma.Web, :controller
import Pleroma.Web.ControllerHelper, only: [json_response: 3] import Pleroma.Web.ControllerHelper, only: [json_response: 3]
@ -10,7 +10,6 @@ defmodule Pleroma.Web.AdminAPI.InviteTokenController do
alias Pleroma.Config alias Pleroma.Config
alias Pleroma.Plugs.OAuthScopesPlug alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.UserInviteToken alias Pleroma.UserInviteToken
alias Pleroma.Web.AdminAPI.AccountView
require Logger require Logger
@ -24,33 +23,30 @@ defmodule Pleroma.Web.AdminAPI.InviteTokenController do
action_fallback(Pleroma.Web.AdminAPI.FallbackController) action_fallback(Pleroma.Web.AdminAPI.FallbackController)
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InviteTokenOperation defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InviteOperation
@doc "Get list of created invites" @doc "Get list of created invites"
def index(conn, _params) do def index(conn, _params) do
invites = UserInviteToken.list_invites() invites = UserInviteToken.list_invites()
conn render(conn, "index.json", invites: invites)
|> put_view(AccountView)
|> render("invites.json", %{invites: invites})
end end
@doc "Create an account registration invite token" @doc "Create an account registration invite token"
def create(%{body_params: params} = conn, _) do def create(%{body_params: params} = conn, _) do
{:ok, invite} = UserInviteToken.create_invite(params) {:ok, invite} = UserInviteToken.create_invite(params)
json(conn, AccountView.render("invite.json", %{invite: invite})) render(conn, "show.json", invite: invite)
end end
@doc "Revokes invite by token" @doc "Revokes invite by token"
def revoke(%{body_params: %{token: token}} = conn, _) do def revoke(%{body_params: %{token: token}} = conn, _) do
with {:ok, invite} <- UserInviteToken.find_by_token(token), with {:ok, invite} <- UserInviteToken.find_by_token(token),
{:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do {:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do
conn render(conn, "show.json", invite: updated_invite)
|> put_view(AccountView)
|> render("invite.json", %{invite: updated_invite})
else else
nil -> {:error, :not_found} nil -> {:error, :not_found}
error -> error
end end
end end
@ -59,14 +55,14 @@ defmodule Pleroma.Web.AdminAPI.InviteTokenController do
with {_, false} <- {:registrations_open, Config.get([:instance, :registrations_open])}, with {_, false} <- {:registrations_open, Config.get([:instance, :registrations_open])},
{_, true} <- {:invites_enabled, Config.get([:instance, :invites_enabled])}, {_, true} <- {:invites_enabled, Config.get([:instance, :invites_enabled])},
{:ok, invite_token} <- UserInviteToken.create_invite(), {:ok, invite_token} <- UserInviteToken.create_invite(),
email <- {:ok, _} <-
Pleroma.Emails.UserEmail.user_invitation_email( user
user, |> Pleroma.Emails.UserEmail.user_invitation_email(
invite_token, invite_token,
email, email,
params[:name] params[:name]
), )
{:ok, _} <- Pleroma.Emails.Mailer.deliver(email) do |> Pleroma.Emails.Mailer.deliver() do
json_response(conn, :no_content, "") json_response(conn, :no_content, "")
else else
{:registrations_open, _} -> {:registrations_open, _} ->
@ -74,6 +70,9 @@ defmodule Pleroma.Web.AdminAPI.InviteTokenController do
{:invites_enabled, _} -> {:invites_enabled, _} ->
{:error, "To send invites you need to set the `invites_enabled` option to true."} {:error, "To send invites you need to set the `invites_enabled` option to true."}
{:error, error} ->
{:error, error}
end end
end end
end end

View file

@ -80,24 +80,6 @@ defmodule Pleroma.Web.AdminAPI.AccountView do
} }
end end
def render("invite.json", %{invite: invite}) do
%{
"id" => invite.id,
"token" => invite.token,
"used" => invite.used,
"expires_at" => invite.expires_at,
"uses" => invite.uses,
"max_use" => invite.max_use,
"invite_type" => invite.invite_type
}
end
def render("invites.json", %{invites: invites}) do
%{
invites: render_many(invites, AccountView, "invite.json", as: :invite)
}
end
def render("created.json", %{user: user}) do def render("created.json", %{user: user}) do
%{ %{
type: "success", type: "success",

View file

@ -0,0 +1,25 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.InviteView do
use Pleroma.Web, :view
def render("index.json", %{invites: invites}) do
%{
invites: render_many(invites, __MODULE__, "show.json", as: :invite)
}
end
def render("show.json", %{invite: invite}) do
%{
"id" => invite.id,
"token" => invite.token,
"used" => invite.used,
"expires_at" => invite.expires_at,
"uses" => invite.uses,
"max_use" => invite.max_use,
"invite_type" => invite.invite_type
}
end
end

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ApiSpec.Admin.InviteTokenOperation do defmodule Pleroma.Web.ApiSpec.Admin.InviteOperation do
alias OpenApiSpex.Operation alias OpenApiSpex.Operation
alias OpenApiSpex.Schema alias OpenApiSpex.Schema
alias Pleroma.Web.ApiSpec.Schemas.ApiError alias Pleroma.Web.ApiSpec.Schemas.ApiError
@ -18,7 +18,7 @@ defmodule Pleroma.Web.ApiSpec.Admin.InviteTokenOperation do
%Operation{ %Operation{
tags: ["Admin", "Invites"], tags: ["Admin", "Invites"],
summary: "Get a list of generated invites", summary: "Get a list of generated invites",
operationId: "AdminAPI.InviteTokenController.index", operationId: "AdminAPI.InviteController.index",
security: [%{"oAuth" => ["read:invites"]}], security: [%{"oAuth" => ["read:invites"]}],
responses: %{ responses: %{
200 => 200 =>
@ -49,7 +49,7 @@ defmodule Pleroma.Web.ApiSpec.Admin.InviteTokenOperation do
%Operation{ %Operation{
tags: ["Admin", "Invites"], tags: ["Admin", "Invites"],
summary: "Create an account registration invite token", summary: "Create an account registration invite token",
operationId: "AdminAPI.InviteTokenController.create", operationId: "AdminAPI.InviteController.create",
security: [%{"oAuth" => ["write:invites"]}], security: [%{"oAuth" => ["write:invites"]}],
requestBody: requestBody:
request_body("Parameters", %Schema{ request_body("Parameters", %Schema{
@ -69,7 +69,7 @@ defmodule Pleroma.Web.ApiSpec.Admin.InviteTokenOperation do
%Operation{ %Operation{
tags: ["Admin", "Invites"], tags: ["Admin", "Invites"],
summary: "Revoke invite by token", summary: "Revoke invite by token",
operationId: "AdminAPI.InviteTokenController.revoke", operationId: "AdminAPI.InviteController.revoke",
security: [%{"oAuth" => ["write:invites"]}], security: [%{"oAuth" => ["write:invites"]}],
requestBody: requestBody:
request_body( request_body(
@ -95,7 +95,7 @@ defmodule Pleroma.Web.ApiSpec.Admin.InviteTokenOperation do
%Operation{ %Operation{
tags: ["Admin", "Invites"], tags: ["Admin", "Invites"],
summary: "Sends registration invite via email", summary: "Sends registration invite via email",
operationId: "AdminAPI.InviteTokenController.email", operationId: "AdminAPI.InviteController.email",
security: [%{"oAuth" => ["write:invites"]}], security: [%{"oAuth" => ["write:invites"]}],
requestBody: requestBody:
request_body( request_body(

View file

@ -164,10 +164,10 @@ defmodule Pleroma.Web.Router do
post("/relay", AdminAPIController, :relay_follow) post("/relay", AdminAPIController, :relay_follow)
delete("/relay", AdminAPIController, :relay_unfollow) delete("/relay", AdminAPIController, :relay_unfollow)
post("/users/invite_token", InviteTokenController, :create) post("/users/invite_token", InviteController, :create)
get("/users/invites", InviteTokenController, :index) get("/users/invites", InviteController, :index)
post("/users/revoke_invite", InviteTokenController, :revoke) post("/users/revoke_invite", InviteController, :revoke)
post("/users/email_invite", InviteTokenController, :email) post("/users/email_invite", InviteController, :email)
get("/users/:nickname/password_reset", AdminAPIController, :get_password_reset) get("/users/:nickname/password_reset", AdminAPIController, :get_password_reset)
patch("/users/force_password_reset", AdminAPIController, :force_password_reset) patch("/users/force_password_reset", AdminAPIController, :force_password_reset)

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.InviteTokenControllerTest do defmodule Pleroma.Web.AdminAPI.InviteControllerTest do
use Pleroma.Web.ConnCase, async: true use Pleroma.Web.ConnCase, async: true
import Pleroma.Factory import Pleroma.Factory