2018-07-04 14:29:17 +02:00
|
|
|
import EctoEnum
|
2019-01-14 15:56:07 +01:00
|
|
|
defenum(Mobilizon.Events.AddressTypeEnum, :address_type, [:physical, :url, :phone, :other])
|
|
|
|
|
|
|
|
defenum(Mobilizon.Events.EventVisibilityEnum, :event_visibility_type, [
|
|
|
|
:public,
|
|
|
|
:unlisted,
|
|
|
|
:private,
|
|
|
|
:moderated,
|
|
|
|
:invite
|
|
|
|
])
|
|
|
|
|
|
|
|
defenum(Mobilizon.Events.EventStatusEnum, :event_status_type, [
|
|
|
|
:tentative,
|
|
|
|
:confirmed,
|
|
|
|
:cancelled
|
|
|
|
])
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Events.Event do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Represents an event
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Events.{Event, Participant, Tag, Category, Session, Track}
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Addresses.Address
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
schema "events" do
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:url, :string)
|
|
|
|
field(:local, :boolean, default: true)
|
|
|
|
field(:begins_on, Timex.Ecto.DateTimeWithTimezone)
|
|
|
|
field(:description, :string)
|
|
|
|
field(:ends_on, Timex.Ecto.DateTimeWithTimezone)
|
|
|
|
field(:title, :string)
|
2019-01-14 15:56:07 +01:00
|
|
|
field(:status, Mobilizon.Events.EventStatusEnum, default: :confirmed)
|
|
|
|
field(:visibility, Mobilizon.Events.EventVisibilityEnum, default: :public)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:thumbnail, :string)
|
|
|
|
field(:large_image, :string)
|
|
|
|
field(:publish_at, Timex.Ecto.DateTimeWithTimezone)
|
|
|
|
field(:uuid, Ecto.UUID, default: Ecto.UUID.generate())
|
2019-01-14 15:56:07 +01:00
|
|
|
field(:address_type, Mobilizon.Events.AddressTypeEnum, default: :physical)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:online_address, :string)
|
|
|
|
field(:phone, :string)
|
|
|
|
belongs_to(:organizer_actor, Actor, foreign_key: :organizer_actor_id)
|
|
|
|
belongs_to(:attributed_to, Actor, foreign_key: :attributed_to_id)
|
|
|
|
many_to_many(:tags, Tag, join_through: "events_tags")
|
|
|
|
belongs_to(:category, Category)
|
|
|
|
many_to_many(:participants, Actor, join_through: Participant)
|
|
|
|
has_many(:tracks, Track)
|
|
|
|
has_many(:sessions, Session)
|
|
|
|
belongs_to(:physical_address, Address)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
timestamps(type: :utc_datetime)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%Event{} = event, attrs) do
|
2018-06-14 17:25:55 +02:00
|
|
|
event
|
2018-07-04 14:29:17 +02:00
|
|
|
|> Ecto.Changeset.cast(attrs, [
|
|
|
|
:title,
|
|
|
|
:description,
|
|
|
|
:url,
|
|
|
|
:begins_on,
|
|
|
|
:ends_on,
|
|
|
|
:organizer_actor_id,
|
|
|
|
:category_id,
|
|
|
|
:status,
|
2019-01-14 15:56:07 +01:00
|
|
|
:visibility,
|
2018-07-04 14:29:17 +02:00
|
|
|
:thumbnail,
|
|
|
|
:large_image,
|
|
|
|
:publish_at,
|
|
|
|
:address_type,
|
|
|
|
:online_address,
|
2018-07-27 10:45:35 +02:00
|
|
|
:phone
|
|
|
|
])
|
2018-01-15 12:17:34 +01:00
|
|
|
|> cast_assoc(:tags)
|
2018-07-04 14:29:17 +02:00
|
|
|
|> cast_assoc(:physical_address)
|
2018-12-03 12:29:31 +01:00
|
|
|
|> build_url()
|
2018-07-27 10:45:35 +02:00
|
|
|
|> validate_required([
|
|
|
|
:title,
|
|
|
|
:begins_on,
|
|
|
|
:organizer_actor_id,
|
|
|
|
:category_id,
|
|
|
|
:url,
|
|
|
|
:uuid,
|
|
|
|
:address_type
|
|
|
|
])
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2018-12-03 12:29:31 +01:00
|
|
|
|
|
|
|
@spec build_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
|
|
|
defp build_url(%Ecto.Changeset{changes: %{url: _url}} = changeset), do: changeset
|
|
|
|
|
|
|
|
defp build_url(%Ecto.Changeset{changes: %{organizer_actor: organizer_actor}} = changeset) do
|
|
|
|
organizer_actor
|
|
|
|
|> Actor.actor_acct_from_actor()
|
|
|
|
|> do_build_url(changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp build_url(%Ecto.Changeset{changes: %{organizer_actor_id: organizer_actor_id}} = changeset) do
|
|
|
|
organizer_actor_id
|
|
|
|
|> Mobilizon.Actors.get_actor!()
|
|
|
|
|> Actor.actor_acct_from_actor()
|
|
|
|
|> do_build_url(changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp build_url(%Ecto.Changeset{} = changeset), do: changeset
|
|
|
|
|
|
|
|
@spec do_build_url(String.t(), Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
|
|
|
defp do_build_url(actor_acct, changeset) do
|
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
|
|
|
changeset
|
|
|
|
|> put_change(:uuid, uuid)
|
|
|
|
|> put_change(:url, "#{MobilizonWeb.Endpoint.url()}/@#{actor_acct}/#{uuid}")
|
|
|
|
end
|
2018-01-14 17:56:50 +01:00
|
|
|
end
|