2020-11-26 10:41:13 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Media do
|
2019-05-22 12:12:11 +00:00
|
|
|
@moduledoc """
|
2020-11-26 10:41:13 +00:00
|
|
|
Handles the media-related GraphQL calls
|
2019-05-22 12:12:11 +00:00
|
|
|
"""
|
2020-01-26 20:11:16 +00:00
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-09-10 09:35:32 +00:00
|
|
|
alias Mobilizon.Medias
|
2020-11-26 10:41:13 +00:00
|
|
|
alias Mobilizon.Medias.Media
|
2020-11-20 15:35:48 +00:00
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 07:53:48 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-05-22 12:12:11 +00:00
|
|
|
|
|
|
|
@doc """
|
2020-11-26 10:41:13 +00:00
|
|
|
Get media for an event
|
2020-11-06 14:43:38 +00:00
|
|
|
|
|
|
|
See Mobilizon.Web.Resolvers.Event.create_event/3
|
2019-05-22 12:12:11 +00:00
|
|
|
"""
|
2020-11-26 10:41:13 +00:00
|
|
|
def media(%{picture_id: media_id} = _parent, _args, _resolution) do
|
2021-11-26 13:30:46 +00:00
|
|
|
do_fetch_media(media_id)
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
|
2020-11-26 10:41:13 +00:00
|
|
|
def media(%{picture: media} = _parent, _args, _resolution), do: {:ok, media}
|
|
|
|
def media(_parent, %{id: media_id}, _resolution), do: do_fetch_media(media_id)
|
|
|
|
def media(_parent, _args, _resolution), do: {:ok, nil}
|
|
|
|
|
|
|
|
def medias(%{media: medias}, _args, _resolution) do
|
|
|
|
{:ok, Enum.map(medias, &transform_media/1)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec do_fetch_media(nil) :: {:error, nil}
|
|
|
|
defp do_fetch_media(nil), do: {:error, nil}
|
|
|
|
|
|
|
|
@spec do_fetch_media(String.t()) :: {:ok, Media.t()} | {:error, :not_found}
|
|
|
|
defp do_fetch_media(media_id) do
|
|
|
|
case Medias.get_media(media_id) do
|
|
|
|
%Media{} = media ->
|
|
|
|
{:ok, transform_media(media)}
|
2019-07-23 16:06:22 +00:00
|
|
|
|
2020-11-20 15:35:48 +00:00
|
|
|
nil ->
|
|
|
|
{:error, :not_found}
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-26 10:41:13 +00:00
|
|
|
@spec upload_media(map, map, map) :: {:ok, Media.t()} | {:error, any}
|
|
|
|
def upload_media(
|
2019-09-07 17:54:11 +00:00
|
|
|
_parent,
|
2020-11-19 16:06:28 +00:00
|
|
|
%{file: %Plug.Upload{} = file} = args,
|
2023-03-17 17:10:59 +00:00
|
|
|
%{context: %{current_actor: %Actor{id: default_actor_id}}}
|
2019-09-07 17:54:11 +00:00
|
|
|
) do
|
2021-09-10 09:35:32 +00:00
|
|
|
with {:ok,
|
2021-06-10 07:36:25 +00:00
|
|
|
%{
|
|
|
|
name: _name,
|
|
|
|
url: url,
|
|
|
|
content_type: content_type,
|
|
|
|
size: size
|
|
|
|
} = uploaded} <-
|
2020-01-26 20:36:50 +00:00
|
|
|
Mobilizon.Web.Upload.store(file),
|
2019-06-03 15:13:47 +00:00
|
|
|
args <-
|
|
|
|
args
|
|
|
|
|> Map.put(:url, url)
|
|
|
|
|> Map.put(:size, size)
|
|
|
|
|> Map.put(:content_type, content_type),
|
2020-11-26 10:41:13 +00:00
|
|
|
{:ok, media = %Media{}} <-
|
2021-06-10 07:36:25 +00:00
|
|
|
Medias.create_media(%{
|
|
|
|
file: args,
|
2023-03-17 17:10:59 +00:00
|
|
|
actor_id: Map.get(args, :actor_id, default_actor_id),
|
2021-06-10 07:36:25 +00:00
|
|
|
metadata: Map.take(uploaded, [:width, :height, :blurhash])
|
|
|
|
}) do
|
2020-11-26 10:41:13 +00:00
|
|
|
{:ok, transform_media(media)}
|
2019-05-22 12:12:11 +00:00
|
|
|
else
|
2020-10-09 17:29:12 +00:00
|
|
|
{:error, :mime_type_not_allowed} ->
|
|
|
|
{:error, dgettext("errors", "File doesn't have an allowed MIME type.")}
|
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
error ->
|
|
|
|
{:error, error}
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-26 10:41:13 +00:00
|
|
|
def upload_media(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
2020-11-20 15:35:48 +00:00
|
|
|
|
|
|
|
@doc """
|
2020-11-26 10:41:13 +00:00
|
|
|
Remove a media that the user owns
|
2020-11-20 15:35:48 +00:00
|
|
|
"""
|
2020-11-26 10:41:13 +00:00
|
|
|
@spec remove_media(map(), map(), map()) ::
|
|
|
|
{:ok, Media.t()}
|
2020-11-20 15:35:48 +00:00
|
|
|
| {:error, :unauthorized}
|
|
|
|
| {:error, :unauthenticated}
|
|
|
|
| {:error, :not_found}
|
2020-11-26 10:41:13 +00:00
|
|
|
def remove_media(_parent, %{id: media_id}, %{context: %{current_user: %User{} = user}}) do
|
|
|
|
with {:media, %Media{actor_id: actor_id} = media} <-
|
|
|
|
{:media, Medias.get_media(media_id)},
|
2020-11-20 15:35:48 +00:00
|
|
|
{:is_owned, %Actor{} = _actor} <- User.owns_actor(user, actor_id) do
|
2020-11-26 10:41:13 +00:00
|
|
|
Medias.delete_media(media)
|
2020-11-20 15:35:48 +00:00
|
|
|
else
|
2020-11-26 10:41:13 +00:00
|
|
|
{:media, nil} -> {:error, :not_found}
|
2020-11-20 15:35:48 +00:00
|
|
|
{:is_owned, _} -> {:error, :unauthorized}
|
2021-09-24 14:46:42 +00:00
|
|
|
{:error, :enofile} -> {:error, "File not found"}
|
2020-11-20 15:35:48 +00:00
|
|
|
end
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
2020-11-20 15:35:48 +00:00
|
|
|
|
2020-11-26 10:41:13 +00:00
|
|
|
def remove_media(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
2020-11-23 11:31:15 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Return the total media size for an actor
|
|
|
|
"""
|
|
|
|
@spec actor_size(map(), map(), map()) ::
|
|
|
|
{:ok, integer()} | {:error, :unauthorized} | {:error, :unauthenticated}
|
|
|
|
def actor_size(%Actor{id: actor_id}, _args, %{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
}) do
|
|
|
|
if can_get_actor_size?(user, actor_id) do
|
2020-11-26 10:41:13 +00:00
|
|
|
{:ok, Medias.media_size_for_actor(actor_id)}
|
2020-11-23 11:31:15 +00:00
|
|
|
else
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def actor_size(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Return the total media size for a local user
|
|
|
|
"""
|
|
|
|
@spec user_size(map(), map(), map()) ::
|
|
|
|
{:ok, integer()} | {:error, :unauthorized} | {:error, :unauthenticated}
|
|
|
|
def user_size(%User{id: user_id}, _args, %{
|
|
|
|
context: %{current_user: %User{} = logged_user}
|
|
|
|
}) do
|
|
|
|
if can_get_user_size?(logged_user, user_id) do
|
2020-11-26 10:41:13 +00:00
|
|
|
{:ok, Medias.media_size_for_user(user_id)}
|
2020-11-23 11:31:15 +00:00
|
|
|
else
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_size(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
|
|
|
|
2020-11-26 10:41:13 +00:00
|
|
|
@spec transform_media(Media.t()) :: map()
|
2021-06-10 07:36:25 +00:00
|
|
|
defp transform_media(%Media{id: id, file: file, metadata: metadata}) do
|
2020-11-26 10:41:13 +00:00
|
|
|
%{
|
|
|
|
name: file.name,
|
|
|
|
url: file.url,
|
|
|
|
id: id,
|
|
|
|
content_type: file.content_type,
|
2021-06-10 07:36:25 +00:00
|
|
|
size: file.size,
|
|
|
|
metadata: metadata
|
2020-11-26 10:41:13 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-11-23 11:31:15 +00:00
|
|
|
@spec can_get_user_size?(User.t(), integer()) :: boolean()
|
|
|
|
defp can_get_actor_size?(%User{role: role} = user, actor_id) do
|
|
|
|
role in [:moderator, :administrator] || owns_actor?(User.owns_actor(user, actor_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec owns_actor?({:is_owned, Actor.t() | nil}) :: boolean()
|
|
|
|
defp owns_actor?({:is_owned, %Actor{} = _actor}), do: true
|
|
|
|
defp owns_actor?({:is_owned, _}), do: false
|
|
|
|
|
|
|
|
@spec can_get_user_size?(User.t(), integer()) :: boolean()
|
|
|
|
defp can_get_user_size?(%User{role: role, id: logged_user_id}, user_id) do
|
|
|
|
user_id == logged_user_id || role in [:moderator, :administrator]
|
|
|
|
end
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|