Questions: Move fixes to validators.

This commit is contained in:
lain 2020-08-05 14:51:33 +02:00
parent b5f0cef156
commit f889400d05
4 changed files with 29 additions and 15 deletions

View File

@ -29,7 +29,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator do
field(:context, :string)
end
def cast_data(data) do
def cast_data(data, meta \\ []) do
data = fix(data, meta)
%__MODULE__{}
|> changeset(data)
end
@ -42,7 +44,7 @@ def cast_and_apply(data) do
def cast_and_validate(data, meta \\ []) do
data
|> cast_data
|> cast_data(meta)
|> validate_data(meta)
end
@ -51,6 +53,19 @@ def changeset(struct, data) do
|> cast(data, __schema__(:fields))
end
defp fix_context(data, meta) do
if object = meta[:object_data] do
Map.put_new(data, "context", object["context"])
else
data
end
end
defp fix(data, meta) do
data
|> fix_context(meta)
end
def validate_data(cng, meta \\ []) do
cng
|> validate_required([:actor, :type, :object])

View File

@ -83,17 +83,23 @@ defp fix_closed(data) do
# based on Pleroma.Web.ActivityPub.Utils.lazy_put_objects_defaults
defp fix_defaults(data) do
%{data: %{"id" => context}, id: context_id} = Utils.create_context(data["context"])
%{data: %{"id" => context}, id: context_id} =
Utils.create_context(data["context"] || data["conversation"])
data
|> Map.put_new_lazy("id", &Utils.generate_object_id/0)
|> Map.put_new_lazy("published", &Utils.make_date/0)
|> Map.put_new("context", context)
|> Map.put_new("context_id", context_id)
end
defp fix_attribution(data) do
data
|> Map.put_new("actor", data["attributedTo"])
end
defp fix(data) do
data
|> fix_attribution()
|> fix_closed()
|> fix_defaults()
end

View File

@ -634,17 +634,10 @@ def handle_incoming(
end
def handle_incoming(
%{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
%{"type" => "Create", "object" => %{"type" => objtype}} = data,
_options
)
when objtype in ["Question", "Answer"] do
data =
data
|> Map.put("object", fix_object(object))
|> fix_addressing()
data = Map.put_new(data, "context", data["object"]["context"])
when objtype in ["Question", "Answer", "ChatMessage"] do
with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
{:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
{:ok, activity}