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
|
2020-01-28 20:15:59 +01:00
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
alias Ecto.Changeset
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.{Addresses, Events, Medias, Mention}
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions.Comment
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
alias Mobilizon.Events.{
|
2021-08-09 14:26:11 +02:00
|
|
|
EventMetadata,
|
2019-09-13 01:01:17 +02:00
|
|
|
EventOptions,
|
2020-01-28 20:15:59 +01:00
|
|
|
EventParticipantStats,
|
2019-09-13 01:01:17 +02:00
|
|
|
EventStatus,
|
|
|
|
EventVisibility,
|
|
|
|
JoinOptions,
|
|
|
|
Participant,
|
|
|
|
Session,
|
2019-09-22 16:26:23 +02:00
|
|
|
Tag,
|
2019-09-13 01:01:17 +02:00
|
|
|
Track
|
|
|
|
}
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.Medias.Media
|
2019-11-08 19:37:14 +01:00
|
|
|
alias Mobilizon.Storage.Repo
|
2019-10-25 17:43:37 +02:00
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.Endpoint
|
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2019-09-13 01:01:17 +02:00
|
|
|
|
|
|
|
@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(),
|
2019-10-02 17:59:07 +02:00
|
|
|
draft: boolean,
|
2019-09-13 01:01:17 +02:00
|
|
|
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(),
|
2020-11-26 11:41:13 +01:00
|
|
|
picture: Media.t(),
|
|
|
|
media: [Media.t()],
|
2019-09-13 01:01:17 +02:00
|
|
|
tracks: [Track.t()],
|
|
|
|
sessions: [Session.t()],
|
2019-10-25 17:43:37 +02:00
|
|
|
mentions: [Mention.t()],
|
2019-09-13 01:01:17 +02:00
|
|
|
tags: [Tag.t()],
|
2020-09-29 09:53:48 +02:00
|
|
|
participants: [Actor.t()],
|
|
|
|
contacts: [Actor.t()]
|
2019-09-13 01:01:17 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
@update_required_attrs [:title, :begins_on, :organizer_actor_id]
|
|
|
|
@required_attrs @update_required_attrs ++ [:url, :uuid]
|
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
@optional_attrs [
|
|
|
|
:slug,
|
|
|
|
:description,
|
|
|
|
:ends_on,
|
|
|
|
:category,
|
|
|
|
:status,
|
2019-10-02 17:59:07 +02:00
|
|
|
:draft,
|
2019-12-03 11:29:51 +01:00
|
|
|
:local,
|
2019-09-13 01:01:17 +02:00
|
|
|
:visibility,
|
2019-10-25 17:43:37 +02:00
|
|
|
:join_options,
|
2019-09-13 01:01:17 +02:00
|
|
|
:publish_at,
|
|
|
|
:online_address,
|
|
|
|
:phone_address,
|
|
|
|
:picture_id,
|
2020-02-18 08:57:00 +01:00
|
|
|
:physical_address_id,
|
|
|
|
:attributed_to_id
|
2019-09-13 01:01:17 +02:00
|
|
|
]
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
@update_attrs @update_required_attrs ++ @optional_attrs
|
2019-09-13 01:01:17 +02:00
|
|
|
|
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)
|
2019-10-02 17:59:07 +02:00
|
|
|
field(:draft, :boolean, default: false)
|
2019-09-13 01:01:17 +02:00
|
|
|
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
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
embeds_one(:options, EventOptions, on_replace: :delete)
|
|
|
|
embeds_one(:participant_stats, EventParticipantStats, on_replace: :update)
|
2021-08-09 14:26:11 +02:00
|
|
|
embeds_many(:metadata, EventMetadata, on_replace: :delete)
|
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)
|
2019-11-08 19:37:14 +01:00
|
|
|
belongs_to(:physical_address, Address, on_replace: :nilify)
|
2020-11-26 11:41:13 +01:00
|
|
|
belongs_to(:picture, Media, on_replace: :update)
|
2019-09-13 01:01:17 +02:00
|
|
|
has_many(:tracks, Track)
|
|
|
|
has_many(:sessions, Session)
|
2019-10-25 17:43:37 +02:00
|
|
|
has_many(:mentions, Mention)
|
2019-11-15 18:36:47 +01:00
|
|
|
has_many(:comments, Comment)
|
2020-09-29 09:53:48 +02:00
|
|
|
many_to_many(:contacts, Actor, join_through: "event_contacts", on_replace: :delete)
|
2019-09-13 01:01:17 +02:00
|
|
|
many_to_many(:tags, Tag, join_through: "events_tags", on_replace: :delete)
|
|
|
|
many_to_many(:participants, Actor, join_through: Participant)
|
2020-11-26 11:41:13 +01:00
|
|
|
many_to_many(:media, Media, join_through: "events_medias", on_replace: :delete)
|
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-10-25 17:43:37 +02:00
|
|
|
@spec changeset(t, map) :: Changeset.t()
|
2019-09-13 01:55:45 +02:00
|
|
|
def changeset(%__MODULE__{} = event, attrs) do
|
2019-10-25 17:43:37 +02:00
|
|
|
attrs = Map.update(attrs, :uuid, Ecto.UUID.generate(), & &1)
|
|
|
|
attrs = Map.update(attrs, :url, Routes.page_url(Endpoint, :event, attrs.uuid), & &1)
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
event
|
2019-09-13 01:01:17 +02:00
|
|
|
|> cast(attrs, @attrs)
|
2019-10-25 17:43:37 +02:00
|
|
|
|> common_changeset(attrs)
|
|
|
|
|> put_creator_if_published(:create)
|
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-10-25 17:43:37 +02:00
|
|
|
@spec update_changeset(t, map) :: 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-10-25 17:43:37 +02:00
|
|
|
|> cast(attrs, @update_attrs)
|
|
|
|
|> common_changeset(attrs)
|
|
|
|
|> put_creator_if_published(:update)
|
2019-09-13 01:01:17 +02:00
|
|
|
|> validate_required(@update_required_attrs)
|
2019-10-18 11:21:31 +02:00
|
|
|
end
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec common_changeset(Changeset.t(), map) :: Changeset.t()
|
|
|
|
defp common_changeset(%Changeset{} = changeset, attrs) do
|
|
|
|
changeset
|
|
|
|
|> cast_embed(:options)
|
2021-08-09 14:26:11 +02:00
|
|
|
|> cast_embed(:metadata)
|
2020-09-29 09:53:48 +02:00
|
|
|
|> put_assoc(:contacts, Map.get(attrs, :contacts, []))
|
2020-11-26 11:41:13 +01:00
|
|
|
|> put_assoc(:media, Map.get(attrs, :media, []))
|
2019-10-25 17:43:37 +02:00
|
|
|
|> put_tags(attrs)
|
|
|
|
|> put_address(attrs)
|
|
|
|
|> put_picture(attrs)
|
2019-11-18 18:40:03 +01:00
|
|
|
|> validate_lengths()
|
|
|
|
|> validate_end_time()
|
2019-10-25 17:43:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec validate_lengths(Changeset.t()) :: Changeset.t()
|
|
|
|
defp validate_lengths(%Changeset{} = changeset) do
|
2019-10-18 11:21:31 +02:00
|
|
|
changeset
|
|
|
|
|> validate_length(:title, min: 3, max: 200)
|
|
|
|
|> validate_length(:online_address, min: 3, max: 2000)
|
|
|
|
|> validate_length(:phone_address, min: 3, max: 200)
|
|
|
|
|> validate_length(:category, min: 2, max: 100)
|
|
|
|
# |> validate_length(:category, min: 2, max: 100)
|
|
|
|
|> validate_length(:slug, min: 3, max: 200)
|
2019-09-04 18:24:31 +02:00
|
|
|
end
|
|
|
|
|
2019-11-18 18:40:03 +01:00
|
|
|
defp validate_end_time(%Changeset{} = changeset) do
|
|
|
|
case fetch_field(changeset, :begins_on) do
|
|
|
|
{_, begins_on} ->
|
|
|
|
validate_change(changeset, :ends_on, fn :ends_on, ends_on ->
|
2020-04-14 17:50:17 +02:00
|
|
|
if DateTime.compare(begins_on, ends_on) == :gt,
|
2019-11-18 18:40:03 +01:00
|
|
|
do: [ends_on: "ends_on cannot be set before begins_on"],
|
|
|
|
else: []
|
|
|
|
end)
|
|
|
|
|
|
|
|
:error ->
|
|
|
|
changeset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
@doc """
|
|
|
|
Checks whether an event can be managed.
|
|
|
|
"""
|
2021-07-26 17:22:19 +02:00
|
|
|
@spec can_be_managed_by?(t, integer | String.t()) :: boolean
|
|
|
|
def can_be_managed_by?(%__MODULE__{organizer_actor_id: organizer_actor_id}, actor_id),
|
|
|
|
do: organizer_actor_id == actor_id
|
2019-01-25 17:06:57 +01:00
|
|
|
|
2021-07-26 17:22:19 +02:00
|
|
|
def can_be_managed_by?(_event, _actor), do: false
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec put_tags(Changeset.t(), map) :: Changeset.t()
|
2019-12-03 11:29:51 +01:00
|
|
|
defp put_tags(%Changeset{} = changeset, %{tags: tags}) do
|
|
|
|
put_assoc(changeset, :tags, Enum.map(tags, &process_tag/1))
|
|
|
|
end
|
2019-10-25 17:43:37 +02:00
|
|
|
|
|
|
|
defp put_tags(%Changeset{} = changeset, _), do: changeset
|
|
|
|
|
|
|
|
# We need a changeset instead of a raw struct because of slug which is generated in changeset
|
2019-12-03 11:29:51 +01:00
|
|
|
defp process_tag(%{id: id} = _tag) do
|
|
|
|
Events.get_tag(id)
|
|
|
|
end
|
2019-11-04 15:10:58 +01:00
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
defp process_tag(tag) do
|
|
|
|
Tag.changeset(%Tag{}, tag)
|
|
|
|
end
|
|
|
|
|
|
|
|
# In case the provided addresses is an existing one
|
|
|
|
@spec put_address(Changeset.t(), map) :: Changeset.t()
|
2019-11-05 17:49:40 +01:00
|
|
|
defp put_address(%Changeset{} = changeset, %{physical_address: %{id: id} = _physical_address})
|
|
|
|
when not is_nil(id) do
|
2019-11-05 11:49:45 +01:00
|
|
|
case Addresses.get_address(id) do
|
2019-10-25 17:43:37 +02:00
|
|
|
%Address{} = address ->
|
|
|
|
put_assoc(changeset, :physical_address, address)
|
|
|
|
|
|
|
|
_ ->
|
2019-11-08 19:37:14 +01:00
|
|
|
cast_assoc(changeset, :physical_address)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# In case it's a new address but the origin_id is an existing one
|
|
|
|
defp put_address(%Changeset{} = changeset, %{physical_address: %{origin_id: origin_id}})
|
|
|
|
when not is_nil(origin_id) do
|
|
|
|
case Repo.get_by(Address, origin_id: origin_id) do
|
|
|
|
%Address{} = address ->
|
|
|
|
put_assoc(changeset, :physical_address, address)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
cast_assoc(changeset, :physical_address)
|
2019-10-25 17:43:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-08 19:37:14 +01:00
|
|
|
# In case it's a new address without any origin_id (manual)
|
2019-10-25 17:43:37 +02:00
|
|
|
defp put_address(%Changeset{} = changeset, _attrs) do
|
|
|
|
cast_assoc(changeset, :physical_address)
|
|
|
|
end
|
|
|
|
|
|
|
|
# In case the provided picture is an existing one
|
|
|
|
@spec put_picture(Changeset.t(), map) :: Changeset.t()
|
2020-11-26 11:41:13 +01:00
|
|
|
defp put_picture(%Changeset{} = changeset, %{picture: %{media_id: id} = _picture}) do
|
|
|
|
case Medias.get_media!(id) do
|
|
|
|
%Media{} = picture ->
|
2019-10-25 17:43:37 +02:00
|
|
|
put_assoc(changeset, :picture, picture)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
changeset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# In case it's a new picture
|
|
|
|
defp put_picture(%Changeset{} = changeset, _attrs) do
|
2021-06-10 09:36:25 +02:00
|
|
|
cast_assoc(changeset, :picture, with: &Media.changeset/2)
|
2019-10-25 17:43:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Created or updated with draft parameter: don't publish
|
|
|
|
defp put_creator_if_published(
|
|
|
|
%Changeset{changes: %{draft: true}} = changeset,
|
|
|
|
_action
|
|
|
|
) do
|
2019-11-08 19:37:14 +01:00
|
|
|
put_embed(changeset, :participant_stats, %{creator: 0})
|
2019-10-25 17:43:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Created with any other value: publish
|
|
|
|
defp put_creator_if_published(
|
|
|
|
%Changeset{} = changeset,
|
|
|
|
:create
|
|
|
|
) do
|
|
|
|
changeset
|
|
|
|
|> put_embed(:participant_stats, %{creator: 1})
|
|
|
|
end
|
|
|
|
|
|
|
|
# Updated from draft false to true: publish
|
|
|
|
defp put_creator_if_published(
|
|
|
|
%Changeset{
|
|
|
|
data: %{draft: false},
|
|
|
|
changes: %{draft: true}
|
|
|
|
} = changeset,
|
|
|
|
:update
|
|
|
|
) do
|
|
|
|
changeset
|
|
|
|
|> put_embed(:participant_stats, %{creator: 1})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp put_creator_if_published(%Changeset{} = changeset, _),
|
|
|
|
do: cast_embed(changeset, :participant_stats)
|
2018-01-14 17:56:50 +01:00
|
|
|
end
|