Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
diff --git a/changelog.d/rich-media-oembed-json.fix b/changelog.d/rich-media-oembed-json.fix
new file mode 100644
index 000000000..2a8b68792
--- /dev/null
+++ b/changelog.d/rich-media-oembed-json.fix
@@ -0,0 +1 @@
+Rich media: accept JSON responses when fetching oEmbed data
diff --git a/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex
index e58db38aa..748af7e8a 100644
--- a/lib/pleroma/web/rich_media/helpers.ex
+++ b/lib/pleroma/web/rich_media/helpers.ex
@@ -54,21 +54,30 @@ defmodule Pleroma.Web.RichMedia.Helpers do
|> hd
end
- @spec rich_media_get(String.t()) :: {:ok, String.t()} | get_errors()
- def rich_media_get(url) do
+ @html_types [{"text", "html"}]
+ @json_types [{"application", "json"}, {"application", "json+oembed"}, {"text", "json"}]
+
+ @doc """
+ Fetches a page for rich media. Pass `accept: :json` to fetch an oEmbed
+ JSON document instead of an HTML page.
+ """
+ @spec rich_media_get(String.t(), keyword()) :: {:ok, String.t()} | get_errors()
+ def rich_media_get(url, opts \\ []) do
+ types = if opts[:accept] == :json, do: @json_types, else: @html_types
+
case Pleroma.HTTP.AdapterHelper.can_stream?() do
- true -> stream(url)
- false -> head_first(url)
+ true -> stream(url, types)
+ false -> head_first(url, types)
end
|> handle_result(url)
end
- defp stream(url) do
+ defp stream(url, types) do
case Pleroma.HTTP.get(url, req_headers(), http_options()) do
{:ok, %Tesla.Env{status: 200, body: stream_body, headers: headers}} ->
result =
try do
- with {_, :ok} <- {:content_type, check_content_type(headers)},
+ with {_, :ok} <- {:content_type, check_content_type(headers, types)},
{_, :ok} <- {:content_length, check_content_length(headers)},
{:read_stream, {:ok, body}} <- {:read_stream, read_stream(stream_body)} do
{:ok, body}
@@ -91,10 +100,10 @@ defmodule Pleroma.Web.RichMedia.Helpers do
end
end
- defp head_first(url) do
+ defp head_first(url, types) do
with {_, {:ok, %Tesla.Env{status: 200, headers: headers}}} <-
{:head, Pleroma.HTTP.head(url, req_headers(), http_options())},
- {_, :ok} <- {:content_type, check_content_type(headers)},
+ {_, :ok} <- {:content_type, check_content_type(headers, types)},
{_, :ok} <- {:content_length, check_content_length(headers)},
{_, {:ok, %Tesla.Env{status: 200, body: body}}} <-
{:get, Pleroma.HTTP.get(url, req_headers(), http_options())} do
@@ -129,11 +138,13 @@ defmodule Pleroma.Web.RichMedia.Helpers do
end
end
- defp check_content_type(headers) do
+ defp check_content_type(headers, types) do
case List.keyfind(headers, "content-type", 0) do
{_, content_type} ->
- case Plug.Conn.Utils.media_type(content_type) do
- {:ok, "text", "html", _} -> :ok
+ with {:ok, type, subtype, _} <- Plug.Conn.Utils.media_type(content_type),
+ true <- {type, subtype} in types do
+ :ok
+ else
_ -> {:error, content_type}
end
diff --git a/lib/pleroma/web/rich_media/parsers/o_embed.ex b/lib/pleroma/web/rich_media/parsers/o_embed.ex
index a89c492f3..3a8e6c80e 100644
--- a/lib/pleroma/web/rich_media/parsers/o_embed.ex
+++ b/lib/pleroma/web/rich_media/parsers/o_embed.ex
@@ -23,7 +23,7 @@ defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
end
defp get_oembed_data(url) do
- with {:ok, json} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url) do
+ with {:ok, json} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url, accept: :json) do
Jason.decode(json)
end
end
diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex
index e080da621..93786973c 100644
--- a/test/support/http_request_mock.ex
+++ b/test/support/http_request_mock.ex
@@ -1507,7 +1507,12 @@ defmodule HttpRequestMock do
end
def get("https://example.com/oembed.json", _, _, _) do
- {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.json")}}
+ {:ok,
+ %Tesla.Env{
+ status: 200,
+ headers: [{"content-type", "application/json; charset=utf-8"}],
+ body: File.read!("test/fixtures/rich_media/oembed.json")
+ }}
end
def get("https://example.com/twitter-card", _, _, _) do
@@ -1772,7 +1777,6 @@ defmodule HttpRequestMock do
"https://example.com/malformed",
"https://example.com/non-ogp",
"https://example.com/oembed",
- "https://example.com/oembed.json",
"https://example.com/oembed-internal",
"http://127.0.0.1:9200/internal.json",
"https://example.com/ogp",
@@ -1789,6 +1793,10 @@ defmodule HttpRequestMock do
{:ok, %Tesla.Env{status: 200, body: ""}}
end
+ def head("https://example.com/oembed.json", _, _, _) do
+ {:ok, %Tesla.Env{status: 200, headers: [{"content-type", "application/json; charset=utf-8"}]}}
+ end
+
def head("https://example.com/pdf-file", _, _, _) do
{:ok,
%Tesla.Env{

File Metadata

Mime Type
text/x-diff
Expires
Tue, Nov 17, 8:54 PM (15 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1839804
Default Alt Text
(4 KB)

Event Timeline