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
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
def find_todo_lists_for_group(
|
|
|
|
%Actor{id: group_id} = group,
|
|
|
|
_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 {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
2020-02-18 08:57:00 +01:00
|
|
|
%Page{} = page <- Todos.get_todo_lists_for_group(group) do
|
|
|
|
{:ok, page}
|
|
|
|
else
|
|
|
|
{:member, _} ->
|
|
|
|
with %Page{} = page <- Todos.get_todo_lists_for_group(group) do
|
|
|
|
{:ok, %Page{page | elements: []}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_todo_lists_for_group(_parent, _args, _resolution) do
|
|
|
|
{:ok, %Page{total: 0, elements: []}}
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_todos_for_todo_list(
|
|
|
|
%TodoList{actor_id: group_id} = todo_list,
|
|
|
|
_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 {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
2020-02-18 08:57:00 +01:00
|
|
|
%Page{} = page <- Todos.get_todos_for_todo_list(todo_list) do
|
|
|
|
{: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
|
|
|
|
|
|
|
|
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)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)} do
|
|
|
|
{: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
|
|
|
|
|
|
|
|
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
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, _, %TodoList{} = todo_list} <-
|
|
|
|
ActivityPub.create(:todo_list, Map.put(args, :actor_id, group_id), true, %{}) do
|
|
|
|
{: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)},
|
|
|
|
# {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
# {:ok, _, %TodoList{} = todo} <-
|
|
|
|
# ActivityPub.update_todo_list(todo_list, actor, true, %{}) do
|
|
|
|
# {: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)},
|
|
|
|
# {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
# {:ok, _, %TodoList{} = todo} <-
|
|
|
|
# ActivityPub.delete_todo_list(todo_list, actor, true, %{}) do
|
|
|
|
# {: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 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)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)} do
|
|
|
|
{: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
|
|
|
|
|
|
|
|
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)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
{:ok, _, %Todo{} = todo} <-
|
|
|
|
ActivityPub.create(:todo, Map.put(args, :creator_id, actor_id), true, %{}) do
|
|
|
|
{: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
|
|
|
|
|
|
|
|
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)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
{:ok, _, %Todo{} = todo} <-
|
2020-07-09 17:24:28 +02:00
|
|
|
ActivityPub.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)},
|
|
|
|
# {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
# {:ok, _, %Todo{} = todo} <-
|
|
|
|
# ActivityPub.delete_todo(todo, actor, true, %{}) do
|
|
|
|
# {: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
|