2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Events do
|
2017-12-08 09:58:14 +01:00
|
|
|
@moduledoc """
|
|
|
|
The Events context.
|
|
|
|
"""
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
import Geo.PostGIS
|
|
|
|
|
2019-09-08 01:49:56 +02:00
|
|
|
import Ecto.Query
|
2019-09-13 01:01:17 +02:00
|
|
|
import EctoEnum
|
2019-09-08 01:49:56 +02:00
|
|
|
|
2020-07-31 17:52:26 +02:00
|
|
|
import Mobilizon.Service.Guards
|
2019-09-08 01:49:56 +02:00
|
|
|
import Mobilizon.Storage.Ecto
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2020-01-28 20:15:59 +01:00
|
|
|
alias Ecto.{Changeset, Multi}
|
2020-01-23 21:59:50 +01:00
|
|
|
|
2020-02-07 16:28:49 +01:00
|
|
|
alias Mobilizon.Actors.{Actor, Follower}
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
2019-09-16 02:07:44 +02:00
|
|
|
|
|
|
|
alias Mobilizon.Events.{
|
|
|
|
Event,
|
2019-10-25 17:43:37 +02:00
|
|
|
EventParticipantStats,
|
2019-09-16 02:07:44 +02:00
|
|
|
FeedToken,
|
|
|
|
Participant,
|
|
|
|
Session,
|
|
|
|
Tag,
|
|
|
|
TagRelation,
|
|
|
|
Track
|
|
|
|
}
|
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
alias Mobilizon.Service.Workers
|
2020-02-07 16:28:49 +01:00
|
|
|
alias Mobilizon.Share
|
2019-09-08 01:49:56 +02:00
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
2020-11-16 18:28:52 +01:00
|
|
|
alias Mobilizon.Users.{Setting, User}
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.Email
|
2020-01-23 00:55:07 +01:00
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
defenum(EventVisibility, :event_visibility, [
|
|
|
|
:public,
|
|
|
|
:unlisted,
|
|
|
|
:restricted,
|
|
|
|
:private
|
|
|
|
])
|
|
|
|
|
|
|
|
defenum(JoinOptions, :join_options, [
|
|
|
|
:free,
|
|
|
|
:restricted,
|
|
|
|
:invite
|
|
|
|
])
|
|
|
|
|
|
|
|
defenum(EventStatus, :event_status, [
|
|
|
|
:tentative,
|
|
|
|
:confirmed,
|
|
|
|
:cancelled
|
|
|
|
])
|
|
|
|
|
|
|
|
defenum(EventCategory, :event_category, [
|
|
|
|
:business,
|
|
|
|
:conference,
|
|
|
|
:birthday,
|
|
|
|
:demonstration,
|
|
|
|
:meeting
|
|
|
|
])
|
|
|
|
|
|
|
|
defenum(ParticipantRole, :participant_role, [
|
|
|
|
:not_approved,
|
2019-12-20 13:04:34 +01:00
|
|
|
:not_confirmed,
|
2019-09-30 13:48:47 +02:00
|
|
|
:rejected,
|
2019-09-13 01:01:17 +02:00
|
|
|
:participant,
|
|
|
|
:moderator,
|
|
|
|
:administrator,
|
|
|
|
:creator
|
|
|
|
])
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@public_visibility [:public, :unlisted]
|
|
|
|
|
|
|
|
@event_preloads [
|
|
|
|
:organizer_actor,
|
2019-10-25 17:43:37 +02:00
|
|
|
:attributed_to,
|
|
|
|
:mentions,
|
2019-09-16 02:07:44 +02:00
|
|
|
:sessions,
|
|
|
|
:tracks,
|
|
|
|
:tags,
|
2019-11-15 18:36:47 +01:00
|
|
|
:comments,
|
2019-09-16 02:07:44 +02:00
|
|
|
:participants,
|
|
|
|
:physical_address,
|
2020-09-29 09:53:48 +02:00
|
|
|
:picture,
|
2020-11-26 11:41:13 +01:00
|
|
|
:contacts,
|
|
|
|
:media
|
2019-09-16 02:07:44 +02:00
|
|
|
]
|
|
|
|
|
2021-07-29 17:50:23 +02:00
|
|
|
@participant_preloads [:event, :actor]
|
|
|
|
|
2019-04-11 18:25:32 +02:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets a single event.
|
2019-04-11 18:25:32 +02:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_event(integer | String.t()) :: {:ok, Event.t()} | {:error, :event_not_found}
|
|
|
|
def get_event(id) do
|
|
|
|
case Repo.get(Event, id) do
|
|
|
|
%Event{} = event ->
|
|
|
|
{:ok, event}
|
2018-12-04 12:06:34 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
nil ->
|
|
|
|
{:error, :event_not_found}
|
2018-08-24 12:31:41 +02:00
|
|
|
end
|
2018-07-09 14:19:24 +02:00
|
|
|
end
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
@doc """
|
|
|
|
Gets a single event.
|
2019-09-16 02:07:44 +02:00
|
|
|
Raises `Ecto.NoResultsError` if the event does not exist.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_event!(integer | String.t()) :: Event.t()
|
2017-12-08 09:58:14 +01:00
|
|
|
def get_event!(id), do: Repo.get!(Event, id)
|
|
|
|
|
2019-01-25 17:06:57 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets a single event, with all associations loaded.
|
2019-01-25 17:06:57 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_event_with_preload(integer | String.t()) ::
|
|
|
|
{:ok, Event.t()} | {:error, :event_not_found}
|
|
|
|
def get_event_with_preload(id) do
|
2019-01-25 17:06:57 +01:00
|
|
|
case Repo.get(Event, id) do
|
2019-09-16 02:07:44 +02:00
|
|
|
%Event{} = event ->
|
|
|
|
{:ok, Repo.preload(event, @event_preloads)}
|
2019-01-25 17:06:57 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
nil ->
|
|
|
|
{:error, :event_not_found}
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets a single event, with all associations loaded.
|
|
|
|
Raises `Ecto.NoResultsError` if the event does not exist.
|
2018-08-24 11:34:00 +02:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_event_with_preload!(integer | String.t()) :: Event.t()
|
|
|
|
def get_event_with_preload!(id) do
|
|
|
|
Event
|
|
|
|
|> Repo.get!(id)
|
|
|
|
|> Repo.preload(@event_preloads)
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
|
2018-01-16 19:45:09 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets an event by its URL.
|
2018-01-16 19:45:09 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_event_by_url(String.t()) :: Event.t() | nil
|
|
|
|
def get_event_by_url(url) do
|
|
|
|
url
|
|
|
|
|> event_by_url_query()
|
2019-01-14 15:56:07 +01:00
|
|
|
|> Repo.one()
|
2018-01-16 19:45:09 +01:00
|
|
|
end
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets an event by its URL.
|
|
|
|
Raises `Ecto.NoResultsError` if the event does not exist.
|
2018-05-17 11:32:23 +02:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_event_by_url!(String.t()) :: Event.t()
|
|
|
|
def get_event_by_url!(url) do
|
|
|
|
url
|
|
|
|
|> event_by_url_query()
|
2021-07-26 17:23:42 +02:00
|
|
|
|> preload_for_event()
|
2019-09-16 02:07:44 +02:00
|
|
|
|> Repo.one!()
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2019-08-14 17:45:11 +02:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets an event by its URL, with all associations loaded.
|
2019-08-14 17:45:11 +02:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_public_event_by_url_with_preload(String.t()) ::
|
|
|
|
{:ok, Event.t()} | {:error, :event_not_found}
|
|
|
|
def get_public_event_by_url_with_preload(url) do
|
|
|
|
event =
|
|
|
|
url
|
|
|
|
|> event_by_url_query()
|
2020-06-12 19:19:19 +02:00
|
|
|
|> filter_unlisted_and_public_visibility()
|
2019-10-02 17:59:07 +02:00
|
|
|
|> filter_draft()
|
2019-09-16 02:07:44 +02:00
|
|
|
|> preload_for_event()
|
|
|
|
|> Repo.one()
|
|
|
|
|
|
|
|
case event do
|
2019-08-14 17:45:11 +02:00
|
|
|
%Event{} = event ->
|
2019-09-16 02:07:44 +02:00
|
|
|
{:ok, event}
|
2019-08-14 17:45:11 +02:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
nil ->
|
|
|
|
{:error, :event_not_found}
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets an event by its URL, with all associations loaded.
|
|
|
|
Raises `Ecto.NoResultsError` if the event does not exist.
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_public_event_by_url_with_preload(String.t()) :: Event.t()
|
|
|
|
def get_public_event_by_url_with_preload!(url) do
|
|
|
|
url
|
|
|
|
|> event_by_url_query()
|
2020-06-12 19:19:19 +02:00
|
|
|
|> filter_unlisted_and_public_visibility()
|
2019-10-02 17:59:07 +02:00
|
|
|
|> filter_draft()
|
2019-09-16 02:07:44 +02:00
|
|
|
|> preload_for_event()
|
|
|
|
|> Repo.one!()
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
@doc """
|
2019-10-02 17:59:07 +02:00
|
|
|
Gets a public event by its UUID, with all associations loaded.
|
2018-11-06 10:30:27 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_public_event_by_uuid_with_preload(String.t()) :: Event.t() | nil
|
|
|
|
def get_public_event_by_uuid_with_preload(uuid) do
|
|
|
|
uuid
|
|
|
|
|> event_by_uuid_query()
|
2020-06-12 19:19:19 +02:00
|
|
|
|> filter_unlisted_and_public_visibility()
|
2019-10-02 17:59:07 +02:00
|
|
|
|> filter_draft()
|
|
|
|
|> preload_for_event()
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2020-02-07 16:28:49 +01:00
|
|
|
@spec check_if_event_has_instance_follow(String.t(), integer()) :: boolean()
|
|
|
|
def check_if_event_has_instance_follow(event_uri, follower_actor_id) do
|
|
|
|
Share
|
|
|
|
|> join(:inner, [s], f in Follower, on: f.target_actor_id == s.actor_id)
|
|
|
|
|> where([s, f], f.actor_id == ^follower_actor_id and s.uri == ^event_uri)
|
|
|
|
|> Repo.exists?()
|
|
|
|
end
|
|
|
|
|
2021-06-27 11:21:34 +02:00
|
|
|
@doc """
|
|
|
|
Gets an event by its UUID, with all associations loaded.
|
|
|
|
"""
|
|
|
|
@spec get_event_by_uuid_with_preload(String.t()) :: Event.t() | nil
|
|
|
|
def get_event_by_uuid_with_preload(uuid) do
|
|
|
|
uuid
|
|
|
|
|> event_by_uuid_query()
|
|
|
|
|> preload_for_event()
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2019-10-02 17:59:07 +02:00
|
|
|
@doc """
|
|
|
|
Gets an event by its UUID, with all associations loaded.
|
|
|
|
"""
|
|
|
|
@spec get_own_event_by_uuid_with_preload(String.t(), integer()) :: Event.t() | nil
|
|
|
|
def get_own_event_by_uuid_with_preload(uuid, user_id) do
|
|
|
|
uuid
|
|
|
|
|> event_by_uuid_query()
|
|
|
|
|> user_events_query(user_id)
|
2019-09-16 02:07:44 +02:00
|
|
|
|> preload_for_event()
|
|
|
|
|> Repo.one()
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets an actor's eventual upcoming public event.
|
2018-05-30 14:27:21 +02:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_upcoming_public_event_for_actor(Actor.t(), String.t() | nil) :: Event.t() | nil
|
|
|
|
def get_upcoming_public_event_for_actor(%Actor{id: actor_id}, not_event_uuid \\ nil) do
|
|
|
|
actor_id
|
|
|
|
|> upcoming_public_event_for_actor_query()
|
|
|
|
|> filter_public_visibility()
|
|
|
|
|> filter_not_event_uuid(not_event_uuid)
|
2019-10-02 17:59:07 +02:00
|
|
|
|> filter_draft()
|
2019-09-16 02:07:44 +02:00
|
|
|
|> Repo.one()
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
def get_or_create_event(%{"url" => url} = attrs) do
|
|
|
|
case Repo.get_by(Event, url: url) do
|
|
|
|
%Event{} = event -> {:ok, Repo.preload(event, @event_preloads)}
|
|
|
|
nil -> create_event(attrs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Creates an event.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec create_event(map) ::
|
|
|
|
{:ok, Event.t()}
|
|
|
|
| {:error, Changeset.t()}
|
|
|
|
| {:error, :update | :write, Changeset.t(), map()}
|
2017-12-08 09:58:14 +01:00
|
|
|
def create_event(attrs \\ %{}) do
|
2019-10-25 17:43:37 +02:00
|
|
|
with {:ok, %{insert: %Event{} = event}} <- do_create_event(attrs),
|
|
|
|
%Event{} = event <- Repo.preload(event, @event_preloads) do
|
2020-09-02 08:59:59 +02:00
|
|
|
unless event.draft do
|
|
|
|
Workers.BuildSearch.enqueue(:insert_search_event, %{"event_id" => event.id})
|
|
|
|
end
|
2019-11-04 15:10:58 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
{:ok, event}
|
2019-10-02 17:59:07 +02:00
|
|
|
else
|
|
|
|
err -> err
|
2019-07-26 11:30:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
# We start by inserting the event and then insert a first participant if the event is not a draft
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec do_create_event(map) ::
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, %{insert: Event.t(), write: Participant.t() | nil}}
|
2021-09-24 16:46:42 +02:00
|
|
|
| {:error, Changeset.t()}
|
|
|
|
| {:error, :update | :write, Changeset.t(), map()}
|
2019-07-26 11:30:28 +02:00
|
|
|
defp do_create_event(attrs) do
|
2019-10-25 17:43:37 +02:00
|
|
|
Multi.new()
|
|
|
|
|> Multi.insert(:insert, Event.changeset(%Event{}, attrs))
|
|
|
|
|> Multi.run(:write, fn _repo, %{insert: %Event{draft: draft} = event} ->
|
|
|
|
with {:is_draft, false} <- {:is_draft, draft},
|
|
|
|
{:ok, %Participant{} = participant} <-
|
|
|
|
create_participant(
|
|
|
|
%{
|
|
|
|
event_id: event.id,
|
|
|
|
role: :creator,
|
|
|
|
actor_id: event.organizer_actor_id
|
|
|
|
},
|
|
|
|
false
|
|
|
|
) do
|
|
|
|
{:ok, participant}
|
|
|
|
else
|
|
|
|
{:is_draft, true} -> {:ok, nil}
|
|
|
|
err -> err
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|> Repo.transaction()
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Updates an event.
|
2019-09-30 18:18:04 +02:00
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
We start by updating the event and then insert a first participant if the event is not a draft anymore
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec update_event(Event.t(), map) ::
|
|
|
|
{:ok, Event.t()}
|
|
|
|
| {:error, Changeset.t()}
|
|
|
|
| {:error, :update | :write, Changeset.t(), map()}
|
2019-11-04 15:10:58 +01:00
|
|
|
def update_event(%Event{draft: old_draft} = old_event, attrs) do
|
2021-07-29 17:50:23 +02:00
|
|
|
with %Event{} = old_event <- Repo.preload(old_event, @event_preloads),
|
|
|
|
%Changeset{changes: changes} = changeset <-
|
|
|
|
Event.update_changeset(old_event, attrs),
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, %{update: %Event{} = new_event}} <-
|
|
|
|
Multi.new()
|
2020-01-23 00:55:07 +01:00
|
|
|
|> Multi.update(:update, changeset)
|
2019-10-25 17:43:37 +02:00
|
|
|
|> Multi.run(:write, fn _repo, %{update: %Event{draft: draft} = event} ->
|
2019-11-04 15:10:58 +01:00
|
|
|
with {:was_draft, true} <- {:was_draft, old_draft == true && draft == false},
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, %Participant{} = participant} <-
|
|
|
|
create_participant(
|
|
|
|
%{
|
|
|
|
event_id: event.id,
|
|
|
|
role: :creator,
|
|
|
|
actor_id: event.organizer_actor_id
|
|
|
|
},
|
|
|
|
false
|
|
|
|
) do
|
|
|
|
{:ok, participant}
|
|
|
|
else
|
2019-11-04 15:10:58 +01:00
|
|
|
{:was_draft, false} -> {:ok, nil}
|
2019-10-25 17:43:37 +02:00
|
|
|
err -> err
|
|
|
|
end
|
|
|
|
end)
|
2021-07-29 17:50:23 +02:00
|
|
|
|> Repo.transaction(),
|
|
|
|
%Event{} = new_event <- Repo.preload(new_event, @event_preloads, force: true) do
|
2019-10-25 17:43:37 +02:00
|
|
|
Cachex.del(:ics, "event_#{new_event.uuid}")
|
|
|
|
|
2020-01-23 21:59:50 +01:00
|
|
|
Email.Event.calculate_event_diff_and_send_notifications(
|
2019-10-25 17:43:37 +02:00
|
|
|
old_event,
|
|
|
|
new_event,
|
|
|
|
changes
|
|
|
|
)
|
2019-10-22 10:25:28 +02:00
|
|
|
|
2019-11-04 15:10:58 +01:00
|
|
|
unless new_event.draft,
|
2020-01-23 21:59:50 +01:00
|
|
|
do: Workers.BuildSearch.enqueue(:update_search_event, %{"event_id" => new_event.id})
|
2019-10-25 17:43:37 +02:00
|
|
|
|
2021-07-29 17:50:23 +02:00
|
|
|
{:ok, new_event}
|
2019-09-30 18:18:04 +02:00
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Deletes an event.
|
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec delete_event(Event.t()) :: {:ok, Event.t()} | {:error, Changeset.t()}
|
2019-09-16 02:07:44 +02:00
|
|
|
def delete_event(%Event{} = event), do: Repo.delete(event)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Deletes an event.
|
|
|
|
Raises an exception if it fails.
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec delete_event!(Event.t()) :: Event.t()
|
2019-09-16 02:07:44 +02:00
|
|
|
def delete_event!(%Event{} = event), do: Repo.delete!(event)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of events.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2020-12-17 16:52:57 +01:00
|
|
|
@spec list_events(integer | nil, integer | nil, atom, atom, boolean) :: Page.t()
|
2019-09-16 02:07:44 +02:00
|
|
|
def list_events(
|
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil,
|
|
|
|
sort \\ :begins_on,
|
|
|
|
direction \\ :asc,
|
|
|
|
is_future \\ true
|
|
|
|
) do
|
2020-12-18 16:32:54 +01:00
|
|
|
Event
|
|
|
|
|> distinct([e], [{^direction, ^sort}, asc: e.id])
|
|
|
|
|> preload([:organizer_actor, :participants])
|
2019-09-16 02:07:44 +02:00
|
|
|
|> sort(sort, direction)
|
|
|
|
|> filter_future_events(is_future)
|
2020-06-15 09:11:35 +02:00
|
|
|
|> filter_public_visibility()
|
2019-10-02 17:59:07 +02:00
|
|
|
|> filter_draft()
|
2020-12-18 16:43:31 +01:00
|
|
|
|> filter_cancelled_events()
|
2020-02-07 16:28:49 +01:00
|
|
|
|> filter_local_or_from_followed_instances_events()
|
2020-12-18 16:32:54 +01:00
|
|
|
|> Page.build_page(page, limit)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2020-08-12 16:05:34 +02:00
|
|
|
@spec stream_events_for_sitemap :: Enum.t()
|
|
|
|
def stream_events_for_sitemap do
|
|
|
|
Event
|
|
|
|
|> filter_public_visibility()
|
|
|
|
|> filter_draft()
|
|
|
|
|> filter_local()
|
|
|
|
|> Repo.stream()
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec list_public_local_events(integer | nil, integer | nil) :: Page.t(Event.t())
|
2020-08-31 17:26:08 +02:00
|
|
|
def list_public_local_events(page \\ nil, limit \\ nil) do
|
|
|
|
Event
|
|
|
|
|> filter_public_visibility()
|
|
|
|
|> filter_draft()
|
|
|
|
|> filter_local()
|
|
|
|
|> preload_for_event()
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2019-01-25 17:06:57 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of events with the same tags.
|
2019-01-25 17:06:57 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec list_events_by_tags([Tag.t()], integer) :: [Event.t()]
|
|
|
|
def list_events_by_tags(tags, limit \\ 2) do
|
|
|
|
tags
|
|
|
|
|> Enum.map(& &1.id)
|
|
|
|
|> events_by_tags_query(limit)
|
2019-10-02 17:59:07 +02:00
|
|
|
|> filter_draft()
|
2019-09-16 02:07:44 +02:00
|
|
|
|> Repo.all()
|
2019-01-25 17:06:57 +01:00
|
|
|
end
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Lists public events for the actor, with all associations loaded.
|
|
|
|
"""
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec list_public_events_for_actor(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
def list_public_events_for_actor(actor, page \\ nil, limit \\ nil)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
def list_public_events_for_actor(%Actor{type: :Group} = group, page, limit),
|
2020-10-19 09:30:20 +02:00
|
|
|
do: list_organized_events_for_group(group, :public, nil, nil, page, limit)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
def list_public_events_for_actor(%Actor{id: actor_id}, page, limit) do
|
|
|
|
actor_id
|
|
|
|
|> event_for_actor_query()
|
|
|
|
|> filter_public_visibility()
|
|
|
|
|> filter_draft()
|
|
|
|
|> preload_for_event()
|
|
|
|
|> Page.build_page(page, limit)
|
2019-09-16 02:07:44 +02:00
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
@spec list_organized_events_for_actor(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
def list_organized_events_for_actor(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do
|
|
|
|
actor_id
|
2021-05-12 18:22:20 +02:00
|
|
|
|> event_for_actor_query(desc: :begins_on)
|
2020-06-11 19:13:21 +02:00
|
|
|
|> preload_for_event()
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2020-10-22 18:06:02 +02:00
|
|
|
def list_simple_organized_events_for_group(%Actor{} = actor, page \\ nil, limit \\ nil) do
|
2020-10-22 16:59:04 +02:00
|
|
|
list_organized_events_for_group(actor, :all, nil, nil, page, limit)
|
|
|
|
end
|
|
|
|
|
2020-09-02 17:42:17 +02:00
|
|
|
@spec list_organized_events_for_group(
|
|
|
|
Actor.t(),
|
2021-09-28 19:40:37 +02:00
|
|
|
EventVisibility.t(),
|
2020-09-02 17:42:17 +02:00
|
|
|
DateTime.t() | nil,
|
|
|
|
DateTime.t() | nil,
|
|
|
|
integer | nil,
|
|
|
|
integer | nil
|
2021-09-24 16:46:42 +02:00
|
|
|
) :: Page.t(Event.t())
|
2020-09-02 17:42:17 +02:00
|
|
|
def list_organized_events_for_group(
|
|
|
|
%Actor{id: group_id},
|
|
|
|
visibility \\ :public,
|
|
|
|
after_datetime \\ nil,
|
|
|
|
before_datetime \\ nil,
|
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
) do
|
2020-02-18 08:57:00 +01:00
|
|
|
group_id
|
|
|
|
|> event_for_group_query()
|
2020-09-02 17:42:17 +02:00
|
|
|
|> event_filter_visibility(visibility)
|
|
|
|
|> event_filter_begins_on(after_datetime, before_datetime)
|
2020-02-18 08:57:00 +01:00
|
|
|
|> preload_for_event()
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2019-10-02 17:59:07 +02:00
|
|
|
@spec list_drafts_for_user(integer, integer | nil, integer | nil) :: [Event.t()]
|
|
|
|
def list_drafts_for_user(user_id, page \\ nil, limit \\ nil) do
|
|
|
|
Event
|
|
|
|
|> user_events_query(user_id)
|
|
|
|
|> filter_draft(true)
|
|
|
|
|> Page.paginate(page, limit)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2020-06-15 19:41:11 +02:00
|
|
|
@spec is_user_moderator_for_event?(integer | String.t(), integer | String.t()) :: boolean
|
|
|
|
def is_user_moderator_for_event?(user_id, event_id) do
|
|
|
|
Participant
|
|
|
|
|> join(:inner, [p], a in Actor, on: p.actor_id == a.id)
|
|
|
|
|> where([p, _a], p.event_id == ^event_id)
|
|
|
|
|> where([_p, a], a.user_id == ^user_id)
|
|
|
|
|> where([p, _a], p.role == ^:creator)
|
|
|
|
|> Repo.exists?()
|
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Finds close events to coordinates.
|
|
|
|
Radius is in meters and defaults to 50km.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec find_close_events(number, number, number, number) :: [Event.t()]
|
|
|
|
def find_close_events(lon, lat, radius \\ 50_000, srid \\ 4326) do
|
|
|
|
"SRID=#{srid};POINT(#{lon} #{lat})"
|
|
|
|
|> Geo.WKT.decode!()
|
|
|
|
|> close_events_query(radius)
|
2019-10-02 17:59:07 +02:00
|
|
|
|> filter_draft()
|
2019-09-16 02:07:44 +02:00
|
|
|
|> Repo.all()
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Counts local events.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec count_local_events :: integer
|
|
|
|
def count_local_events do
|
|
|
|
count_local_events_query()
|
2020-06-12 19:19:19 +02:00
|
|
|
|> filter_unlisted_and_public_visibility()
|
2019-10-02 17:59:07 +02:00
|
|
|
|> filter_draft()
|
2019-09-16 02:07:44 +02:00
|
|
|
|> Repo.one()
|
2019-02-14 14:19:55 +01:00
|
|
|
end
|
|
|
|
|
2020-09-30 10:42:19 +02:00
|
|
|
@doc """
|
|
|
|
Counts all events.
|
|
|
|
"""
|
|
|
|
@spec count_events :: integer
|
|
|
|
def count_events do
|
|
|
|
count_events_query()
|
|
|
|
|> filter_unlisted_and_public_visibility()
|
|
|
|
|> filter_draft()
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2019-02-14 14:19:55 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Builds a page struct for events by their name.
|
2019-02-14 14:19:55 +01:00
|
|
|
"""
|
2020-07-31 17:52:26 +02:00
|
|
|
@spec build_events_for_search(map(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
def build_events_for_search(%{term: term} = args, page \\ nil, limit \\ nil) do
|
|
|
|
term
|
2019-10-22 10:25:28 +02:00
|
|
|
|> normalize_search_string()
|
|
|
|
|> events_for_search_query()
|
2020-08-05 11:42:23 +02:00
|
|
|
|> events_for_begins_on(args)
|
|
|
|
|> events_for_ends_on(args)
|
2020-08-03 17:34:50 +02:00
|
|
|
|> events_for_tags(args)
|
2020-07-31 17:52:26 +02:00
|
|
|
|> events_for_location(args)
|
2021-07-28 10:13:24 +02:00
|
|
|
|> filter_draft()
|
2020-02-07 16:28:49 +01:00
|
|
|
|> filter_local_or_from_followed_instances_events()
|
2021-01-14 11:52:37 +01:00
|
|
|
|> filter_public_visibility()
|
|
|
|
|> event_order_begins_on_asc()
|
2021-03-17 17:09:52 +01:00
|
|
|
|> Page.build_page(page, limit, :begins_on)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single tag.
|
2019-09-16 02:07:44 +02:00
|
|
|
"""
|
|
|
|
@spec get_tag(integer | String.t()) :: Tag.t() | nil
|
|
|
|
def get_tag(id), do: Repo.get(Tag, id)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Gets a single tag.
|
|
|
|
Raises `Ecto.NoResultsError` if the tag does not exist.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_tag!(integer | String.t()) :: Tag.t()
|
2017-12-08 09:58:14 +01:00
|
|
|
def get_tag!(id), do: Repo.get!(Tag, id)
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Gets a tag by its slug.
|
|
|
|
"""
|
|
|
|
@spec get_tag_by_slug(String.t()) :: Tag.t() | nil
|
2019-09-13 01:01:17 +02:00
|
|
|
def get_tag_by_slug(slug) do
|
2019-09-16 02:07:44 +02:00
|
|
|
slug
|
|
|
|
|> tag_by_slug_query()
|
|
|
|
|> Repo.one()
|
2019-09-13 01:01:17 +02:00
|
|
|
end
|
|
|
|
|
2019-11-04 15:10:58 +01:00
|
|
|
@doc """
|
|
|
|
Gets a tag by its title.
|
|
|
|
"""
|
|
|
|
@spec get_tag_by_title(String.t()) :: Tag.t() | nil
|
|
|
|
def get_tag_by_title(slug) do
|
|
|
|
slug
|
|
|
|
|> tag_by_title_query()
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets an existing tag or creates the new one.
|
2020-11-06 15:43:38 +01:00
|
|
|
|
|
|
|
From a map containing a %{"name" => "#mytag"} or a direct binary
|
2019-07-26 11:30:28 +02:00
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec get_or_create_tag(map) :: {:ok, Tag.t()} | {:error, Changeset.t()}
|
2019-09-16 02:07:44 +02:00
|
|
|
def get_or_create_tag(%{"name" => "#" <> title}) do
|
2019-07-26 11:30:28 +02:00
|
|
|
case Repo.get_by(Tag, title: title) do
|
|
|
|
%Tag{} = tag ->
|
|
|
|
{:ok, tag}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
create_tag(%{"title" => title})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec get_or_create_tag(String.t()) :: {:ok, Tag.t()} | {:error, Changeset.t()}
|
|
|
|
def get_or_create_tag(title) do
|
|
|
|
case Repo.get_by(Tag, title: title) do
|
|
|
|
%Tag{} = tag ->
|
|
|
|
{:ok, tag}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
create_tag(%{"title" => title})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
@doc """
|
|
|
|
Creates a tag.
|
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec create_tag(map) :: {:ok, Tag.t()} | {:error, Changeset.t()}
|
2017-12-08 09:58:14 +01:00
|
|
|
def create_tag(attrs \\ %{}) do
|
|
|
|
%Tag{}
|
|
|
|
|> Tag.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a tag.
|
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec update_tag(Tag.t(), map) :: {:ok, Tag.t()} | {:error, Changeset.t()}
|
2017-12-08 09:58:14 +01:00
|
|
|
def update_tag(%Tag{} = tag, attrs) do
|
|
|
|
tag
|
|
|
|
|> Tag.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Deletes a tag.
|
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec delete_tag(Tag.t()) :: {:ok, Tag.t()} | {:error, Changeset.t()}
|
2019-09-16 02:07:44 +02:00
|
|
|
def delete_tag(%Tag{} = tag), do: Repo.delete(tag)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of tags.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec list_tags(String.t() | nil, integer | nil, integer | nil) :: [Tag.t()]
|
|
|
|
def list_tags(filter \\ nil, page \\ nil, limit \\ nil) do
|
2019-09-16 02:07:44 +02:00
|
|
|
Tag
|
2021-09-10 11:27:59 +02:00
|
|
|
|> tag_filter(filter)
|
2019-09-16 02:07:44 +02:00
|
|
|
|> Page.paginate(page, limit)
|
|
|
|
|> Repo.all()
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of tags for the event.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec list_tags_for_event(integer | String.t(), integer | nil, integer | nil) :: [Tag.t()]
|
|
|
|
def list_tags_for_event(event_id, page \\ nil, limit \\ nil) do
|
|
|
|
event_id
|
|
|
|
|> tags_for_event_query()
|
|
|
|
|> Page.paginate(page, limit)
|
|
|
|
|> Repo.all()
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Checks whether two tags are linked or not.
|
|
|
|
"""
|
|
|
|
@spec are_tags_linked(Tag.t(), Tag.t()) :: boolean
|
|
|
|
def are_tags_linked(%Tag{id: tag1_id}, %Tag{id: tag2_id}) do
|
|
|
|
tag_relation =
|
|
|
|
tag1_id
|
|
|
|
|> tags_linked_query(tag2_id)
|
|
|
|
|> Repo.one()
|
|
|
|
|
|
|
|
!!tag_relation
|
|
|
|
end
|
2019-02-14 14:19:55 +01:00
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Creates a relation between two tags.
|
2019-02-14 14:19:55 +01:00
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec create_tag_relation(map) :: {:ok, TagRelation.t()} | {:error, Changeset.t()}
|
2019-02-14 14:19:55 +01:00
|
|
|
def create_tag_relation(attrs \\ {}) do
|
|
|
|
%TagRelation{}
|
|
|
|
|> TagRelation.changeset(attrs)
|
2019-09-16 02:07:44 +02:00
|
|
|
|> Repo.insert(
|
|
|
|
conflict_target: [:tag_id, :link_id],
|
|
|
|
on_conflict: [inc: [weight: 1]]
|
|
|
|
)
|
2019-02-14 14:19:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Removes a tag relation.
|
2019-02-14 14:19:55 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec delete_tag_relation(TagRelation.t()) ::
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, TagRelation.t()} | {:error, Changeset.t()}
|
2019-02-14 14:19:55 +01:00
|
|
|
def delete_tag_relation(%TagRelation{} = tag_relation) do
|
|
|
|
Repo.delete(tag_relation)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the tags neighbors for a given tag
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
We can't rely on the single many_to_many relation since we also want tags that
|
|
|
|
link to our tag, not just tags linked by this one.
|
2019-02-14 14:19:55 +01:00
|
|
|
|
|
|
|
The SQL query looks like this:
|
|
|
|
```sql
|
|
|
|
SELECT * FROM tags t
|
|
|
|
RIGHT JOIN (
|
|
|
|
SELECT weight, link_id AS id
|
|
|
|
FROM tag_relations t2
|
|
|
|
WHERE tag_id = 1
|
|
|
|
UNION ALL
|
|
|
|
SELECT tag_id AS id, weight
|
|
|
|
FROM tag_relations t2
|
|
|
|
WHERE link_id = 1
|
|
|
|
) tr
|
|
|
|
ON t.id = tr.id
|
|
|
|
ORDER BY tr.weight
|
|
|
|
DESC;
|
|
|
|
```
|
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec list_tag_neighbors(Tag.t(), integer, integer) :: [Tag.t()]
|
|
|
|
def list_tag_neighbors(%Tag{id: tag_id}, relation_minimum \\ 1, limit \\ 10) do
|
|
|
|
tag_id
|
|
|
|
|> tag_relation_subquery()
|
|
|
|
|> tag_relation_union_subquery(tag_id)
|
|
|
|
|> tag_neighbors_query(relation_minimum, limit)
|
|
|
|
|> Repo.all()
|
2019-02-14 14:19:55 +01:00
|
|
|
end
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Gets a single participant.
|
2019-09-20 18:22:03 +02:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2020-06-04 10:58:27 +02:00
|
|
|
iex> get_participant(123)
|
|
|
|
%Participant{}
|
2019-09-20 18:22:03 +02:00
|
|
|
|
|
|
|
iex> get_participant(456)
|
|
|
|
nil
|
|
|
|
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get_participant(integer) :: Participant.t() | nil
|
2021-09-10 11:35:32 +02:00
|
|
|
@spec get_participant(integer | String.t(), integer | String.t(), map()) ::
|
|
|
|
{:ok, Participant.t()} | {:error, :participant_not_found}
|
2019-09-20 18:22:03 +02:00
|
|
|
def get_participant(participant_id) do
|
|
|
|
Participant
|
|
|
|
|> where([p], p.id == ^participant_id)
|
2021-07-29 17:50:23 +02:00
|
|
|
|> preload([p], ^@participant_preloads)
|
2019-09-20 18:22:03 +02:00
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single participation for an event and actor.
|
2019-09-16 02:07:44 +02:00
|
|
|
"""
|
2019-12-20 13:04:34 +01:00
|
|
|
def get_participant(event_id, actor_id, params \\ %{})
|
|
|
|
|
|
|
|
# This one if to check someone doesn't go to the same event twice
|
|
|
|
def get_participant(event_id, actor_id, %{email: email}) do
|
|
|
|
case Participant
|
|
|
|
|> where([p], event_id: ^event_id, actor_id: ^actor_id)
|
|
|
|
|> where([p], fragment("? ->>'email' = ?", p.metadata, ^email))
|
2021-07-29 17:50:23 +02:00
|
|
|
|> preload([p], ^@participant_preloads)
|
2019-12-20 13:04:34 +01:00
|
|
|
|> Repo.one() do
|
|
|
|
%Participant{} = participant ->
|
|
|
|
{:ok, participant}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :participant_not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This one if for finding participants by their cancellation token when wanting to cancel a participation
|
|
|
|
def get_participant(event_id, actor_id, %{cancellation_token: cancellation_token}) do
|
|
|
|
case Participant
|
|
|
|
|> where([p], event_id: ^event_id, actor_id: ^actor_id)
|
|
|
|
|> where([p], fragment("? ->>'cancellation_token' = ?", p.metadata, ^cancellation_token))
|
2021-07-29 17:50:23 +02:00
|
|
|
|> preload([p], ^@participant_preloads)
|
2019-12-20 13:04:34 +01:00
|
|
|
|> Repo.one() do
|
|
|
|
%Participant{} = participant ->
|
|
|
|
{:ok, participant}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :participant_not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_participant(event_id, actor_id, %{}) do
|
2021-07-29 17:50:23 +02:00
|
|
|
case Participant
|
|
|
|
|> Repo.get_by(event_id: event_id, actor_id: actor_id)
|
|
|
|
|> Repo.preload(@participant_preloads) do
|
2019-09-16 02:07:44 +02:00
|
|
|
%Participant{} = participant ->
|
|
|
|
{:ok, participant}
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
nil ->
|
|
|
|
{:error, :participant_not_found}
|
|
|
|
end
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
@spec get_participant_by_confirmation_token(String.t()) :: Participant.t()
|
|
|
|
def get_participant_by_confirmation_token(confirmation_token) do
|
|
|
|
Participant
|
|
|
|
|> where([p], fragment("? ->>'confirmation_token' = ?", p.metadata, ^confirmation_token))
|
2021-07-29 17:50:23 +02:00
|
|
|
|> preload([p], ^@participant_preloads)
|
2019-12-20 13:04:34 +01:00
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
2019-09-20 18:22:03 +02:00
|
|
|
Gets a single participation for an event and actor.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Participant does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2020-06-04 10:58:27 +02:00
|
|
|
iex> get_participant!(123, 19)
|
|
|
|
%Participant{}
|
2019-09-20 18:22:03 +02:00
|
|
|
|
|
|
|
iex> get_participant!(456, 5)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
"""
|
|
|
|
@spec get_participant!(integer | String.t(), integer | String.t()) :: Participant.t()
|
|
|
|
def get_participant!(event_id, actor_id) do
|
|
|
|
Repo.get_by!(Participant, event_id: event_id, actor_id: actor_id)
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Gets a participant by its URL.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_participant_by_url(String.t()) :: Participant.t() | nil
|
|
|
|
def get_participant_by_url(url) do
|
|
|
|
url
|
|
|
|
|> participant_by_url_query()
|
|
|
|
|> Repo.one()
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2020-06-15 19:41:11 +02:00
|
|
|
@moderator_roles [:moderator, :administrator, :creator]
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2020-10-15 11:04:05 +02:00
|
|
|
@doc """
|
|
|
|
Returns the number of participations for all local events
|
|
|
|
"""
|
|
|
|
@spec count_confirmed_participants_for_local_events :: integer()
|
|
|
|
def count_confirmed_participants_for_local_events do
|
|
|
|
count_confirmed_participants_for_local_events_query()
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
@doc """
|
2019-09-20 18:22:03 +02:00
|
|
|
Returns the list of participants for an event.
|
2019-12-20 13:04:34 +01:00
|
|
|
Default behaviour is to not return :not_approved or :not_confirmed participants
|
2018-01-13 23:33:03 +01:00
|
|
|
"""
|
2019-09-20 18:22:03 +02:00
|
|
|
@spec list_participants_for_event(String.t(), list(atom()), integer | nil, integer | nil) ::
|
2020-03-05 19:32:34 +01:00
|
|
|
Page.t()
|
2019-09-22 11:22:16 +02:00
|
|
|
def list_participants_for_event(
|
2019-09-26 16:38:58 +02:00
|
|
|
id,
|
2020-06-18 15:23:05 +02:00
|
|
|
roles \\ [],
|
2019-09-22 11:22:16 +02:00
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
) do
|
2019-09-26 16:38:58 +02:00
|
|
|
id
|
2019-09-20 18:22:03 +02:00
|
|
|
|> list_participants_for_event_query()
|
|
|
|
|> filter_role(roles)
|
2020-06-18 15:23:05 +02:00
|
|
|
|> order_by(asc: :role)
|
2020-03-05 19:32:34 +01:00
|
|
|
|> Page.build_page(page, limit)
|
2018-01-13 23:33:03 +01:00
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec list_actors_participants_for_event(String.t()) :: [Actor.t()]
|
|
|
|
def list_actors_participants_for_event(id) do
|
|
|
|
id
|
|
|
|
|> list_participant_actors_for_event_query
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2019-09-18 17:32:37 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of participations for an actor.
|
|
|
|
|
|
|
|
Default behaviour is to not return :not_approved participants
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> list_event_participations_for_user(5)
|
|
|
|
[%Participant{}, ...]
|
|
|
|
|
|
|
|
"""
|
2019-09-20 18:22:03 +02:00
|
|
|
@spec list_participations_for_user(
|
|
|
|
integer,
|
|
|
|
DateTime.t() | nil,
|
|
|
|
DateTime.t() | nil,
|
|
|
|
integer | nil,
|
|
|
|
integer | nil
|
2020-02-18 08:57:00 +01:00
|
|
|
) :: Page.t()
|
2019-09-20 18:22:03 +02:00
|
|
|
def list_participations_for_user(user_id, after_datetime, before_datetime, page, limit) do
|
2019-09-18 17:32:37 +02:00
|
|
|
user_id
|
2019-09-20 18:22:03 +02:00
|
|
|
|> list_participations_for_user_query()
|
|
|
|
|> participation_filter_begins_on(after_datetime, before_datetime)
|
2020-02-18 08:57:00 +01:00
|
|
|
|> Page.build_page(page, limit)
|
2019-09-18 17:32:37 +02:00
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
@doc """
|
2019-09-20 18:22:03 +02:00
|
|
|
Returns the list of moderator participants for an event.
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2019-09-20 18:22:03 +02:00
|
|
|
## Examples
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2019-09-20 18:22:03 +02:00
|
|
|
iex> moderator_for_event?(5, 3)
|
|
|
|
true
|
2019-09-16 02:07:44 +02:00
|
|
|
|
2019-09-20 18:22:03 +02:00
|
|
|
"""
|
|
|
|
@spec moderator_for_event?(integer, integer) :: boolean
|
|
|
|
def moderator_for_event?(event_id, actor_id) do
|
|
|
|
!(Repo.one(
|
|
|
|
from(
|
|
|
|
p in Participant,
|
|
|
|
where:
|
|
|
|
p.event_id == ^event_id and
|
|
|
|
p.actor_id ==
|
2020-06-15 19:41:11 +02:00
|
|
|
^actor_id and p.role in ^@moderator_roles
|
2019-09-20 18:22:03 +02:00
|
|
|
)
|
|
|
|
) == nil)
|
2019-02-01 15:38:35 +01:00
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of organizers participants for an event.
|
2019-09-18 17:32:37 +02:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> list_organizers_participants_for_event(id)
|
|
|
|
[%Participant{role: :creator}, ...]
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec list_organizers_participants_for_event(
|
|
|
|
integer | String.t(),
|
|
|
|
integer | nil,
|
|
|
|
integer | nil
|
|
|
|
) ::
|
|
|
|
[Participant.t()]
|
|
|
|
def list_organizers_participants_for_event(event_id, page \\ nil, limit \\ nil) do
|
|
|
|
event_id
|
|
|
|
|> organizers_participants_for_event()
|
|
|
|
|> Page.paginate(page, limit)
|
|
|
|
|> Repo.all()
|
2018-01-13 23:33:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of event participation requests for an actor.
|
2018-01-13 23:33:03 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec list_requests_for_actor(Actor.t()) :: [Participant.t()]
|
|
|
|
def list_requests_for_actor(%Actor{id: actor_id}) do
|
|
|
|
actor_id
|
|
|
|
|> requests_for_actor_query()
|
|
|
|
|> Repo.all()
|
2018-01-13 23:33:03 +01:00
|
|
|
end
|
|
|
|
|
2018-01-16 19:45:09 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of participations for an actor.
|
2018-01-16 19:45:09 +01:00
|
|
|
"""
|
2020-06-11 19:13:21 +02:00
|
|
|
@spec list_event_participations_for_actor(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
2019-09-16 02:07:44 +02:00
|
|
|
def list_event_participations_for_actor(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do
|
|
|
|
actor_id
|
|
|
|
|> event_participations_for_actor_query()
|
2020-06-11 19:13:21 +02:00
|
|
|
|> Page.build_page(page, limit)
|
2018-01-16 19:45:09 +01:00
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
2019-09-21 23:59:07 +02:00
|
|
|
Counts approved participants.
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2019-09-21 23:59:07 +02:00
|
|
|
@spec count_approved_participants(integer | String.t()) :: integer
|
|
|
|
def count_approved_participants(event_id) do
|
|
|
|
event_id
|
|
|
|
|> count_participants_query()
|
|
|
|
|> filter_approved_role()
|
|
|
|
|> Repo.aggregate(:count, :id)
|
2018-01-13 23:33:03 +01:00
|
|
|
end
|
|
|
|
|
2019-10-11 11:50:06 +02:00
|
|
|
@doc """
|
2019-10-25 17:43:37 +02:00
|
|
|
Counts participant participants (participants with no extra role)
|
2019-10-11 11:50:06 +02:00
|
|
|
"""
|
|
|
|
@spec count_participant_participants(integer | String.t()) :: integer
|
|
|
|
def count_participant_participants(event_id) do
|
|
|
|
event_id
|
|
|
|
|> count_participants_query()
|
|
|
|
|> filter_participant_role()
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
end
|
|
|
|
|
2019-09-30 13:48:47 +02:00
|
|
|
@doc """
|
|
|
|
Counts rejected participants.
|
|
|
|
"""
|
|
|
|
@spec count_rejected_participants(integer | String.t()) :: integer
|
|
|
|
def count_rejected_participants(event_id) do
|
|
|
|
event_id
|
|
|
|
|> count_participants_query()
|
|
|
|
|> filter_rejected_role()
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
end
|
|
|
|
|
2019-09-20 18:22:03 +02:00
|
|
|
@doc """
|
|
|
|
Gets the default participant role depending on the event join options.
|
|
|
|
"""
|
|
|
|
@spec get_default_participant_role(Event.t()) :: :participant | :not_approved
|
|
|
|
def get_default_participant_role(%Event{join_options: :free}), do: :participant
|
|
|
|
def get_default_participant_role(%Event{join_options: _}), do: :not_approved
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a participant.
|
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec create_participant(map) :: {:ok, Participant.t()} | {:error, Changeset.t()}
|
|
|
|
def create_participant(attrs \\ %{}, update_event_participation_stats \\ true) do
|
|
|
|
with {:ok, %{participant: %Participant{} = participant}} <-
|
|
|
|
Multi.new()
|
|
|
|
|> Multi.insert(:participant, Participant.changeset(%Participant{}, attrs))
|
|
|
|
|> Multi.run(:update_event_participation_stats, fn _repo,
|
|
|
|
%{
|
|
|
|
participant:
|
2019-12-03 11:29:51 +01:00
|
|
|
%Participant{role: new_role} =
|
|
|
|
participant
|
2019-10-25 17:43:37 +02:00
|
|
|
} ->
|
2019-12-03 11:29:51 +01:00
|
|
|
update_participant_stats(
|
|
|
|
participant,
|
|
|
|
nil,
|
|
|
|
new_role,
|
|
|
|
update_event_participation_stats
|
|
|
|
)
|
2019-10-25 17:43:37 +02:00
|
|
|
end)
|
|
|
|
|> Repo.transaction() do
|
2019-09-20 18:22:03 +02:00
|
|
|
{:ok, Repo.preload(participant, [:event, :actor])}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a participant.
|
|
|
|
"""
|
|
|
|
@spec update_participant(Participant.t(), map) ::
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, Participant.t()} | {:error, Changeset.t()}
|
2019-12-03 11:29:51 +01:00
|
|
|
def update_participant(%Participant{role: old_role} = participant, attrs) do
|
|
|
|
with {:ok, %{participant: %Participant{} = participant}} <-
|
|
|
|
Multi.new()
|
|
|
|
|> Multi.update(:participant, Participant.changeset(participant, attrs))
|
|
|
|
|> Multi.run(:update_event_participation_stats, fn _repo,
|
|
|
|
%{
|
|
|
|
participant:
|
|
|
|
%Participant{role: new_role} =
|
|
|
|
participant
|
|
|
|
} ->
|
|
|
|
update_participant_stats(participant, old_role, new_role)
|
|
|
|
end)
|
|
|
|
|> Repo.transaction() do
|
|
|
|
{:ok, Repo.preload(participant, [:event, :actor])}
|
|
|
|
end
|
2019-09-20 18:22:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a participant.
|
|
|
|
"""
|
|
|
|
@spec delete_participant(Participant.t()) ::
|
2021-09-24 16:46:42 +02:00
|
|
|
{:ok, %{participant: Participant.t()}}
|
|
|
|
| {:error, :participant | :update_event_participation_stats, Changeset.t(), map()}
|
2019-12-03 11:29:51 +01:00
|
|
|
def delete_participant(%Participant{role: old_role} = participant) do
|
2021-09-24 16:46:42 +02:00
|
|
|
Multi.new()
|
|
|
|
|> Multi.delete(:participant, participant)
|
|
|
|
|> Multi.run(:update_event_participation_stats, fn _repo,
|
|
|
|
%{
|
|
|
|
participant: %Participant{} = participant
|
|
|
|
} ->
|
|
|
|
update_participant_stats(participant, old_role, nil)
|
|
|
|
end)
|
|
|
|
|> Repo.transaction()
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp update_participant_stats(
|
|
|
|
%Participant{
|
|
|
|
event_id: event_id
|
|
|
|
} = _participant,
|
|
|
|
old_role,
|
|
|
|
new_role,
|
|
|
|
update_event_participation_stats \\ true
|
|
|
|
) do
|
|
|
|
with {:update_event_participation_stats, true} <-
|
|
|
|
{:update_event_participation_stats, update_event_participation_stats},
|
2020-09-29 09:53:48 +02:00
|
|
|
{:ok, %Event{} = event} <- get_event_with_preload(event_id),
|
2019-12-03 11:29:51 +01:00
|
|
|
%EventParticipantStats{} = participant_stats <-
|
|
|
|
Map.get(event, :participant_stats),
|
|
|
|
%EventParticipantStats{} = participant_stats <-
|
|
|
|
do_update_participant_stats(participant_stats, old_role, new_role),
|
|
|
|
{:ok, %Event{} = event} <-
|
|
|
|
event
|
|
|
|
|> Event.update_changeset(%{
|
|
|
|
participant_stats: Map.from_struct(participant_stats)
|
|
|
|
})
|
|
|
|
|> Repo.update() do
|
|
|
|
{:ok, event}
|
|
|
|
else
|
|
|
|
{:update_event_participation_stats, false} ->
|
|
|
|
{:ok, nil}
|
|
|
|
|
|
|
|
{:error, :event_not_found} ->
|
|
|
|
{:error, :event_not_found}
|
|
|
|
|
|
|
|
err ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_update_participant_stats(participant_stats, old_role, new_role) do
|
|
|
|
participant_stats
|
|
|
|
|> decrease_participant_stats(old_role)
|
|
|
|
|> increase_participant_stats(new_role)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp increase_participant_stats(participant_stats, nil), do: participant_stats
|
|
|
|
|
|
|
|
defp increase_participant_stats(participant_stats, role),
|
|
|
|
do: Map.update(participant_stats, role, 0, &(&1 + 1))
|
|
|
|
|
|
|
|
defp decrease_participant_stats(participant_stats, nil), do: participant_stats
|
|
|
|
|
|
|
|
defp decrease_participant_stats(participant_stats, role),
|
|
|
|
do: Map.update(participant_stats, role, 0, &(&1 - 1))
|
2019-09-20 18:22:03 +02:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
@doc """
|
|
|
|
Gets a single session.
|
2019-09-16 02:07:44 +02:00
|
|
|
Raises `Ecto.NoResultsError` if the session does not exist.
|
2018-01-13 23:33:03 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_session!(integer | String.t()) :: Session.t()
|
2018-01-13 23:33:03 +01:00
|
|
|
def get_session!(id), do: Repo.get!(Session, id)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
@doc """
|
2018-01-13 23:33:03 +01:00
|
|
|
Creates a session.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec create_session(map) :: {:ok, Session.t()} | {:error, Changeset.t()}
|
2018-01-13 23:33:03 +01:00
|
|
|
def create_session(attrs \\ %{}) do
|
|
|
|
%Session{}
|
|
|
|
|> Session.changeset(attrs)
|
2017-12-08 09:58:14 +01:00
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2018-01-13 23:33:03 +01:00
|
|
|
Updates a session.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec update_session(Session.t(), map) :: {:ok, Session.t()} | {:error, Changeset.t()}
|
2018-01-13 23:33:03 +01:00
|
|
|
def update_session(%Session{} = session, attrs) do
|
|
|
|
session
|
|
|
|
|> Session.changeset(attrs)
|
2017-12-08 09:58:14 +01:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Deletes a session.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec delete_session(Session.t()) :: {:ok, Session.t()} | {:error, Changeset.t()}
|
2019-09-16 02:07:44 +02:00
|
|
|
def delete_session(%Session{} = session), do: Repo.delete(session)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of sessions.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec list_sessions :: [Session.t()]
|
|
|
|
def list_sessions, do: Repo.all(Session)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of sessions for the event.
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec list_sessions_for_event(Event.t()) :: [Session.t()]
|
|
|
|
def list_sessions_for_event(%Event{id: event_id}) do
|
|
|
|
event_id
|
|
|
|
|> sessions_for_event_query()
|
|
|
|
|> Repo.all()
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
@doc """
|
2018-01-13 23:33:03 +01:00
|
|
|
Gets a single track.
|
2019-09-16 02:07:44 +02:00
|
|
|
Raises `Ecto.NoResultsError` if the track does not exist.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_track!(integer | String.t()) :: Track.t()
|
2018-01-13 23:33:03 +01:00
|
|
|
def get_track!(id), do: Repo.get!(Track, id)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
@doc """
|
2018-01-13 23:33:03 +01:00
|
|
|
Creates a track.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec create_track(map) :: {:ok, Track.t()} | {:error, Changeset.t()}
|
2018-01-13 23:33:03 +01:00
|
|
|
def create_track(attrs \\ %{}) do
|
|
|
|
%Track{}
|
|
|
|
|> Track.changeset(attrs)
|
2017-12-08 09:58:14 +01:00
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2018-01-13 23:33:03 +01:00
|
|
|
Updates a track.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec update_track(Track.t(), map) :: {:ok, Track.t()} | {:error, Changeset.t()}
|
2018-01-13 23:33:03 +01:00
|
|
|
def update_track(%Track{} = track, attrs) do
|
|
|
|
track
|
|
|
|
|> Track.changeset(attrs)
|
2017-12-08 09:58:14 +01:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Deletes a track.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec delete_track(Track.t()) :: {:ok, Track.t()} | {:error, Changeset.t()}
|
2019-09-16 02:07:44 +02:00
|
|
|
def delete_track(%Track{} = track), do: Repo.delete(track)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of tracks.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec list_tracks :: [Track.t()]
|
|
|
|
def list_tracks, do: Repo.all(Track)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of sessions for the track.
|
2018-05-17 11:32:23 +02:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec list_sessions_for_track(Track.t()) :: [Session.t()]
|
|
|
|
def list_sessions_for_track(%Track{id: track_id}) do
|
|
|
|
track_id
|
|
|
|
|> sessions_for_track_query()
|
|
|
|
|> Repo.all()
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Gets a single feed token.
|
2018-05-17 11:32:23 +02:00
|
|
|
"""
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec get_feed_token(String.t()) :: FeedToken.t() | nil
|
|
|
|
def get_feed_token(token) do
|
|
|
|
token
|
|
|
|
|> feed_token_query()
|
|
|
|
|> Repo.one()
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single feed token.
|
2019-09-16 02:07:44 +02:00
|
|
|
Raises `Ecto.NoResultsError` if the feed token does not exist.
|
|
|
|
"""
|
|
|
|
@spec get_feed_token!(String.t()) :: FeedToken.t()
|
|
|
|
def get_feed_token!(token) do
|
|
|
|
token
|
|
|
|
|> feed_token_query()
|
|
|
|
|> Repo.one!()
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Creates a feed token.
|
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec create_feed_token(map) :: {:ok, FeedToken.t()} | {:error, Changeset.t()}
|
2019-09-16 02:07:44 +02:00
|
|
|
def create_feed_token(attrs \\ %{}) do
|
2020-02-13 15:48:12 +01:00
|
|
|
attrs = Map.put(attrs, :token, Ecto.UUID.generate())
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
%FeedToken{}
|
|
|
|
|> FeedToken.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Updates a feed token.
|
|
|
|
"""
|
|
|
|
@spec update_feed_token(FeedToken.t(), map) ::
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, FeedToken.t()} | {:error, Changeset.t()}
|
2019-09-16 02:07:44 +02:00
|
|
|
def update_feed_token(%FeedToken{} = feed_token, attrs) do
|
|
|
|
feed_token
|
|
|
|
|> FeedToken.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@doc """
|
|
|
|
Deletes a feed token.
|
2019-03-08 12:25:06 +01:00
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec delete_feed_token(FeedToken.t()) :: {:ok, FeedToken.t()} | {:error, Changeset.t()}
|
2019-09-16 02:07:44 +02:00
|
|
|
def delete_feed_token(%FeedToken{} = feed_token), do: Repo.delete(feed_token)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of feed tokens for an user.
|
|
|
|
"""
|
|
|
|
@spec list_feed_tokens_for_user(User.t()) :: [FeedTokens.t()]
|
|
|
|
def list_feed_tokens_for_user(%User{id: user_id}) do
|
|
|
|
user_id
|
|
|
|
|> feed_token_for_user_query()
|
|
|
|
|> Repo.all()
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-16 02:07:44 +02:00
|
|
|
Returns the list of feed tokens for an actor.
|
|
|
|
"""
|
|
|
|
@spec list_feed_tokens_for_actor(Actor.t()) :: [FeedTokens.t()]
|
|
|
|
def list_feed_tokens_for_actor(%Actor{id: actor_id, domain: nil}) do
|
|
|
|
actor_id
|
|
|
|
|> feed_token_for_actor_query()
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec event_by_url_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp event_by_url_query(url) do
|
|
|
|
from(e in Event, where: e.url == ^url)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec event_by_uuid_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp event_by_uuid_query(uuid) do
|
|
|
|
from(e in Event, where: e.uuid == ^uuid)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec event_for_actor_query(integer | String.t()) :: Ecto.Query.t()
|
2021-05-12 18:22:20 +02:00
|
|
|
defp event_for_actor_query(actor_id, order_by \\ [desc: :id]) do
|
|
|
|
Event
|
|
|
|
|> where([e], e.organizer_actor_id == ^actor_id)
|
|
|
|
|> order_by([e], ^order_by)
|
2019-09-16 02:07:44 +02:00
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec event_for_group_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp event_for_group_query(group_id) do
|
|
|
|
from(
|
|
|
|
e in Event,
|
|
|
|
where: e.attributed_to_id == ^group_id,
|
2021-01-11 18:21:38 +01:00
|
|
|
order_by: [asc: :begins_on]
|
2020-02-18 08:57:00 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec upcoming_public_event_for_actor_query(integer | String.t()) :: Ecto.Query.t()
|
|
|
|
defp upcoming_public_event_for_actor_query(actor_id) do
|
|
|
|
from(
|
|
|
|
e in Event,
|
|
|
|
where:
|
|
|
|
e.organizer_actor_id == ^actor_id and
|
|
|
|
e.begins_on > ^DateTime.utc_now(),
|
|
|
|
order_by: [asc: :begins_on],
|
|
|
|
limit: 1,
|
|
|
|
preload: [
|
|
|
|
:organizer_actor,
|
|
|
|
:tags,
|
|
|
|
:participants,
|
|
|
|
:physical_address
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec close_events_query(Geo.geometry(), number) :: Ecto.Query.t()
|
|
|
|
defp close_events_query(point, radius) do
|
2019-03-08 12:25:06 +01:00
|
|
|
from(
|
2019-09-16 02:07:44 +02:00
|
|
|
e in Event,
|
|
|
|
join: a in Address,
|
|
|
|
on: a.id == e.physical_address_id,
|
|
|
|
where: e.visibility == ^:public and st_dwithin_in_meters(^point, a.geom, ^radius),
|
|
|
|
preload: :organizer_actor
|
2019-03-08 12:25:06 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec user_events_query(Ecto.Queryable.t(), number()) :: Ecto.Query.t()
|
2019-10-02 17:59:07 +02:00
|
|
|
defp user_events_query(query, user_id) do
|
|
|
|
from(
|
|
|
|
e in query,
|
|
|
|
join: a in Actor,
|
|
|
|
on: a.id == e.organizer_actor_id,
|
|
|
|
where: a.user_id == ^user_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-10-22 10:25:28 +02:00
|
|
|
defmacro matching_event_ids_and_ranks(search_string) do
|
|
|
|
quote do
|
|
|
|
fragment(
|
|
|
|
"""
|
|
|
|
SELECT event_search.id AS id,
|
|
|
|
ts_rank(
|
|
|
|
event_search.document, plainto_tsquery(unaccent(?))
|
|
|
|
) AS rank
|
|
|
|
FROM event_search
|
|
|
|
WHERE event_search.document @@ plainto_tsquery(unaccent(?))
|
|
|
|
OR event_search.title ILIKE ?
|
|
|
|
""",
|
|
|
|
^unquote(search_string),
|
|
|
|
^unquote(search_string),
|
|
|
|
^"%#{unquote(search_string)}%"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec events_for_search_query(String.t()) :: Ecto.Query.t()
|
2021-01-26 20:13:52 +01:00
|
|
|
defp events_for_search_query("") do
|
|
|
|
Event
|
2021-03-17 17:09:52 +01:00
|
|
|
|> distinct([e], asc: e.begins_on, asc: e.id)
|
2021-01-26 20:13:52 +01:00
|
|
|
end
|
2020-07-31 17:52:26 +02:00
|
|
|
|
2021-01-14 11:52:37 +01:00
|
|
|
defp events_for_search_query(search_string) do
|
|
|
|
from(event in Event,
|
2021-03-17 17:09:52 +01:00
|
|
|
distinct: [asc: event.begins_on, asc: event.id],
|
2019-10-22 10:25:28 +02:00
|
|
|
join: id_and_rank in matching_event_ids_and_ranks(search_string),
|
2021-01-14 11:52:37 +01:00
|
|
|
on: id_and_rank.id == event.id
|
2019-03-08 18:52:27 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec events_for_begins_on(Ecto.Queryable.t(), map()) :: Ecto.Query.t()
|
2020-08-05 11:42:23 +02:00
|
|
|
defp events_for_begins_on(query, args) do
|
|
|
|
begins_on = Map.get(args, :begins_on, DateTime.utc_now())
|
|
|
|
|
|
|
|
query
|
|
|
|
|> where([q], q.begins_on >= ^begins_on)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec events_for_ends_on(Ecto.Queryable.t(), map()) :: Ecto.Query.t()
|
2020-08-05 11:42:23 +02:00
|
|
|
defp events_for_ends_on(query, args) do
|
|
|
|
ends_on = Map.get(args, :ends_on)
|
|
|
|
|
|
|
|
if is_nil(ends_on),
|
|
|
|
do: query,
|
|
|
|
else:
|
|
|
|
where(
|
|
|
|
query,
|
|
|
|
[q],
|
|
|
|
(is_nil(q.ends_on) and q.begins_on <= ^ends_on) or
|
|
|
|
q.ends_on <= ^ends_on
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec events_for_tags(Ecto.Queryable.t(), map()) :: Ecto.Query.t()
|
2021-09-10 11:27:59 +02:00
|
|
|
defp events_for_tags(query, %{tags: tags}) when is_valid_string(tags) do
|
2020-07-31 17:52:26 +02:00
|
|
|
query
|
2020-08-03 17:34:50 +02:00
|
|
|
|> join(:inner, [q], te in "events_tags", on: q.id == te.event_id)
|
|
|
|
|> join(:inner, [q, ..., te], t in Tag, on: te.tag_id == t.id)
|
|
|
|
|> where([q, ..., t], t.title in ^String.split(tags, ",", trim: true))
|
2020-07-31 17:52:26 +02:00
|
|
|
end
|
|
|
|
|
2020-08-03 17:34:50 +02:00
|
|
|
defp events_for_tags(query, _args), do: query
|
2020-07-31 17:52:26 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec events_for_location(Ecto.Queryable.t(), map()) :: Ecto.Query.t()
|
2020-08-05 11:42:23 +02:00
|
|
|
defp events_for_location(query, %{radius: radius}) when is_nil(radius),
|
|
|
|
do: query
|
|
|
|
|
2020-07-31 17:52:26 +02:00
|
|
|
defp events_for_location(query, %{location: location, radius: radius})
|
2021-09-10 11:27:59 +02:00
|
|
|
when is_valid_string(location) and not is_nil(radius) do
|
2020-07-31 17:52:26 +02:00
|
|
|
with {lon, lat} <- Geohax.decode(location),
|
|
|
|
point <- Geo.WKT.decode!("SRID=4326;POINT(#{lon} #{lat})") do
|
|
|
|
query
|
|
|
|
|> join(:inner, [q], a in Address, on: a.id == q.physical_address_id, as: :address)
|
|
|
|
|> where(
|
|
|
|
[q],
|
|
|
|
st_dwithin_in_meters(^point, as(:address).geom, ^(radius * 1000))
|
|
|
|
)
|
|
|
|
else
|
|
|
|
_ -> query
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp events_for_location(query, _args), do: query
|
|
|
|
|
2019-10-22 10:25:28 +02:00
|
|
|
@spec normalize_search_string(String.t()) :: String.t()
|
|
|
|
defp normalize_search_string(search_string) do
|
|
|
|
search_string
|
|
|
|
|> String.downcase()
|
|
|
|
|> String.replace(~r/\n/, " ")
|
|
|
|
|> String.replace(~r/\t/, " ")
|
|
|
|
|> String.replace(~r/\s{2,}/, " ")
|
|
|
|
|> String.trim()
|
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec events_by_tags_query([integer], integer) :: Ecto.Query.t()
|
|
|
|
def events_by_tags_query(tags_ids, limit) do
|
2019-03-08 18:52:27 +01:00
|
|
|
from(
|
2019-09-16 02:07:44 +02:00
|
|
|
e in Event,
|
|
|
|
distinct: e.uuid,
|
|
|
|
join: te in "events_tags",
|
|
|
|
on: e.id == te.event_id,
|
|
|
|
where: e.begins_on > ^DateTime.utc_now(),
|
2020-06-12 19:19:19 +02:00
|
|
|
where: e.visibility == ^:public,
|
2019-09-16 02:07:44 +02:00
|
|
|
where: te.tag_id in ^tags_ids,
|
|
|
|
order_by: [asc: e.begins_on],
|
|
|
|
limit: ^limit
|
2019-03-08 18:52:27 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec count_local_events_query :: Ecto.Query.t()
|
|
|
|
defp count_local_events_query do
|
|
|
|
from(e in Event, select: count(e.id), where: e.local == ^true)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2020-09-30 10:42:19 +02:00
|
|
|
@spec count_events_query :: Ecto.Query.t()
|
|
|
|
defp count_events_query do
|
|
|
|
from(e in Event, select: count(e.id))
|
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec tag_by_slug_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp tag_by_slug_query(slug) do
|
|
|
|
from(t in Tag, where: t.slug == ^slug)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-11-04 15:10:58 +01:00
|
|
|
@spec tag_by_title_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp tag_by_title_query(title) do
|
|
|
|
from(t in Tag, where: t.title == ^title, limit: 1)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec tag_filter(Ecto.Queryable.t(), String.t() | nil) :: Ecto.Query.t()
|
2021-09-10 11:27:59 +02:00
|
|
|
defp tag_filter(query, nil), do: query
|
|
|
|
defp tag_filter(query, ""), do: query
|
|
|
|
|
|
|
|
defp tag_filter(query, filter) when is_binary(filter) do
|
|
|
|
query
|
|
|
|
|> where([q], ilike(q.slug, ^"%#{filter}%"))
|
|
|
|
|> or_where([q], ilike(q.title, ^"%#{filter}%"))
|
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec tags_for_event_query(integer) :: Ecto.Query.t()
|
|
|
|
defp tags_for_event_query(event_id) do
|
|
|
|
from(
|
|
|
|
t in Tag,
|
|
|
|
join: e in "events_tags",
|
|
|
|
on: t.id == e.tag_id,
|
|
|
|
where: e.event_id == ^event_id
|
|
|
|
)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec tags_linked_query(integer, integer) :: Ecto.Query.t()
|
|
|
|
defp tags_linked_query(tag1_id, tag2_id) do
|
|
|
|
from(
|
|
|
|
tr in TagRelation,
|
|
|
|
where:
|
|
|
|
tr.tag_id == ^min(tag1_id, tag2_id) and
|
|
|
|
tr.link_id == ^max(tag1_id, tag2_id)
|
|
|
|
)
|
|
|
|
end
|
2019-03-08 18:52:27 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec tag_relation_subquery(integer) :: Ecto.Query.t()
|
|
|
|
defp tag_relation_subquery(tag_id) do
|
|
|
|
from(
|
|
|
|
tr in TagRelation,
|
|
|
|
select: %{id: tr.tag_id, weight: tr.weight},
|
|
|
|
where: tr.link_id == ^tag_id
|
|
|
|
)
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec tag_relation_union_subquery(Ecto.Queryable.t(), integer) :: Ecto.Query.t()
|
2019-09-16 02:07:44 +02:00
|
|
|
defp tag_relation_union_subquery(subquery, tag_id) do
|
|
|
|
from(
|
|
|
|
tr in TagRelation,
|
|
|
|
select: %{id: tr.link_id, weight: tr.weight},
|
|
|
|
union_all: ^subquery,
|
|
|
|
where: tr.tag_id == ^tag_id
|
|
|
|
)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec tag_neighbors_query(Ecto.Queryable.t(), integer, integer) :: Ecto.Query.t()
|
2019-09-16 02:07:44 +02:00
|
|
|
defp tag_neighbors_query(subquery, relation_minimum, limit) do
|
|
|
|
from(
|
|
|
|
t in Tag,
|
|
|
|
right_join: q in subquery(subquery),
|
|
|
|
on: [id: t.id],
|
|
|
|
where: q.weight >= ^relation_minimum,
|
|
|
|
limit: ^limit,
|
|
|
|
order_by: [desc: q.weight]
|
|
|
|
)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec participant_by_url_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp participant_by_url_query(url) do
|
|
|
|
from(
|
|
|
|
p in Participant,
|
|
|
|
where: p.url == ^url,
|
|
|
|
preload: [:actor, :event]
|
|
|
|
)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
defp organizers_participants_for_event(event_id) do
|
|
|
|
from(
|
|
|
|
p in Participant,
|
|
|
|
where: p.event_id == ^event_id and p.role == ^:creator,
|
|
|
|
preload: [:actor]
|
|
|
|
)
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec requests_for_actor_query(integer) :: Ecto.Query.t()
|
|
|
|
defp requests_for_actor_query(actor_id) do
|
|
|
|
from(p in Participant, where: p.actor_id == ^actor_id and p.role == ^:not_approved)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-21 23:59:07 +02:00
|
|
|
@spec count_participants_query(integer) :: Ecto.Query.t()
|
2019-10-25 17:43:37 +02:00
|
|
|
def count_participants_query(event_id) do
|
2019-09-21 23:59:07 +02:00
|
|
|
from(p in Participant, where: p.event_id == ^event_id)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec event_participations_for_actor_query(integer) :: Ecto.Query.t()
|
|
|
|
def event_participations_for_actor_query(actor_id) do
|
|
|
|
from(
|
2019-09-26 16:38:58 +02:00
|
|
|
p in Participant,
|
|
|
|
join: e in Event,
|
2019-09-16 02:07:44 +02:00
|
|
|
on: p.event_id == e.id,
|
2019-09-26 16:38:58 +02:00
|
|
|
where: p.actor_id == ^actor_id and p.role != ^:not_approved,
|
2020-06-11 19:13:21 +02:00
|
|
|
preload: [:event],
|
|
|
|
order_by: [desc: e.begins_on]
|
2019-09-16 02:07:44 +02:00
|
|
|
)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec sessions_for_event_query(integer) :: Ecto.Query.t()
|
|
|
|
defp sessions_for_event_query(event_id) do
|
|
|
|
from(
|
|
|
|
s in Session,
|
|
|
|
join: e in Event,
|
|
|
|
on: s.event_id == e.id,
|
|
|
|
where: e.id == ^event_id
|
|
|
|
)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec sessions_for_track_query(integer) :: Ecto.Query.t()
|
|
|
|
defp sessions_for_track_query(track_id) do
|
|
|
|
from(s in Session, where: s.track_id == ^track_id)
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2020-10-15 11:04:05 +02:00
|
|
|
@spec count_confirmed_participants_for_local_events_query :: Ecto.Query.t()
|
|
|
|
defp count_confirmed_participants_for_local_events_query do
|
|
|
|
Participant
|
|
|
|
|> join(:inner, [p], e in Event, on: p.event_id == e.id)
|
|
|
|
|> where([p, e], e.local and p.role not in [^:not_approved, ^:not_confirmed, ^:rejected])
|
|
|
|
|> select([p], count(p.id))
|
|
|
|
end
|
|
|
|
|
2019-09-20 18:22:03 +02:00
|
|
|
@spec list_participants_for_event_query(String.t()) :: Ecto.Query.t()
|
2019-09-26 16:38:58 +02:00
|
|
|
defp list_participants_for_event_query(event_id) do
|
2019-09-20 18:22:03 +02:00
|
|
|
from(
|
|
|
|
p in Participant,
|
2019-12-03 11:29:51 +01:00
|
|
|
where: p.event_id == ^event_id,
|
2020-09-30 10:42:19 +02:00
|
|
|
preload: [:actor, :event]
|
2019-09-20 18:22:03 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec list_participant_actors_for_event_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp list_participant_actors_for_event_query(event_id) do
|
|
|
|
from(
|
|
|
|
a in Actor,
|
|
|
|
join: p in Participant,
|
|
|
|
on: p.actor_id == a.id,
|
|
|
|
where: p.event_id == ^event_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-16 18:28:52 +01:00
|
|
|
@doc """
|
|
|
|
List emails for local users (including anonymous ones) participating to an event
|
|
|
|
|
|
|
|
Returns {participation, actor, user, user_settings}
|
|
|
|
"""
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec list_local_emails_user_participants_for_event_query(String.t()) :: Ecto.Query.t()
|
2019-09-30 18:18:04 +02:00
|
|
|
def list_local_emails_user_participants_for_event_query(event_id) do
|
|
|
|
Participant
|
|
|
|
|> join(:inner, [p], a in Actor, on: p.actor_id == a.id and is_nil(a.domain))
|
|
|
|
|> join(:left, [_p, a], u in User, on: a.user_id == u.id)
|
2020-11-16 18:28:52 +01:00
|
|
|
|> join(:left, [_p, _a, u], s in Setting, on: s.user_id == u.id)
|
2020-10-08 08:53:25 +02:00
|
|
|
|> where(
|
|
|
|
[p],
|
|
|
|
p.event_id == ^event_id and p.role not in [^:not_approved, ^:not_confirmed, ^:rejected]
|
|
|
|
)
|
2020-11-16 18:28:52 +01:00
|
|
|
|> select([p, a, u, s], {p, a, u, s})
|
2019-09-30 18:18:04 +02:00
|
|
|
end
|
|
|
|
|
2021-06-27 18:16:34 +02:00
|
|
|
@doc """
|
|
|
|
List anonymous participations to an event
|
|
|
|
"""
|
|
|
|
@spec list_anonymous_participants_for_event(String.t()) :: [Participant.t()]
|
|
|
|
def list_anonymous_participants_for_event(event_id) do
|
|
|
|
Participant
|
|
|
|
|> where(
|
|
|
|
[p],
|
|
|
|
p.event_id == ^event_id and p.role not in [^:not_approved, ^:not_confirmed, ^:rejected]
|
|
|
|
)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2019-09-20 18:22:03 +02:00
|
|
|
@spec list_participations_for_user_query(integer()) :: Ecto.Query.t()
|
|
|
|
defp list_participations_for_user_query(user_id) do
|
|
|
|
from(
|
|
|
|
p in Participant,
|
|
|
|
join: e in Event,
|
|
|
|
join: a in Actor,
|
|
|
|
on: p.actor_id == a.id,
|
|
|
|
on: p.event_id == e.id,
|
|
|
|
where: a.user_id == ^user_id and p.role != ^:not_approved,
|
|
|
|
preload: [:event, :actor]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec feed_token_query(String.t()) :: Ecto.Query.t()
|
|
|
|
defp feed_token_query(token) do
|
|
|
|
from(ftk in FeedToken, where: ftk.token == ^token, preload: [:actor, :user])
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2019-09-16 02:07:44 +02:00
|
|
|
@spec feed_token_for_user_query(integer) :: Ecto.Query.t()
|
|
|
|
defp feed_token_for_user_query(user_id) do
|
|
|
|
from(tk in FeedToken, where: tk.user_id == ^user_id, preload: [:actor, :user])
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec feed_token_for_actor_query(integer) :: Ecto.Query.t()
|
|
|
|
defp feed_token_for_actor_query(actor_id) do
|
|
|
|
from(tk in FeedToken, where: tk.actor_id == ^actor_id, preload: [:actor, :user])
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_public_visibility(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2019-09-16 02:07:44 +02:00
|
|
|
defp filter_public_visibility(query) do
|
2020-06-12 19:19:19 +02:00
|
|
|
from(e in query, where: e.visibility == ^:public)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_unlisted_and_public_visibility(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2020-06-12 19:19:19 +02:00
|
|
|
defp filter_unlisted_and_public_visibility(query) do
|
|
|
|
from(q in query, where: q.visibility in ^@public_visibility)
|
2019-09-16 02:07:44 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_not_event_uuid(Ecto.Queryable.t(), String.t() | nil) :: Ecto.Query.t()
|
2019-09-16 02:07:44 +02:00
|
|
|
defp filter_not_event_uuid(query, nil), do: query
|
|
|
|
|
|
|
|
defp filter_not_event_uuid(query, not_event_uuid) do
|
|
|
|
from(e in query, where: e.uuid != ^not_event_uuid)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_draft(Ecto.Queryable.t(), boolean) :: Ecto.Query.t()
|
2019-10-02 17:59:07 +02:00
|
|
|
defp filter_draft(query, is_draft \\ false) do
|
|
|
|
from(e in query, where: e.draft == ^is_draft)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_cancelled_events(Ecto.Queryable.t()) :: Ecto.Query.t()
|
|
|
|
defp filter_cancelled_events(query) do
|
2020-12-18 16:43:31 +01:00
|
|
|
from(e in query, where: e.status != ^:cancelled)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_future_events(Ecto.Queryable.t(), boolean) :: Ecto.Query.t()
|
2019-09-16 02:07:44 +02:00
|
|
|
defp filter_future_events(query, true) do
|
2019-10-09 16:37:39 +02:00
|
|
|
from(q in query,
|
2020-06-10 09:44:01 +02:00
|
|
|
where: q.begins_on > ^DateTime.utc_now()
|
2019-10-09 16:37:39 +02:00
|
|
|
)
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
2019-09-16 02:07:44 +02:00
|
|
|
|
|
|
|
defp filter_future_events(query, false), do: query
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_local(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2020-08-12 16:05:34 +02:00
|
|
|
defp filter_local(query) do
|
|
|
|
where(query, [q], q.local == true)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_local_or_from_followed_instances_events(Ecto.Queryable.t()) ::
|
2020-10-14 09:45:58 +02:00
|
|
|
Ecto.Query.t()
|
2020-02-07 16:28:49 +01:00
|
|
|
defp filter_local_or_from_followed_instances_events(query) do
|
2020-10-14 09:45:58 +02:00
|
|
|
follower_actor_id = Mobilizon.Config.relay_actor_id()
|
|
|
|
|
|
|
|
query
|
|
|
|
|> join(:left, [q], s in Share, on: s.uri == q.url)
|
|
|
|
|> join(:left, [_q, ..., s], f in Follower, on: f.target_actor_id == s.actor_id)
|
|
|
|
|> where(
|
|
|
|
[q, ..., s, f],
|
|
|
|
q.local == true or (f.actor_id == ^follower_actor_id and not is_nil(s.uri))
|
2020-02-07 16:28:49 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_approved_role(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2019-09-21 23:59:07 +02:00
|
|
|
defp filter_approved_role(query) do
|
2019-10-25 17:43:37 +02:00
|
|
|
filter_role(query, [:not_approved, :rejected])
|
2019-09-16 02:07:44 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_participant_role(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2019-10-11 11:50:06 +02:00
|
|
|
defp filter_participant_role(query) do
|
2019-10-25 17:43:37 +02:00
|
|
|
filter_role(query, :participant)
|
2019-09-21 23:59:07 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_rejected_role(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2019-09-30 13:48:47 +02:00
|
|
|
defp filter_rejected_role(query) do
|
2019-10-25 17:43:37 +02:00
|
|
|
filter_role(query, :rejected)
|
2019-09-30 13:48:47 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_role(Ecto.Queryable.t(), list(atom()) | atom()) :: Ecto.Query.t()
|
2019-10-25 17:43:37 +02:00
|
|
|
def filter_role(query, []), do: query
|
2019-09-20 18:22:03 +02:00
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
def filter_role(query, roles) when is_list(roles) do
|
2019-09-20 18:22:03 +02:00
|
|
|
where(query, [p], p.role in ^roles)
|
|
|
|
end
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
def filter_role(query, role) when is_atom(role) do
|
|
|
|
from(p in query, where: p.role == ^role)
|
|
|
|
end
|
|
|
|
|
2020-09-02 17:42:17 +02:00
|
|
|
defp event_filter_visibility(query, :all), do: query
|
|
|
|
|
|
|
|
defp event_filter_visibility(query, :public) do
|
|
|
|
query
|
2020-10-22 16:58:18 +02:00
|
|
|
|> where(visibility: ^:public, draft: false)
|
2020-09-02 17:42:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
defp event_filter_begins_on(query, nil, nil),
|
|
|
|
do: event_order_begins_on_desc(query)
|
|
|
|
|
|
|
|
defp event_filter_begins_on(query, %DateTime{} = after_datetime, nil) do
|
|
|
|
query
|
|
|
|
|> where([e], e.begins_on > ^after_datetime)
|
|
|
|
|> event_order_begins_on_asc()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp event_filter_begins_on(query, nil, %DateTime{} = before_datetime) do
|
|
|
|
query
|
|
|
|
|> where([e], e.begins_on < ^before_datetime)
|
|
|
|
|> event_order_begins_on_desc()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp event_filter_begins_on(
|
|
|
|
query,
|
|
|
|
%DateTime{} = after_datetime,
|
|
|
|
%DateTime{} = before_datetime
|
|
|
|
) do
|
|
|
|
query
|
|
|
|
|> where([e], e.begins_on < ^before_datetime)
|
|
|
|
|> where([e], e.begins_on > ^after_datetime)
|
|
|
|
|> event_order_begins_on_asc()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp event_order_begins_on_asc(query),
|
|
|
|
do: order_by(query, [e], asc: e.begins_on)
|
|
|
|
|
|
|
|
defp event_order_begins_on_desc(query),
|
|
|
|
do: order_by(query, [e], desc: e.begins_on)
|
|
|
|
|
2019-09-20 18:22:03 +02:00
|
|
|
defp participation_filter_begins_on(query, nil, nil),
|
|
|
|
do: participation_order_begins_on_desc(query)
|
|
|
|
|
|
|
|
defp participation_filter_begins_on(query, %DateTime{} = after_datetime, nil) do
|
|
|
|
query
|
|
|
|
|> where([_p, e, _a], e.begins_on > ^after_datetime)
|
|
|
|
|> participation_order_begins_on_asc()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp participation_filter_begins_on(query, nil, %DateTime{} = before_datetime) do
|
|
|
|
query
|
|
|
|
|> where([_p, e, _a], e.begins_on < ^before_datetime)
|
|
|
|
|> participation_order_begins_on_desc()
|
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
defp participation_filter_begins_on(
|
|
|
|
query,
|
|
|
|
%DateTime{} = after_datetime,
|
|
|
|
%DateTime{} = before_datetime
|
|
|
|
) do
|
|
|
|
query
|
|
|
|
|> where([_p, e, _a], e.begins_on < ^before_datetime)
|
|
|
|
|> where([_p, e, _a], e.begins_on > ^after_datetime)
|
|
|
|
|> participation_order_begins_on_asc()
|
|
|
|
end
|
|
|
|
|
2019-09-20 18:22:03 +02:00
|
|
|
defp participation_order_begins_on_asc(query),
|
|
|
|
do: order_by(query, [_p, e, _a], asc: e.begins_on)
|
|
|
|
|
|
|
|
defp participation_order_begins_on_desc(query),
|
|
|
|
do: order_by(query, [_p, e, _a], desc: e.begins_on)
|
2019-09-16 02:07:44 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec preload_for_event(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2019-09-16 02:07:44 +02:00
|
|
|
defp preload_for_event(query), do: preload(query, ^@event_preloads)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|