ObjectValidator.CommonFixes: Introduce fix_objects_defaults and fix_activity_defaults

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-09-10 11:08:05 +02:00
parent e56779dd8d
commit e2a3365b5c
No known key found for this signature in database
GPG Key ID: D5B7A8E43C997DEE
10 changed files with 50 additions and 36 deletions

View File

@ -15,19 +15,23 @@ def cast(object) when is_binary(object) do
def cast(data) when is_list(data) do def cast(data) when is_list(data) do
data data
|> Enum.reduce_while({:ok, []}, fn element, {:ok, list} -> |> Enum.reduce_while({:ok, []}, fn
nil, {:ok, list} ->
{:cont, {:ok, list}}
element, {:ok, list} ->
case ObjectID.cast(element) do case ObjectID.cast(element) do
{:ok, id} -> {:ok, id} ->
{:cont, {:ok, [id | list]}} {:cont, {:ok, [id | list]}}
_ -> _ ->
{:halt, :error} {:halt, {:error, element}}
end end
end) end)
end end
def cast(_) do def cast(data) do
:error {:error, data}
end end
def dump(data) do def dump(data) do

View File

@ -79,9 +79,8 @@ defp fix_url(data), do: data
defp fix(data) do defp fix(data) do
data data
|> CommonFixes.fix_defaults()
|> CommonFixes.fix_attribution()
|> CommonFixes.fix_actor() |> CommonFixes.fix_actor()
|> CommonFixes.fix_object_defaults()
|> fix_url() |> fix_url()
|> Transmogrifier.fix_emoji() |> Transmogrifier.fix_emoji()
end end

View File

@ -120,9 +120,8 @@ defp fix_content(data), do: data
defp fix(data) do defp fix(data) do
data data
|> CommonFixes.fix_defaults()
|> CommonFixes.fix_attribution()
|> CommonFixes.fix_actor() |> CommonFixes.fix_actor()
|> CommonFixes.fix_object_defaults()
|> Transmogrifier.fix_emoji() |> Transmogrifier.fix_emoji()
|> fix_url() |> fix_url()
|> fix_content() |> fix_content()

View File

@ -3,26 +3,44 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes do defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes do
alias Pleroma.EctoType.ActivityPub.ObjectValidators
alias Pleroma.Object.Containment alias Pleroma.Object.Containment
alias Pleroma.User
alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Utils
# based on Pleroma.Web.ActivityPub.Utils.lazy_put_objects_defaults def fix_object_defaults(data) do
def fix_defaults(data) do
%{data: %{"id" => context}, id: context_id} = %{data: %{"id" => context}, id: context_id} =
Utils.create_context(data["context"] || data["conversation"]) Utils.create_context(data["context"] || data["conversation"])
%User{follower_address: follower_collection} = User.get_cached_by_ap_id(data["attributedTo"])
{:ok, to} = ObjectValidators.Recipients.cast(data["to"] || [])
{:ok, cc} = ObjectValidators.Recipients.cast(data["cc"] || [])
data data
|> Map.put("context", context) |> Map.put("context", context)
|> Map.put("context_id", context_id) |> Map.put("context_id", context_id)
|> Map.put("to", to)
|> Map.put("cc", cc)
|> Transmogrifier.fix_explicit_addressing(follower_collection)
|> Transmogrifier.fix_implicit_addressing(follower_collection)
end end
def fix_attribution(data) do def fix_activity_defaults(data, meta) do
object = meta[:object_data] || %{}
data data
|> Map.put_new("actor", data["attributedTo"]) |> Map.put_new("to", object["to"] || [])
|> Map.put_new("cc", object["cc"] || [])
|> Map.put_new("bto", object["bto"] || [])
|> Map.put_new("bcc", object["bcc"] || [])
end end
def fix_actor(data) do def fix_actor(data) do
actor = Containment.get_actor(data) actor =
data
|> Map.put_new("actor", data["attributedTo"])
|> Containment.get_actor()
data data
|> Map.put("actor", actor) |> Map.put("actor", actor)

View File

@ -62,21 +62,11 @@ defp fix_context(data, meta) do
end end
end end
defp fix_addressing(data, meta) do
if object = meta[:object_data] do
data
|> Map.put_new("to", object["to"] || [])
|> Map.put_new("cc", object["cc"] || [])
else
data
end
end
defp fix(data, meta) do defp fix(data, meta) do
data data
|> fix_context(meta) |> fix_context(meta)
|> fix_addressing(meta)
|> CommonFixes.fix_actor() |> CommonFixes.fix_actor()
|> CommonFixes.fix_activity_defaults(meta)
end end
defp validate_data(cng, meta) do defp validate_data(cng, meta) do

View File

@ -72,8 +72,8 @@ def cast_data(data) do
defp fix(data) do defp fix(data) do
data data
|> CommonFixes.fix_defaults() |> CommonFixes.fix_actor()
|> CommonFixes.fix_attribution() |> CommonFixes.fix_object_defaults()
|> Transmogrifier.fix_emoji() |> Transmogrifier.fix_emoji()
end end

View File

@ -83,8 +83,8 @@ defp fix_closed(data) do
defp fix(data) do defp fix(data) do
data data
|> CommonFixes.fix_defaults() |> CommonFixes.fix_actor()
|> CommonFixes.fix_attribution() |> CommonFixes.fix_object_defaults()
|> Transmogrifier.fix_emoji() |> Transmogrifier.fix_emoji()
|> fix_closed() |> fix_closed()
end end

View File

@ -9,7 +9,7 @@ defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.RecipientsTest do
test "it asserts that all elements of the list are object ids" do test "it asserts that all elements of the list are object ids" do
list = ["https://lain.com/users/lain", "invalid"] list = ["https://lain.com/users/lain", "invalid"]
assert :error == Recipients.cast(list) assert {:error, "invalid"} == Recipients.cast(list)
end end
test "it works with a list" do test "it works with a list" do

View File

@ -24,6 +24,8 @@ test "it works for incoming listens" do
"actor" => "http://mastodon.example.org/users/admin", "actor" => "http://mastodon.example.org/users/admin",
"object" => %{ "object" => %{
"type" => "Audio", "type" => "Audio",
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
"cc" => [],
"id" => "http://mastodon.example.org/users/admin/listens/1234", "id" => "http://mastodon.example.org/users/admin/listens/1234",
"attributedTo" => "http://mastodon.example.org/users/admin", "attributedTo" => "http://mastodon.example.org/users/admin",
"title" => "lain radio episode 1", "title" => "lain radio episode 1",
@ -61,7 +63,9 @@ test "Funkwhale Audio object" do
assert object.data["to"] == ["https://www.w3.org/ns/activitystreams#Public"] assert object.data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
assert object.data["cc"] == [] assert object.data["cc"] == [
"https://channels.tests.funkwhale.audio/federation/actors/compositions/followers"
]
assert object.data["url"] == "https://channels.tests.funkwhale.audio/library/tracks/74" assert object.data["url"] == "https://channels.tests.funkwhale.audio/library/tracks/74"

View File

@ -31,7 +31,7 @@ test "Mobilizon Event object" do
) )
assert object.data["to"] == ["https://www.w3.org/ns/activitystreams#Public"] assert object.data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
assert object.data["cc"] == [] assert object.data["cc"] == ["https://mobilizon.org/@tcit/followers"]
assert object.data["url"] == assert object.data["url"] ==
"https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39" "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"