diff --git a/lib/graphql/resolvers/resource.ex b/lib/graphql/resolvers/resource.ex index d68439f5..16e5040b 100644 --- a/lib/graphql/resolvers/resource.ex +++ b/lib/graphql/resolvers/resource.ex @@ -11,6 +11,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do alias Mobilizon.Service.RichMedia.Parser alias Mobilizon.Storage.Page alias Mobilizon.Users.User + alias Mobilizon.Web.MediaProxy import Mobilizon.Web.Gettext require Logger @@ -210,6 +211,16 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do {:error, dgettext("errors", "You need to be logged-in to view a resource preview")} end + 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 + @spec get_eventual_parent(map()) :: Resource.t() | nil defp get_eventual_parent(args) do parent = args |> Map.get(:parent_id) |> get_parent_resource() @@ -234,4 +245,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do defp check_resource_owned_by_group(%Resource{actor_id: actor_id}, group_id) when is_number(group_id), do: actor_id == group_id + + @spec proxify_picture(String.t() | nil) :: String.t() | nil + defp proxify_picture(nil), do: nil + + defp proxify_picture(url) do + MediaProxy.url(url) + end end diff --git a/lib/graphql/schema/resource.ex b/lib/graphql/schema/resource.ex index 8fef1f42..320c3fce 100644 --- a/lib/graphql/schema/resource.ex +++ b/lib/graphql/schema/resource.ex @@ -45,7 +45,12 @@ defmodule Mobilizon.GraphQL.Schema.ResourceType do field(:type, :string, description: "The type of the resource") field(:title, :string, description: "The resource's metadata title") field(:description, :string, description: "The resource's metadata description") - field(:image_remote_url, :string, description: "The resource's metadata image") + + field(:image_remote_url, :string, + description: "The resource's metadata image", + resolve: &Resource.proxyify_pictures/3 + ) + field(:width, :integer, description: "The resource's metadata image width") field(:height, :integer, description: "The resource's metadata image height") field(:author_name, :string, description: "The resource's author name") @@ -53,7 +58,11 @@ defmodule Mobilizon.GraphQL.Schema.ResourceType do field(:provider_name, :string, description: "The resource's provider name") field(:provider_url, :string, description: "The resource's provider URL") field(:html, :string, description: "The resource's author name") - field(:favicon_url, :string, description: "The resource's favicon URL") + + field(:favicon_url, :string, + description: "The resource's favicon URL", + resolve: &Resource.proxyify_pictures/3 + ) end object :resource_queries do