summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMark Felder <feld@feld.me>2024-08-06 11:15:35 -0400
committerMark Felder <feld@feld.me>2024-08-06 11:16:04 -0400
commitf8bdcaa161575e40097a82481009620edc5a0696 (patch)
tree85464761e8908d90670e8c411df21f2fd309ab36 /lib
parent16ba2742b78b136d8e89edfe7847dc3d2f35ed14 (diff)
downloadpleroma-f8bdcaa161575e40097a82481009620edc5a0696.tar.gz
pleroma-f8bdcaa161575e40097a82481009620edc5a0696.zip
Split Federator.publish_one/1 into a second function called prepare_one/1
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/web/activity_pub/publisher.ex52
-rw-r--r--lib/pleroma/web/federator.ex5
2 files changed, 47 insertions, 10 deletions
diff --git a/lib/pleroma/web/activity_pub/publisher.ex b/lib/pleroma/web/activity_pub/publisher.ex
index e63b8ff1f..2d2c09f1c 100644
--- a/lib/pleroma/web/activity_pub/publisher.ex
+++ b/lib/pleroma/web/activity_pub/publisher.ex
@@ -76,14 +76,12 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
end
@doc """
- Publish a single message to a peer. Takes a struct with the following
- parameters set:
-
+ Prepare an activity for publishing from an Oban job
* `inbox`: the inbox to publish to
* `activity_id`: the internal activity id
* `cc`: the cc recipients relevant to this inbox (optional)
"""
- def publish_one(%{inbox: inbox, activity_id: activity_id} = params) do
+ def prepare_one(%{inbox: inbox, activity_id: activity_id} = params) do
activity = Activity.get_by_id_with_user_actor(activity_id)
actor = activity.user_actor
@@ -113,6 +111,38 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
date: date
})
+ %{
+ activity_id: activity_id,
+ json: json,
+ date: date,
+ signature: signature,
+ digest: digest,
+ inbox: inbox,
+ unreachable_since: params[:unreachable_since]
+ }
+ end
+
+ @doc """
+ Publish a single message to a peer. Takes a struct with the following
+ parameters set:
+ * `activity_id`: the activity id
+ * `json`: the json payload
+ * `date`: the signed date from Pleroma.Signature.signed_date()
+ * `signature`: the signature from Pleroma.Signature.sign/2
+ * `digest`: base64 encoded the hash of the json payload prefixed with "SHA-256="
+ * `inbox`: the inbox URI of this delivery
+ * `unreachable_since`: timestamp the instance was marked unreachable
+
+ """
+ def publish_one(%{
+ activity_id: activity_id,
+ json: json,
+ date: date,
+ signature: signature,
+ digest: digest,
+ inbox: inbox,
+ unreachable_since: unreachable_since
+ }) do
with {:ok, %{status: code}} = result when code in 200..299 <-
HTTP.post(
inbox,
@@ -124,14 +154,12 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
{"digest", digest}
]
) do
- if not Map.has_key?(params, :unreachable_since) || params[:unreachable_since] do
- Instances.set_reachable(inbox)
- end
+ maybe_set_reachable(unreachable_since, inbox)
result
else
{_post_result, %{status: code} = response} = e ->
- unless params[:unreachable_since], do: Instances.set_unreachable(inbox)
+ maybe_set_unreachable(unreachable_since, inbox)
Logger.metadata(activity: activity_id, inbox: inbox, status: code)
Logger.error("Publisher failed to inbox #{inbox} with status #{code}")
@@ -152,7 +180,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
connection_pool_snooze()
e ->
- unless params[:unreachable_since], do: Instances.set_unreachable(inbox)
+ maybe_set_unreachable(unreachable_since, inbox)
Logger.metadata(activity: activity_id, inbox: inbox)
Logger.error("Publisher failed to inbox #{inbox} #{inspect(e)}")
{:error, e}
@@ -161,6 +189,12 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
defp connection_pool_snooze, do: {:snooze, 3}
+ defp maybe_set_reachable(%NaiveDateTime{}, inbox), do: Instances.set_reachable(inbox)
+ defp maybe_set_reachable(_, _), do: :ok
+
+ defp maybe_set_unreachable(nil, inbox), do: Instances.set_unreachable(inbox)
+ defp maybe_set_unreachable(%NaiveDateTime{}, _), do: :ok
+
defp signature_host(%URI{port: port, scheme: scheme, host: host}) do
if port == URI.default_port(scheme) do
host
diff --git a/lib/pleroma/web/federator.ex b/lib/pleroma/web/federator.ex
index 3d3101d61..242cf4bfd 100644
--- a/lib/pleroma/web/federator.ex
+++ b/lib/pleroma/web/federator.ex
@@ -71,7 +71,10 @@ defmodule Pleroma.Web.Federator do
# Job Worker Callbacks
@spec perform(atom(), any()) :: {:ok, any()} | {:error, any()}
- def perform(:publish_one, params), do: Publisher.publish_one(params)
+ def perform(:publish_one, params) do
+ Publisher.prepare_one(params)
+ |> Publisher.publish_one()
+ end
def perform(:publish, activity) do
Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)