2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Picture do
|
2019-05-22 12:12:11 +00:00
|
|
|
@moduledoc """
|
|
|
|
Handles the picture-related GraphQL calls
|
|
|
|
"""
|
2020-01-26 20:11:16 +00:00
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-05-22 12:12:11 +00:00
|
|
|
alias Mobilizon.Media
|
|
|
|
alias Mobilizon.Media.Picture
|
2019-05-31 15:58:03 +00:00
|
|
|
alias Mobilizon.Users.User
|
2019-05-22 12:12:11 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Get picture for an event's pic
|
|
|
|
"""
|
|
|
|
def picture(%{picture_id: picture_id} = _parent, _args, _resolution) do
|
2019-09-07 17:54:11 +00:00
|
|
|
with {:ok, picture} <- do_fetch_picture(picture_id), do: {:ok, picture}
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Get picture for an event that has an attached
|
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
See Mobilizon.Web.Resolvers.Event.create_event/3
|
2019-05-22 12:12:11 +00:00
|
|
|
"""
|
2019-09-07 17:54:11 +00:00
|
|
|
def picture(%{picture: picture} = _parent, _args, _resolution), do: {:ok, picture}
|
2019-05-22 12:12:11 +00:00
|
|
|
def picture(_parent, %{id: picture_id}, _resolution), do: do_fetch_picture(picture_id)
|
2019-09-07 17:54:11 +00:00
|
|
|
def picture(_parent, _args, _resolution), do: {:ok, nil}
|
2019-05-22 12:12:11 +00:00
|
|
|
|
|
|
|
@spec do_fetch_picture(nil) :: {:error, nil}
|
|
|
|
defp do_fetch_picture(nil), do: {:error, nil}
|
|
|
|
|
|
|
|
@spec do_fetch_picture(String.t()) :: {:ok, Picture.t()} | {:error, :not_found}
|
|
|
|
defp do_fetch_picture(picture_id) do
|
2019-07-23 16:06:22 +00:00
|
|
|
case Media.get_picture(picture_id) do
|
2019-09-07 17:54:11 +00:00
|
|
|
%Picture{id: id, file: file} ->
|
2019-07-23 16:06:22 +00:00
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
name: file.name,
|
|
|
|
url: file.url,
|
|
|
|
id: id,
|
|
|
|
content_type: file.content_type,
|
|
|
|
size: file.size
|
|
|
|
}}
|
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
_error ->
|
2019-05-22 12:12:11 +00:00
|
|
|
{:error, "Picture with ID #{picture_id} was not found"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 19:34:25 +00:00
|
|
|
@spec upload_picture(map, map, map) :: {:ok, Picture.t()} | {:error, any}
|
2019-09-07 17:54:11 +00:00
|
|
|
def upload_picture(
|
|
|
|
_parent,
|
|
|
|
%{file: %Plug.Upload{} = file, actor_id: actor_id} = args,
|
|
|
|
%{context: %{current_user: user}}
|
|
|
|
) do
|
|
|
|
with {:is_owned, %Actor{}} <- User.owns_actor(user, actor_id),
|
2019-10-25 15:43:37 +00:00
|
|
|
{:ok, %{name: _name, url: url, content_type: content_type, size: size}} <-
|
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),
|
2019-05-31 15:58:03 +00:00
|
|
|
{:ok, picture = %Picture{}} <-
|
|
|
|
Media.create_picture(%{"file" => args, "actor_id" => actor_id}) do
|
2019-06-03 15:13:47 +00:00
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
name: picture.file.name,
|
|
|
|
url: picture.file.url,
|
|
|
|
id: picture.id,
|
|
|
|
content_type: picture.file.content_type,
|
|
|
|
size: picture.file.size
|
|
|
|
}}
|
2019-05-22 12:12:11 +00:00
|
|
|
else
|
2019-09-07 17:54:11 +00:00
|
|
|
{:is_owned, nil} ->
|
2019-05-31 15:58:03 +00:00
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
error ->
|
|
|
|
{:error, error}
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_picture(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to login to upload a picture"}
|
|
|
|
end
|
|
|
|
end
|