Move to GraphQL

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2018-11-06 10:30:27 +01:00
parent 7e137d1a1c
commit b54dae7e15
149 changed files with 5605 additions and 4665 deletions

View File

@@ -0,0 +1,26 @@
defmodule MobilizonWeb.Resolvers.Actor do
alias Mobilizon.Actors.Actor, as: ActorSchema
alias Mobilizon.Actors.User
alias Mobilizon.Actors
def find_actor(_parent, %{preferred_username: name}, _resolution) do
case Actors.get_actor_by_name_with_everything(name) do
nil ->
{:error, "Actor with name #{name} not found"}
actor ->
{:ok, actor}
end
end
@doc """
Returns the current actor for the currently logged-in user
"""
def get_current_actor(_parent, _args, %{context: %{current_user: user}}) do
{:ok, Actors.get_actor_for_user(user)}
end
def get_current_actor(_parent, _args, _resolution) do
{:error, "You need to be logged-in to view current actor"}
end
end

View File

@@ -0,0 +1,37 @@
defmodule MobilizonWeb.Resolvers.Category do
require Logger
def list_categories(_parent, _args, _resolution) do
categories =
Mobilizon.Events.list_categories()
|> Enum.map(fn category ->
urls = MobilizonWeb.Uploaders.Category.urls({category.picture, category})
Map.put(category, :picture, %{url: urls.original, url_thumbnail: urls.thumb})
end)
{:ok, categories}
end
def create_category(_parent, %{title: title, picture: picture, description: description}, %{
context: %{current_user: user}
}) do
with {:ok, category} <-
Mobilizon.Events.create_category(%{
title: title,
description: description,
picture: picture
}),
urls <- MobilizonWeb.Uploaders.Category.urls({category.picture, category}) do
Logger.info("Created category " <> title)
{:ok, Map.put(category, :picture, %{url: urls.original, url_thumbnail: urls.thumb})}
else
{:error, %Ecto.Changeset{errors: errors} = _changeset} ->
# This is pretty ridiculous for changeset to error
errors =
Enum.into(errors, %{})
|> Enum.map(fn {key, {value, _}} -> Atom.to_string(key) <> ": " <> value end)
{:error, errors}
end
end
end

View File

@@ -0,0 +1,54 @@
defmodule MobilizonWeb.Resolvers.Event do
def list_events(_parent, _args, _resolution) do
{:ok, Mobilizon.Events.list_events()}
end
def find_event(_parent, %{uuid: uuid}, _resolution) do
case Mobilizon.Events.get_event_full_by_uuid(uuid) do
nil ->
{:error, "Event with UUID #{uuid} not found"}
event ->
{:ok, event}
end
end
def list_participants_for_event(_parent, %{uuid: uuid}, _resolution) do
{:ok, Mobilizon.Events.list_participants_for_event(uuid)}
end
@doc """
Search events by title
"""
def search_events(_parent, %{search: search, page: page, limit: limit}, _resolution) do
{:ok, Mobilizon.Events.find_events_by_name(search, page, limit)}
end
@doc """
Search events and actors by title
"""
def search_events_and_actors(_parent, %{search: search, page: page, limit: limit}, _resolution) do
found =
Mobilizon.Events.find_events_by_name(search, page, limit) ++
Mobilizon.Actors.find_actors_by_username_or_name(search, page, limit)
require Logger
Logger.debug(inspect(found))
{:ok, found}
end
@doc """
List participants for event (through an event request)
"""
def list_participants_for_event(%{uuid: uuid}, _args, _resolution) do
{:ok, Mobilizon.Events.list_participants_for_event(uuid)}
end
def create_event(_parent, args, %{context: %{current_user: user}}) do
Mobilizon.Events.create_event(args)
end
def create_event(_parent, _args, _resolution) do
{:error, "You need to be logged-in to create events"}
end
end

View File

