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) field(:context, :string)
end end
def cast_data(data) do def cast_data(data, meta \\ []) do
data = fix(data, meta)
%__MODULE__{} %__MODULE__{}
|> changeset(data) |> changeset(data)
end end
@ -42,7 +44,7 @@ def cast_and_apply(data) do
def cast_and_validate(data, meta \\ []) do def cast_and_validate(data, meta \\ []) do
data data
|> cast_data |> cast_data(meta)
|> validate_data(meta) |> validate_data(meta)
end end
@ -51,6 +53,19 @@ def changeset(struct, data) do
|> cast(data, __schema__(:fields)) |> cast(data, __schema__(:fields))
end 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 def validate_data(cng, meta \\ []) do
cng cng
|> validate_required([:actor, :type, :object]) |> 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 # based on Pleroma.Web.ActivityPub.Utils.lazy_put_objects_defaults
defp fix_defaults(data) do 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 data
|> Map.put_new_lazy("id", &Utils.generate_object_id/0)
|> Map.put_new_lazy("published", &Utils.make_date/0) |> Map.put_new_lazy("published", &Utils.make_date/0)
|> Map.put_new("context", context) |> Map.put_new("context", context)
|> Map.put_new("context_id", context_id) |> Map.put_new("context_id", context_id)
end end
defp fix_attribution(data) do
data
|> Map.put_new("actor", data["attributedTo"])
end
defp fix(data) do defp fix(data) do
data data
|> fix_attribution()
|> fix_closed() |> fix_closed()
|> fix_defaults() |> fix_defaults()
end end

View file

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

View file

@ -28,10 +28,10 @@ def render("show.json", %{object: object, multiple: multiple, options: options}
def render("show.json", %{object: object} = params) do def render("show.json", %{object: object} = params) do
case object.data do case object.data do
%{"anyOf" => [ _ | _] = options} -> %{"anyOf" => [_ | _] = options} ->
render(__MODULE__, "show.json", Map.merge(params, %{multiple: true, options: options})) render(__MODULE__, "show.json", Map.merge(params, %{multiple: true, options: options}))
%{"oneOf" => [ _ | _] = options} -> %{"oneOf" => [_ | _] = options} ->
render(__MODULE__, "show.json", Map.merge(params, %{multiple: false, options: options})) render(__MODULE__, "show.json", Map.merge(params, %{multiple: false, options: options}))
_ -> _ ->