Improve <title> fallback; Add a test
authorEgor Kislitsyn <egor@kislitsyn.com>
Thu, 13 Jun 2019 08:02:46 +0000 (15:02 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Thu, 13 Jun 2019 08:02:46 +0000 (15:02 +0700)
lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex
test/fixtures/rich_media/ogp-missing-title.html [new file with mode: 0644]
test/web/rich_media/parser_test.exs

index 7da4e7561ca54bdc8b54da1bf7f4eecdd93cffa1..82f1cce299e971642e0bb547419c5ba913646219 100644 (file)
@@ -1,17 +1,19 @@
 defmodule Pleroma.Web.RichMedia.Parsers.MetaTagsParser do
   def parse(html, data, prefix, error_message, key_name, value_name \\ "content") do
-    with elements = [_ | _] <- get_elements(html, key_name, prefix),
-         page_title = get_page_title(html),
-         meta_data =
-           Enum.reduce(elements, data, fn el, acc ->
-             attributes = normalize_attributes(el, prefix, key_name, value_name)
-
-             Map.merge(acc, attributes)
-           end)
-           |> Map.put_new(:title, page_title) do
-      {:ok, meta_data}
+    meta_data =
+      html
+      |> get_elements(key_name, prefix)
+      |> Enum.reduce(data, fn el, acc ->
+        attributes = normalize_attributes(el, prefix, key_name, value_name)
+
+        Map.merge(acc, attributes)
+      end)
+      |> maybe_put_title(html)
+
+    if Enum.empty?(meta_data) do
+      {:error, error_message}
     else
-      _e -> {:error, error_message}
+      {:ok, meta_data}
     end
   end
 
@@ -30,6 +32,15 @@ defmodule Pleroma.Web.RichMedia.Parsers.MetaTagsParser do
     %{String.to_atom(data[key_name]) => data[value_name]}
   end
 
+  defp maybe_put_title(%{title: _} = meta, _), do: meta
+
+  defp maybe_put_title(meta, html) do
+    case get_page_title(html) do
+      "" -> meta
+      title -> Map.put_new(meta, :title, title)
+    end
+  end
+
   defp get_page_title(html) do
     Floki.find(html, "title") |> Floki.text()
   end
diff --git a/test/fixtures/rich_media/ogp-missing-title.html b/test/fixtures/rich_media/ogp-missing-title.html
new file mode 100644 (file)
index 0000000..fcdbedf
--- /dev/null
@@ -0,0 +1,12 @@
+<html prefix="og: http://ogp.me/ns#">
+
+<head>
+  <title>The Rock (1996)</title>
+  <meta property="og:type" content="video.movie" />
+  <meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
+  <meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
+  <meta property="og:description"
+    content="Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.">
+</head>
+
+</html>
index 3a9cc1854935c3f375a64f0dc07273538e3fe72b..a49ba95497c71d8ba62195314d7a0344a29b999b 100644 (file)
@@ -9,6 +9,15 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
       } ->
         %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
 
+      %{
+        method: :get,
+        url: "http://example.com/ogp-missing-title"
+      } ->
+        %Tesla.Env{
+          status: 200,
+          body: File.read!("test/fixtures/rich_media/ogp-missing-title.html")
+        }
+
       %{
         method: :get,
         url: "http://example.com/twitter-card"
@@ -51,6 +60,19 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
               }}
   end
 
+  test "falls back to <title> when ogp:title is missing" do
+    assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp-missing-title") ==
+             {:ok,
+              %{
+                image: "http://ia.media-imdb.com/images/rock.jpg",
+                title: "The Rock (1996)",
+                description:
+                  "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
+                type: "video.movie",
+                url: "http://www.imdb.com/title/tt0117500/"
+              }}
+  end
+
   test "parses twitter card" do
     assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/twitter-card") ==
              {:ok,