Show related events

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-04-12 17:00:55 +02:00
parent a877e4d7d9
commit f5b02ed154
8 changed files with 89 additions and 42 deletions

View File

@@ -57,6 +57,7 @@ defmodule Mobilizon.Events do
e.organizer_actor_id == ^actor_id and e.visibility in [^:public, ^:unlisted] and
e.begins_on > ^DateTime.utc_now(),
order_by: [asc: :begins_on],
limit: 1,
preload: [
:organizer_actor,
:tags,
@@ -259,18 +260,42 @@ defmodule Mobilizon.Events do
[%Event{}, ...]
"""
def list_events(page \\ nil, limit \\ nil) do
@spec list_events(integer(), integer(), atom(), atom()) :: list(Event.t())
def list_events(
page \\ nil,
limit \\ nil,
sort \\ :begins_on,
direction \\ :asc,
unlisted \\ false,
future \\ true
) do
query =
from(
e in Event,
where: e.visibility == ^:public,
preload: [:organizer_actor, :participants]
)
|> paginate(page, limit)
|> sort(sort, direction)
|> restrict_future_events(future)
|> allow_unlisted(unlisted)
Repo.all(query)
end
# Make sure we only show future events
@spec restrict_future_events(Ecto.Query.t(), boolean()) :: Ecto.Query.t()
defp restrict_future_events(query, true),
do: from(q in query, where: q.begins_on > ^DateTime.utc_now())
defp restrict_future_events(query, false), do: query
# Make sure unlisted events don't show up where they're not allowed
@spec allow_unlisted(Ecto.Query.t(), boolean()) :: Ecto.Query.t()
defp allow_unlisted(query, true),
do: from(q in query, where: q.visibility in [^:public, ^:unlisted])
defp allow_unlisted(query, false), do: from(q in query, where: q.visibility == ^:public)
@doc """
Find events by name
"""