2020-01-26 19:34:25 +00:00
|
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Member do
|
2019-04-03 15:29:03 +00:00
|
|
|
|
@moduledoc """
|
|
|
|
|
Handles the member-related GraphQL calls
|
|
|
|
|
"""
|
2020-01-26 20:11:16 +00:00
|
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
|
import Mobilizon.Users.Guards
|
2020-02-18 07:57:00 +00:00
|
|
|
|
alias Mobilizon.{Actors, Users}
|
|
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2020-08-14 09:32:23 +00:00
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Refresher
|
2020-02-18 07:57:00 +00:00
|
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 07:53:48 +00:00
|
|
|
|
import Mobilizon.Web.Gettext
|
2019-04-03 15:29:03 +00:00
|
|
|
|
|
|
|
|
|
@doc """
|
2020-02-18 07:57:00 +00:00
|
|
|
|
Find members for group.
|
|
|
|
|
|
|
|
|
|
If actor requesting is not part of the group, we only return the number of members, not members
|
2019-04-03 15:29:03 +00:00
|
|
|
|
"""
|
2020-02-18 07:57:00 +00:00
|
|
|
|
def find_members_for_group(
|
|
|
|
|
%Actor{id: group_id} = group,
|
2020-07-09 15:24:28 +00:00
|
|
|
|
%{page: page, limit: limit, roles: roles},
|
2020-02-18 07:57:00 +00:00
|
|
|
|
%{
|
2020-08-27 09:53:24 +00:00
|
|
|
|
context: %{current_user: %User{role: user_role} = user}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
} = _resolution
|
|
|
|
|
) do
|
|
|
|
|
with %Actor{id: actor_id} <- Users.get_actor_for_user(user),
|
2020-08-27 09:53:24 +00:00
|
|
|
|
{:member, true} <-
|
|
|
|
|
{:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)} do
|
2020-07-09 15:24:28 +00:00
|
|
|
|
roles =
|
|
|
|
|
case roles do
|
|
|
|
|
"" ->
|
|
|
|
|
[]
|
|
|
|
|
|
|
|
|
|
roles ->
|
|
|
|
|
roles
|
|
|
|
|
|> String.split(",")
|
|
|
|
|
|> Enum.map(&String.downcase/1)
|
|
|
|
|
|> Enum.map(&String.to_existing_atom/1)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
%Page{} = page = Actors.list_members_for_group(group, roles, page, limit)
|
2020-02-18 07:57:00 +00:00
|
|
|
|
{:ok, page}
|
|
|
|
|
else
|
|
|
|
|
{:member, false} ->
|
|
|
|
|
# Actor is not member of group, fallback to public
|
|
|
|
|
with %Page{} = page <- Actors.list_members_for_group(group) do
|
|
|
|
|
{:ok, %Page{page | elements: []}}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_members_for_group(%Actor{} = group, _args, _resolution) do
|
|
|
|
|
with %Page{} = page <- Actors.list_members_for_group(group) do
|
|
|
|
|
{:ok, %Page{page | elements: []}}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def invite_member(
|
|
|
|
|
_parent,
|
|
|
|
|
%{group_id: group_id, target_actor_username: target_actor_username},
|
|
|
|
|
%{context: %{current_user: %User{} = user}}
|
|
|
|
|
) do
|
|
|
|
|
with %Actor{id: actor_id} = actor <- Users.get_actor_for_user(user),
|
|
|
|
|
{:ok, %Actor{} = group} <- Actors.get_group_by_actor_id(group_id),
|
|
|
|
|
{:has_rights_to_invite, {:ok, %Member{role: role}}}
|
|
|
|
|
when role in [:moderator, :administrator, :creator] <-
|
|
|
|
|
{:has_rights_to_invite, Actors.get_member(actor_id, group_id)},
|
2020-10-15 12:23:55 +00:00
|
|
|
|
target_actor_username <-
|
|
|
|
|
target_actor_username |> String.trim() |> String.trim_leading("@"),
|
2020-02-18 07:57:00 +00:00
|
|
|
|
{:target_actor_username, {:ok, %Actor{id: target_actor_id} = target_actor}} <-
|
|
|
|
|
{:target_actor_username,
|
|
|
|
|
ActivityPub.find_or_make_actor_from_nickname(target_actor_username)},
|
2020-10-15 12:23:55 +00:00
|
|
|
|
{:existant, true} <-
|
|
|
|
|
{:existant, check_member_not_existant_or_rejected(target_actor_id, group.id)},
|
2020-02-18 07:57:00 +00:00
|
|
|
|
{:ok, _activity, %Member{} = member} <- ActivityPub.invite(group, actor, target_actor) do
|
|
|
|
|
{:ok, member}
|
|
|
|
|
else
|
|
|
|
|
{:is_owned, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
|
|
{:error, :group_not_found} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
|
{:error, dgettext("errors", "Group not found")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
|
|
{:target_actor_username, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
|
{:error, dgettext("errors", "Profile invited doesn't exist")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
|
|
{:has_rights_to_invite, {:error, :member_not_found}} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
|
{:error, dgettext("errors", "You are not a member of this group")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
|
|
{:has_rights_to_invite, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
|
{:error, dgettext("errors", "You cannot invite to this group")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
2020-10-15 12:23:55 +00:00
|
|
|
|
{:existant, _} ->
|
|
|
|
|
{:error, dgettext("errors", "Profile is already a member of this group")}
|
|
|
|
|
|
|
|
|
|
# Remove me ?
|
2020-02-18 07:57:00 +00:00
|
|
|
|
{:ok, %Member{}} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
|
{:error, dgettext("errors", "Profile is already a member of this group")}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def accept_invitation(_parent, %{id: member_id}, %{context: %{current_user: %User{} = user}}) do
|
|
|
|
|
with %Actor{id: actor_id} <- Users.get_actor_for_user(user),
|
2020-08-14 09:32:23 +00:00
|
|
|
|
%Member{actor: %Actor{id: member_actor_id} = actor} = member <-
|
|
|
|
|
Actors.get_member(member_id),
|
2020-02-18 07:57:00 +00:00
|
|
|
|
{:is_same_actor, true} <- {:is_same_actor, member_actor_id === actor_id},
|
|
|
|
|
{:ok, _activity, %Member{} = member} <-
|
|
|
|
|
ActivityPub.accept(
|
|
|
|
|
:invite,
|
|
|
|
|
member,
|
|
|
|
|
true
|
|
|
|
|
) do
|
|
|
|
|
# Launch an async task to refresh the group profile, fetch resources, discussions, members
|
2020-08-14 09:32:23 +00:00
|
|
|
|
Refresher.fetch_group(member.parent.url, actor)
|
|
|
|
|
{:ok, member}
|
2020-10-02 14:19:15 +00:00
|
|
|
|
else
|
|
|
|
|
{:is_same_actor, false} ->
|
|
|
|
|
{:error, dgettext("errors", "You can't accept this invitation with this profile.")}
|
2020-08-14 09:32:23 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def reject_invitation(_parent, %{id: member_id}, %{context: %{current_user: %User{} = user}}) do
|
|
|
|
|
with %Actor{id: actor_id} <- Users.get_actor_for_user(user),
|
2020-10-15 12:23:55 +00:00
|
|
|
|
{:invitation_exists, %Member{actor: %Actor{id: member_actor_id}} = member} <-
|
|
|
|
|
{:invitation_exists, Actors.get_member(member_id)},
|
2020-08-14 09:32:23 +00:00
|
|
|
|
{:is_same_actor, true} <- {:is_same_actor, member_actor_id === actor_id},
|
|
|
|
|
{:ok, _activity, %Member{} = member} <-
|
|
|
|
|
ActivityPub.reject(
|
|
|
|
|
:invite,
|
|
|
|
|
member,
|
|
|
|
|
true
|
|
|
|
|
) do
|
|
|
|
|
{:ok, member}
|
2020-10-02 14:19:15 +00:00
|
|
|
|
else
|
|
|
|
|
{:is_same_actor, false} ->
|
|
|
|
|
{:error, dgettext("errors", "You can't reject this invitation with this profile.")}
|
2020-10-15 12:23:55 +00:00
|
|
|
|
|
|
|
|
|
{:invitation_exists, _} ->
|
|
|
|
|
{:error, dgettext("errors", "This invitation doesn't exist.")}
|
2020-08-14 09:32:23 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-20 08:54:58 +00:00
|
|
|
|
def update_member(_parent, %{member_id: member_id, role: role}, %{
|
2020-08-14 09:32:23 +00:00
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
|
}) do
|
|
|
|
|
with %Actor{} = moderator <- Users.get_actor_for_user(user),
|
2020-08-20 08:54:58 +00:00
|
|
|
|
%Member{} = member <- Actors.get_member(member_id),
|
|
|
|
|
{:ok, _activity, %Member{} = member} <-
|
|
|
|
|
ActivityPub.update(member, %{role: role}, true, %{moderator: moderator}) do
|
|
|
|
|
{:ok, member}
|
|
|
|
|
else
|
|
|
|
|
{:has_rights_to_update_role, {:error, :member_not_found}} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
|
{:error, dgettext("errors", "You are not a moderator or admin for this group")}
|
2020-08-20 08:54:58 +00:00
|
|
|
|
|
|
|
|
|
{:is_only_admin, true} ->
|
|
|
|
|
{:error,
|
2020-09-29 07:53:48 +00:00
|
|
|
|
dgettext(
|
|
|
|
|
"errors",
|
|
|
|
|
"You can't set yourself to a lower member role for this group because you are the only administrator"
|
|
|
|
|
)}
|
2020-08-20 08:54:58 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update_member(_parent, _args, _resolution),
|
|
|
|
|
do: {:error, "You must be logged-in to update a member"}
|
|
|
|
|
|
|
|
|
|
def remove_member(_parent, %{member_id: member_id, group_id: group_id}, %{
|
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
|
}) do
|
|
|
|
|
with %Actor{id: moderator_id} = moderator <- Users.get_actor_for_user(user),
|
2020-10-15 12:23:55 +00:00
|
|
|
|
%Member{role: role} = member when role != :rejected <- Actors.get_member(member_id),
|
2020-08-14 09:32:23 +00:00
|
|
|
|
%Actor{type: :Group} = group <- Actors.get_actor(group_id),
|
2020-10-15 12:23:55 +00:00
|
|
|
|
{:has_rights_to_remove, {:ok, %Member{role: role}}}
|
2020-08-20 08:54:58 +00:00
|
|
|
|
when role in [:moderator, :administrator, :creator] <-
|
2020-10-15 12:23:55 +00:00
|
|
|
|
{:has_rights_to_remove, Actors.get_member(moderator_id, group_id)},
|
2020-08-14 09:32:23 +00:00
|
|
|
|
{:ok, _activity, %Member{}} <- ActivityPub.remove(member, group, moderator, true) do
|
2020-02-18 07:57:00 +00:00
|
|
|
|
{:ok, member}
|
2020-10-15 12:23:55 +00:00
|
|
|
|
else
|
|
|
|
|
%Member{role: :rejected} ->
|
|
|
|
|
{:error,
|
|
|
|
|
dgettext(
|
|
|
|
|
"errors",
|
|
|
|
|
"This member already has been rejected."
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{:has_rights_to_remove, _} ->
|
|
|
|
|
{:error,
|
|
|
|
|
dgettext(
|
|
|
|
|
"errors",
|
|
|
|
|
"You don't have the right to remove this member."
|
|
|
|
|
)}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
end
|
2019-04-03 15:29:03 +00:00
|
|
|
|
end
|
2020-08-14 09:32:23 +00:00
|
|
|
|
|
|
|
|
|
# Rejected members can be invited again
|
|
|
|
|
@spec check_member_not_existant_or_rejected(String.t() | integer, String.t() | integer()) ::
|
|
|
|
|
boolean()
|
|
|
|
|
defp check_member_not_existant_or_rejected(target_actor_id, group_id) do
|
|
|
|
|
case Actors.get_member(target_actor_id, group_id) do
|
|
|
|
|
{:ok, %Member{role: :rejected}} ->
|
|
|
|
|
true
|
|
|
|
|
|
|
|
|
|
{:error, :member_not_found} ->
|
|
|
|
|
true
|
|
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
|
_err ->
|
2020-08-14 09:32:23 +00:00
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-04-03 15:29:03 +00:00
|
|
|
|
end
|