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
11 alias Pleroma.Workers.BackgroundWorker
20 def perform(:prefetch, url) do
21 Logger.debug("Prefetching #{inspect(url)}")
25 |> HTTP.get([], adapter: @hackney_options)
28 def perform(:preload, %{"object" => %{"attachment" => attachments}} = _message) do
29 Enum.each(attachments, fn
30 %{"url" => url} when is_list(url) ->
34 BackgroundWorker.enqueue("media_proxy_prefetch", %{"url" => href})
37 Logger.debug("Unhandled attachment URL object #{inspect(x)}")
41 Logger.debug("Unhandled attachment #{inspect(x)}")
47 %{"type" => "Create", "object" => %{"attachment" => attachments} = _object} = message
49 when is_list(attachments) and length(attachments) > 0 do
50 BackgroundWorker.enqueue("media_proxy_preload", %{"message" => message})
56 def filter(message), do: {:ok, message}
59 def describe, do: {:ok, %{}}