Work on dashboard

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-09-18 17:32:37 +02:00
parent 48fd14bf9c
commit ffa4ec9209
33 changed files with 931 additions and 204 deletions

View File

@@ -585,6 +585,61 @@ defmodule Mobilizon.Events do
|> Repo.update()
end
@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{}, ...]
"""
def list_participations_for_user(
user_id,
after_datetime \\ nil,
before_datetime \\ nil,
page \\ nil,
limit \\ nil
)
def list_participations_for_user(user_id, %DateTime{} = after_datetime, nil, page, limit) do
user_id
|> do_list_participations_for_user(page, limit)
|> where([_p, e, _a], e.begins_on > ^after_datetime)
|> order_by([_p, e, _a], asc: e.begins_on)
|> Repo.all()
end
def list_participations_for_user(user_id, nil, %DateTime{} = before_datetime, page, limit) do
user_id
|> do_list_participations_for_user(page, limit)
|> where([_p, e, _a], e.begins_on < ^before_datetime)
|> order_by([_p, e, _a], desc: e.begins_on)
|> Repo.all()
end
def list_participations_for_user(user_id, nil, nil, page, limit) do
user_id
|> do_list_participations_for_user(page, limit)
|> order_by([_p, e, _a], desc: e.begins_on)
|> Repo.all()
end
defp do_list_participations_for_user(user_id, page, limit) 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]
)
|> Page.paginate(page, limit)
end
@doc """
Deletes a participant.
"""
@@ -621,6 +676,11 @@ defmodule Mobilizon.Events do
@doc """
Returns the list of organizers participants for an event.
## Examples
iex> list_organizers_participants_for_event(id)
[%Participant{role: :creator}, ...]
"""
@spec list_organizers_participants_for_event(
integer | String.t(),

View File

@@ -29,12 +29,12 @@ defmodule MobilizonWeb.Resolvers.Event do
end
def find_event(_parent, %{uuid: uuid}, _resolution) do
case Mobilizon.Events.get_public_event_by_uuid_with_preload(uuid) do
nil ->
{:error, "Event with UUID #{uuid} not found"}
event ->
case {:has_event, Mobilizon.Events.get_public_event_by_uuid_with_preload(uuid)} do
{:has_event, %Event{} = event} ->
{:ok, Map.put(event, :organizer_actor, Person.proxify_pictures(event.organizer_actor))}
{:has_event, _} ->
{:error, "Event with UUID #{uuid} not found"}
end
end

View File

@@ -3,7 +3,7 @@ defmodule MobilizonWeb.Resolvers.User do
Handles the user-related GraphQL calls
"""
alias Mobilizon.{Actors, Config, Users}
alias Mobilizon.{Actors, Config, Users, Events}
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.Users.{ResetPassword, Activation}
alias Mobilizon.Users.User
@@ -220,4 +220,22 @@ defmodule MobilizonWeb.Resolvers.User do
{:error, :unable_to_change_default_actor}
end
end
@doc """
Returns the list of events for all of this user's identities are going to
"""
def user_participations(_parent, args, %{
context: %{current_user: %User{id: user_id}}
}) do
with participations <-
Events.list_participations_for_user(
user_id,
Map.get(args, :after_datetime),
Map.get(args, :before_datetime),
Map.get(args, :page),
Map.get(args, :limit)
) do
{:ok, participations}
end
end
end

View File

@@ -45,6 +45,16 @@ defmodule MobilizonWeb.Schema.UserType do
)
field(:role, :user_role, description: "The role for the user")
field(:participations, list_of(:participant),
description: "The list of events this person goes to"
) do
arg(:after_datetime, :datetime)
arg(:before_datetime, :datetime)
arg(:page, :integer, default_value: 1)
arg(:limit, :integer, default_value: 10)
resolve(&User.user_participations/3)
end
end
enum :user_role do

View File

@@ -5,13 +5,27 @@ defmodule MobilizonWeb.ErrorView do
use MobilizonWeb, :view
def render("404.html", _assigns) do
"Page not found"
with {:ok, index_content} <- File.read(index_file_path()) do
{:safe, index_content}
end
end
def render("404.json", _assigns) do
%{msg: "Resource not found"}
end
def render("404.activity-json", _assigns) do
%{msg: "Resource not found"}
end
def render("404.ics", _assigns) do
"Bad feed"
end
def render("404.atom", _assigns) do
"Bad feed"
end
def render("invalid_request.json", _assigns) do
%{errors: "Invalid request"}
end
@@ -31,8 +45,11 @@ defmodule MobilizonWeb.ErrorView do
# template is found, let's render it as 500
def template_not_found(template, assigns) do
require Logger
Logger.warn("Template not found")
Logger.debug(inspect(template))
Logger.warn("Template #{inspect(template)} not found")
render("500.html", assigns)
end
defp index_file_path() do
Path.join(Application.app_dir(:mobilizon, "priv/static"), "index.html")
end
end