little channges

This commit is contained in:
Alex S 2019-04-06 22:38:35 +07:00
parent 76d8928cf1
commit ce8d457132
3 changed files with 47 additions and 39 deletions

View File

@ -30,16 +30,16 @@ defmodule Mix.Tasks.Pleroma.User do
mix pleroma.user invite [OPTION...]
Options:
- `--expire_date DATE` - last day on which token is active (e.g. "2019-04-05")
- `--max_use NUMBER` - maximum numbers of token use
- `--expire_at DATE` - last day on which token is active (e.g. "2019-04-05")
- `--max_use NUMBER` - maximum numbers of token uses
## Generated invites list
## List generated invites
mix pleroma.user invites_list
mix pleroma.user invites
## Revoke invite
mix pleroma.user invite_revoke TOKEN OR TOKEN_ID
mix pleroma.user revoke_invite TOKEN OR TOKEN_ID
## Delete the user's account.
@ -304,21 +304,24 @@ defmodule Mix.Tasks.Pleroma.User do
{options, [], []} =
OptionParser.parse(rest,
strict: [
expire_date: :string,
expire_at: :string,
max_use: :integer
]
)
expire_at =
with expire_date when expire_date != nil <- Keyword.get(options, :expire_date) do
Date.from_iso8601!(expire_date)
end
options =
options
|> Keyword.update(:expire_at, {:ok, nil}, fn
nil -> {:ok, nil}
val -> Date.from_iso8601(val)
end)
|> Enum.into(%{})
options = Keyword.put(options, :expire_at, expire_at)
options = Enum.into(options, %{})
Common.start_pleroma()
with {:ok, invite} <- UserInviteToken.create_invite(options) do
with {:ok, val} <- options[:expire_at],
options = Map.put(options, :expire_at, val),
{:ok, invite} <- UserInviteToken.create_invite(options) do
Mix.shell().info(
"Generated user invite token " <> String.replace(invite.invite_type, "_", " ")
)
@ -332,44 +335,41 @@ defmodule Mix.Tasks.Pleroma.User do
IO.puts(url)
else
_ ->
Mix.shell().error("Could not create invite token.")
error ->
Mix.shell().error("Could not create invite token: #{inspect(error)}")
end
end
def run(["invites_list"]) do
def run(["invites"]) do
Common.start_pleroma()
Mix.shell().info("Invites list:")
UserInviteToken.list_invites()
|> Enum.each(fn invite ->
expire_date =
case invite.expire_at do
nil -> nil
date -> " | Expire date: #{Date.to_string(date)}"
expire_info =
with expire_at when not is_nil(expire_at) <- invite.expire_at do
" | Expire at: #{Date.to_string(expire_at)}"
end
using_info =
case invite.max_use do
nil -> nil
max_use -> " | Max use: #{max_use} Left use: #{max_use - invite.uses}"
with max_use when not is_nil(max_use) <- invite.max_use do
" | Max use: #{max_use} Left use: #{max_use - invite.uses}"
end
Mix.shell().info(
"ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.token_type} | Used: #{
"ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
invite.used
}#{expire_date}#{using_info}"
}#{expire_info}#{using_info}"
)
end)
end
def run(["invite_revoke", token]) do
def run(["revoke_invite", token]) do
Common.start_pleroma()
invite = UserInviteToken.find_by_token!(token)
with {:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
with {:ok, invite} <- UserInviteToken.find_by_token(token),
{:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
Mix.shell().info("Invite for token #{token} was revoked.")
else
_ -> Mix.shell().error("No invite found with token #{token}")

View File

@ -71,6 +71,13 @@ defmodule Pleroma.UserInviteToken do
@spec find_by_token!(token()) :: UserInviteToken.t() | no_return()
def find_by_token!(token), do: Repo.get_by!(UserInviteToken, token: token)
@spec find_by_token(token()) :: {:ok, UserInviteToken.t()} | nil
def find_by_token(token) do
with invite <- Repo.get_by(UserInviteToken, token: token) do
{:ok, invite}
end
end
@spec valid_invite?(UserInviteToken.t()) :: boolean()
def valid_invite?(%{invite_type: "one_time"} = invite) do
not invite.used

View File

@ -252,7 +252,7 @@ defmodule Mix.Tasks.Pleroma.UserTest do
assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
"invite",
"--expire-date",
"--expire-at",
Date.to_string(Date.utc_today())
])
end)
@ -280,7 +280,7 @@ defmodule Mix.Tasks.Pleroma.UserTest do
"invite",
"--max-use",
"5",
"--expire-date",
"--expire-at",
Date.to_string(Date.utc_today())
])
end)
@ -290,18 +290,19 @@ defmodule Mix.Tasks.Pleroma.UserTest do
end
end
describe "running invites_list" do
describe "running invites" do
test "invites are listed" do
{:ok, invite} = Pleroma.UserInviteToken.create_invite()
{:ok, invite2} =
Pleroma.UserInviteToken.create_invite(%{expire_at: Date.utc_today(), max_use: 15})
assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
"invites_list"
])
end)
# assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
"invites"
])
# end)
assert_received {:mix_shell, :info, [message]}
assert_received {:mix_shell, :info, [message2]}
@ -312,13 +313,13 @@ defmodule Mix.Tasks.Pleroma.UserTest do
end
end
describe "running invite revoke" do
describe "running revoke_invite" do
test "invite is revoked" do
{:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expire_at: Date.utc_today()})
assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
"invite_revoke",
"revoke_invite",
invite.token
])
end)