From ac39d363eeee518cf62b33eb3ef00caea3d5daaa Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 20 Oct 2020 09:36:26 +0200 Subject: [PATCH] Fix OEmbed preview parser Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/resource.ex | 7 +++---- lib/service/rich_media/parser.ex | 7 +++++-- lib/service/rich_media/parsers/oembed_parser.ex | 8 ++++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/graphql/resolvers/resource.ex b/lib/graphql/resolvers/resource.ex index d68439f58..2ea2f7603 100644 --- a/lib/graphql/resolvers/resource.ex +++ b/lib/graphql/resolvers/resource.ex @@ -196,10 +196,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do } } = _resolution ) do - case Parser.parse(resource_url) do - {:ok, data} when is_map(data) -> - {:ok, struct(Metadata, data)} - + with {:ok, data} when is_map(data) <- Parser.parse(resource_url) do + {:ok, struct(Metadata, data)} + else {:error, _err} -> Logger.warn("Error while fetching preview from #{inspect(resource_url)}") {:error, :unknown_resource} diff --git a/lib/service/rich_media/parser.ex b/lib/service/rich_media/parser.ex index c313990f0..a65e33dac 100644 --- a/lib/service/rich_media/parser.ex +++ b/lib/service/rich_media/parser.ex @@ -189,8 +189,11 @@ defmodule Mobilizon.Service.RichMedia.Parser do defp maybe_parse(html) do Enum.reduce_while(parsers(), %{}, fn parser, acc -> case parser.parse(html, acc) do - {:ok, data} -> {:halt, data} - {:error, _msg} -> {:cont, acc} + {:ok, data} -> + {:halt, data} + + {:error, _msg} -> + {:cont, acc} end end) end diff --git a/lib/service/rich_media/parsers/oembed_parser.ex b/lib/service/rich_media/parsers/oembed_parser.ex index e1e3c19f8..dcc2d3e0b 100644 --- a/lib/service/rich_media/parsers/oembed_parser.ex +++ b/lib/service/rich_media/parsers/oembed_parser.ex @@ -21,7 +21,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do with elements = [_ | _] <- get_discovery_data(html), {:ok, oembed_url} <- get_oembed_url(elements), {:ok, oembed_data} <- get_oembed_data(oembed_url), - oembed_data <- filter_oembed_data(oembed_data) do + {:ok, oembed_data} <- filter_oembed_data(oembed_data) do Logger.debug("Data found with OEmbed parser") Logger.debug(inspect(oembed_data)) {:ok, oembed_data} @@ -55,7 +55,9 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do {:error, "No type declared for OEmbed data"} "link" -> - Map.put(data, :image_remote_url, Map.get(data, :thumbnail_url)) + data + |> Map.put(:image_remote_url, Map.get(data, :thumbnail_url)) + |> (&{:ok, &1}).() "photo" -> if Map.get(data, :url, "") == "" do @@ -65,6 +67,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do |> Map.put(:image_remote_url, Map.get(data, :url)) |> Map.put(:width, Map.get(data, :width, 0)) |> Map.put(:height, Map.get(data, :height, 0)) + |> (&{:ok, &1}).() end "video" -> @@ -75,6 +78,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do |> Map.put(:width, Map.get(data, :width, 0)) |> Map.put(:height, Map.get(data, :height, 0)) |> Map.put(:image_remote_url, Map.get(data, :thumbnail_url)) + |> (&{:ok, &1}).() "rich" -> {:error, "OEmbed data has rich type, which we don't support"}