Use proper workers for fetching pins instead of an ad-hoc task

BUG: https://git.pleroma.social/pleroma/pleroma/-/issues/3276
This commit is contained in:
Floatingghost 2024-05-31 09:04:00 -04:00 committed by tusooa
parent ff6f5a417f
commit 0302431888
No known key found for this signature in database
GPG Key ID: ED5657F8FEDDA66C
3 changed files with 38 additions and 18 deletions

View File

@ -0,0 +1 @@
Use proper workers for fetching pins instead of an ad-hoc task, fixing a potential fetch loop

View File

@ -1794,24 +1794,25 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
end end
def pinned_fetch_task(nil), do: nil def enqueue_pin_fetches(%{pinned_objects: pins}) do
# enqueue a task to fetch all pinned objects
def pinned_fetch_task(%{pinned_objects: pins}) do Enum.each(pins, fn {ap_id, _} ->
if Enum.all?(pins, fn {ap_id, _} -> if is_nil(Object.get_cached_by_ap_id(ap_id)) do
Object.get_cached_by_ap_id(ap_id) || Pleroma.Workers.RemoteFetcherWorker.enqueue("fetch_remote", %{
match?({:ok, _object}, Fetcher.fetch_object_from_id(ap_id)) "id" => ap_id,
end) do "depth" => 1
:ok })
else end
:error end)
end
end end
def enqueue_pin_fetches(_), do: nil
def make_user_from_ap_id(ap_id, additional \\ []) do def make_user_from_ap_id(ap_id, additional \\ []) do
user = User.get_cached_by_ap_id(ap_id) user = User.get_cached_by_ap_id(ap_id)
with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id, additional) do with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id, additional) do
{:ok, _pid} = Task.start(fn -> pinned_fetch_task(data) end) enqueue_pin_fetches(data)
if user do if user do
user user

View File

@ -291,9 +291,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
body: featured_data, body: featured_data,
headers: [{"content-type", "application/activity+json"}] headers: [{"content-type", "application/activity+json"}]
} }
end)
Tesla.Mock.mock_global(fn
%{ %{
method: :get, method: :get,
url: ^object_url url: ^object_url
@ -306,7 +304,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
end) end)
{:ok, user} = ActivityPub.make_user_from_ap_id(ap_id) {:ok, user} = ActivityPub.make_user_from_ap_id(ap_id)
Process.sleep(50)
assert_enqueued(
worker: Pleroma.Workers.RemoteFetcherWorker,
args: %{
"op" => "fetch_remote",
"id" => object_url,
"depth" => 1
}
)
# wait for oban
Pleroma.Tests.ObanHelpers.perform_all()
assert user.featured_address == featured_url assert user.featured_address == featured_url
assert Map.has_key?(user.pinned_objects, object_url) assert Map.has_key?(user.pinned_objects, object_url)
@ -368,9 +377,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
body: featured_data, body: featured_data,
headers: [{"content-type", "application/activity+json"}] headers: [{"content-type", "application/activity+json"}]
} }
end)
Tesla.Mock.mock_global(fn
%{ %{
method: :get, method: :get,
url: ^object_url url: ^object_url
@ -383,7 +390,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
end) end)
{:ok, user} = ActivityPub.make_user_from_ap_id(ap_id) {:ok, user} = ActivityPub.make_user_from_ap_id(ap_id)
Process.sleep(50)
assert_enqueued(
worker: Pleroma.Workers.RemoteFetcherWorker,
args: %{
"op" => "fetch_remote",
"id" => object_url,
"depth" => 1
}
)
# wait for oban
Pleroma.Tests.ObanHelpers.perform_all()
assert user.featured_address == featured_url assert user.featured_address == featured_url
assert Map.has_key?(user.pinned_objects, object_url) assert Map.has_key?(user.pinned_objects, object_url)