@@ -0,0 +1,2 @@
defmodule MobilizonWeb.Resolvers.Upload do
end

View File

@@ -0,0 +1,118 @@
defmodule MobilizonWeb.Resolvers.User do
alias Mobilizon.Actors.{User, Actor}
alias Mobilizon.Actors
@doc """
Find an user by it's ID
"""
def find_user(_parent, %{id: id}, _resolution) do
Actors.get_user_with_actor(id)
end
@doc """
Return current logged-in user
"""
def get_current_user(_parent, _args, %{context: %{current_user: user}}) do
{:ok, user}
end
def get_current_user(_parent, _args, _resolution) do
{:error, "You need to be logged-in to view current user"}
end
@desc """
Login an user. Returns a token and the user
"""
def login_user(_parent, %{email: email, password: password}, _resolution) do
with {:ok, %User{} = user} <- Actors.get_user_by_email(email, true),
{:ok, token, _} <- Actors.authenticate(%{user: user, password: password}),
%Actor{} = actor <- Actors.get_actor_for_user(user) do
{:ok, %{token: token, user: user, actor: actor}}
else
{:error, :user_not_found} ->
{:error, "User with email not found"}
{:error, :unauthorized} ->
{:error, "Impossible to authenticate"}
end
end
@desc """
Register an user :
- create the user
- create the actor
- set the user's default_actor to the newly created actor
- send a validation email to the user
"""
@spec create_user_actor(any(), map(), any()) :: tuple()
def create_user_actor(_parent, args, _resolution) do
with {:ok, %Actor{user: user} = actor} <- Actors.register(args) do
Mobilizon.Actors.Service.Activation.send_confirmation_email(user)
{:ok, actor}
end
end
@doc """
Validate an user, get it's actor and a token
"""
def validate_user(_parent, %{token: token}, _resolution) do
with {:ok, %User{} = user} <-
Mobilizon.Actors.Service.Activation.check_confirmation_token(token),
%Actor{} = actor <- Actors.get_actor_for_user(user),
{:ok, token, _} <- MobilizonWeb.Guardian.encode_and_sign(user) do
{:ok, %{token: token, user: user, actor: actor}}
end
end
@doc """
Send the confirmation email again.
We only do this to accounts unconfirmed
"""
def resend_confirmation_email(_parent, %{email: email, locale: locale}, _resolution) do
with {:ok, user} <- Actors.get_user_by_email(email, false),
{:ok, email} <- Mobilizon.Actors.Service.Activation.resend_confirmation_email(user, locale) do
{:ok, email}
else
{:error, :user_not_found} ->
{:error, "No user to validate with this email was found"}
{:error, :email_too_soon} ->
{:error, "You requested again a confirmation email too soon"}
end
end
@doc """
Send an email to reset the password from an user
"""
def send_reset_password(_parent, %{email: email, locale: locale}, _resolution) do
with {:ok, user} <- Actors.get_user_by_email(email, false),
{:ok, email} <- Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user, locale) do
{:ok, email}
else
{:error, :user_not_found} ->
{:error, "No user to validate with this email was found"}
{:error, :email_too_soon} ->
{:error, "You requested again a confirmation email too soon"}
end
end
@doc """
Reset the password from an user
"""
def reset_password(_parent, %{password: password, token: token}, _resolution) do
with {:ok, %User{} = user} <-
Mobilizon.Actors.Service.ResetPassword.check_reset_password_token(password, token),
%Actor{} = actor <- Actors.get_actor_for_user(user),
{:ok, token, _} <- MobilizonWeb.Guardian.encode_and_sign(user) do
{:ok, %{token: token, user: user, actor: actor}}
end
end
@desc "Change an user default actor"
def change_default_actor(_parent, %{preferred_username: username}, %{
context: %{current_user: user}
}) do
with %Actor{id: id} <- Actors.get_local_actor_by_name(username) do
Actors.update_user(user, %{default_actor_id: id})
end
end
end