1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-29 03:15:27 +00:00
mobilizon/lib/mobilizon_web/resolvers/group.ex

199 lines
6 KiB
Elixir
Raw Normal View History

defmodule MobilizonWeb.Resolvers.Group do
@moduledoc """
2019-09-22 14:26:23 +00:00
Handles the group-related GraphQL calls.
"""
2019-09-22 14:26:23 +00:00
alias Mobilizon.Actors
alias Mobilizon.Actors.{Actor, Member}
2019-09-22 14:26:23 +00:00
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Activity
alias Mobilizon.Users.User
2019-09-12 23:35:03 +00:00
2019-09-22 16:29:13 +00:00
alias MobilizonWeb.API
alias MobilizonWeb.Resolvers.Person
2019-09-12 23:35:03 +00:00
require Logger
@doc """
Find a group
"""
def find_group(_parent, %{preferred_username: name}, _resolution) do
with {:ok, actor} <- ActivityPub.find_or_make_group_from_nickname(name),
actor <- Person.proxify_pictures(actor) do
{:ok, actor}
else
_ ->
{:error, "Group with name #{name} not found"}
end
end
@doc """
Lists all groups
"""
def list_groups(_parent, %{page: page, limit: limit}, _resolution) do
2019-09-02 08:50:00 +00:00
{
:ok,
2019-09-22 11:41:24 +00:00
page
|> Actors.list_groups(limit)
2019-09-02 08:50:00 +00:00
|> Enum.map(fn actor -> Person.proxify_pictures(actor) end)
}
end
@doc """
Create a new group. The creator is automatically added as admin
"""
def create_group(
_parent,
args,
2019-09-07 17:54:11 +00:00
%{context: %{current_user: user}}
) do
2019-03-01 16:11:28 +00:00
with {
:ok,
2019-09-07 17:54:11 +00:00
%Activity{data: %{"object" => %{"type" => "Group"} = _object}},
%Actor{} = group
2019-03-01 16:11:28 +00:00
} <-
2019-09-22 16:29:13 +00:00
API.Groups.create_group(
2019-09-02 08:50:00 +00:00
user,
%{
preferred_username: args.preferred_username,
creator_actor_id: args.creator_actor_id,
name: Map.get(args, "name", args.preferred_username),
summary: args.summary,
avatar: Map.get(args, "avatar"),
banner: Map.get(args, "banner")
}
) do
2019-09-07 17:54:11 +00:00
{:ok, group}
end
end
def create_group(_parent, _args, _resolution) do
{:error, "You need to be logged-in to create a group"}
end
2019-01-25 08:23:44 +00:00
@doc """
Delete an existing group
"""
def delete_group(
_parent,
%{group_id: group_id, actor_id: actor_id},
2019-09-07 17:54:11 +00:00
%{context: %{current_user: user}}
2019-01-25 08:23:44 +00:00
) do
with {actor_id, ""} <- Integer.parse(actor_id),
{group_id, ""} <- Integer.parse(group_id),
{:ok, %Actor{} = group} <- Actors.get_group_by_actor_id(group_id),
2019-09-07 17:54:11 +00:00
{:is_owned, %Actor{}} <- User.owns_actor(user, actor_id),
2019-09-08 22:52:49 +00:00
{:ok, %Member{} = member} <- Actors.get_member(actor_id, group.id),
2019-01-25 08:23:44 +00:00
{:is_admin, true} <- Member.is_administrator(member),
group <- Actors.delete_group!(group) do
{:ok, %{id: group.id}}
else
{:error, :group_not_found} ->
2019-01-25 16:06:57 +00:00
{:error, "Group not found"}
2019-01-25 08:23:44 +00:00
2019-09-07 17:54:11 +00:00
{:is_owned, nil} ->
2019-01-25 08:23:44 +00:00
{:error, "Actor id is not owned by authenticated user"}
{:error, :member_not_found} ->
{:error, "Actor id is not a member of this group"}
{:is_admin, false} ->
2019-02-01 08:42:31 +00:00
{:error, "Actor id is not an administrator of the selected group"}
2019-01-25 08:23:44 +00:00
end
end
def delete_group(_parent, _args, _resolution) do
{:error, "You need to be logged-in to delete a group"}
end
2019-03-01 16:11:28 +00:00
@doc """
Join an existing group
"""
def join_group(
_parent,
%{group_id: group_id, actor_id: actor_id},
2019-09-07 17:54:11 +00:00
%{context: %{current_user: user}}
2019-03-01 16:11:28 +00:00
) do
with {actor_id, ""} <- Integer.parse(actor_id),
{group_id, ""} <- Integer.parse(group_id),
{:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id),
2019-03-01 16:11:28 +00:00
{:ok, %Actor{} = group} <- Actors.get_group_by_actor_id(group_id),
2019-09-08 22:52:49 +00:00
{:error, :member_not_found} <- Actors.get_member(actor.id, group.id),
2019-03-01 16:11:28 +00:00
{:is_able_to_join, true} <- {:is_able_to_join, Member.can_be_joined(group)},
2019-09-08 22:52:49 +00:00
role <- Member.get_default_member_role(group),
2019-09-07 17:54:11 +00:00
{:ok, _} <- Actors.create_member(%{parent_id: group.id, actor_id: actor.id, role: role}) do
2019-09-02 08:50:00 +00:00
{
:ok,
%{
2019-09-07 17:54:11 +00:00
parent: Person.proxify_pictures(group),
actor: Person.proxify_pictures(actor),
2019-09-02 08:50:00 +00:00
role: role
}
}
2019-03-01 16:11:28 +00:00
else
2019-09-07 17:54:11 +00:00
{:is_owned, nil} ->
2019-03-01 16:11:28 +00:00
{:error, "Actor id is not owned by authenticated user"}
{:error, :group_not_found} ->
{:error, "Group id not found"}
{:is_able_to_join, false} ->
{:error, "You cannot join this group"}
{:ok, %Member{}} ->
{:error, "You are already a member of this group"}
end
end
def join_group(_parent, _args, _resolution) do
{:error, "You need to be logged-in to join a group"}
end
@doc """
Leave a existing group
"""
def leave_group(
_parent,
%{group_id: group_id, actor_id: actor_id},
2019-09-07 17:54:11 +00:00
%{context: %{current_user: user}}
2019-03-01 16:11:28 +00:00
) do
with {actor_id, ""} <- Integer.parse(actor_id),
{group_id, ""} <- Integer.parse(group_id),
{:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id),
2019-09-08 22:52:49 +00:00
{:ok, %Member{} = member} <- Actors.get_member(actor.id, group_id),
2019-03-01 16:11:28 +00:00
{:only_administrator, false} <-
2019-08-26 13:44:02 +00:00
{:only_administrator, check_that_member_is_not_last_administrator(group_id, actor_id)},
2019-03-01 16:11:28 +00:00
{:ok, _} <-
Mobilizon.Actors.delete_member(member) do
2019-09-07 17:54:11 +00:00
{:ok, %{parent: %{id: group_id}, actor: %{id: actor_id}}}
2019-03-01 16:11:28 +00:00
else
2019-09-07 17:54:11 +00:00
{:is_owned, nil} ->
2019-03-01 16:11:28 +00:00
{:error, "Actor id is not owned by authenticated user"}
{:error, :member_not_found} ->
{:error, "Member not found"}
{:only_administrator, true} ->
{:error, "You can't leave this group because you are the only administrator"}
end
end
def leave_group(_parent, _args, _resolution) do
{:error, "You need to be logged-in to leave a group"}
end
# We check that the actor asking to leave the group is not it's only administrator
# We start by fetching the list of administrator or creators and if there's only one of them
# and that it's the actor requesting leaving the group we return true
2019-08-26 13:44:02 +00:00
@spec check_that_member_is_not_last_administrator(integer(), integer()) :: boolean()
defp check_that_member_is_not_last_administrator(group_id, actor_id) do
2019-09-08 22:52:49 +00:00
case Actors.list_administrator_members_for_group(group_id) do
2019-09-07 17:54:11 +00:00
[%Member{actor: %Actor{id: member_actor_id}}] ->
actor_id == member_actor_id
_ ->
false
2019-03-01 16:11:28 +00:00
end
end
end