Strip internal fields in formerRepresentation

This commit is contained in:
Tusooa Zhu 2022-06-01 12:02:03 -04:00
parent b613a9ec6b
commit 410e177b2a
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
2 changed files with 72 additions and 1 deletions

View File

@ -902,7 +902,24 @@ def prepare_attachments(object) do
end end
def strip_internal_fields(object) do def strip_internal_fields(object) do
Map.drop(object, Pleroma.Constants.object_internal_fields()) outer = Map.drop(object, Pleroma.Constants.object_internal_fields())
case outer do
%{"formerRepresentations" => %{"orderedItems" => list}} when is_list(list) ->
update_in(
outer["formerRepresentations"]["orderedItems"],
&Enum.map(
&1,
fn
item when is_map(item) -> Map.drop(item, Pleroma.Constants.object_internal_fields())
item -> item
end
)
)
_ ->
outer
end
end end
defp strip_internal_tags(%{"tag" => tags} = object) do defp strip_internal_tags(%{"tag" => tags} = object) do

View File

@ -575,4 +575,58 @@ test "puts dimensions into attachment url field" do
assert Transmogrifier.fix_attachments(object) == expected assert Transmogrifier.fix_attachments(object) == expected
end end
end end
describe "strip_internal_fields/1" do
test "it strips internal fields in formerRepresentations" do
original = %{
"formerRepresentations" => %{
"orderedItems" => [
%{"generator" => %{}}
]
}
}
stripped = Transmogrifier.strip_internal_fields(original)
refute Map.has_key?(
Enum.at(stripped["formerRepresentations"]["orderedItems"], 0),
"generator"
)
end
test "it strips internal fields in maybe badly-formed formerRepresentations" do
original = %{
"formerRepresentations" => %{
"orderedItems" => [
%{"generator" => %{}},
"https://example.com/1"
]
}
}
stripped = Transmogrifier.strip_internal_fields(original)
refute Map.has_key?(
Enum.at(stripped["formerRepresentations"]["orderedItems"], 0),
"generator"
)
assert Enum.at(stripped["formerRepresentations"]["orderedItems"], 1) ==
"https://example.com/1"
end
test "it ignores if formerRepresentations does not look like an OrderedCollection" do
original = %{
"formerRepresentations" => %{
"items" => [
%{"generator" => %{}}
]
}
}
stripped = Transmogrifier.strip_internal_fields(original)
assert Map.has_key?(Enum.at(stripped["formerRepresentations"]["items"], 0), "generator")
end
end
end end