2020-02-18 07:57:00 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Resource do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the resources-related GraphQL calls
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.{Actors, Resources, Users}
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Resources.Resource
|
|
|
|
alias Mobilizon.Resources.Resource.Metadata
|
|
|
|
alias Mobilizon.Service.RichMedia.Parser
|
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Users.User
|
2021-01-26 09:05:31 +00:00
|
|
|
alias Mobilizon.Web.MediaProxy
|
2020-09-29 07:53:48 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Find resources for group.
|
|
|
|
|
|
|
|
Returns only if actor requesting is a member of the group
|
|
|
|
"""
|
|
|
|
def find_resources_for_group(
|
|
|
|
%Actor{id: group_id} = group,
|
|
|
|
%{page: page, limit: limit},
|
|
|
|
%{
|
|
|
|
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 <- Resources.get_resources_for_group(group, page, limit) do
|
|
|
|
{:ok, page}
|
|
|
|
else
|
|
|
|
{:member, _} ->
|
|
|
|
find_resources_for_group(nil, nil, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_resources_for_group(
|
|
|
|
_group,
|
|
|
|
_args,
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
{:ok, %Page{total: 0, elements: []}}
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_resources_for_parent(
|
|
|
|
%Resource{actor_id: group_id} = parent,
|
|
|
|
_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 <- Resources.get_resources_for_folder(parent) do
|
|
|
|
{:ok, page}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_resources_for_parent(_parent, _args, _resolution),
|
|
|
|
do: {:ok, %Page{total: 0, elements: []}}
|
|
|
|
|
|
|
|
def get_resource(
|
|
|
|
_parent,
|
|
|
|
%{path: path, username: username},
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: %User{} = user
|
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with {:current_actor, %Actor{id: actor_id}} <-
|
|
|
|
{:current_actor, Users.get_actor_for_user(user)},
|
|
|
|
{:group, %Actor{id: group_id}} <- {:group, Actors.get_actor_by_name(username, :Group)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
{:resource, %Resource{} = resource} <-
|
|
|
|
{:resource, Resources.get_resource_by_group_and_path_with_preloads(group_id, path)} do
|
|
|
|
{:ok, resource}
|
|
|
|
else
|
2020-10-19 17:21:39 +00:00
|
|
|
{:group, _} -> {:error, :group_not_found}
|
2020-09-29 07:53:48 +00:00
|
|
|
{:member, false} -> {:error, dgettext("errors", "Profile is not member of group")}
|
2020-10-19 17:21:39 +00:00
|
|
|
{:resource, _} -> {:error, :resource_not_found}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_resource(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to access resources")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_resource(
|
|
|
|
_parent,
|
|
|
|
%{actor_id: group_id} = 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)},
|
|
|
|
parent <- get_eventual_parent(args),
|
|
|
|
{:own_check, true} <- {:own_check, check_resource_owned_by_group(parent, group_id)},
|
|
|
|
{:ok, _, %Resource{} = resource} <-
|
|
|
|
ActivityPub.create(
|
|
|
|
:resource,
|
|
|
|
args
|
|
|
|
|> Map.put(:actor_id, group_id)
|
|
|
|
|> Map.put(:creator_id, actor_id),
|
|
|
|
true,
|
|
|
|
%{}
|
|
|
|
) do
|
|
|
|
{:ok, resource}
|
|
|
|
else
|
2021-03-24 09:45:29 +00:00
|
|
|
{:error, _} ->
|
|
|
|
{:error, dgettext("errors", "Error while creating resource")}
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
{:own_check, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Parent resource doesn't belong to this group")}
|
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_resource(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to create resources")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_resource(
|
|
|
|
_parent,
|
|
|
|
%{id: resource_id} = args,
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: %User{} = user
|
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
2020-10-19 17:21:39 +00:00
|
|
|
with %Actor{id: actor_id, url: actor_url} <- Users.get_actor_for_user(user),
|
2020-02-18 07:57:00 +00:00
|
|
|
{:resource, %Resource{actor_id: group_id} = resource} <-
|
|
|
|
{:resource, Resources.get_resource_with_preloads(resource_id)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
{:ok, _, %Resource{} = resource} <-
|
2020-10-19 17:21:39 +00:00
|
|
|
ActivityPub.update(resource, args, true, %{"actor" => actor_url}) do
|
2020-02-18 07:57:00 +00:00
|
|
|
{:ok, resource}
|
|
|
|
else
|
|
|
|
{:resource, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Resource 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_resource(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to update resources")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_resource(
|
|
|
|
_parent,
|
|
|
|
%{id: resource_id},
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: %User{} = user
|
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
2020-07-09 15:24:28 +00:00
|
|
|
with %Actor{id: actor_id} = actor <- Users.get_actor_for_user(user),
|
2020-02-18 07:57:00 +00:00
|
|
|
{:resource, %Resource{parent_id: _parent_id, actor_id: group_id} = resource} <-
|
|
|
|
{:resource, Resources.get_resource_with_preloads(resource_id)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
{:ok, _, %Resource{} = resource} <-
|
2020-07-09 15:24:28 +00:00
|
|
|
ActivityPub.delete(resource, actor) do
|
2020-02-18 07:57:00 +00:00
|
|
|
{:ok, resource}
|
|
|
|
else
|
|
|
|
{:resource, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Resource 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_resource(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to delete resources")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def preview_resource_link(
|
|
|
|
_parent,
|
|
|
|
%{resource_url: resource_url},
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: %User{} = _user
|
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
2020-10-20 08:06:58 +00:00
|
|
|
case Parser.parse(resource_url) do
|
|
|
|
{:ok, data} when is_map(data) ->
|
|
|
|
{:ok, struct(Metadata, data)}
|
|
|
|
|
2021-03-24 09:45:29 +00:00
|
|
|
{:error, :invalid_parsed_data} ->
|
|
|
|
{:error, dgettext("errors", "Unable to fetch resource details from this URL.")}
|
|
|
|
|
|
|
|
{:error, err} ->
|
2020-10-19 17:21:39 +00:00
|
|
|
Logger.warn("Error while fetching preview from #{inspect(resource_url)}")
|
2021-03-24 09:45:29 +00:00
|
|
|
Logger.debug(inspect(err))
|
2020-10-19 17:21:39 +00:00
|
|
|
{:error, :unknown_resource}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def preview_resource_link(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to view a resource preview")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
2021-01-26 09:05:31 +00:00
|
|
|
def proxyify_pictures(%Metadata{} = metadata, _args, %{
|
|
|
|
definition: %{schema_node: %{name: name}}
|
|
|
|
}) do
|
|
|
|
case name do
|
|
|
|
"image_remote_url" -> {:ok, proxify_picture(metadata.image_remote_url)}
|
|
|
|
"favicon_url" -> {:ok, proxify_picture(metadata.favicon_url)}
|
|
|
|
_ -> {:error, "Unknown field"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
@spec get_eventual_parent(map()) :: Resource.t() | nil
|
|
|
|
defp get_eventual_parent(args) do
|
|
|
|
parent = args |> Map.get(:parent_id) |> get_parent_resource()
|
|
|
|
|
|
|
|
case parent do
|
|
|
|
%Resource{} -> parent
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_parent_resource(integer | nil) :: nil | Resource.t()
|
|
|
|
defp get_parent_resource(nil), do: nil
|
|
|
|
defp get_parent_resource(parent_id), do: Resources.get_resource(parent_id)
|
|
|
|
|
|
|
|
@spec check_resource_owned_by_group(Resource.t() | nil, integer) :: boolean
|
|
|
|
defp check_resource_owned_by_group(nil, _group_id), do: true
|
|
|
|
|
|
|
|
defp check_resource_owned_by_group(%Resource{actor_id: actor_id}, group_id)
|
|
|
|
when is_binary(group_id),
|
|
|
|
do: actor_id == String.to_integer(group_id)
|
|
|
|
|
|
|
|
defp check_resource_owned_by_group(%Resource{actor_id: actor_id}, group_id)
|
|
|
|
when is_number(group_id),
|
|
|
|
do: actor_id == group_id
|
2021-01-26 09:05:31 +00:00
|
|
|
|
|
|
|
@spec proxify_picture(String.t() | nil) :: String.t() | nil
|
|
|
|
defp proxify_picture(nil), do: nil
|
|
|
|
|
|
|
|
defp proxify_picture(url) do
|
|
|
|
MediaProxy.url(url)
|
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|