2020-01-26 20:34:25 +01:00
|
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Group do
|
2019-01-03 14:59:59 +01:00
|
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
|
Handles the group-related GraphQL calls.
|
2019-01-03 14:59:59 +01:00
|
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
|
import Mobilizon.Users.Guards
|
2020-02-18 08:57:00 +01:00
|
|
|
|
alias Mobilizon.{Actors, Events, Users}
|
2019-03-05 17:23:05 +01:00
|
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2020-01-26 21:11:16 +01:00
|
|
|
|
alias Mobilizon.GraphQL.API
|
2020-01-26 20:34:25 +01:00
|
|
|
|
alias Mobilizon.GraphQL.Resolvers.Person
|
2020-02-18 08:57:00 +01:00
|
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 09:53:48 +02:00
|
|
|
|
alias Mobilizon.Web.Upload
|
|
|
|
|
import Mobilizon.Web.Gettext
|
2020-01-22 02:14:42 +01:00
|
|
|
|
|
2018-12-03 11:58:57 +01:00
|
|
|
|
require Logger
|
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
|
@doc """
|
|
|
|
|
Find a group
|
|
|
|
|
"""
|
|
|
|
|
def find_group(
|
|
|
|
|
parent,
|
|
|
|
|
%{preferred_username: name} = args,
|
|
|
|
|
%{
|
|
|
|
|
context: %{
|
|
|
|
|
current_user: %User{} = user
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
) do
|
|
|
|
|
with {:ok, %Actor{id: group_id} = group} <-
|
|
|
|
|
ActivityPub.find_or_make_group_from_nickname(name),
|
|
|
|
|
{:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)},
|
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
|
group <- Person.proxify_pictures(group) do
|
|
|
|
|
{:ok, group}
|
|
|
|
|
else
|
|
|
|
|
{:member, false} ->
|
|
|
|
|
find_group(parent, args, nil)
|
|
|
|
|
|
|
|
|
|
_ ->
|
2020-10-12 12:16:36 +02:00
|
|
|
|
{:error, :group_not_found}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-03 11:58:57 +01:00
|
|
|
|
def find_group(_parent, %{preferred_username: name}, _resolution) do
|
2019-05-28 10:51:02 +02:00
|
|
|
|
with {:ok, actor} <- ActivityPub.find_or_make_group_from_nickname(name),
|
2020-02-18 08:57:00 +01:00
|
|
|
|
%Actor{} = actor <- Person.proxify_pictures(actor),
|
|
|
|
|
%Actor{} = actor <- restrict_fields_for_non_member_request(actor) do
|
2019-05-28 10:51:02 +02:00
|
|
|
|
{:ok, actor}
|
|
|
|
|
else
|
2018-12-03 11:58:57 +01:00
|
|
|
|
_ ->
|
2020-10-12 12:16:36 +02:00
|
|
|
|
{:error, :group_not_found}
|
2018-12-03 11:58:57 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
|
@doc """
|
|
|
|
|
Get a group
|
|
|
|
|
"""
|
|
|
|
|
def get_group(_parent, %{id: id}, %{context: %{current_user: %User{role: role}}}) do
|
|
|
|
|
with %Actor{type: :Group, suspended: suspended} = actor <-
|
|
|
|
|
Actors.get_actor_with_preload(id, true),
|
|
|
|
|
true <- suspended == false or is_moderator(role) do
|
|
|
|
|
{:ok, actor}
|
|
|
|
|
else
|
|
|
|
|
_ ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "Group with ID %{id} not found", id: id)}
|
2020-08-27 11:53:24 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-03 11:58:57 +01:00
|
|
|
|
@doc """
|
|
|
|
|
Lists all groups
|
|
|
|
|
"""
|
2020-08-27 11:53:24 +02:00
|
|
|
|
def list_groups(
|
|
|
|
|
_parent,
|
|
|
|
|
%{
|
|
|
|
|
preferred_username: preferred_username,
|
|
|
|
|
name: name,
|
|
|
|
|
domain: domain,
|
|
|
|
|
local: local,
|
|
|
|
|
suspended: suspended,
|
|
|
|
|
page: page,
|
|
|
|
|
limit: limit
|
|
|
|
|
},
|
|
|
|
|
%{
|
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
when is_moderator(role) do
|
|
|
|
|
{:ok,
|
|
|
|
|
Actors.list_actors(:Group, preferred_username, name, domain, local, suspended, page, limit)}
|
2018-12-03 11:58:57 +01:00
|
|
|
|
end
|
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
|
def list_groups(_parent, _args, _resolution),
|
2020-09-29 09:53:48 +02:00
|
|
|
|
do: {:error, dgettext("errors", "You may not list groups unless moderator.")}
|
|
|
|
|
|
|
|
|
|
# TODO Move me to somewhere cleaner
|
|
|
|
|
defp save_attached_pictures(args) do
|
|
|
|
|
Enum.reduce([:avatar, :banner], args, fn key, args ->
|
2020-11-26 11:41:13 +01:00
|
|
|
|
if Map.has_key?(args, key) && !is_nil(args[key][:media]) do
|
|
|
|
|
pic = args[key][:media]
|
2020-09-29 09:53:48 +02:00
|
|
|
|
|
|
|
|
|
with {:ok, %{name: name, url: url, content_type: content_type, size: _size}} <-
|
|
|
|
|
Upload.store(pic.file, type: key, description: pic.alt) do
|
|
|
|
|
Map.put(args, key, %{"name" => name, "url" => url, "mediaType" => content_type})
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
args
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end
|
2020-08-27 11:53:24 +02:00
|
|
|
|
|
2018-12-03 11:58:57 +01:00
|
|
|
|
@doc """
|
|
|
|
|
Create a new group. The creator is automatically added as admin
|
|
|
|
|
"""
|
2020-02-18 08:57:00 +01:00
|
|
|
|
def create_group(
|
|
|
|
|
_parent,
|
|
|
|
|
args,
|
|
|
|
|
%{
|
|
|
|
|
context: %{
|
|
|
|
|
current_user: user
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
) do
|
2020-11-19 17:06:28 +01:00
|
|
|
|
with %Actor{id: creator_actor_id} = creator_actor <- Users.get_actor_for_user(user),
|
2020-11-17 19:14:55 +01:00
|
|
|
|
args <- Map.update(args, :preferred_username, "", &String.downcase/1),
|
2019-12-03 11:29:51 +01:00
|
|
|
|
args <- Map.put(args, :creator_actor, creator_actor),
|
2020-11-19 17:06:28 +01:00
|
|
|
|
args <- Map.put(args, :creator_actor_id, creator_actor_id),
|
2020-09-29 09:53:48 +02:00
|
|
|
|
args <- save_attached_pictures(args),
|
2019-10-25 17:43:37 +02:00
|
|
|
|
{:ok, _activity, %Actor{type: :Group} = group} <-
|
|
|
|
|
API.Groups.create_group(args) do
|
2019-09-07 19:54:11 +02:00
|
|
|
|
{:ok, group}
|
2019-10-25 17:43:37 +02:00
|
|
|
|
else
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:error, err} when is_binary(err) ->
|
2019-10-25 17:43:37 +02:00
|
|
|
|
{:error, err}
|
2018-12-03 11:58:57 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_group(_parent, _args, _resolution) do
|
|
|
|
|
{:error, "You need to be logged-in to create a group"}
|
|
|
|
|
end
|
2019-01-25 09:23:44 +01:00
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
@doc """
|
2020-10-09 15:26:37 +02:00
|
|
|
|
Update a group. The creator is automatically added as admin
|
2020-07-09 17:24:28 +02:00
|
|
|
|
"""
|
|
|
|
|
def update_group(
|
|
|
|
|
_parent,
|
2020-10-09 15:26:37 +02:00
|
|
|
|
%{id: group_id} = args,
|
2020-07-09 17:24:28 +02:00
|
|
|
|
%{
|
|
|
|
|
context: %{
|
|
|
|
|
current_user: %User{} = user
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
) do
|
|
|
|
|
with %Actor{} = updater_actor <- Users.get_actor_for_user(user),
|
2020-10-09 15:26:37 +02:00
|
|
|
|
{:administrator, true} <-
|
|
|
|
|
{:administrator, Actors.is_administrator?(updater_actor.id, group_id)},
|
2020-07-09 17:24:28 +02:00
|
|
|
|
args <- Map.put(args, :updater_actor, updater_actor),
|
2020-09-29 09:53:48 +02:00
|
|
|
|
args <- save_attached_pictures(args),
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:ok, _activity, %Actor{type: :Group} = group} <-
|
|
|
|
|
API.Groups.update_group(args) do
|
|
|
|
|
{:ok, group}
|
|
|
|
|
else
|
|
|
|
|
{:error, err} when is_binary(err) ->
|
|
|
|
|
{:error, err}
|
|
|
|
|
|
2020-10-09 15:26:37 +02:00
|
|
|
|
{:administrator, false} ->
|
|
|
|
|
{:error, dgettext("errors", "Profile is not administrator for the group")}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update_group(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "You need to be logged-in to update a group")}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-01-25 09:23:44 +01:00
|
|
|
|
@doc """
|
|
|
|
|
Delete an existing group
|
|
|
|
|
"""
|
|
|
|
|
def delete_group(
|
|
|
|
|
_parent,
|
2020-08-27 11:53:24 +02:00
|
|
|
|
%{group_id: group_id},
|
2020-02-18 08:57:00 +01:00
|
|
|
|
%{
|
|
|
|
|
context: %{
|
|
|
|
|
current_user: user
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-25 09:23:44 +01:00
|
|
|
|
) do
|
2020-08-27 11:53:24 +02:00
|
|
|
|
with %Actor{id: actor_id} = actor <- Users.get_actor_for_user(user),
|
2019-09-09 09:31:08 +02:00
|
|
|
|
{:ok, %Actor{} = group} <- Actors.get_group_by_actor_id(group_id),
|
2019-09-09 00:52:49 +02:00
|
|
|
|
{:ok, %Member{} = member} <- Actors.get_member(actor_id, group.id),
|
2020-02-18 08:57:00 +01:00
|
|
|
|
{:is_admin, true} <- {:is_admin, Member.is_administrator(member)},
|
2020-08-27 11:53:24 +02:00
|
|
|
|
{:ok, _activity, group} <- ActivityPub.delete(group, actor, true) do
|
2019-01-25 09:23:44 +01:00
|
|
|
|
{:ok, %{id: group.id}}
|
|
|
|
|
else
|
|
|
|
|
{:error, :group_not_found} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "Group not found")}
|
2019-01-25 09:23:44 +01:00
|
|
|
|
|
|
|
|
|
{:error, :member_not_found} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "Current profile is not a member of this group")}
|
2019-01-25 09:23:44 +01:00
|
|
|
|
|
|
|
|
|
{:is_admin, false} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error,
|
|
|
|
|
dgettext("errors", "Current profile is not an administrator of the selected group")}
|
2019-01-25 09:23:44 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def delete_group(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "You need to be logged-in to delete a group")}
|
2019-01-25 09:23:44 +01:00
|
|
|
|
end
|
2019-03-01 17:11:28 +01:00
|
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
|
Join an existing group
|
|
|
|
|
"""
|
2020-11-06 11:34:32 +01:00
|
|
|
|
def join_group(_parent, %{group_id: group_id} = args, %{
|
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
|
}) do
|
|
|
|
|
with %Actor{} = actor <- Users.get_actor_for_user(user),
|
|
|
|
|
{:ok, %Actor{type: :Group} = group} <-
|
|
|
|
|
Actors.get_group_by_actor_id(group_id),
|
2019-09-09 00:52:49 +02:00
|
|
|
|
{:error, :member_not_found} <- Actors.get_member(actor.id, group.id),
|
2019-03-01 17:11:28 +01:00
|
|
|
|
{:is_able_to_join, true} <- {:is_able_to_join, Member.can_be_joined(group)},
|
2020-11-06 11:34:32 +01:00
|
|
|
|
{:ok, _activity, %Member{} = member} <-
|
|
|
|
|
ActivityPub.join(group, actor, true, args) do
|
|
|
|
|
{:ok, member}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
else
|
|
|
|
|
{:error, :group_not_found} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "Group not found")}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
|
|
|
|
|
{:is_able_to_join, false} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "You cannot join this group")}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
|
|
|
|
|
{:ok, %Member{}} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "You are already a member of this group")}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def join_group(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "You need to be logged-in to join a group")}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
|
Leave a existing group
|
|
|
|
|
"""
|
|
|
|
|
def leave_group(
|
|
|
|
|
_parent,
|
2020-08-14 11:32:23 +02:00
|
|
|
|
%{group_id: group_id},
|
2020-02-18 08:57:00 +01:00
|
|
|
|
%{
|
|
|
|
|
context: %{
|
2020-08-14 11:32:23 +02:00
|
|
|
|
current_user: %User{} = user
|
2020-02-18 08:57:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
) do
|
2020-08-14 11:32:23 +02:00
|
|
|
|
with {:actor, %Actor{} = actor} <- {:actor, Users.get_actor_for_user(user)},
|
|
|
|
|
{:group, %Actor{type: :Group} = group} <- {:group, Actors.get_actor(group_id)},
|
|
|
|
|
{:ok, _activity, %Member{} = member} <- ActivityPub.leave(group, actor, true) do
|
|
|
|
|
{:ok, member}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
else
|
|
|
|
|
{:error, :member_not_found} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "Member not found")}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
|
2020-08-14 11:32:23 +02:00
|
|
|
|
{:group, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "Group not found")}
|
2020-08-14 11:32:23 +02:00
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
|
{:is_not_only_admin, false} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error,
|
|
|
|
|
dgettext("errors", "You can't leave this group because you are the only administrator")}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def leave_group(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
|
{:error, dgettext("errors", "You need to be logged-in to leave a group")}
|
2019-03-01 17:11:28 +01:00
|
|
|
|
end
|
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
|
def find_events_for_group(
|
|
|
|
|
%Actor{id: group_id} = group,
|
2020-09-02 17:42:17 +02:00
|
|
|
|
%{
|
|
|
|
|
page: page,
|
|
|
|
|
limit: limit
|
|
|
|
|
} = args,
|
2020-02-18 08:57:00 +01:00
|
|
|
|
%{
|
|
|
|
|
context: %{
|
2020-08-27 11:53:24 +02:00
|
|
|
|
current_user: %User{role: user_role} = user
|
2020-02-18 08:57:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
) do
|
|
|
|
|
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)},
|
2020-08-27 11:53:24 +02:00
|
|
|
|
{:member, true} <-
|
|
|
|
|
{:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)} do
|
2020-02-18 08:57:00 +01:00
|
|
|
|
# TODO : Handle public / restricted to group members events
|
2020-09-02 17:42:17 +02:00
|
|
|
|
{:ok,
|
|
|
|
|
Events.list_organized_events_for_group(
|
|
|
|
|
group,
|
|
|
|
|
:all,
|
|
|
|
|
Map.get(args, :after_datetime),
|
|
|
|
|
Map.get(args, :before_datetime),
|
|
|
|
|
page,
|
|
|
|
|
limit
|
|
|
|
|
)}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
else
|
|
|
|
|
{:member, false} ->
|
2020-09-02 17:42:17 +02:00
|
|
|
|
find_events_for_group(group, args, nil)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-09-02 17:42:17 +02:00
|
|
|
|
def find_events_for_group(
|
|
|
|
|
%Actor{} = group,
|
|
|
|
|
%{
|
|
|
|
|
page: page,
|
|
|
|
|
limit: limit
|
|
|
|
|
} = args,
|
|
|
|
|
_resolution
|
|
|
|
|
) do
|
|
|
|
|
{:ok,
|
|
|
|
|
Events.list_organized_events_for_group(
|
|
|
|
|
group,
|
|
|
|
|
:public,
|
|
|
|
|
Map.get(args, :after_datetime),
|
|
|
|
|
Map.get(args, :before_datetime),
|
|
|
|
|
page,
|
|
|
|
|
limit
|
|
|
|
|
)}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp restrict_fields_for_non_member_request(%Actor{} = group) do
|
|
|
|
|
Map.merge(
|
|
|
|
|
group,
|
|
|
|
|
%{
|
|
|
|
|
followers: [],
|
|
|
|
|
followings: [],
|
|
|
|
|
organized_events: [],
|
|
|
|
|
comments: [],
|
|
|
|
|
feed_tokens: []
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
end
|
2018-12-03 11:58:57 +01:00
|
|
|
|
end
|