tests: fix up nodeinfo tests
[akkoma] / lib / pleroma / web / activity_pub / mrf / mediaproxy_warming_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 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
8
9 alias Pleroma.HTTP
10 alias Pleroma.Web.MediaProxy
11
12 require Logger
13
14 @hackney_options [
15 pool: :media,
16 recv_timeout: 10_000
17 ]
18
19 def perform(:prefetch, url) do
20 Logger.info("Prefetching #{inspect(url)}")
21
22 url
23 |> MediaProxy.url()
24 |> HTTP.get([], adapter: @hackney_options)
25 end
26
27 def perform(:preload, %{"object" => %{"attachment" => attachments}} = _message) do
28 Enum.each(attachments, fn
29 %{"url" => url} when is_list(url) ->
30 url
31 |> Enum.each(fn
32 %{"href" => href} ->
33 PleromaJobQueue.enqueue(:background, __MODULE__, [:prefetch, href])
34
35 x ->
36 Logger.debug("Unhandled attachment URL object #{inspect(x)}")
37 end)
38
39 x ->
40 Logger.debug("Unhandled attachment #{inspect(x)}")
41 end)
42 end
43
44 @impl true
45 def filter(
46 %{"type" => "Create", "object" => %{"attachment" => attachments} = _object} = message
47 )
48 when is_list(attachments) and length(attachments) > 0 do
49 PleromaJobQueue.enqueue(:background, __MODULE__, [:preload, message])
50
51 {:ok, message}
52 end
53
54 @impl true
55 def filter(message), do: {:ok, message}
56
57 @impl true
58 def describe(), do: {:ok, %{}}
59 end