AP ingest: fill missing attributedTo from activity
This commit is contained in:
parent
9912a9f603
commit
3b3e369f83
5 changed files with 108 additions and 9 deletions
|
|
@ -15,12 +15,28 @@ defmodule Pleroma.Object.Containment do
|
|||
actor
|
||||
end
|
||||
|
||||
def get_actor(%{"actor" => nil, "attributedTo" => actor}) when not is_nil(actor) do
|
||||
get_actor(%{"actor" => actor})
|
||||
end
|
||||
|
||||
def get_actor(%{"actor" => nil}), do: nil
|
||||
|
||||
def get_actor(%{"actor" => []}), do: nil
|
||||
|
||||
def get_actor(%{"actor" => actor}) when is_list(actor) do
|
||||
if is_binary(Enum.at(actor, 0)) do
|
||||
Enum.at(actor, 0)
|
||||
else
|
||||
Enum.find(actor, fn %{"type" => type} -> type in ["Person", "Service", "Application"] end)
|
||||
|> Map.get("id")
|
||||
case actor do
|
||||
[first | _] when is_binary(first) ->
|
||||
first
|
||||
|
||||
_ ->
|
||||
Enum.find_value(actor, fn
|
||||
%{"type" => type, "id" => id}
|
||||
when type in ["Person", "Service", "Application"] and is_binary(id) ->
|
||||
id
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -28,10 +44,6 @@ defmodule Pleroma.Object.Containment do
|
|||
id
|
||||
end
|
||||
|
||||
def get_actor(%{"actor" => nil, "attributedTo" => actor}) when not is_nil(actor) do
|
||||
get_actor(%{"actor" => actor})
|
||||
end
|
||||
|
||||
def get_object(%{"object" => id}) when is_binary(id) do
|
||||
id
|
||||
end
|
||||
|
|
|
|||
|
|
@ -118,6 +118,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
|||
when objtype in ~w[Question Answer Audio Video Image Event Article Note Page] do
|
||||
with {:ok, object_data} <-
|
||||
object
|
||||
|> CommonFixes.maybe_set_attributed_to_from_activity(create_activity)
|
||||
|> CommonFixes.maybe_add_language_from_activity(create_activity)
|
||||
|> cast_and_apply_and_stringify_with_history(),
|
||||
meta = Keyword.put(meta, :object_data, object_data),
|
||||
|
|
@ -172,6 +173,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
|||
{_, {:ok, object_data, _}} <-
|
||||
{:object_validation,
|
||||
object
|
||||
|> CommonFixes.maybe_set_attributed_to_from_activity(update_activity)
|
||||
|> CommonFixes.maybe_add_language_from_activity(update_activity)
|
||||
|> validate(meta)},
|
||||
meta = Keyword.put(meta, :object_data, object_data),
|
||||
|
|
|
|||
|
|
@ -91,6 +91,17 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes do
|
|||
|> Map.put("attributedTo", actor)
|
||||
end
|
||||
|
||||
def maybe_set_attributed_to_from_activity(object, activity) when is_map(object) do
|
||||
if is_nil(object["attributedTo"]) and is_nil(object["actor"]) do
|
||||
case Containment.get_actor(activity) do
|
||||
actor when is_binary(actor) -> Map.put(object, "attributedTo", actor)
|
||||
_ -> object
|
||||
end
|
||||
else
|
||||
object
|
||||
end
|
||||
end
|
||||
|
||||
def fix_activity_context(data, %Object{data: %{"context" => object_context}}) do
|
||||
data
|
||||
|> Map.put("context", object_context)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue