debianize-mobilizon/lib/mobilizon/actors/member.ex

129 lines
3.6 KiB
Elixir
Raw Normal View History

defmodule Mobilizon.Actors.Member do
@moduledoc """
2019-09-09 00:52:49 +02:00
Represents the membership of an actor to a group.
"""
2019-09-08 01:49:56 +02:00
use Ecto.Schema
2019-03-01 17:11:28 +01:00
import Ecto.Changeset
2019-03-01 17:11:28 +01:00
2019-09-13 01:55:45 +02:00
alias Mobilizon.Actors.{Actor, MemberRole}
alias Mobilizon.Web.Endpoint
2019-09-09 00:52:49 +02:00
@type t :: %__MODULE__{
role: MemberRole.t(),
parent: Actor.t(),
actor: Actor.t()
}
@required_attrs [:parent_id, :actor_id, :url]
@optional_attrs [:role, :invited_by_id]
2019-09-09 00:52:49 +02:00
@attrs @required_attrs ++ @optional_attrs
@metadata_attrs []
@primary_key {:id, :binary_id, autogenerate: true}
schema "members" do
2019-09-09 00:52:49 +02:00
field(:role, MemberRole, default: :member)
field(:url, :string)
field(:member_since, :utc_datetime)
2019-09-09 00:52:49 +02:00
embeds_one :metadata, Metadata, on_replace: :delete do
# TODO : Use this space to put notes when someone is invited / requested to join
end
belongs_to(:invited_by, Actor)
belongs_to(:parent, Actor)
belongs_to(:actor, Actor)
timestamps()
end
2019-01-25 09:23:44 +01:00
@doc """
2019-09-09 00:52:49 +02:00
Gets the default member role depending on the actor openness.
2019-01-25 09:23:44 +01:00
"""
2019-09-09 00:52:49 +02:00
@spec get_default_member_role(Actor.t()) :: atom
def get_default_member_role(%Actor{openness: :open}), do: :member
def get_default_member_role(%Actor{}), do: :not_approved
2019-01-25 09:23:44 +01:00
2019-03-01 17:11:28 +01:00
@doc """
2019-09-09 00:52:49 +02:00
Checks whether the actor can be joined to the group.
2019-03-01 17:11:28 +01:00
"""
def can_be_joined(%Actor{type: :Group, openness: :invite_only}), do: false
def can_be_joined(%Actor{type: :Group}), do: true
2019-01-25 09:23:44 +01:00
2019-03-01 17:11:28 +01:00
@doc """
2019-09-09 00:52:49 +02:00
Checks whether the member is an administrator (admin or creator) of the group.
2019-08-26 15:44:02 +02:00
"""
def is_administrator(%__MODULE__{role: :administrator}), do: true
def is_administrator(%__MODULE__{role: :creator}), do: true
def is_administrator(%__MODULE__{}), do: false
2019-09-09 00:52:49 +02:00
@doc false
2019-09-13 01:55:45 +02:00
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = member, attrs) do
2019-09-09 00:52:49 +02:00
member
|> cast(attrs, @attrs)
|> cast_embed(:metadata, with: &metadata_changeset/2)
|> ensure_url()
|> update_member_since()
2019-09-09 00:52:49 +02:00
|> validate_required(@required_attrs)
# On both parent_id and actor_id
2019-09-09 00:52:49 +02:00
|> unique_constraint(:parent_id, name: :members_actor_parent_unique_index)
|> unique_constraint(:url, name: :members_url_index)
end
defp metadata_changeset(schema, params) do
schema
|> cast(params, @metadata_attrs)
end
# If there's a blank URL that's because we're doing the first insert
@spec ensure_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp ensure_url(%Ecto.Changeset{data: %__MODULE__{url: nil}} = changeset) do
case fetch_change(changeset, :url) do
{:ok, _url} ->
changeset
:error ->
generate_url(changeset)
end
end
# Most time just go with the given URL
defp ensure_url(%Ecto.Changeset{} = changeset), do: changeset
@spec generate_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp generate_url(%Ecto.Changeset{} = changeset) do
uuid = Ecto.UUID.generate()
changeset
|> put_change(:id, uuid)
|> put_change(:url, "#{Endpoint.url()}/member/#{uuid}")
2019-09-09 00:52:49 +02:00
end
@spec update_member_since(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp update_member_since(%Ecto.Changeset{data: data} = changeset) do
new_role = get_change(changeset, :role)
cond do
new_role in [
:member,
:moderator,
:administrator,
:creator
] ->
put_change(
changeset,
:member_since,
DateTime.truncate(data.member_since || DateTime.utc_now(), :second)
)
new_role in [:invited, :not_approved, :rejected] ->
put_change(changeset, :member_since, nil)
true ->
changeset
end
end
end