2018-10-11 15:37:39 +00:00
|
|
|
defmodule Mobilizon.Events.Participant do
|
2018-01-14 16:56:50 +00:00
|
|
|
@moduledoc """
|
2019-09-12 23:01:17 +00:00
|
|
|
Represents a participant, an actor participating to an event.
|
2018-01-14 16:56:50 +00:00
|
|
|
"""
|
2019-09-12 23:01:17 +00:00
|
|
|
|
2018-01-13 22:33:03 +00:00
|
|
|
use Ecto.Schema
|
2019-09-12 23:01:17 +00:00
|
|
|
|
2018-01-13 22:33:03 +00:00
|
|
|
import Ecto.Changeset
|
2019-09-12 23:01:17 +00:00
|
|
|
|
2018-10-11 15:37:39 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-12 23:01:17 +00:00
|
|
|
alias Mobilizon.Events
|
2019-09-12 23:55:45 +00:00
|
|
|
alias Mobilizon.Events.{Event, ParticipantRole}
|
2019-09-12 23:01:17 +00:00
|
|
|
|
2019-09-17 20:10:22 +00:00
|
|
|
alias MobilizonWeb.Endpoint
|
|
|
|
|
2019-09-12 23:01:17 +00:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
role: ParticipantRole.t(),
|
|
|
|
url: String.t(),
|
|
|
|
event: Event.t(),
|
|
|
|
actor: Actor.t()
|
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:url, :role, :event_id, :actor_id]
|
|
|
|
@attrs @required_attrs
|
2018-01-13 22:33:03 +00:00
|
|
|
|
2019-08-14 15:45:11 +00:00
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
2018-01-13 22:33:03 +00:00
|
|
|
schema "participants" do
|
2019-09-12 23:01:17 +00:00
|
|
|
field(:role, ParticipantRole, default: :participant)
|
2019-08-14 15:45:11 +00:00
|
|
|
field(:url, :string)
|
2019-09-12 23:01:17 +00:00
|
|
|
|
2018-07-27 08:45:35 +00:00
|
|
|
belongs_to(:event, Event, primary_key: true)
|
|
|
|
belongs_to(:actor, Actor, primary_key: true)
|
2018-01-13 22:33:03 +00:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-09-12 23:01:17 +00:00
|
|
|
@doc """
|
|
|
|
We check that the actor asking to leave the event is not it's only organizer.
|
|
|
|
We start by fetching the list of organizers and if there's only one of them
|
|
|
|
and that it's the actor requesting leaving the event we return true.
|
|
|
|
"""
|
|
|
|
@spec is_not_only_organizer(integer | String.t(), integer | String.t()) :: boolean
|
|
|
|
def is_not_only_organizer(event_id, actor_id) do
|
|
|
|
case Events.list_organizers_participants_for_event(event_id) do
|
2019-09-12 23:55:45 +00:00
|
|
|
[%__MODULE__{actor: %Actor{id: participant_actor_id}}] ->
|
2019-09-12 23:01:17 +00:00
|
|
|
participant_actor_id == actor_id
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-13 22:33:03 +00:00
|
|
|
@doc false
|
2019-09-12 23:01:17 +00:00
|
|
|
@spec changeset(t, map) :: Ecto.Changeset.t()
|
2019-09-12 23:55:45 +00:00
|
|
|
def changeset(%__MODULE__{} = participant, attrs) do
|
2018-01-13 22:33:03 +00:00
|
|
|
participant
|
2019-09-12 23:01:17 +00:00
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
|> ensure_url()
|
|
|
|
|> validate_required(@required_attrs)
|
2019-08-14 15:45:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If there's a blank URL that's because we're doing the first insert
|
2019-09-12 23:01:17 +00:00
|
|
|
@spec ensure_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
2019-09-12 23:55:45 +00:00
|
|
|
defp ensure_url(%Ecto.Changeset{data: %__MODULE__{url: nil}} = changeset) do
|
2019-08-14 15:45:11 +00:00
|
|
|
case fetch_change(changeset, :url) do
|
2019-09-12 23:01:17 +00:00
|
|
|
{:ok, _url} ->
|
|
|
|
changeset
|
|
|
|
|
|
|
|
:error ->
|
|
|
|
update_url(changeset)
|
2019-08-14 15:45:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-12 23:01:17 +00:00
|
|
|
defp ensure_url(%Ecto.Changeset{} = changeset), do: changeset
|
2019-08-14 15:45:11 +00:00
|
|
|
|
2019-09-12 23:01:17 +00:00
|
|
|
defp update_url(%Ecto.Changeset{} = changeset) do
|
2019-08-14 15:45:11 +00:00
|
|
|
uuid = Ecto.UUID.generate()
|
2019-09-12 23:01:17 +00:00
|
|
|
url = generate_url(uuid)
|
2019-08-14 15:45:11 +00:00
|
|
|
|
|
|
|
changeset
|
2019-09-12 23:01:17 +00:00
|
|
|
|> put_change(:id, uuid)
|
|
|
|
|> put_change(:url, url)
|
2019-08-14 15:45:11 +00:00
|
|
|
end
|
|
|
|
|
2019-09-12 23:01:17 +00:00
|
|
|
@spec generate_url(String.t()) :: String.t()
|
2019-09-17 20:10:22 +00:00
|
|
|
defp generate_url(uuid), do: "#{Endpoint.url()}/join/event/#{uuid}"
|
2018-01-13 22:33:03 +00:00
|
|
|
end
|