commit: 9ae9e2fc5ce61200712ea02201c2ba87b174f06a
parent 83fcf42c709c390888f13a37f2d381b071a65231
Author: Mark Felder <feld@feld.me>
Date: Tue, 6 Aug 2024 12:16:06 -0400
Use a struct to hold the prepared data passed to publish_one/1
Diffstat:
2 files changed, 24 insertions(+), 22 deletions(-)
diff --git a/lib/pleroma/web/activity_pub/publisher.ex b/lib/pleroma/web/activity_pub/publisher.ex
@@ -11,6 +11,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
alias Pleroma.Object
alias Pleroma.Repo
alias Pleroma.User
+ alias Pleroma.Web.ActivityPub.Publisher.Prepared
alias Pleroma.Web.ActivityPub.Relay
alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Workers.PublisherWorker
@@ -81,6 +82,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
* `activity_id`: the internal activity id
* `cc`: the cc recipients relevant to this inbox (optional)
"""
+ @spec prepare_one(map()) :: Prepared.t()
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
@@ -111,7 +113,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
date: date
})
- %{
+ %Prepared{
activity_id: activity_id,
json: json,
date: date,
@@ -134,34 +136,26 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
* `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
+ def publish_one(p = %Prepared{}) do
with {:ok, %{status: code}} = result when code in 200..299 <-
HTTP.post(
- inbox,
- json,
+ p.inbox,
+ p.json,
[
{"Content-Type", "application/activity+json"},
- {"Date", date},
- {"signature", signature},
- {"digest", digest}
+ {"Date", p.date},
+ {"signature", p.signature},
+ {"digest", p.digest}
]
) do
- maybe_set_reachable(unreachable_since, inbox)
+ maybe_set_reachable(p.unreachable_since, p.inbox)
result
else
{_post_result, %{status: code} = response} = e ->
- 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}")
+ maybe_set_unreachable(p.unreachable_since, p.inbox)
+ Logger.metadata(activity: p.activity_id, inbox: p.inbox, status: code)
+ Logger.error("Publisher failed to inbox #{p.inbox} with status #{code}")
case response do
%{status: 400} -> {:cancel, :bad_request}
@@ -180,9 +174,9 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
connection_pool_snooze()
e ->
- maybe_set_unreachable(unreachable_since, inbox)
- Logger.metadata(activity: activity_id, inbox: inbox)
- Logger.error("Publisher failed to inbox #{inbox} #{inspect(e)}")
+ maybe_set_unreachable(p.unreachable_since, p.inbox)
+ Logger.metadata(activity: p.activity_id, inbox: p.inbox)
+ Logger.error("Publisher failed to inbox #{p.inbox} #{inspect(e)}")
{:error, e}
end
end
diff --git a/lib/pleroma/web/activity_pub/publisher/prepared.ex b/lib/pleroma/web/activity_pub/publisher/prepared.ex
@@ -0,0 +1,8 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ActivityPub.Publisher.Prepared do
+ @type t :: %__MODULE__{}
+ defstruct [:activity_id, :json, :date, :signature, :digest, :inbox, :unreachable_since]
+end