1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserInviteToken do
11 alias Pleroma.UserInviteToken
13 @type t :: %__MODULE__{}
14 @type token :: String.t()
16 schema "user_invite_tokens" do
17 field(:token, :string)
18 field(:used, :boolean, default: false)
19 field(:max_use, :integer)
20 field(:expires_at, :date)
21 field(:uses, :integer, default: 0)
22 field(:invite_type, :string)
27 @spec create_invite(map()) :: {:ok, UserInviteToken.t()}
28 def create_invite(params \\ %{}) do
30 |> cast(params, [:max_use, :expires_at])
36 defp add_token(changeset) do
37 token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
38 put_change(changeset, :token, token)
41 defp assign_type(%{changes: %{max_use: _max_use, expires_at: _expires_at}} = changeset) do
42 put_change(changeset, :invite_type, "reusable_date_limited")
45 defp assign_type(%{changes: %{expires_at: _expires_at}} = changeset) do
46 put_change(changeset, :invite_type, "date_limited")
49 defp assign_type(%{changes: %{max_use: _max_use}} = changeset) do
50 put_change(changeset, :invite_type, "reusable")
53 defp assign_type(changeset), do: put_change(changeset, :invite_type, "one_time")
55 @spec list_invites() :: [UserInviteToken.t()]
57 query = from(u in UserInviteToken, order_by: u.id)
61 @spec update_invite!(UserInviteToken.t(), map()) :: UserInviteToken.t() | no_return()
62 def update_invite!(invite, changes) do
63 change(invite, changes) |> Repo.update!()
66 @spec update_invite(UserInviteToken.t(), map()) ::
67 {:ok, UserInviteToken.t()} | {:error, Changeset.t()}
68 def update_invite(invite, changes) do
69 change(invite, changes) |> Repo.update()
72 @spec find_by_token!(token()) :: UserInviteToken.t() | no_return()
73 def find_by_token!(token), do: Repo.get_by!(UserInviteToken, token: token)
75 @spec find_by_token(token()) :: {:ok, UserInviteToken.t()} | nil
76 def find_by_token(token) do
77 with %UserInviteToken{} = invite <- Repo.get_by(UserInviteToken, token: token) do
82 @spec valid_invite?(UserInviteToken.t()) :: boolean()
83 def valid_invite?(%{invite_type: "one_time"} = invite) do
87 def valid_invite?(%{invite_type: "date_limited"} = invite) do
88 not_overdue_date?(invite) and not invite.used
91 def valid_invite?(%{invite_type: "reusable"} = invite) do
92 invite.uses < invite.max_use and not invite.used
95 def valid_invite?(%{invite_type: "reusable_date_limited"} = invite) do
96 not_overdue_date?(invite) and invite.uses < invite.max_use and not invite.used
99 defp not_overdue_date?(%{expires_at: expires_at}) do
100 Date.compare(Date.utc_today(), expires_at) in [:lt, :eq]
103 @spec update_usage!(UserInviteToken.t()) :: nil | UserInviteToken.t() | no_return()
104 def update_usage!(%{invite_type: "date_limited"}), do: nil
106 def update_usage!(%{invite_type: "one_time"} = invite),
107 do: update_invite!(invite, %{used: true})
109 def update_usage!(%{invite_type: invite_type} = invite)
110 when invite_type == "reusable" or invite_type == "reusable_date_limited" do
112 uses: invite.uses + 1
116 if changes.uses >= invite.max_use do
117 Map.put(changes, :used, true)
122 update_invite!(invite, changes)