2020-02-18 07:57:00 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Todos do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the todos related GraphQL calls
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.{Actors, Todos, Users}
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Todos.{Todo, TodoList}
|
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 07:53:48 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-02-18 07:57:00 +00: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,
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with %Actor{id: actor_id} <- Users.get_actor_for_user(user),
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
%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,
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with %Actor{id: actor_id} <- Users.get_actor_for_user(user),
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
%Page{} = page <- Todos.get_todos_for_todo_list(todo_list) do
|
|
|
|
{:ok, page}
|
|
|
|
else
|
|
|
|
{:member, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_todo_list(
|
|
|
|
_parent,
|
|
|
|
%{id: todo_list_id},
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with {:actor, %Actor{id: actor_id}} <- {:actor, Users.get_actor_for_user(user)},
|
|
|
|
{:todo, %TodoList{actor_id: group_id} = todo} <-
|
|
|
|
{: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 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Todo list doesn't exist")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
{:actor, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "No profile found for user")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_todo_list(
|
|
|
|
_parent,
|
|
|
|
%{group_id: group_id} = args,
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
{:ok, _, %TodoList{} = todo_list} <-
|
|
|
|
ActivityPub.create(:todo_list, Map.put(args, :actor_id, group_id), true, %{}) do
|
|
|
|
{:ok, todo_list}
|
|
|
|
else
|
|
|
|
{:member, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 07:57:00 +00: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 07:53:48 +00:00
|
|
|
# {:error, "Todo list doesn't exist"}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
# {:member, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
# {:error, "Profile is not member of group"}
|
2020-02-18 07:57:00 +00: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 07:53:48 +00:00
|
|
|
# {:error, "Todo list doesn't exist"}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
# {:member, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
# {:error, "Profile is not member of group"}
|
2020-02-18 07:57:00 +00:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
def get_todo(
|
|
|
|
_parent,
|
|
|
|
%{id: todo_id},
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with {:actor, %Actor{id: actor_id}} <- {:actor, Users.get_actor_for_user(user)},
|
|
|
|
{: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)} do
|
|
|
|
{:ok, todo}
|
|
|
|
else
|
|
|
|
{:todo, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Todo doesn't exist")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
{:actor, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "No profile found for user")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_todo(
|
|
|
|
_parent,
|
|
|
|
%{todo_list_id: todo_list_id} = args,
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)},
|
|
|
|
{: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, _, %Todo{} = todo} <-
|
|
|
|
ActivityPub.create(:todo, Map.put(args, :creator_id, actor_id), true, %{}) do
|
|
|
|
{:ok, todo}
|
|
|
|
else
|
|
|
|
{:todo_list, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Todo list doesn't exist")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_todo(
|
|
|
|
_parent,
|
|
|
|
%{id: todo_id} = args,
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)},
|
|
|
|
{: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} <-
|
2020-07-09 15:24:28 +00:00
|
|
|
ActivityPub.update(todo, args, true, %{}) do
|
2020-02-18 07:57:00 +00:00
|
|
|
{:ok, todo}
|
|
|
|
else
|
|
|
|
{:todo_list, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Todo list doesn't exist")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
{:todo, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Todo doesn't exist")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-02-18 07:57:00 +00: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 07:53:48 +00:00
|
|
|
# {:error, "Todo list doesn't exist"}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
# {:todo, _} ->
|
|
|
|
# {:error, "Todo doesn't exist"}
|
|
|
|
|
|
|
|
# {:member, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
# {:error, "Profile is not member of group"}
|
2020-02-18 07:57:00 +00:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
end
|