Use a struct to hold the prepared data passed to publish_one/1

This commit is contained in:
Mark Felder 2024-08-06 12:16:06 -04:00
parent 83fcf42c70
commit 9ae9e2fc5c
2 changed files with 24 additions and 22 deletions

View file

@ -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 @@ def representable?(%Activity{} = activity) 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 @@ def prepare_one(%{inbox: inbox, activity_id: activity_id} = params) do
date: date
})
%{
%Prepared{
activity_id: activity_id,
json: json,
date: date,
@ -134,34 +136,26 @@ def prepare_one(%{inbox: inbox, activity_id: activity_id} = params) 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 @@ def publish_one(%{
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

View file

@ -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