2019-02-07 16:37:40 +01:00
|
|
|
import EctoEnum
|
|
|
|
|
|
|
|
defenum(Mobilizon.Events.ParticipantRoleEnum, :participant_role_type, [
|
|
|
|
:not_approved,
|
|
|
|
:participant,
|
|
|
|
:moderator,
|
|
|
|
:administrator,
|
|
|
|
:creator
|
|
|
|
])
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Events.Participant do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
2018-05-18 09:56:21 +02:00
|
|
|
Represents a participant, an actor participating to an event
|
2018-01-14 17:56:50 +01:00
|
|
|
"""
|
2018-01-13 23:33:03 +01:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Events.{Participant, Event}
|
|
|
|
alias Mobilizon.Actors.Actor
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2019-08-14 17:45:11 +02:00
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
2018-01-13 23:33:03 +01:00
|
|
|
schema "participants" do
|
2019-02-07 16:37:40 +01:00
|
|
|
field(:role, Mobilizon.Events.ParticipantRoleEnum, default: :participant)
|
2019-08-14 17:45:11 +02:00
|
|
|
field(:url, :string)
|
2018-07-27 10:45:35 +02:00
|
|
|
belongs_to(:event, Event, primary_key: true)
|
|
|
|
belongs_to(:actor, Actor, primary_key: true)
|
2018-01-13 23:33:03 +01:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%Participant{} = participant, attrs) do
|
|
|
|
participant
|
2019-08-14 17:45:11 +02:00
|
|
|
|> Ecto.Changeset.cast(attrs, [:url, :role, :event_id, :actor_id])
|
|
|
|
|> generate_url()
|
|
|
|
|> validate_required([:url, :role, :event_id, :actor_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
# If there's a blank URL that's because we're doing the first insert
|
|
|
|
defp generate_url(%Ecto.Changeset{data: %Participant{url: nil}} = changeset) do
|
|
|
|
case fetch_change(changeset, :url) do
|
|
|
|
{:ok, _url} -> changeset
|
|
|
|
:error -> do_generate_url(changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Most time just go with the given URL
|
|
|
|
defp generate_url(%Ecto.Changeset{} = changeset), do: changeset
|
|
|
|
|
|
|
|
defp do_generate_url(%Ecto.Changeset{} = changeset) do
|
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
|
|
|
changeset
|
|
|
|
|> put_change(
|
|
|
|
:url,
|
|
|
|
"#{MobilizonWeb.Endpoint.url()}/join/event/#{uuid}"
|
|
|
|
)
|
|
|
|
|> put_change(
|
|
|
|
:id,
|
|
|
|
uuid
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@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 check_that_participant_is_not_only_organizer(integer(), integer()) :: boolean()
|
|
|
|
def check_that_participant_is_not_only_organizer(event_id, actor_id) do
|
|
|
|
case Mobilizon.Events.list_organizers_participants_for_event(event_id) do
|
|
|
|
[%Participant{actor: %Actor{id: participant_actor_id}}] ->
|
|
|
|
participant_actor_id == actor_id
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
2018-01-13 23:33:03 +01:00
|
|
|
end
|
|
|
|
end
|