Inject history when object is refetched

This commit is contained in:
Tusooa Zhu 2022-06-01 19:30:50 -04:00
commit fa31ae50e6
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
4 changed files with 237 additions and 23 deletions

View file

@ -26,8 +26,35 @@ defmodule Pleroma.Object.Fetcher do
end
defp maybe_reinject_internal_fields(%{data: %{} = old_data}, new_data) do
has_history? = fn
%{"formerRepresentations" => %{"orderedItems" => list}} when is_list(list) -> true
_ -> false
end
internal_fields = Map.take(old_data, Pleroma.Constants.object_internal_fields())
remote_history_exists? = has_history?.(new_data)
# If the remote history exists, we treat that as the only source of truth.
new_data =
if has_history?.(old_data) and not remote_history_exists? do
Map.put(new_data, "formerRepresentations", old_data["formerRepresentations"])
else
new_data
end
# If the remote does not have history information, we need to manage it ourselves
new_data =
if not remote_history_exists? do
changed? =
Pleroma.Constants.status_updatable_fields()
|> Enum.any?(fn field -> Map.get(old_data, field) != Map.get(new_data, field) end)
new_data |> Object.maybe_update_history(old_data, changed?)
else
new_data
end
Map.merge(new_data, internal_fields)
end