From 0ac12a0a29d859b997ef194a8316362afa085d2f Mon Sep 17 00:00:00 2001
From: Thomas Citharel <tcit@tcit.fr>
Date: Tue, 26 Jan 2021 10:05:31 +0100
Subject: [PATCH] Proxify resource metadata pictures

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
---
 lib/graphql/resolvers/resource.ex | 18 ++++++++++++++++++
 lib/graphql/schema/resource.ex    | 13 +++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/lib/graphql/resolvers/resource.ex b/lib/graphql/resolvers/resource.ex
index d68439f58..16e5040b7 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 8fef1f42e..320c3fce5 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