2020-02-18 08:57:00 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Todos do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the todos related GraphQL calls
|
|
|
|
"""
|
|
|
|
|
2021-09-10 11:35:32 +02:00
|
|
|
alias Mobilizon.{Actors, Todos}
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actions
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Todos.{Todo, TodoList}
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Find todo lists for group.
|
|
|
|
|
|
|
|
Returns only if actor requesting is a member of the group
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec find_todo_lists_for_group(Actor.t(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(TodoList.t())}
|
2020-02-18 08:57:00 +01:00
|
|
|
def find_todo_lists_for_group(
|
|
|
|
%Actor{id: group_id} = group,
|
2021-10-05 15:29:06 +02:00
|
|
|
%{page: page, limit: limit},
|
2020-02-18 08:57:00 +01:00
|
|
|
%{
|
2021-09-10 11:35:32 +02:00
|
|
|
context: %{current_actor: %Actor{id: actor_id}}
|
2020-02-18 08:57:00 +01:00
|
|
|
} = _resolution
|
|
|
|
) do
|
2024-01-04 13:35:02 +01:00
|
|
|
with {:member, true} <- {:member, Actors.member?(actor_id, group_id)},
|
2021-10-05 15:29:06 +02:00
|
|
|
%Page{} = page <- Todos.get_todo_lists_for_group(group, page, limit) do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, page}
|
|
|
|
else
|
|
|
|
{:member, _} ->
|
2021-10-05 15:29:06 +02:00
|
|
|
with %Page{} = page <- Todos.get_todo_lists_for_group(group, page, limit) do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, %Page{page | elements: []}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_todo_lists_for_group(_parent, _args, _resolution) do
|
|
|
|
{:ok, %Page{total: 0, elements: []}}
|
|
|
|
end
|
|
|
|
|
2021-10-05 15:29:06 +02:00
|
|
|
@spec find_todos_for_todo_list(TodoList.t(), map(), Absinthe.Resolution.t()) ::
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, Page.t(Todo.t())} | {:error, String.t()}
|
2020-02-18 08:57:00 +01:00
|
|
|
def find_todos_for_todo_list(
|
|
|
|
%TodoList{actor_id: group_id} = todo_list,
|
2021-10-05 15:29:06 +02:00
|
|
|
%{page: page, limit: limit},
|
2020-02-18 08:57:00 +01:00
|
|
|
%{
|
2021-09-10 11:35:32 +02:00
|
|
|
context: %{current_actor: %Actor{id: actor_id}}
|
2020-02-18 08:57:00 +01:00
|
|
|
} = _resolution
|
|
|
|
) do
|
2024-01-04 13:35:02 +01:00
|
|
|
with {:member, true} <- {:member, Actors.member?(actor_id, group_id)},
|
2021-10-05 15:29:06 +02:00
|
|
|
%Page{} = page <- Todos.get_todos_for_todo_list(todo_list, page, limit) do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, page}
|
|
|
|
else
|
|
|
|
{:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_todo_list(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, TodoList.t()} | {:error, String.t()}
|
2020-02-18 08:57:00 +01:00
|
|
|
def get_todo_list(
|
|
|
|
_parent,
|
|
|
|
%{id: todo_list_id},
|
|
|
|
%{
|
2021-09-10 11:35:32 +02:00
|
|
|
context: %{current_actor: %Actor{id: actor_id}}
|
2020-02-18 08:57:00 +01:00
|
|
|
} = _resolution
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:todo, %TodoList{actor_id: group_id} = todo} <-
|
2020-02-18 08:57:00 +01:00
|
|
|
{:todo, Todos.get_todo_list(todo_list_id)},
|
2024-01-04 13:35:02 +01:00
|
|
|
{:member, true} <- {:member, Actors.member?(actor_id, group_id)} do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, todo}
|
|
|
|
else
|
|
|
|
{:todo, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Todo list doesn't exist")}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
{:actor, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "No profile found for user")}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec create_todo_list(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, TodoList.t()} | {:error, String.t()}
|
2020-02-18 08:57:00 +01:00
|
|
|
def create_todo_list(
|
|
|
|
_parent,
|
|
|
|
%{group_id: group_id} = args,
|
|
|
|
%{
|
2021-09-10 11:35:32 +02:00
|
|
|
context: %{current_actor: %Actor{id: actor_id}}
|
2020-02-18 08:57:00 +01:00
|
|
|
} = _resolution
|
|
|
|
) do
|
2024-01-04 13:35:02 +01:00
|
|
|
with {:member, true} <- {:member, Actors.member?(actor_id, group_id)},
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, _, %TodoList{} = todo_list} <-
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Create.create(
|
|
|
|
:todo_list,
|
|
|
|
Map.put(args, :actor_id, group_id),
|
|
|
|
true,
|
|
|
|
%{}
|
|
|
|
) do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, todo_list}
|
|
|
|
else
|
2021-09-10 11:27:59 +02:00
|
|
|
{:actor, nil} ->
|
|
|
|
{:error, dgettext("errors", "No profile found for user")}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
{:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# def update_todo_list(
|
|
|
|
# _parent,
|
|
|
|
# %{id: todo_list_id, actor_id: actor_id},
|
|
|
|
# %{
|
|
|
|
# context: %{current_user: %User{} = user}
|
|
|
|
# } = _resolution
|
|
|
|
# ) do
|
|
|
|
# with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id),
|
|
|
|
# {:todo_list, %TodoList{actor_id: group_id} = todo_list} <-
|
|
|
|
# {:todo_list, Todos.get_todo_list(todo_list_id)},
|
2024-01-04 13:35:02 +01:00
|
|
|
# {:member, true} <- {:member, Actors.member?(actor_id, group_id)},
|
2020-02-18 08:57:00 +01:00
|
|
|
# {:ok, _, %TodoList{} = todo} <-
|
2021-09-28 19:40:37 +02:00
|
|
|
# Actions.Update.update_todo_list(todo_list, actor, true, %{}) do
|
2020-02-18 08:57:00 +01:00
|
|
|
# {:ok, todo}
|
|
|
|
# else
|
|
|
|
# {:todo_list, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
# {:error, "Todo list doesn't exist"}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
# {:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
# {:error, "Profile is not member of group"}
|
2020-02-18 08:57:00 +01:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def delete_todo_list(
|
|
|
|
# _parent,
|
|
|
|
# %{id: todo_list_id, actor_id: actor_id},
|
|
|
|
# %{
|
|
|
|
# context: %{current_user: %User{} = user}
|
|
|
|
# } = _resolution
|
|
|
|
# ) do
|
|
|
|
# with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id),
|
|
|
|
# {:todo_list, %TodoList{actor_id: group_id} = todo_list} <-
|
|
|
|
# {:todo_list, Todos.get_todo_list(todo_list_id)},
|
2024-01-04 13:35:02 +01:00
|
|
|
# {:member, true} <- {:member, Actors.member?(actor_id, group_id)},
|
2020-02-18 08:57:00 +01:00
|
|
|
# {:ok, _, %TodoList{} = todo} <-
|
2021-09-28 19:40:37 +02:00
|
|
|
# Actions.Delete.delete_todo_list(todo_list, actor, true, %{}) do
|
2020-02-18 08:57:00 +01:00
|
|
|
# {:ok, todo}
|
|
|
|
# else
|
|
|
|
# {:todo_list, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
# {:error, "Todo list doesn't exist"}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
# {:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
# {:error, "Profile is not member of group"}
|
2020-02-18 08:57:00 +01:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_todo(any(), map(), Absinthe.Resolution.t()) :: {:ok, Todo.t()} | {:error, String.t()}
|
2020-02-18 08:57:00 +01:00
|
|
|
def get_todo(
|
|
|
|
_parent,
|
|
|
|
%{id: todo_id},
|
|
|
|
%{
|
2021-09-10 11:35:32 +02:00
|
|
|
context: %{current_actor: %Actor{id: actor_id}}
|
2020-02-18 08:57:00 +01:00
|
|
|
} = _resolution
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:todo, %Todo{todo_list_id: todo_list_id} = todo} <-
|
2020-02-18 08:57:00 +01:00
|
|
|
{:todo, Todos.get_todo(todo_id)},
|
|
|
|
{:todo_list, %TodoList{actor_id: group_id}} <-
|
|
|
|
{:todo_list, Todos.get_todo_list(todo_list_id)},
|
2024-01-04 13:35:02 +01:00
|
|
|
{:member, true} <- {:member, Actors.member?(actor_id, group_id)} do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, todo}
|
|
|
|
else
|
|
|
|
{:todo, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Todo doesn't exist")}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
{:actor, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "No profile found for user")}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec create_todo(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Todo.t()} | {:error, String.t()}
|
2020-02-18 08:57:00 +01:00
|
|
|
def create_todo(
|
|
|
|
_parent,
|
|
|
|
%{todo_list_id: todo_list_id} = args,
|
|
|
|
%{
|
2021-09-10 11:35:32 +02:00
|
|
|
context: %{current_actor: %Actor{id: actor_id}}
|
2020-02-18 08:57:00 +01:00
|
|
|
} = _resolution
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:todo_list, %TodoList{actor_id: group_id} = _todo_list} <-
|
2020-02-18 08:57:00 +01:00
|
|
|
{:todo_list, Todos.get_todo_list(todo_list_id)},
|
2024-01-04 13:35:02 +01:00
|
|
|
{:member, true} <- {:member, Actors.member?(actor_id, group_id)},
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, _, %Todo{} = todo} <-
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Create.create(
|
|
|
|
:todo,
|
|
|
|
Map.put(args, :creator_id, actor_id),
|
|
|
|
true,
|
|
|
|
%{}
|
|
|
|
) do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, todo}
|
|
|
|
else
|
2021-09-10 11:27:59 +02:00
|
|
|
{:actor, nil} ->
|
|
|
|
{:error, dgettext("errors", "No profile found for user")}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
{:todo_list, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Todo list doesn't exist")}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec update_todo(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Todo.t()} | {:error, String.t()}
|
2020-02-18 08:57:00 +01:00
|
|
|
def update_todo(
|
|
|
|
_parent,
|
|
|
|
%{id: todo_id} = args,
|
|
|
|
%{
|
2021-09-10 11:35:32 +02:00
|
|
|
context: %{current_actor: %Actor{id: actor_id}}
|
2020-02-18 08:57:00 +01:00
|
|
|
} = _resolution
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:todo, %Todo{todo_list_id: todo_list_id} = todo} <-
|
2020-02-18 08:57:00 +01:00
|
|
|
{:todo, Todos.get_todo(todo_id)},
|
|
|
|
{:todo_list, %TodoList{actor_id: group_id}} <-
|
|
|
|
{:todo_list, Todos.get_todo_list(todo_list_id)},
|
2024-01-04 13:35:02 +01:00
|
|
|
{:member, true} <- {:member, Actors.member?(actor_id, group_id)},
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, _, %Todo{} = todo} <-
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Update.update(todo, args, true, %{}) do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, todo}
|
|
|
|
else
|
2021-09-10 11:27:59 +02:00
|
|
|
{:actor, nil} ->
|
|
|
|
{:error, dgettext("errors", "No profile found for user")}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
{:todo_list, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Todo list doesn't exist")}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
{:todo, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Todo doesn't exist")}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# def delete_todo(
|
|
|
|
# _parent,
|
|
|
|
# %{id: todo_id, actor_id: actor_id},
|
|
|
|
# %{
|
|
|
|
# context: %{current_user: %User{} = user}
|
|
|
|
# } = _resolution
|
|
|
|
# ) do
|
|
|
|
# with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id),
|
|
|
|
# {:todo, %Todo{todo_list_id: todo_list_id} = todo} <-
|
|
|
|
# {:todo, Todos.get_todo(todo_id)},
|
|
|
|
# {:todo_list, %TodoList{actor_id: group_id}} <-
|
|
|
|
# {:todo_list, Todos.get_todo_list(todo_list_id)},
|
2024-01-04 13:35:02 +01:00
|
|
|
# {:member, true} <- {:member, Actors.member?(actor_id, group_id)},
|
2020-02-18 08:57:00 +01:00
|
|
|
# {:ok, _, %Todo{} = todo} <-
|
2021-09-28 19:40:37 +02:00
|
|
|
# Actions.Delete.delete_todo(todo, actor, true, %{}) do
|
2020-02-18 08:57:00 +01:00
|
|
|
# {:ok, todo}
|
|
|
|
# else
|
|
|
|
# {:todo_list, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
# {:error, "Todo list doesn't exist"}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
# {:todo, _} ->
|
|
|
|
# {:error, "Todo doesn't exist"}
|
|
|
|
|
|
|
|
# {:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
# {:error, "Profile is not member of group"}
|
2020-02-18 08:57:00 +01:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
end
|