mobilizon.chapril.org-mobil.../lib/mobilizon/actors/member.ex

47 lines
1.1 KiB
Elixir
Raw Normal View History

defmodule Mobilizon.Actors.Member do
@moduledoc """
Represents the membership of an actor to a group
"""
use Ecto.Schema
import Ecto.Changeset
alias Mobilizon.Actors.Member
alias Mobilizon.Actors.Actor
2019-01-25 09:23:44 +01:00
alias Mobilizon.Repo
schema "members" do
field(:approved, :boolean, default: true)
# 0 : Member, 1 : Moderator, 2 : Admin
field(:role, :integer, default: 0)
belongs_to(:parent, Actor)
belongs_to(:actor, Actor)
timestamps()
end
@doc false
def changeset(%Member{} = member, attrs) do
member
|> cast(attrs, [:role, :approved, :parent_id, :actor_id])
|> validate_required([:parent_id, :actor_id])
2018-08-24 11:34:00 +02:00
|> unique_constraint(:parent_id, name: :members_actor_parent_unique_index)
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-01-25 17:06:57 +01:00
def is_administrator(%Member{role: 2}) do
2019-01-25 09:23:44 +01:00
{:is_admin, true}
end
def is_administrator(%Member{}) do
{:is_admin, false}
end
end