Proxify resource metadata pictures

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-01-26 10:05:31 +01:00
parent a8e58547fd
commit 0ac12a0a29
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
2 changed files with 29 additions and 2 deletions

View file

@ -11,6 +11,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
alias Mobilizon.Service.RichMedia.Parser alias Mobilizon.Service.RichMedia.Parser
alias Mobilizon.Storage.Page alias Mobilizon.Storage.Page
alias Mobilizon.Users.User alias Mobilizon.Users.User
alias Mobilizon.Web.MediaProxy
import Mobilizon.Web.Gettext import Mobilizon.Web.Gettext
require Logger 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")} {:error, dgettext("errors", "You need to be logged-in to view a resource preview")}
end 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 @spec get_eventual_parent(map()) :: Resource.t() | nil
defp get_eventual_parent(args) do defp get_eventual_parent(args) do
parent = args |> Map.get(:parent_id) |> get_parent_resource() 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) defp check_resource_owned_by_group(%Resource{actor_id: actor_id}, group_id)
when is_number(group_id), when is_number(group_id),
do: actor_id == 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 end

View file

@ -45,7 +45,12 @@ defmodule Mobilizon.GraphQL.Schema.ResourceType do
field(:type, :string, description: "The type of the resource") field(:type, :string, description: "The type of the resource")
field(:title, :string, description: "The resource's metadata title") field(:title, :string, description: "The resource's metadata title")
field(:description, :string, description: "The resource's metadata description") 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(:width, :integer, description: "The resource's metadata image width")
field(:height, :integer, description: "The resource's metadata image height") field(:height, :integer, description: "The resource's metadata image height")
field(:author_name, :string, description: "The resource's author name") 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_name, :string, description: "The resource's provider name")
field(:provider_url, :string, description: "The resource's provider URL") field(:provider_url, :string, description: "The resource's provider URL")
field(:html, :string, description: "The resource's author name") 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 end
object :resource_queries do object :resource_queries do