2019-03-01 17:11:28 +01:00
|
|
|
import EctoEnum
|
|
|
|
|
|
|
|
defenum(Mobilizon.Actors.MemberRoleEnum, :member_role_type, [
|
|
|
|
:not_approved,
|
|
|
|
:member,
|
|
|
|
:moderator,
|
|
|
|
:administrator,
|
|
|
|
:creator
|
|
|
|
])
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Actors.Member do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
2018-05-18 09:56:21 +02:00
|
|
|
Represents the membership of an actor to a group
|
2018-01-14 17:56:50 +01: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
|
|
|
import Ecto.Query, warn: false
|
|
|
|
import Mobilizon.Ecto
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Member
|
|
|
|
alias Mobilizon.Actors.Actor
|
2019-01-25 09:23:44 +01:00
|
|
|
alias Mobilizon.Repo
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
schema "members" do
|
2019-03-01 17:11:28 +01:00
|
|
|
field(:role, Mobilizon.Actors.MemberRoleEnum, 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
|
|
|
|
|
|
|
|
@doc false
|
2018-01-13 23:33:03 +01:00
|
|
|
def changeset(%Member{} = member, attrs) do
|
|
|
|
member
|
2019-03-01 17:11:28 +01:00
|
|
|
|> cast(attrs, [:role, :parent_id, :actor_id])
|
2018-06-06 17:42:53 +02:00
|
|
|
|> validate_required([:parent_id, :actor_id])
|
2018-08-24 11:34:00 +02:00
|
|
|
|> unique_constraint(:parent_id, name: :members_actor_parent_unique_index)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2019-01-25 09:23:44 +01:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single member of an actor (for example a group)
|
|
|
|
"""
|
|
|
|
def get_member(actor_id, parent_id) do
|
|
|
|
case Repo.get_by(Member, actor_id: actor_id, parent_id: parent_id) do
|
|
|
|
nil -> {:error, :member_not_found}
|
|
|
|
member -> {:ok, member}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 17:11:28 +01:00
|
|
|
@doc """
|
|
|
|
Gets a single member of an actor (for example a group)
|
|
|
|
"""
|
|
|
|
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 """
|
|
|
|
Returns the list of administrator members for a group.
|
|
|
|
"""
|
|
|
|
def list_administrator_members_for_group(id, page \\ nil, limit \\ nil) do
|
|
|
|
Repo.all(
|
|
|
|
from(
|
|
|
|
m in Member,
|
|
|
|
where: m.parent_id == ^id and (m.role == ^:creator or m.role == ^:administrator),
|
|
|
|
preload: [:actor]
|
|
|
|
)
|
|
|
|
|> paginate(page, limit)
|
|
|
|
)
|
2019-01-25 09:23:44 +01:00
|
|
|
end
|
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}
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|