Make MRF Keyword history-aware

This commit is contained in:
Tusooa Zhu 2022-07-23 15:56:36 -04:00
commit eba9b0760f
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
4 changed files with 231 additions and 12 deletions

View file

@ -197,4 +197,44 @@ defmodule Pleroma.Object.Updater do
used_history_in_new_object?: used_history_in_new_object?
}
end
defp for_each_history_item(%{"orderedItems" => items} = history, _object, fun) do
new_items =
Enum.map(items, fun)
|> Enum.reduce_while(
{:ok, []},
fn
{:ok, item}, {:ok, acc} -> {:cont, {:ok, acc ++ [item]}}
e, _acc -> {:halt, e}
end
)
case new_items do
{:ok, items} -> {:ok, Map.put(history, "orderedItems", items)}
e -> e
end
end
defp for_each_history_item(history, _, _) do
{:ok, history}
end
def do_with_history(object, fun) do
with history <- object["formerRepresentations"],
object <- Map.drop(object, ["formerRepresentations"]),
{_, {:ok, object}} <- {:main_body, fun.(object)},
{_, {:ok, history}} <- {:history_items, for_each_history_item(history, object, fun)} do
object =
if history do
Map.put(object, "formerRepresentations", history)
else
object
end
{:ok, object}
else
{:main_body, e} -> e
{:history_items, e} -> e
end
end
end