2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-06-12 05:52:54 -06:00
|
|
|
defmodule Pleroma.UserInviteToken do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
2019-04-06 03:58:22 -06:00
|
|
|
import Ecto.Query
|
2018-12-09 02:12:48 -07:00
|
|
|
alias Pleroma.Repo
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.UserInviteToken
|
2018-06-12 05:52:54 -06:00
|
|
|
|
2019-04-08 06:16:59 -06:00
|
|
|
@type t :: %__MODULE__{}
|
2019-04-06 03:58:22 -06:00
|
|
|
@type token :: String.t()
|
|
|
|
|
2018-06-12 05:52:54 -06:00
|
|
|
schema "user_invite_tokens" do
|
|
|
|
field(:token, :string)
|
|
|
|
field(:used, :boolean, default: false)
|
2019-04-06 03:58:22 -06:00
|
|
|
field(:max_use, :integer)
|
2019-04-08 03:01:28 -06:00
|
|
|
field(:expires_at, :date)
|
2019-04-06 07:24:22 -06:00
|
|
|
field(:uses, :integer, default: 0)
|
|
|
|
field(:invite_type, :string)
|
2018-06-12 05:52:54 -06:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-05-06 10:45:22 -06:00
|
|
|
@spec create_invite(map()) :: {:ok, UserInviteToken.t()}
|
2019-04-06 07:24:22 -06:00
|
|
|
def create_invite(params \\ %{}) do
|
|
|
|
%UserInviteToken{}
|
2019-04-08 03:01:28 -06:00
|
|
|
|> cast(params, [:max_use, :expires_at])
|
2019-04-06 07:24:22 -06:00
|
|
|
|> add_token()
|
|
|
|
|> assign_type()
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp add_token(changeset) do
|
2018-06-12 05:52:54 -06:00
|
|
|
token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
|
2019-04-06 07:24:22 -06:00
|
|
|
put_change(changeset, :token, token)
|
|
|
|
end
|
2018-06-12 05:52:54 -06:00
|
|
|
|
2019-04-08 03:01:28 -06:00
|
|
|
defp assign_type(%{changes: %{max_use: _max_use, expires_at: _expires_at}} = changeset) do
|
2019-04-06 07:24:22 -06:00
|
|
|
put_change(changeset, :invite_type, "reusable_date_limited")
|
|
|
|
end
|
2019-04-06 03:58:22 -06:00
|
|
|
|
2019-04-08 03:01:28 -06:00
|
|
|
defp assign_type(%{changes: %{expires_at: _expires_at}} = changeset) do
|
2019-04-06 07:24:22 -06:00
|
|
|
put_change(changeset, :invite_type, "date_limited")
|
|
|
|
end
|
2018-06-12 05:52:54 -06:00
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
defp assign_type(%{changes: %{max_use: _max_use}} = changeset) do
|
|
|
|
put_change(changeset, :invite_type, "reusable")
|
2018-06-12 05:52:54 -06:00
|
|
|
end
|
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
defp assign_type(changeset), do: put_change(changeset, :invite_type, "one_time")
|
|
|
|
|
|
|
|
@spec list_invites() :: [UserInviteToken.t()]
|
2019-04-06 03:58:22 -06:00
|
|
|
def list_invites do
|
|
|
|
query = from(u in UserInviteToken, order_by: u.id)
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
@spec update_invite!(UserInviteToken.t(), map()) :: UserInviteToken.t() | no_return()
|
|
|
|
def update_invite!(invite, changes) do
|
|
|
|
change(invite, changes) |> Repo.update!()
|
2018-06-12 05:52:54 -06:00
|
|
|
end
|
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
@spec update_invite(UserInviteToken.t(), map()) ::
|
|
|
|
{:ok, UserInviteToken.t()} | {:error, Changeset.t()}
|
|
|
|
def update_invite(invite, changes) do
|
|
|
|
change(invite, changes) |> Repo.update()
|
2018-06-12 05:52:54 -06:00
|
|
|
end
|
2019-04-06 03:58:22 -06:00
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
@spec find_by_token!(token()) :: UserInviteToken.t() | no_return()
|
|
|
|
def find_by_token!(token), do: Repo.get_by!(UserInviteToken, token: token)
|
2019-04-06 03:58:22 -06:00
|
|
|
|
2019-04-06 09:38:35 -06:00
|
|
|
@spec find_by_token(token()) :: {:ok, UserInviteToken.t()} | nil
|
|
|
|
def find_by_token(token) do
|
2019-07-21 20:43:15 -06:00
|
|
|
with %UserInviteToken{} = invite <- Repo.get_by(UserInviteToken, token: token) do
|
2019-04-06 09:38:35 -06:00
|
|
|
{:ok, invite}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
@spec valid_invite?(UserInviteToken.t()) :: boolean()
|
|
|
|
def valid_invite?(%{invite_type: "one_time"} = invite) do
|
|
|
|
not invite.used
|
2019-04-06 03:58:22 -06:00
|
|
|
end
|
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
def valid_invite?(%{invite_type: "date_limited"} = invite) do
|
|
|
|
not_overdue_date?(invite) and not invite.used
|
2019-04-06 03:58:22 -06:00
|
|
|
end
|
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
def valid_invite?(%{invite_type: "reusable"} = invite) do
|
|
|
|
invite.uses < invite.max_use and not invite.used
|
2019-04-06 03:58:22 -06:00
|
|
|
end
|
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
def valid_invite?(%{invite_type: "reusable_date_limited"} = invite) do
|
|
|
|
not_overdue_date?(invite) and invite.uses < invite.max_use and not invite.used
|
2019-04-06 03:58:22 -06:00
|
|
|
end
|
|
|
|
|
2019-04-08 07:08:16 -06:00
|
|
|
defp not_overdue_date?(%{expires_at: expires_at}) do
|
|
|
|
Date.compare(Date.utc_today(), expires_at) in [:lt, :eq]
|
2019-04-06 03:58:22 -06:00
|
|
|
end
|
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
@spec update_usage!(UserInviteToken.t()) :: nil | UserInviteToken.t() | no_return()
|
|
|
|
def update_usage!(%{invite_type: "date_limited"}), do: nil
|
2019-04-06 03:58:22 -06:00
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
def update_usage!(%{invite_type: "one_time"} = invite),
|
|
|
|
do: update_invite!(invite, %{used: true})
|
2019-04-06 03:58:22 -06:00
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
def update_usage!(%{invite_type: invite_type} = invite)
|
|
|
|
when invite_type == "reusable" or invite_type == "reusable_date_limited" do
|
2019-04-06 03:58:22 -06:00
|
|
|
changes = %{
|
2019-04-06 07:24:22 -06:00
|
|
|
uses: invite.uses + 1
|
2019-04-06 03:58:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
changes =
|
2019-04-06 07:24:22 -06:00
|
|
|
if changes.uses >= invite.max_use do
|
2019-04-06 03:58:22 -06:00
|
|
|
Map.put(changes, :used, true)
|
|
|
|
else
|
|
|
|
changes
|
|
|
|
end
|
|
|
|
|
2019-04-06 07:24:22 -06:00
|
|
|
update_invite!(invite, changes)
|
2019-04-06 03:58:22 -06:00
|
|
|
end
|
2018-06-12 05:52:54 -06:00
|
|
|
end
|