X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fuser_invite_token.ex;h=a08ba99f1107710aa33e794f791c9562992ceaee;hb=f341a8e142ad9d4c92afc4a97ef387df068e38e0;hp=4efdbdc320f4de36ce939ee8d4a03450c047beb0;hpb=47b07cec495528ce22f83ca56717cc74aa0096f3;p=akkoma
diff --git a/lib/pleroma/user_invite_token.ex b/lib/pleroma/user_invite_token.ex
index 4efdbdc32..a08ba99f1 100644
--- a/lib/pleroma/user_invite_token.ex
+++ b/lib/pleroma/user_invite_token.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.UserInviteToken do
@@ -10,23 +10,24 @@ defmodule Pleroma.UserInviteToken do
alias Pleroma.Repo
alias Pleroma.UserInviteToken
+ @type t :: %__MODULE__{}
@type token :: String.t()
schema "user_invite_tokens" do
field(:token, :string)
field(:used, :boolean, default: false)
field(:max_use, :integer)
- field(:expire_at, :date)
+ field(:expires_at, :date)
field(:uses, :integer, default: 0)
field(:invite_type, :string)
timestamps()
end
- @spec create_invite(map()) :: UserInviteToken.t()
+ @spec create_invite(map()) :: {:ok, UserInviteToken.t()}
def create_invite(params \\ %{}) do
%UserInviteToken{}
- |> cast(params, ~w(max_use expire_at)a)
+ |> cast(params, [:max_use, :expires_at])
|> add_token()
|> assign_type()
|> Repo.insert()
@@ -37,11 +38,11 @@ defmodule Pleroma.UserInviteToken do
put_change(changeset, :token, token)
end
- defp assign_type(%{changes: %{max_use: _max_use, expire_at: _expire_at}} = changeset) do
+ defp assign_type(%{changes: %{max_use: _max_use, expires_at: _expires_at}} = changeset) do
put_change(changeset, :invite_type, "reusable_date_limited")
end
- defp assign_type(%{changes: %{expire_at: _expire_at}} = changeset) do
+ defp assign_type(%{changes: %{expires_at: _expires_at}} = changeset) do
put_change(changeset, :invite_type, "date_limited")
end
@@ -71,6 +72,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 %UserInviteToken{} = 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
@@ -88,9 +96,8 @@ defmodule Pleroma.UserInviteToken do
not_overdue_date?(invite) and invite.uses < invite.max_use and not invite.used
end
- defp not_overdue_date?(%{expire_at: expire_at} = invite) do
- Date.compare(Date.utc_today(), expire_at) in [:lt, :eq] ||
- (update_invite!(invite, %{used: true}) && false)
+ defp not_overdue_date?(%{expires_at: expires_at}) do
+ Date.compare(Date.utc_today(), expires_at) in [:lt, :eq]
end
@spec update_usage!(UserInviteToken.t()) :: nil | UserInviteToken.t() | no_return()