Reuse formerRepresentations from remote if possible

This commit is contained in:
Tusooa Zhu 2022-06-25 01:03:46 -04:00
parent 99a6f50316
commit 40953a8f5c
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
2 changed files with 71 additions and 2 deletions

View file

@ -67,7 +67,7 @@ defmodule Pleroma.Object.Updater do
# Note that we may have got the edit history by first fetching the object
{new_history, used_history_in_new_object?} =
with true <- use_history_in_new_object?,
updated_history when not is_nil(updated_history) <- maybe_history(updated_object) do
updated_history when not is_nil(updated_history) <- maybe_history(opts[:new_data]) do
{updated_history, true}
else
_ ->
@ -142,7 +142,11 @@ defmodule Pleroma.Object.Updater do
%{updated_object: updated_data, used_history_in_new_object?: used_history_in_new_object?} =
updated_data
|> maybe_update_history(original_data, updated: updated, use_history_in_new_object?: false)
|> maybe_update_history(original_data,
updated: updated,
use_history_in_new_object?: true,
new_data: new_data
)
updated_data =
updated_data

View file

@ -0,0 +1,65 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Object.UpdaterTest do
use Pleroma.DataCase
use Oban.Testing, repo: Pleroma.Repo
import Pleroma.Factory
alias Pleroma.Object.Updater
describe "make_update_object_data/3" do
setup do
note = insert(:note)
%{original_data: note.data}
end
test "it makes an updated field", %{original_data: original_data} do
new_data = Map.put(original_data, "content", "new content")
date = Pleroma.Web.ActivityPub.Utils.make_date()
update_object_data = Updater.make_update_object_data(original_data, new_data, date)
assert %{"updated" => ^date} = update_object_data
end
test "it creates formerRepresentations", %{original_data: original_data} do
new_data = Map.put(original_data, "content", "new content")
date = Pleroma.Web.ActivityPub.Utils.make_date()
update_object_data = Updater.make_update_object_data(original_data, new_data, date)
history_item = original_data |> Map.drop(["id", "formerRepresentations"])
assert %{
"formerRepresentations" => %{
"totalItems" => 1,
"orderedItems" => [^history_item]
}
} = update_object_data
end
end
describe "make_new_object_data_from_update_object/2" do
test "it reuses formerRepresentations if it exists" do
%{data: original_data} = insert(:note)
new_data =
original_data
|> Map.put("content", "edited")
date = Pleroma.Web.ActivityPub.Utils.make_date()
update_object_data = Updater.make_update_object_data(original_data, new_data, date)
%{
updated_data: _updated_data,
updated: updated,
used_history_in_new_object?: used_history_in_new_object?
} = Updater.make_new_object_data_from_update_object(original_data, update_object_data)
assert updated
assert used_history_in_new_object?
end
end
end