2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Events.Event do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
2019-09-13 01:01:17 +02:00
|
|
|
Represents an event.
|
2018-01-14 17:56:50 +01:00
|
|
|
"""
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
import Ecto.Changeset
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Addresses.Address
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
alias Mobilizon.Events.{
|
|
|
|
EventOptions,
|
|
|
|
EventStatus,
|
|
|
|
EventVisibility,
|
|
|
|
JoinOptions,
|
|
|
|
Participant,
|
|
|
|
Session,
|
2019-09-22 16:26:23 +02:00
|
|
|
Tag,
|
2019-09-13 01:01:17 +02:00
|
|
|
Track
|
|
|
|
}
|
|
|
|
|
|
|
|
alias Mobilizon.Media.Picture
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{
|
|
|
|
url: String.t(),
|
|
|
|
local: boolean,
|
|
|
|
begins_on: DateTime.t(),
|
|
|
|
slug: String.t(),
|
|
|
|
description: String.t(),
|
|
|
|
ends_on: DateTime.t(),
|
|
|
|
title: String.t(),
|
|
|
|
status: EventStatus.t(),
|
|
|
|
visibility: EventVisibility.t(),
|
|
|
|
join_options: JoinOptions.t(),
|
|
|
|
publish_at: DateTime.t(),
|
|
|
|
uuid: Ecto.UUID.t(),
|
|
|
|
online_address: String.t(),
|
|
|
|
phone_address: String.t(),
|
|
|
|
category: String.t(),
|
|
|
|
options: EventOptions.t(),
|
|
|
|
organizer_actor: Actor.t(),
|
|
|
|
attributed_to: Actor.t(),
|
|
|
|
physical_address: Address.t(),
|
|
|
|
picture: Picture.t(),
|
|
|
|
tracks: [Track.t()],
|
|
|
|
sessions: [Session.t()],
|
|
|
|
tags: [Tag.t()],
|
|
|
|
participants: [Actor.t()]
|
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:title, :begins_on, :organizer_actor_id, :url, :uuid]
|
|
|
|
@optional_attrs [
|
|
|
|
:slug,
|
|
|
|
:description,
|
|
|
|
:ends_on,
|
|
|
|
:category,
|
|
|
|
:status,
|
|
|
|
:visibility,
|
|
|
|
:publish_at,
|
|
|
|
:online_address,
|
|
|
|
:phone_address,
|
|
|
|
:picture_id,
|
|
|
|
:physical_address_id
|
|
|
|
]
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
|
|
|
|
|
|
|
@update_required_attrs @required_attrs
|
2019-09-20 18:22:03 +02:00
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
@update_optional_attrs [
|
|
|
|
:slug,
|
|
|
|
:description,
|
|
|
|
:ends_on,
|
|
|
|
:category,
|
|
|
|
:status,
|
|
|
|
:visibility,
|
2019-09-20 18:22:03 +02:00
|
|
|
:join_options,
|
2019-09-13 01:01:17 +02:00
|
|
|
:publish_at,
|
|
|
|
:online_address,
|
|
|
|
:phone_address,
|
|
|
|
:picture_id,
|
|
|
|
:physical_address_id
|
|
|
|
]
|
|
|
|
@update_attrs @update_required_attrs ++ @update_optional_attrs
|
|
|
|
|
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)
|
2019-02-14 14:19:55 +01:00
|
|
|
field(:begins_on, :utc_datetime)
|
2019-04-11 14:45:31 +02:00
|
|
|
field(:slug, :string)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:description, :string)
|
2019-02-14 14:19:55 +01:00
|
|
|
field(:ends_on, :utc_datetime)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:title, :string)
|
2019-09-13 01:01:17 +02:00
|
|
|
field(:status, EventStatus, default: :confirmed)
|
|
|
|
field(:visibility, EventVisibility, default: :public)
|
|
|
|
field(:join_options, JoinOptions, default: :free)
|
2019-02-14 14:19:55 +01:00
|
|
|
field(:publish_at, :utc_datetime)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:uuid, Ecto.UUID, default: Ecto.UUID.generate())
|
|
|
|
field(:online_address, :string)
|
2019-01-14 17:48:08 +01:00
|
|
|
field(:phone_address, :string)
|
2019-02-22 16:54:01 +01:00
|
|
|
field(:category, :string)
|
2019-09-13 01:01:17 +02:00
|
|
|
|
|
|
|
embeds_one(:options, EventOptions, on_replace: :update)
|
2018-07-27 10:45:35 +02:00
|
|
|
belongs_to(:organizer_actor, Actor, foreign_key: :organizer_actor_id)
|
|
|
|
belongs_to(:attributed_to, Actor, foreign_key: :attributed_to_id)
|
|
|
|
belongs_to(:physical_address, Address)
|
2019-05-22 14:12:11 +02:00
|
|
|
belongs_to(:picture, Picture)
|
2019-09-13 01:01:17 +02:00
|
|
|
has_many(:tracks, Track)
|
|
|
|
has_many(:sessions, Session)
|
|
|
|
many_to_many(:tags, Tag, join_through: "events_tags", on_replace: :delete)
|
|
|
|
many_to_many(:participants, Actor, join_through: Participant)
|
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
|
2019-09-13 01:01:17 +02:00
|
|
|
@spec changeset(t, map) :: Ecto.Changeset.t()
|
2019-09-13 01:55:45 +02:00
|
|
|
def changeset(%__MODULE__{} = event, attrs) do
|
2018-06-14 17:25:55 +02:00
|
|
|
event
|
2019-09-13 01:01:17 +02:00
|
|
|
|> cast(attrs, @attrs)
|
2019-08-28 11:28:27 +02:00
|
|
|
|> cast_embed(:options)
|
2019-09-13 01:01:17 +02:00
|
|
|
|> validate_required(@required_attrs)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2019-01-25 17:06:57 +01:00
|
|
|
|
2019-09-04 18:24:31 +02:00
|
|
|
@doc false
|
2019-09-13 01:01:17 +02:00
|
|
|
@spec update_changeset(t, map) :: Ecto.Changeset.t()
|
2019-09-13 01:55:45 +02:00
|
|
|
def update_changeset(%__MODULE__{} = event, attrs) do
|
2019-09-04 18:24:31 +02:00
|
|
|
event
|
2019-09-13 01:01:17 +02:00
|
|
|
|> Ecto.Changeset.cast(attrs, @update_attrs)
|
2019-09-04 18:24:31 +02:00
|
|
|
|> cast_embed(:options)
|
|
|
|
|> put_tags(attrs)
|
2019-09-13 01:01:17 +02:00
|
|
|
|> validate_required(@update_required_attrs)
|
2019-09-04 18:24:31 +02:00
|
|
|
end
|
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
@doc """
|
|
|
|
Checks whether an event can be managed.
|
|
|
|
"""
|
|
|
|
@spec can_be_managed_by(t, integer | String.t()) :: boolean
|
2019-09-13 01:55:45 +02:00
|
|
|
def can_be_managed_by(%__MODULE__{organizer_actor_id: organizer_actor_id}, actor_id)
|
2019-01-25 17:06:57 +01:00
|
|
|
when organizer_actor_id == actor_id do
|
|
|
|
{:event_can_be_managed, true}
|
|
|
|
end
|
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
def can_be_managed_by(_event, _actor), do: {:event_can_be_managed, false}
|
|
|
|
|
|
|
|
@spec put_tags(Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
|
|
|
|
defp put_tags(changeset, %{"tags" => tags}), do: put_assoc(changeset, :tags, tags)
|
|
|
|
defp put_tags(changeset, _), do: changeset
|
2018-01-14 17:56:50 +01:00
|
|
|
end
|