Pipeline: Unify, refactor, DRY.

This commit is contained in:
lain 2020-04-28 17:29:54 +02:00
parent abd0928229
commit dedffd100c
5 changed files with 24 additions and 36 deletions

View File

@ -11,13 +11,13 @@ defmodule Pleroma.Web.ActivityPub.Builder do
alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.ActivityPub.Visibility
def create(actor, object_id, recipients) do
def create(actor, object, recipients) do
{:ok,
%{
"id" => Utils.generate_activity_id(),
"actor" => actor.ap_id,
"to" => recipients,
"object" => object_id,
"object" => object,
"type" => "Create",
"published" => DateTime.utc_now() |> DateTime.to_iso8601()
}, []}

View File

@ -38,16 +38,24 @@ def validate(%{"type" => "ChatMessage"} = object, meta) do
end
end
def validate(%{"type" => "Create"} = object, meta) do
with {:ok, object} <-
object
def validate(%{"type" => "Create", "object" => object} = create_activity, meta) do
with {:ok, object_data} <- cast_and_apply(object),
meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
{:ok, create_activity} <-
create_activity
|> CreateChatMessageValidator.cast_and_validate(meta)
|> Ecto.Changeset.apply_action(:insert) do
object = stringify_keys(object)
{:ok, object, meta}
create_activity = stringify_keys(create_activity)
{:ok, create_activity, meta}
end
end
def cast_and_apply(%{"type" => "ChatMessage"} = object) do
ChatMessageValidator.cast_and_apply(object)
end
def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
def stringify_keys(%{__struct__: _} = object) do
object
|> Map.from_struct()

View File

@ -4,9 +4,6 @@
defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageHandling do
alias Pleroma.Repo
alias Pleroma.Web.ActivityPub.ObjectValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
alias Pleroma.Web.ActivityPub.Pipeline
def handle_incoming(
@ -15,31 +12,15 @@ def handle_incoming(
) do
# Create has to be run inside a transaction because the object is created as a side effect.
# If this does not work, we need to roll back creating the activity.
case Repo.transaction(fn -> do_handle_incoming(data) end) do
{:ok, value} ->
value
case Repo.transaction(fn -> Pipeline.common_pipeline(data, local: false) end) do
{:ok, {:ok, activity, _}} ->
{:ok, activity}
{:ok, e} ->
e
{:error, e} ->
{:error, e}
end
end
def do_handle_incoming(
%{"type" => "Create", "object" => %{"type" => "ChatMessage"} = object_data} = data
) do
with {_, {:ok, cast_data_sym}} <-
{:casting_data, data |> CreateChatMessageValidator.cast_and_apply()},
cast_data = ObjectValidator.stringify_keys(cast_data_sym),
{_, {:ok, object_cast_data_sym}} <-
{:casting_object_data, object_data |> ChatMessageValidator.cast_and_apply()},
object_cast_data = ObjectValidator.stringify_keys(object_cast_data_sym),
{_, {:ok, activity, _meta}} <-
{:common_pipeline,
Pipeline.common_pipeline(cast_data, local: false, object_data: object_cast_data)} do
{:ok, activity}
else
e ->
{:error, e}
end
end
end

View File

@ -40,12 +40,11 @@ def post_chat_message(%User{} = user, %User{} = recipient, content) do
)},
{_, {:ok, create_activity_data, _meta}} <-
{:build_create_activity,
Builder.create(user, chat_message_data["id"], [recipient.ap_id])},
Builder.create(user, chat_message_data, [recipient.ap_id])},
{_, {:ok, %Activity{} = activity, _meta}} <-
{:common_pipeline,
Pipeline.common_pipeline(create_activity_data,
local: true,
object_data: chat_message_data
local: true
)} do
{:ok, activity}
else

View File

@ -17,7 +17,7 @@ test "it is invalid if the object already exists" do
{:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
object = Object.normalize(activity, false)
{:ok, create_data, _} = Builder.create(user, object.data["id"], [recipient.ap_id])
{:ok, create_data, _} = Builder.create(user, object.data, [recipient.ap_id])
{:error, cng} = ObjectValidator.validate(create_data, [])