Use a struct to hold the prepared data passed to publish_one/1
This commit is contained in:
parent
83fcf42c70
commit
9ae9e2fc5c
2 changed files with 24 additions and 22 deletions
|
@ -11,6 +11,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
alias Pleroma.Web.ActivityPub.Publisher.Prepared
|
||||||
alias Pleroma.Web.ActivityPub.Relay
|
alias Pleroma.Web.ActivityPub.Relay
|
||||||
alias Pleroma.Web.ActivityPub.Transmogrifier
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
||||||
alias Pleroma.Workers.PublisherWorker
|
alias Pleroma.Workers.PublisherWorker
|
||||||
|
@ -81,6 +82,7 @@ def representable?(%Activity{} = activity) do
|
||||||
* `activity_id`: the internal activity id
|
* `activity_id`: the internal activity id
|
||||||
* `cc`: the cc recipients relevant to this inbox (optional)
|
* `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
|
def prepare_one(%{inbox: inbox, activity_id: activity_id} = params) do
|
||||||
activity = Activity.get_by_id_with_user_actor(activity_id)
|
activity = Activity.get_by_id_with_user_actor(activity_id)
|
||||||
actor = activity.user_actor
|
actor = activity.user_actor
|
||||||
|
@ -111,7 +113,7 @@ def prepare_one(%{inbox: inbox, activity_id: activity_id} = params) do
|
||||||
date: date
|
date: date
|
||||||
})
|
})
|
||||||
|
|
||||||
%{
|
%Prepared{
|
||||||
activity_id: activity_id,
|
activity_id: activity_id,
|
||||||
json: json,
|
json: json,
|
||||||
date: date,
|
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
|
* `unreachable_since`: timestamp the instance was marked unreachable
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def publish_one(%{
|
def publish_one(p = %Prepared{}) do
|
||||||
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 <-
|
with {:ok, %{status: code}} = result when code in 200..299 <-
|
||||||
HTTP.post(
|
HTTP.post(
|
||||||
inbox,
|
p.inbox,
|
||||||
json,
|
p.json,
|
||||||
[
|
[
|
||||||
{"Content-Type", "application/activity+json"},
|
{"Content-Type", "application/activity+json"},
|
||||||
{"Date", date},
|
{"Date", p.date},
|
||||||
{"signature", signature},
|
{"signature", p.signature},
|
||||||
{"digest", digest}
|
{"digest", p.digest}
|
||||||
]
|
]
|
||||||
) do
|
) do
|
||||||
maybe_set_reachable(unreachable_since, inbox)
|
maybe_set_reachable(p.unreachable_since, p.inbox)
|
||||||
|
|
||||||
result
|
result
|
||||||
else
|
else
|
||||||
{_post_result, %{status: code} = response} = e ->
|
{_post_result, %{status: code} = response} = e ->
|
||||||
maybe_set_unreachable(unreachable_since, inbox)
|
maybe_set_unreachable(p.unreachable_since, p.inbox)
|
||||||
Logger.metadata(activity: activity_id, inbox: inbox, status: code)
|
Logger.metadata(activity: p.activity_id, inbox: p.inbox, status: code)
|
||||||
Logger.error("Publisher failed to inbox #{inbox} with status #{code}")
|
Logger.error("Publisher failed to inbox #{p.inbox} with status #{code}")
|
||||||
|
|
||||||
case response do
|
case response do
|
||||||
%{status: 400} -> {:cancel, :bad_request}
|
%{status: 400} -> {:cancel, :bad_request}
|
||||||
|
@ -180,9 +174,9 @@ def publish_one(%{
|
||||||
connection_pool_snooze()
|
connection_pool_snooze()
|
||||||
|
|
||||||
e ->
|
e ->
|
||||||
maybe_set_unreachable(unreachable_since, inbox)
|
maybe_set_unreachable(p.unreachable_since, p.inbox)
|
||||||
Logger.metadata(activity: activity_id, inbox: inbox)
|
Logger.metadata(activity: p.activity_id, inbox: p.inbox)
|
||||||
Logger.error("Publisher failed to inbox #{inbox} #{inspect(e)}")
|
Logger.error("Publisher failed to inbox #{p.inbox} #{inspect(e)}")
|
||||||
{:error, e}
|
{:error, e}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
8
lib/pleroma/web/activity_pub/publisher/prepared.ex
Normal file
8
lib/pleroma/web/activity_pub/publisher/prepared.ex
Normal 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
|
Loading…
Reference in a new issue