mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-23 06:58:58 +00:00
Add RichMediaController and tests
This commit is contained in:
parent
917d48d09b
commit
48e81d3d40
|
@ -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
|
|
@ -1,3 +0,0 @@
|
|||
defmodule Pleroma.Web.RichMedia.Data do
|
||||
defstruct [:title, :type, :image, :url, :description]
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
|
@ -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,
|
||||
{: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
|
||||
|
|
Loading…
Reference in a new issue