2019-10-23 03:52:27 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2019-10-23 03:52:27 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.Pipeline do
|
|
|
|
alias Pleroma.Activity
|
2020-05-21 05:31:52 -06:00
|
|
|
alias Pleroma.Config
|
2020-05-04 06:34:19 -06:00
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Repo
|
2021-04-20 10:16:24 -06:00
|
|
|
alias Pleroma.Utils
|
2019-10-23 03:52:27 -06:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.ActivityPub.MRF
|
|
|
|
alias Pleroma.Web.ActivityPub.ObjectValidator
|
|
|
|
alias Pleroma.Web.ActivityPub.SideEffects
|
2020-11-11 07:47:57 -07:00
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
2019-10-23 03:52:27 -06:00
|
|
|
alias Pleroma.Web.Federator
|
|
|
|
|
2021-05-21 15:58:20 -06:00
|
|
|
defp side_effects, do: Config.get([:pipeline, :side_effects], SideEffects)
|
|
|
|
defp federator, do: Config.get([:pipeline, :federator], Federator)
|
|
|
|
defp object_validator, do: Config.get([:pipeline, :object_validator], ObjectValidator)
|
|
|
|
defp mrf, do: Config.get([:pipeline, :mrf], MRF)
|
|
|
|
defp activity_pub, do: Config.get([:pipeline, :activity_pub], ActivityPub)
|
|
|
|
defp config, do: Config.get([:pipeline, :config], Config)
|
2020-12-16 09:51:48 -07:00
|
|
|
|
2020-05-04 06:34:19 -06:00
|
|
|
@spec common_pipeline(map(), keyword()) ::
|
|
|
|
{:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
|
2019-10-23 03:52:27 -06:00
|
|
|
def common_pipeline(object, meta) do
|
2021-04-20 10:16:24 -06:00
|
|
|
case Repo.transaction(fn -> do_common_pipeline(object, meta) end, Utils.query_timeout()) do
|
2020-06-05 08:47:02 -06:00
|
|
|
{:ok, {:ok, activity, meta}} ->
|
2021-05-21 15:58:20 -06:00
|
|
|
side_effects().handle_after_transaction(meta)
|
2020-06-05 08:47:02 -06:00
|
|
|
{:ok, activity, meta}
|
|
|
|
|
2020-05-04 06:34:19 -06:00
|
|
|
{:ok, value} ->
|
|
|
|
value
|
|
|
|
|
|
|
|
{:error, e} ->
|
|
|
|
{:error, e}
|
2020-09-12 04:05:36 -06:00
|
|
|
|
|
|
|
{:reject, e} ->
|
|
|
|
{:reject, e}
|
2020-05-04 06:34:19 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-12 06:11:29 -07:00
|
|
|
def do_common_pipeline(%{__struct__: _}, _meta), do: {:error, :is_struct}
|
|
|
|
|
2021-01-12 01:30:22 -07:00
|
|
|
def do_common_pipeline(message, meta) do
|
2021-05-29 12:32:57 -06:00
|
|
|
with {_, {:ok, message, meta}} <- {:validate, object_validator().validate(message, meta)},
|
|
|
|
{_, {:ok, message, meta}} <- {:mrf, mrf().pipeline_filter(message, meta)},
|
|
|
|
{_, {:ok, message, meta}} <- {:persist, activity_pub().persist(message, meta)},
|
|
|
|
{_, {:ok, message, meta}} <- {:side_effects, side_effects().handle(message, meta)},
|
2021-01-12 01:30:22 -07:00
|
|
|
{_, {:ok, _}} <- {:federation, maybe_federate(message, meta)} do
|
|
|
|
{:ok, message, meta}
|
2019-10-23 03:52:27 -06:00
|
|
|
else
|
2021-01-12 01:30:22 -07:00
|
|
|
{:mrf, {:reject, message, _}} -> {:reject, message}
|
2019-10-23 03:52:27 -06:00
|
|
|
e -> {:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-04 06:34:19 -06:00
|
|
|
defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}
|
|
|
|
|
|
|
|
defp maybe_federate(%Activity{} = activity, meta) do
|
2019-10-23 03:52:27 -06:00
|
|
|
with {:ok, local} <- Keyword.fetch(meta, :local) do
|
2021-05-21 15:58:20 -06:00
|
|
|
do_not_federate = meta[:do_not_federate] || !config().get([:instance, :federating])
|
2020-04-30 13:23:18 -06:00
|
|
|
|
2020-11-11 07:47:57 -07:00
|
|
|
if !do_not_federate and local and not Visibility.is_local_public?(activity) do
|
2020-07-24 06:06:41 -06:00
|
|
|
activity =
|
2020-07-27 08:39:50 -06:00
|
|
|
if object = Keyword.get(meta, :object_data) do
|
2020-07-24 06:06:41 -06:00
|
|
|
%{activity | data: Map.put(activity.data, "object", object)}
|
|
|
|
else
|
|
|
|
activity
|
|
|
|
end
|
|
|
|
|
2021-05-21 15:58:20 -06:00
|
|
|
federator().publish(activity)
|
2019-10-23 03:52:27 -06:00
|
|
|
{:ok, :federated}
|
|
|
|
else
|
|
|
|
{:ok, :not_federated}
|
|
|
|
end
|
|
|
|
else
|
2020-03-24 12:24:09 -06:00
|
|
|
_e -> {:error, :badarg}
|
2019-10-23 03:52:27 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|