1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy do
6 @moduledoc "Preloads any attachments in the MediaProxy cache by prefetching them"
7 @behaviour Pleroma.Web.ActivityPub.MRF
10 alias Pleroma.Web.MediaProxy
19 def perform(:prefetch, url) do
20 Logger.info("Prefetching #{inspect(url)}")
24 |> HTTP.get([], adapter: @hackney_options)
27 def perform(:preload, %{"object" => %{"attachment" => attachments}} = _message) do
28 Enum.each(attachments, fn
29 %{"url" => url} when is_list(url) ->
33 PleromaJobQueue.enqueue(:background, __MODULE__, [:prefetch, href])
36 Logger.debug("Unhandled attachment URL object #{inspect(x)}")
40 Logger.debug("Unhandled attachment #{inspect(x)}")
46 %{"type" => "Create", "object" => %{"attachment" => attachments} = _object} = message
48 when is_list(attachments) and length(attachments) > 0 do
49 PleromaJobQueue.enqueue(:background, __MODULE__, [:preload, message])
55 def filter(message), do: {:ok, message}
58 def describe, do: {:ok, %{}}