diff options
-rw-r--r-- | lib/pleroma/web/rich_media/parser/card.ex | 17 | ||||
-rw-r--r-- | test/pleroma/web/mastodon_api/controllers/status_controller_test.exs | 31 | ||||
-rw-r--r-- | test/pleroma/web/rich_media/parser/card_test.exs | 10 |
3 files changed, 33 insertions, 25 deletions
diff --git a/lib/pleroma/web/rich_media/parser/card.ex b/lib/pleroma/web/rich_media/parser/card.ex index f6193728a..fc0e3f6a4 100644 --- a/lib/pleroma/web/rich_media/parser/card.ex +++ b/lib/pleroma/web/rich_media/parser/card.ex @@ -24,8 +24,10 @@ defmodule Pleroma.Web.RichMedia.Parser.Card do embed_url: "", blurhash: nil - def parse(%{url: url, oembed: %{"type" => type, "title" => title} = oembed} = embed) - when type in @types do + def parse(%Embed{url: url, oembed: %{"type" => type, "title" => title} = oembed} = embed) + when type in @types and is_binary(url) do + uri = URI.parse(url) + %Card{ url: url, title: title, @@ -33,8 +35,8 @@ defmodule Pleroma.Web.RichMedia.Parser.Card do type: oembed["type"], author_name: oembed["author_name"], author_url: oembed["author_url"], - provider_name: oembed["provider_name"] || URI.parse(url).host, - provider_url: oembed["provider_url"], + provider_name: oembed["provider_name"] || uri.host, + provider_url: oembed["provider_url"] || "#{uri.scheme}://#{uri.host}", html: oembed["html"], width: oembed["width"], height: oembed["height"], @@ -44,13 +46,16 @@ defmodule Pleroma.Web.RichMedia.Parser.Card do |> validate() end - def parse(%{url: url} = embed) do + def parse(%Embed{url: url} = embed) when is_binary(url) do + uri = URI.parse(url) + %Card{ url: url, title: get_title(embed), description: get_description(embed), type: "link", - provider_name: URI.parse(url).host, + provider_name: uri.host, + provider_url: "#{uri.scheme}://#{uri.host}", image: get_image(embed) |> proxy() } |> validate() diff --git a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs index e76c2760d..d200cdf72 100644 --- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs @@ -1331,16 +1331,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do "url" => "https://example.com/ogp", "description" => "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.", - "pleroma" => %{ - "opengraph" => %{ - "image" => "http://ia.media-imdb.com/images/rock.jpg", - "title" => "The Rock", - "type" => "video.movie", - "url" => "https://example.com/ogp", - "description" => - "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer." - } - } + "author_name" => "", + "author_url" => "", + "blurhash" => nil, + "embed_url" => "", + "height" => 0, + "html" => "", + "width" => 0 } response = @@ -1380,13 +1377,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do "provider_name" => "example.com", "provider_url" => "https://example.com", "url" => "https://example.com/ogp-missing-data", - "pleroma" => %{ - "opengraph" => %{ - "title" => "Pleroma", - "type" => "website", - "url" => "https://example.com/ogp-missing-data" - } - } + "author_name" => "", + "author_url" => "", + "blurhash" => nil, + "embed_url" => "", + "height" => 0, + "html" => "", + "width" => 0 } end end diff --git a/test/pleroma/web/rich_media/parser/card_test.exs b/test/pleroma/web/rich_media/parser/card_test.exs index 489d2d848..e09dfa6db 100644 --- a/test/pleroma/web/rich_media/parser/card_test.exs +++ b/test/pleroma/web/rich_media/parser/card_test.exs @@ -10,10 +10,13 @@ defmodule Pleroma.Web.RichMedia.Parser.CardTest do describe "parse/1" do test "converts an %Embed{} into a %Card{}" do + url = + "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html" + embed = File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html") |> Floki.parse_document!() - |> TwitterCard.parse(%Embed{}) + |> TwitterCard.parse(%Embed{url: url}) expected = %Card{ description: @@ -21,7 +24,10 @@ defmodule Pleroma.Web.RichMedia.Parser.CardTest do image: "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-videoSixteenByNineJumbo1600.jpg", title: "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.", - type: "link" + type: "link", + provider_name: "www.nytimes.com", + provider_url: "https://www.nytimes.com", + url: url } assert Card.parse(embed) == {:ok, expected} |