# to fetch the preview. However it should be fine considering
# pagination is restricted to 40 activities at a time
defp fetch_rich_media_for_activities(activities) do
- Enum.each(activities, fn activity ->
+ Enum.map(activities, fn activity ->
spawn(fn ->
Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
end)
if Pleroma.Config.get(:env) == :test do
@spec parse(String.t()) :: {:ok, map()} | {:error, any()}
- def parse(url), do: parse_url(url)
+ def parse(url), do: parse_with_timeout(url)
else
@spec parse(String.t()) :: {:ok, map()} | {:error, any()}
def parse(url) do
defp get_cached_or_parse(url) do
case @cachex.fetch(:rich_media_cache, url, fn ->
- case parse_url(url) do
+ case parse_with_timeout(url) do
{:ok, _} = res ->
{:commit, res}
end
end
+ def parse_with_timeout(url) do
+ try do
+ task =
+ Task.Supervisor.async_nolink(Pleroma.TaskSupervisor, fn ->
+ parse_url(url)
+ end)
+
+ Task.await(task, 5000)
+ catch
+ :exit, {:timeout, _} ->
+ Logger.warn("Timeout while fetching rich media for #{url}")
+ {:error, :timeout}
+ end
+ end
+
defp maybe_parse(html) do
Enum.reduce_while(parsers(), %{}, fn parser, acc ->
case parser.parse(html, acc) do
test "fake statuses' preview card is not cached", %{conn: conn} do
clear_config([:rich_media, :enabled], true)
- Tesla.Mock.mock(fn
+ Tesla.Mock.mock_global(fn
%{
method: :get,
url: "https://example.com/twitter-card"