From ee0370089b98b891ba511dcc0343eba110279893 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Tue, 26 Nov 2024 23:16:48 +0100 Subject: [PATCH] better control flow to check outbox refetch --- lib/pleroma/user.ex | 2 +- lib/pleroma/web/activity_pub/activity_pub.ex | 25 +++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 6bbfd5b72..7bc7b4971 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -2774,7 +2774,7 @@ def accepts_direct_messages?(%User{accepts_direct_messages_from: :nobody}, _), do: false def outbox_refreshed(%User{} = user) do - #now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second) + # now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second) user |> cast(%{last_outbox_fetch: NaiveDateTime.utc_now()}, [:last_outbox_fetch]) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 36a7c9b4e..bfd14ebbd 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1858,21 +1858,19 @@ def enqueue_pin_fetches(%{pinned_objects: pins}) do def enqueue_pin_fetches(_), do: nil + defp need_outbox_refresh?(last_fetch) + + defp need_outbox_refresh?(nil), do: true + + defp need_outbox_refresh?(%NaiveDateTime{} = last_fetch) do + NaiveDateTime.diff(NaiveDateTime.utc_now(), last_fetch) > + Config.get!([:activitypub, :outbox_refetch_cooldown]) + end + def enqueue_outbox_fetches( %{outbox: outbox_address, last_outbox_fetch: last_fetch, local: false} = user ) do - with( - last_fetch <- - if last_fetch == nil do - 0 - else - last_fetch - end, - now <- - NaiveDateTime.utc_now(), - {true} <- - {last_fetch < NaiveDateTime.add(now, -60)} - ) do + if need_outbox_refresh?(last_fetch) do # enqueue a task to fetch the outbox Logger.debug("Refetching outbox #{outbox_address}") @@ -1884,8 +1882,7 @@ def enqueue_outbox_fetches( :ok else - _ -> - Logger.debug("Not refetching outbox (it was fetched <1 min ago)") + Logger.debug("Not refetching outbox (TTL not reached)") end :ok