From 2dbd969e6e7a39583e6eae7ebdb95c213b0316bc Mon Sep 17 00:00:00 2001 From: Tusooa Zhu Date: Fri, 24 Jun 2022 10:26:01 -0400 Subject: [PATCH] Use meta[:object_data] in SideEffects for Update --- lib/pleroma/web/activity_pub/side_effects.ex | 2 + .../web/activity_pub/side_effects_test.exs | 49 ++++++++++++++----- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/pleroma/web/activity_pub/side_effects.ex b/lib/pleroma/web/activity_pub/side_effects.ex index 1e1570e33..1c216ca38 100644 --- a/lib/pleroma/web/activity_pub/side_effects.ex +++ b/lib/pleroma/web/activity_pub/side_effects.ex @@ -464,6 +464,8 @@ defp handle_update_object( orig_object = Object.get_by_ap_id(orig_object_ap_id) orig_object_data = orig_object.data + updated_object = meta[:object_data] + if orig_object_data["type"] in @updatable_object_types do %{data: updated_object_data, updated: updated} = orig_object_data diff --git a/test/pleroma/web/activity_pub/side_effects_test.exs b/test/pleroma/web/activity_pub/side_effects_test.exs index 9fb583dbf..d98fb38cf 100644 --- a/test/pleroma/web/activity_pub/side_effects_test.exs +++ b/test/pleroma/web/activity_pub/side_effects_test.exs @@ -159,21 +159,44 @@ test "it uses a given changeset to update", %{user: user, update: update} do {:ok, update_data, []} = Builder.update(user, updated_note) {:ok, update, _meta} = ActivityPub.persist(update_data, local: true) - %{user: user, note: note, object_id: note.id, update_data: update_data, update: update} + %{ + user: user, + note: note, + object_id: note.id, + update_data: update_data, + update: update, + updated_note: updated_note + } end - test "it updates the note", %{object_id: object_id, update: update} do - {:ok, _, _} = SideEffects.handle(update) + test "it updates the note", %{ + object_id: object_id, + update: update, + updated_note: updated_note + } do + {:ok, _, _} = SideEffects.handle(update, object_data: updated_note) new_note = Pleroma.Object.get_by_id(object_id) assert %{"summary" => "edited summary", "content" => "edited content"} = new_note.data end + test "it updates using object_data", %{ + object_id: object_id, + update: update, + updated_note: updated_note + } do + updated_note = Map.put(updated_note, "summary", "mew mew") + {:ok, _, _} = SideEffects.handle(update, object_data: updated_note) + new_note = Pleroma.Object.get_by_id(object_id) + assert %{"summary" => "mew mew", "content" => "edited content"} = new_note.data + end + test "it records the original note in formerRepresentations", %{ note: note, object_id: object_id, - update: update + update: update, + updated_note: updated_note } do - {:ok, _, _} = SideEffects.handle(update) + {:ok, _, _} = SideEffects.handle(update, object_data: updated_note) %{data: new_note} = Pleroma.Object.get_by_id(object_id) assert %{"summary" => "edited summary", "content" => "edited content"} = new_note @@ -187,9 +210,10 @@ test "it puts the original note at the front of formerRepresentations", %{ user: user, note: note, object_id: object_id, - update: update + update: update, + updated_note: updated_note } do - {:ok, _, _} = SideEffects.handle(update) + {:ok, _, _} = SideEffects.handle(update, object_data: updated_note) %{data: first_edit} = Pleroma.Object.get_by_id(object_id) second_updated_note = @@ -199,7 +223,7 @@ test "it puts the original note at the front of formerRepresentations", %{ {:ok, second_update_data, []} = Builder.update(user, second_updated_note) {:ok, update, _meta} = ActivityPub.persist(second_update_data, local: true) - {:ok, _, _} = SideEffects.handle(update) + {:ok, _, _} = SideEffects.handle(update, object_data: second_updated_note) %{data: new_note} = Pleroma.Object.get_by_id(object_id) assert %{"summary" => "edited summary 2", "content" => "edited content 2"} = new_note @@ -215,12 +239,13 @@ test "it puts the original note at the front of formerRepresentations", %{ test "it does not prepend to formerRepresentations if no actual changes are made", %{ note: note, object_id: object_id, - update: update + update: update, + updated_note: updated_note } do - {:ok, _, _} = SideEffects.handle(update) + {:ok, _, _} = SideEffects.handle(update, object_data: updated_note) %{data: _first_edit} = Pleroma.Object.get_by_id(object_id) - {:ok, _, _} = SideEffects.handle(update) + {:ok, _, _} = SideEffects.handle(update, object_data: updated_note) %{data: new_note} = Pleroma.Object.get_by_id(object_id) assert %{"summary" => "edited summary", "content" => "edited content"} = new_note @@ -255,7 +280,7 @@ test "allows updating choice count without generating edit history", %{ {:ok, update_data, []} = Builder.update(user, updated_question) {:ok, update, _meta} = ActivityPub.persist(update_data, local: true) - {:ok, _, _} = SideEffects.handle(update) + {:ok, _, _} = SideEffects.handle(update, object_data: updated_question) %{data: new_question} = Pleroma.Object.get_by_id(id)