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