Merge branch 'bugfix/rich_media_config' into 'develop'
RichMedia: Respect configuration on status previews See merge request pleroma/pleroma!4130
This commit is contained in:
commit
f44987bd0f
0
changelog.d/rich_media_config.skip
Normal file
0
changelog.d/rich_media_config.skip
Normal file
@ -77,19 +77,23 @@ defmodule Pleroma.Web.RichMedia.Card do
|
|||||||
|
|
||||||
@spec get_or_backfill_by_url(String.t(), map()) :: t() | nil
|
@spec get_or_backfill_by_url(String.t(), map()) :: t() | nil
|
||||||
def get_or_backfill_by_url(url, backfill_opts \\ %{}) do
|
def get_or_backfill_by_url(url, backfill_opts \\ %{}) do
|
||||||
case get_by_url(url) do
|
if @config_impl.get([:rich_media, :enabled]) do
|
||||||
%__MODULE__{} = card ->
|
case get_by_url(url) do
|
||||||
card
|
%__MODULE__{} = card ->
|
||||||
|
card
|
||||||
|
|
||||||
nil ->
|
nil ->
|
||||||
backfill_opts = Map.put(backfill_opts, :url, url)
|
backfill_opts = Map.put(backfill_opts, :url, url)
|
||||||
|
|
||||||
Backfill.start(backfill_opts)
|
Backfill.start(backfill_opts)
|
||||||
|
|
||||||
nil
|
nil
|
||||||
|
|
||||||
:error ->
|
:error ->
|
||||||
nil
|
nil
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -104,7 +108,8 @@ defmodule Pleroma.Web.RichMedia.Card do
|
|||||||
@spec get_by_activity(Activity.t()) :: t() | nil | :error
|
@spec get_by_activity(Activity.t()) :: t() | nil | :error
|
||||||
# Fake/Draft activity
|
# Fake/Draft activity
|
||||||
def get_by_activity(%Activity{id: "pleroma:fakeid"} = activity) do
|
def get_by_activity(%Activity{id: "pleroma:fakeid"} = activity) do
|
||||||
with %Object{} = object <- Object.normalize(activity, fetch: false),
|
with {_, true} <- {:config, @config_impl.get([:rich_media, :enabled])},
|
||||||
|
%Object{} = object <- Object.normalize(activity, fetch: false),
|
||||||
url when not is_nil(url) <- HTML.extract_first_external_url_from_object(object) do
|
url when not is_nil(url) <- HTML.extract_first_external_url_from_object(object) do
|
||||||
case get_by_url(url) do
|
case get_by_url(url) do
|
||||||
# Cache hit
|
# Cache hit
|
||||||
|
@ -15,10 +15,14 @@ defmodule Pleroma.Web.RichMedia.Parser do
|
|||||||
|
|
||||||
@spec parse(String.t()) :: {:ok, map()} | {:error, any()}
|
@spec parse(String.t()) :: {:ok, map()} | {:error, any()}
|
||||||
def parse(url) do
|
def parse(url) do
|
||||||
with :ok <- validate_page_url(url),
|
with {_, true} <- {:config, @config_impl.get([:rich_media, :enabled])},
|
||||||
|
:ok <- validate_page_url(url),
|
||||||
{:ok, data} <- parse_url(url) do
|
{:ok, data} <- parse_url(url) do
|
||||||
data = Map.put(data, "url", url)
|
data = Map.put(data, "url", url)
|
||||||
{:ok, data}
|
{:ok, data}
|
||||||
|
else
|
||||||
|
{:config, _} -> {:error, :rich_media_disabled}
|
||||||
|
e -> e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,6 +13,8 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
|
|||||||
mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
setup_all do: clear_config([:rich_media, :enabled], true)
|
||||||
|
|
||||||
test "returns error when no metadata present" do
|
test "returns error when no metadata present" do
|
||||||
assert {:error, _} = Parser.parse("https://example.com/empty")
|
assert {:error, _} = Parser.parse("https://example.com/empty")
|
||||||
end
|
end
|
||||||
@ -127,4 +129,10 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
|
|||||||
assert :error == Parser.parse(url)
|
assert :error == Parser.parse(url)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "returns error when disabled" do
|
||||||
|
clear_config([:rich_media, :enabled], false)
|
||||||
|
|
||||||
|
assert match?({:error, :rich_media_disabled}, Parser.parse("https://example.com/ogp"))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user