Record edit history for Note and Question Updates

This commit is contained in:
Tusooa Zhu 2022-05-29 13:54:16 -04:00
commit 5e8aac0e07
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
3 changed files with 72 additions and 2 deletions

View file

@ -410,6 +410,26 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
{:ok, object, meta}
end
defp history_for_object(object) do
with history <- Map.get(object, "formerRepresentations"),
true <- is_map(history),
"OrderedCollection" <- Map.get(history, "type"),
true <- is_list(Map.get(history, "orderedItems")),
true <- is_integer(Map.get(history, "totalItems")) do
history
else
_ -> history_skeleton()
end
end
defp history_skeleton do
%{
"type" => "OrderedCollection",
"totalItems" => 0,
"orderedItems" => []
}
end
@updatable_object_types ["Note", "Question"]
# We do not allow poll options to be changed, but the poll description can be.
@updatable_fields [
@ -431,6 +451,19 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
orig_object_data = orig_object.data
if orig_object_data["type"] in @updatable_object_types do
# Put edit history
# Note that we may have got the edit history by first fetching the object
history = history_for_object(orig_object_data)
latest_history_item =
orig_object_data
|> Map.drop(["id", "formerRepresentations"])
new_history =
history
|> Map.put("orderedItems", [latest_history_item | history["orderedItems"]])
|> Map.put("totalItems", history["totalItems"] + 1)
updated_object_data =
@updatable_fields
|> Enum.reduce(
@ -443,6 +476,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
end
end
)
|> Map.put("formerRepresentations", new_history)
orig_object
|> Object.change(%{data: updated_object_data})