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-13 01:55:45 +02:00
|
|
|
|
alias Mobilizon.Actors.{Actor, MemberRole}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
alias Mobilizon.Web.Endpoint
|
2019-09-09 09:35:50 +02:00
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
|
@type t :: %__MODULE__{
|
|
|
|
|
role: MemberRole.t(),
|
|
|
|
|
parent: Actor.t(),
|
|
|
|
|
actor: Actor.t()
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
|
@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
|
2020-02-18 08:57:00 +01:00
|
|
|
|
@metadata_attrs []
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
2018-01-13 23:33:03 +01:00
|
|
|
|
schema "members" do
|
2019-09-09 00:52:49 +02:00
|
|
|
|
field(:role, MemberRole, default: :member)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
field(:url, :string)
|
2021-02-24 19:06:48 +01:00
|
|
|
|
field(:member_since, :utc_datetime)
|
2019-09-09 00:52:49 +02:00
|
|
|
|
|
2020-02-18 08:57:00 +01: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)
|
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
|
|
|
|
"""
|
2020-02-18 08:57:00 +01: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
|
2021-09-24 16:46:42 +02:00
|
|
|
|
@spec changeset(t | Ecto.Schema.t(), map) :: Ecto.Changeset.t()
|
2019-09-13 01:55:45 +02:00
|
|
|
|
def changeset(%__MODULE__{} = member, attrs) do
|
2019-09-09 00:52:49 +02:00
|
|
|
|
member
|
|
|
|
|
|> cast(attrs, @attrs)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|> cast_embed(:metadata, with: &metadata_changeset/2)
|
|
|
|
|
|> ensure_url()
|
2021-02-24 19:06:48 +01:00
|
|
|
|
|> update_member_since()
|
2019-09-09 00:52:49 +02:00
|
|
|
|
|> validate_required(@required_attrs)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
# 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)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|> 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
|
2021-02-24 19:06:48 +01:00
|
|
|
|
|
|
|
|
|
@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
|
2017-12-08 09:58:14 +01:00
|
|
|
|
end
|