Merge branch 'embedded-object-federation' into 'develop'

Embedded object federation

Closes #1959

See merge request pleroma/pleroma!2794
This commit is contained in:
Haelwenn 2020-07-27 21:20:52 +00:00
commit 057936bf0b
5 changed files with 84 additions and 8 deletions

View File

@ -9,6 +9,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
the system. the system.
""" """
alias Pleroma.Activity
alias Pleroma.EctoType.ActivityPub.ObjectValidators alias Pleroma.EctoType.ActivityPub.ObjectValidators
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.User alias Pleroma.User
@ -71,6 +72,12 @@ def validate(%{"type" => "Undo"} = object, meta) do
|> UndoValidator.cast_and_validate() |> UndoValidator.cast_and_validate()
|> Ecto.Changeset.apply_action(:insert) do |> Ecto.Changeset.apply_action(:insert) do
object = stringify_keys(object) object = stringify_keys(object)
undone_object = Activity.get_by_ap_id(object["object"])
meta =
meta
|> Keyword.put(:object_data, undone_object.data)
{:ok, object, meta} {:ok, object, meta}
end end
end end

View File

@ -52,6 +52,13 @@ defp maybe_federate(%Activity{} = activity, meta) do
do_not_federate = meta[:do_not_federate] || !Config.get([:instance, :federating]) do_not_federate = meta[:do_not_federate] || !Config.get([:instance, :federating])
if !do_not_federate && local do if !do_not_federate && local do
activity =
if object = Keyword.get(meta, :object_data) do
%{activity | data: Map.put(activity.data, "object", object)}
else
activity
end
Federator.publish(activity) Federator.publish(activity)
{:ok, :federated} {:ok, :federated}
else else

View File

@ -14,6 +14,51 @@ defmodule Pleroma.Web.ActivityPub.PipelineTest do
:ok :ok
end end
test "when given an `object_data` in meta, Federation will receive a the original activity with the `object` field set to this embedded object" do
activity = insert(:note_activity)
object = %{"id" => "1", "type" => "Love"}
meta = [local: true, object_data: object]
activity_with_object = %{activity | data: Map.put(activity.data, "object", object)}
with_mocks([
{Pleroma.Web.ActivityPub.ObjectValidator, [], [validate: fn o, m -> {:ok, o, m} end]},
{
Pleroma.Web.ActivityPub.MRF,
[],
[filter: fn o -> {:ok, o} end]
},
{
Pleroma.Web.ActivityPub.ActivityPub,
[],
[persist: fn o, m -> {:ok, o, m} end]
},
{
Pleroma.Web.ActivityPub.SideEffects,
[],
[
handle: fn o, m -> {:ok, o, m} end,
handle_after_transaction: fn m -> m end
]
},
{
Pleroma.Web.Federator,
[],
[publish: fn _o -> :ok end]
}
]) do
assert {:ok, ^activity, ^meta} =
Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity, meta)
assert_called(Pleroma.Web.ActivityPub.ObjectValidator.validate(activity, meta))
assert_called(Pleroma.Web.ActivityPub.MRF.filter(activity))
assert_called(Pleroma.Web.ActivityPub.ActivityPub.persist(activity, meta))
assert_called(Pleroma.Web.ActivityPub.SideEffects.handle(activity, meta))
refute called(Pleroma.Web.Federator.publish(activity))
assert_called(Pleroma.Web.Federator.publish(activity_with_object))
end
end
test "it goes through validation, filtering, persisting, side effects and federation for local activities" do test "it goes through validation, filtering, persisting, side effects and federation for local activities" do
activity = insert(:note_activity) activity = insert(:note_activity)
meta = [local: true] meta = [local: true]

View File

@ -312,8 +312,12 @@ test "when activation is required", %{delete: delete, user: user} do
} }
end end
test "deletes the original block", %{block_undo: block_undo, block: block} do test "deletes the original block", %{
{:ok, _block_undo, _} = SideEffects.handle(block_undo) block_undo: block_undo,
block: block
} do
{:ok, _block_undo, _meta} = SideEffects.handle(block_undo)
refute Activity.get_by_id(block.id) refute Activity.get_by_id(block.id)
end end

View File

@ -624,6 +624,10 @@ test "unreacting to a status with an emoji" do
user = insert(:user) user = insert(:user)
other_user = insert(:user) other_user = insert(:user)
clear_config([:instance, :federating], true)
with_mock Pleroma.Web.Federator,
publish: fn _ -> nil end do
{:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"}) {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
{:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍") {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
@ -632,6 +636,15 @@ test "unreacting to a status with an emoji" do
assert unreaction.data["type"] == "Undo" assert unreaction.data["type"] == "Undo"
assert unreaction.data["object"] == reaction.data["id"] assert unreaction.data["object"] == reaction.data["id"]
assert unreaction.local assert unreaction.local
# On federation, it contains the undone (and deleted) object
unreaction_with_object = %{
unreaction
| data: Map.put(unreaction.data, "object", reaction.data)
}
assert called(Pleroma.Web.Federator.publish(unreaction_with_object))
end
end end
test "repeating a status" do test "repeating a status" do