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

60 lines
1.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}
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]
@optional_attrs [:role]
@attrs @required_attrs ++ @optional_attrs
schema "members" do
2019-09-09 00:52:49 +02:00
field(:role, MemberRole, default: :member)
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
"""
2019-09-13 01:55:45 +02:00
def is_administrator(%__MODULE__{role: :administrator}), do: {:is_admin, true}
def is_administrator(%__MODULE__{role: :creator}), do: {:is_admin, true}
def is_administrator(%__MODULE__{}), do: {:is_admin, 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)
|> validate_required(@required_attrs)
|> unique_constraint(:parent_id, name: :members_actor_parent_unique_index)
end
end