MRF: add mediaproxy warming policy
authorWilliam Pitcock <nenolod@dereferenced.org>
Thu, 27 Jun 2019 03:06:58 +0000 (03:06 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Fri, 28 Jun 2019 23:19:20 +0000 (23:19 +0000)
lib/pleroma/web/activity_pub/mrf/mediaproxy_warming_policy.ex [new file with mode: 0644]

diff --git a/lib/pleroma/web/activity_pub/mrf/mediaproxy_warming_policy.ex b/lib/pleroma/web/activity_pub/mrf/mediaproxy_warming_policy.ex
new file mode 100644 (file)
index 0000000..01d21a2
--- /dev/null
@@ -0,0 +1,56 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy do
+  @moduledoc "Preloads any attachments in the MediaProxy cache by prefetching them"
+  @behaviour Pleroma.Web.ActivityPub.MRF
+
+  alias Pleroma.HTTP
+  alias Pleroma.Web.MediaProxy
+
+  require Logger
+
+  @hackney_options [
+    pool: :media,
+    recv_timeout: 10_000
+  ]
+
+  def perform(:prefetch, url) do
+    Logger.info("Prefetching #{inspect(url)}")
+
+    url
+    |> MediaProxy.url()
+    |> HTTP.get([], adapter: @hackney_options)
+  end
+
+  def perform(:preload, %{"object" => %{"attachment" => attachments}} = _message) do
+    Enum.each(attachments, fn
+      %{"url" => url} when is_list(url) ->
+        url
+        |> Enum.each(fn
+          %{"href" => href} ->
+            PleromaJobQueue.enqueue(:background, __MODULE__, [:prefetch, href])
+
+          x ->
+            Logger.debug("Unhandled attachment URL object #{inspect(x)}")
+        end)
+
+      x ->
+        Logger.debug("Unhandled attachment #{inspect(x)}")
+    end)
+  end
+
+  @impl true
+  def filter(
+        %{"type" => "Create", "object" => %{"attachment" => attachments} = _object} = message
+      )
+      when is_list(attachments) and length(attachments) > 0 do
+    PleromaJobQueue.enqueue(:background, __MODULE__, [:preload, message])
+
+    {:ok, message}
+  end
+
+  @impl true
+  def filter(message), do: {:ok, message}
+end