72455afd0a5f518267616a6da965adf42308ee6a
[akkoma] / lib / pleroma / web / activity_pub / mrf / media_proxy_warming_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
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.Policy
8
9 alias Pleroma.HTTP
10 alias Pleroma.Web.MediaProxy
11
12 require Logger
13
14 @adapter_options [
15 recv_timeout: 10_000
16 ]
17
18 @impl true
19 def history_awareness, do: :auto
20
21 defp prefetch(url) do
22 # Fetching only proxiable resources
23 if MediaProxy.enabled?() and MediaProxy.url_proxiable?(url) do
24 # If preview proxy is enabled, it'll also hit media proxy (so we're caching both requests)
25 prefetch_url = MediaProxy.preview_url(url)
26
27 Logger.debug("Prefetching #{inspect(url)} as #{inspect(prefetch_url)}")
28
29 if Pleroma.Config.get(:env) == :test do
30 fetch(prefetch_url)
31 else
32 ConcurrentLimiter.limit(__MODULE__, fn ->
33 Task.start(fn -> fetch(prefetch_url) end)
34 end)
35 end
36 end
37 end
38
39 defp fetch(url), do: HTTP.get(url, [], @adapter_options)
40
41 defp preload(%{"object" => %{"attachment" => attachments}} = _message) do
42 Enum.each(attachments, fn
43 %{"url" => url} when is_list(url) ->
44 url
45 |> Enum.each(fn
46 %{"href" => href} ->
47 prefetch(href)
48
49 x ->
50 Logger.debug("Unhandled attachment URL object #{inspect(x)}")
51 end)
52
53 x ->
54 Logger.debug("Unhandled attachment #{inspect(x)}")
55 end)
56 end
57
58 @impl true
59 def filter(%{"type" => type, "object" => %{"attachment" => attachments} = _object} = message)
60 when type in ["Create", "Update"] and is_list(attachments) and length(attachments) > 0 do
61 preload(message)
62
63 {:ok, message}
64 end
65
66 @impl true
67 def filter(message), do: {:ok, message}
68
69 @impl true
70 def describe, do: {:ok, %{}}
71 end