Add RichMediaController and tests
authorMaxim Filippov <colixer@gmail.com>
Wed, 2 Jan 2019 14:02:50 +0000 (17:02 +0300)
committerMaxim Filippov <colixer@gmail.com>
Wed, 2 Jan 2019 14:02:50 +0000 (17:02 +0300)
lib/pleroma/web/rich_media/controllers/rich_media_controller.ex [new file with mode: 0644]
lib/pleroma/web/rich_media/data.ex [deleted file]
lib/pleroma/web/rich_media/parser.ex
lib/pleroma/web/router.ex
test/web/rich_media/controllers/rich_media_controller_test.exs [new file with mode: 0644]
test/web/rich_media/parser_test.exs

diff --git a/lib/pleroma/web/rich_media/controllers/rich_media_controller.ex b/lib/pleroma/web/rich_media/controllers/rich_media_controller.ex
new file mode 100644 (file)
index 0000000..9101996
--- /dev/null
@@ -0,0 +1,17 @@
+defmodule Pleroma.Web.RichMedia.RichMediaController do
+  use Pleroma.Web, :controller
+
+  import Pleroma.Web.ControllerHelper, only: [json_response: 3]
+
+  def parse(conn, %{"url" => url}) do
+    case Pleroma.Web.RichMedia.Parser.parse(url) do
+      {:ok, data} ->
+        conn
+        |> json_response(200, data)
+
+      {:error, msg} ->
+        conn
+        |> json_response(404, msg)
+    end
+  end
+end
diff --git a/lib/pleroma/web/rich_media/data.ex b/lib/pleroma/web/rich_media/data.ex
deleted file mode 100644 (file)
index 403d1d3..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-defmodule Pleroma.Web.RichMedia.Data do
-  defstruct [:title, :type, :image, :url, :description]
-end
index d9c1684d58449a80771c2a3f8282d781955167f3..477a381964b0a6f6da1dc776af119a97997f4a72 100644 (file)
@@ -4,11 +4,23 @@ defmodule Pleroma.Web.RichMedia.Parser do
   def parse(url) do
     {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
 
-    Enum.reduce_while(@parsers, %Pleroma.Web.RichMedia.Data{}, fn parser, acc ->
+    html |> maybe_parse() |> get_parsed_data()
+  end
+
+  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}
       end
     end)
   end
+
+  defp get_parsed_data(data) when data == %{} do
+    {:error, "No metadata found"}
+  end
+
+  defp get_parsed_data(data) do
+    {:ok, data}
+  end
 end
index 1f929ee21f1593d3f4ebf1422cb8a77b5db5d1c0..8df45bf4da2344053a835375019b580529ed05e9 100644 (file)
@@ -232,6 +232,12 @@ defmodule Pleroma.Web.Router do
     put("/settings", MastodonAPIController, :put_settings)
   end
 
+  scope "/api", Pleroma.Web.RichMedia do
+    pipe_through(:authenticated_api)
+
+    get("/rich_media/parse", RichMediaController, :parse)
+  end
+
   scope "/api/v1", Pleroma.Web.MastodonAPI do
     pipe_through(:api)
     get("/instance", MastodonAPIController, :masto_instance)
diff --git a/test/web/rich_media/controllers/rich_media_controller_test.exs b/test/web/rich_media/controllers/rich_media_controller_test.exs
new file mode 100644 (file)
index 0000000..37c8263
--- /dev/null
@@ -0,0 +1,54 @@
+defmodule Pleroma.Web.RichMedia.RichMediaControllerTest do
+  use Pleroma.Web.ConnCase
+  import Pleroma.Factory
+
+  setup do
+    Tesla.Mock.mock(fn
+      %{
+        method: :get,
+        url: "http://example.com/ogp"
+      } ->
+        %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
+
+      %{method: :get, url: "http://example.com/empty"} ->
+        %Tesla.Env{status: 200, body: "hello"}
+    end)
+
+    :ok
+  end
+
+  describe "GET /api/rich_media/parse" do
+    setup do
+      user = insert(:user)
+
+      [user: user]
+    end
+
+    test "returns 404 if not metadata found", %{user: user} do
+      build_conn()
+      |> with_credentials(user.nickname, "test")
+      |> get("/api/rich_media/parse", %{"url" => "http://example.com/empty"})
+      |> json_response(404)
+    end
+
+    test "returns OGP metadata", %{user: user} do
+      response =
+        build_conn()
+        |> with_credentials(user.nickname, "test")
+        |> get("/api/rich_media/parse", %{"url" => "http://example.com/ogp"})
+        |> json_response(200)
+
+      assert response == %{
+               "image" => "http://ia.media-imdb.com/images/rock.jpg",
+               "title" => "The Rock",
+               "type" => "video.movie",
+               "url" => "http://www.imdb.com/title/tt0117500/"
+             }
+    end
+  end
+
+  defp with_credentials(conn, username, password) do
+    header_content = "Basic " <> Base.encode64("#{username}:#{password}")
+    put_req_header(conn, "authorization", header_content)
+  end
+end
index bb0d663e9e576e0865cdc15aafda62d2bd5979f5..caf81e9fa44b6ced32ed0bbb69c1e0513e5ad99e 100644 (file)
@@ -8,19 +8,26 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
         url: "http://example.com/ogp"
       } ->
         %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
+
+      %{method: :get, url: "http://example.com/empty"} ->
+        %Tesla.Env{status: 200, body: "hello"}
     end)
 
     :ok
   end
 
+  test "returns error when no metadata present" do
+    assert {:error, _} = Pleroma.Web.RichMedia.Parser.parse("http://example.com/empty")
+  end
+
   test "parses ogp" do
     assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp") ==
-             %Pleroma.Web.RichMedia.Data{
-               description: nil,
-               image: "http://ia.media-imdb.com/images/rock.jpg",
-               title: "The Rock",
-               type: "video.movie",
-               url: "http://www.imdb.com/title/tt0117500/"
-             }
+             {:ok,
+              %{
+                image: "http://ia.media-imdb.com/images/rock.jpg",
+                title: "The Rock",
+                type: "video.movie",
+                url: "http://www.imdb.com/title/tt0117500/"
+              }}
   end
 end