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

View file

@ -71,6 +71,13 @@ def update_invite(invite, changes) do
@spec find_by_token!(token()) :: UserInviteToken.t() | no_return() @spec find_by_token!(token()) :: UserInviteToken.t() | no_return()
def find_by_token!(token), do: Repo.get_by!(UserInviteToken, token: token) 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() @spec valid_invite?(UserInviteToken.t()) :: boolean()
def valid_invite?(%{invite_type: "one_time"} = invite) do def valid_invite?(%{invite_type: "one_time"} = invite) do
not invite.used not invite.used

View file

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