[WIP] Test transmogrifier

Introduce MobilizonWeb.API namespace

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Format

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

WIP

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

remove unneeded code

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Fix tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Fix warnings

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2018-12-14 17:41:55 +01:00
parent e3a8343112
commit c1e6612405
41 changed files with 2961 additions and 246 deletions

View File

@@ -2,6 +2,9 @@ defmodule MobilizonWeb.Resolvers.Category do
require Logger
alias Mobilizon.Actors.User
###
# TODO : Refactor this into MobilizonWeb.API.Categories when a standard AS category is defined
###
def list_categories(_parent, %{page: page, limit: limit}, _resolution) do
categories =
Mobilizon.Events.list_categories(page, limit)

View File

@@ -0,0 +1,25 @@
defmodule MobilizonWeb.Resolvers.Comment do
require Logger
alias Mobilizon.Events.Comment
alias Mobilizon.Activity
alias Mobilizon.Actors.User
alias MobilizonWeb.API.Comments
def create_comment(_parent, %{text: comment, actor_username: username}, %{
context: %{current_user: %User{} = _user}
}) do
with {:ok, %Activity{data: %{"object" => %{"type" => "Note"} = object}}} <-
Comments.create_comment(username, comment) do
{:ok,
%Comment{
text: object["content"],
url: object["id"],
uuid: object["uuid"]
}}
end
end
def create_comment(_parent, _args, %{}) do
{:error, "You are not allowed to create a comment if not connected"}
end
end

View File

@@ -1,6 +1,8 @@
defmodule MobilizonWeb.Resolvers.Event do
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Activity
alias Mobilizon.Actors
alias Mobilizon.Events.Event
def list_events(_parent, %{page: page, limit: limit}, _resolution) do
{:ok, Mobilizon.Events.list_events(page, limit)}
@@ -63,10 +65,27 @@ defmodule MobilizonWeb.Resolvers.Event do
{:ok, found}
end
@doc """
Create an event
"""
def create_event(_parent, args, %{context: %{current_user: user}}) do
organizer_actor_id = Map.get(args, :organizer_actor_id) || Actors.get_actor_for_user(user).id
args = args |> Map.put(:organizer_actor_id, organizer_actor_id)
Mobilizon.Events.create_event(args)
with {:ok, %Activity{data: %{"object" => %{"type" => "Event"} = object}}} <-
args
# Set default organizer_actor_id if none set
|> Map.update(
:organizer_actor_username,
Actors.get_actor_for_user(user).preferred_username,
& &1
)
|> MobilizonWeb.API.Events.create_event() do
{:ok,
%Event{
title: object["name"],
description: object["content"],
uuid: object["uuid"],
url: object["id"]
}}
end
end
def create_event(_parent, _args, _resolution) do

View File

@@ -2,6 +2,7 @@ defmodule MobilizonWeb.Resolvers.Group do
alias Mobilizon.Actors
alias Mobilizon.Actors.{Actor}
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Activity
require Logger
@doc """
@@ -29,24 +30,36 @@ defmodule MobilizonWeb.Resolvers.Group do
"""
def create_group(
_parent,
%{preferred_username: preferred_username, creator_username: actor_username},
args,
%{
context: %{current_user: user}
context: %{current_user: _user}
}
) do
with %Actor{id: actor_id} <- Actors.get_local_actor_by_name(actor_username),
{:user_actor, true} <-
{:user_actor, actor_id in Enum.map(Actors.get_actors_for_user(user), & &1.id)},
{:ok, %Actor{} = group} <- Actors.create_group(%{preferred_username: preferred_username}) do
{:ok, group}
else
{:error, %Ecto.Changeset{errors: [url: {"has already been taken", []}]}} ->
{:error, :group_name_not_available}
err ->
Logger.error(inspect(err))
err
with {:ok, %Activity{data: %{"object" => %{"type" => "Group"} = object}}} <-
MobilizonWeb.API.Groups.create_group(args) do
{:ok,
%Actor{
preferred_username: object["preferredUsername"],
summary: object["summary"],
type: :Group,
# uuid: object["uuid"],
url: object["id"]
}}
end
# with %Actor{id: actor_id} <- Actors.get_local_actor_by_name(actor_username),
# {:user_actor, true} <-
# {:user_actor, actor_id in Enum.map(Actors.get_actors_for_user(user), & &1.id)},
# {:ok, %Actor{} = group} <- Actors.create_group(%{preferred_username: preferred_username}) do
# {:ok, group}
# else
# {:error, %Ecto.Changeset{errors: [url: {"has already been taken", []}]}} ->
# {:error, :group_name_not_available}
# err ->
# Logger.error(inspect(err))
# err
# end
end
def create_group(_parent, _args, _resolution) do