2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Actors.Member do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Represents the membership of an actor to a group.
|
2018-01-14 17:56:50 +01:00
|
|
|
"""
|
2019-09-08 01:49:56 +02:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
2019-03-01 17:11:28 +01:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
import Ecto.Changeset
|
2019-03-01 17:11:28 +01:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
alias Mobilizon.Actors.{Actor, Member, MemberRole}
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{
|
|
|
|
role: MemberRole.t(),
|
|
|
|
parent: Actor.t(),
|
|
|
|
actor: Actor.t()
|
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:parent_id, :actor_id]
|
|
|
|
@optional_attrs [:role]
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
schema "members" do
|
2019-09-09 00:52:49 +02:00
|
|
|
field(:role, MemberRole, default: :member)
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
belongs_to(:parent, Actor)
|
|
|
|
belongs_to(:actor, Actor)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
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
|
|
|
"""
|
2019-03-01 17:11:28 +01:00
|
|
|
def is_administrator(%Member{role: :administrator}), do: {:is_admin, true}
|
|
|
|
def is_administrator(%Member{role: :creator}), do: {:is_admin, true}
|
|
|
|
def is_administrator(%Member{}), do: {:is_admin, false}
|
2019-09-09 00:52:49 +02:00
|
|
|
|
|
|
|
@doc false
|
|
|
|
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
|
|
|
|
def changeset(member, attrs) do
|
|
|
|
member
|
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
|> validate_required(@required_attrs)
|
|
|
|
|> unique_constraint(:parent_id, name: :members_actor_parent_unique_index)
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|