Pipeline: Unify, refactor, DRY.
This commit is contained in:
parent
abd0928229
commit
dedffd100c
|
@ -11,13 +11,13 @@ defmodule Pleroma.Web.ActivityPub.Builder do
|
||||||
alias Pleroma.Web.ActivityPub.Utils
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
alias Pleroma.Web.ActivityPub.Visibility
|
alias Pleroma.Web.ActivityPub.Visibility
|
||||||
|
|
||||||
def create(actor, object_id, recipients) do
|
def create(actor, object, recipients) do
|
||||||
{:ok,
|
{:ok,
|
||||||
%{
|
%{
|
||||||
"id" => Utils.generate_activity_id(),
|
"id" => Utils.generate_activity_id(),
|
||||||
"actor" => actor.ap_id,
|
"actor" => actor.ap_id,
|
||||||
"to" => recipients,
|
"to" => recipients,
|
||||||
"object" => object_id,
|
"object" => object,
|
||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
"published" => DateTime.utc_now() |> DateTime.to_iso8601()
|
"published" => DateTime.utc_now() |> DateTime.to_iso8601()
|
||||||
}, []}
|
}, []}
|
||||||
|
|
|
@ -38,16 +38,24 @@ def validate(%{"type" => "ChatMessage"} = object, meta) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate(%{"type" => "Create"} = object, meta) do
|
def validate(%{"type" => "Create", "object" => object} = create_activity, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object_data} <- cast_and_apply(object),
|
||||||
object
|
meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
|
||||||
|
{:ok, create_activity} <-
|
||||||
|
create_activity
|
||||||
|> CreateChatMessageValidator.cast_and_validate(meta)
|
|> CreateChatMessageValidator.cast_and_validate(meta)
|
||||||
|> Ecto.Changeset.apply_action(:insert) do
|
|> Ecto.Changeset.apply_action(:insert) do
|
||||||
object = stringify_keys(object)
|
create_activity = stringify_keys(create_activity)
|
||||||
{:ok, object, meta}
|
{:ok, create_activity, meta}
|
||||||
end
|
end
|
||||||
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
|
def stringify_keys(%{__struct__: _} = object) do
|
||||||
object
|
object
|
||||||
|> Map.from_struct()
|
|> Map.from_struct()
|
||||||
|
|
|
@ -4,9 +4,6 @@
|
||||||
|
|
||||||
defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageHandling do
|
defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageHandling do
|
||||||
alias Pleroma.Repo
|
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
|
alias Pleroma.Web.ActivityPub.Pipeline
|
||||||
|
|
||||||
def handle_incoming(
|
def handle_incoming(
|
||||||
|
@ -15,31 +12,15 @@ def handle_incoming(
|
||||||
) do
|
) do
|
||||||
# Create has to be run inside a transaction because the object is created as a side effect.
|
# 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.
|
# If this does not work, we need to roll back creating the activity.
|
||||||
case Repo.transaction(fn -> do_handle_incoming(data) end) do
|
case Repo.transaction(fn -> Pipeline.common_pipeline(data, local: false) end) do
|
||||||
{:ok, value} ->
|
{:ok, {:ok, activity, _}} ->
|
||||||
value
|
{:ok, activity}
|
||||||
|
|
||||||
|
{:ok, e} ->
|
||||||
|
e
|
||||||
|
|
||||||
{:error, e} ->
|
{:error, e} ->
|
||||||
{:error, e}
|
{:error, e}
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -40,12 +40,11 @@ def post_chat_message(%User{} = user, %User{} = recipient, content) do
|
||||||
)},
|
)},
|
||||||
{_, {:ok, create_activity_data, _meta}} <-
|
{_, {:ok, create_activity_data, _meta}} <-
|
||||||
{:build_create_activity,
|
{: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}} <-
|
{_, {:ok, %Activity{} = activity, _meta}} <-
|
||||||
{:common_pipeline,
|
{:common_pipeline,
|
||||||
Pipeline.common_pipeline(create_activity_data,
|
Pipeline.common_pipeline(create_activity_data,
|
||||||
local: true,
|
local: true
|
||||||
object_data: chat_message_data
|
|
||||||
)} do
|
)} do
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
else
|
else
|
||||||
|
|
|
@ -17,7 +17,7 @@ test "it is invalid if the object already exists" do
|
||||||
{:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
|
{:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
|
||||||
object = Object.normalize(activity, false)
|
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, [])
|
{:error, cng} = ObjectValidator.validate(create_data, [])
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue