transmogrifier.ex: rework fix_attachment for better IR
This commit is contained in:
parent
f9d622d25a
commit
c1fd4f6653
|
@ -202,6 +202,51 @@ def fix_context(object) do
|
||||||
|> Map.put("conversation", context)
|
|> Map.put("conversation", context)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp add_if_present(map, _key, nil), do: map
|
||||||
|
|
||||||
|
defp add_if_present(map, key, value) do
|
||||||
|
Map.put(map, key, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
|
||||||
|
attachments =
|
||||||
|
Enum.map(attachment, fn data ->
|
||||||
|
url =
|
||||||
|
cond do
|
||||||
|
is_list(data["url"]) -> List.first(data["url"])
|
||||||
|
is_map(data["url"]) -> data["url"]
|
||||||
|
true -> nil
|
||||||
|
end
|
||||||
|
|
||||||
|
media_type =
|
||||||
|
cond do
|
||||||
|
is_map(url) && is_binary(url["mediaType"]) -> url["mediaType"]
|
||||||
|
is_binary(data["mediaType"]) -> data["mediaType"]
|
||||||
|
is_binary(data["mimeType"]) -> data["mimeType"]
|
||||||
|
true -> nil
|
||||||
|
end
|
||||||
|
|
||||||
|
href =
|
||||||
|
cond do
|
||||||
|
is_map(url) && is_binary(url["href"]) -> url["href"]
|
||||||
|
is_binary(data["url"]) -> data["url"]
|
||||||
|
is_binary(data["href"]) -> data["href"]
|
||||||
|
end
|
||||||
|
|
||||||
|
attachment_url =
|
||||||
|
%{"href" => href}
|
||||||
|
|> add_if_present("mediaType", media_type)
|
||||||
|
|> add_if_present("type", Map.get(url || %{}, "type"))
|
||||||
|
|
||||||
|
%{"url" => [attachment_url]}
|
||||||
|
|> add_if_present("mediaType", media_type)
|
||||||
|
|> add_if_present("type", data["type"])
|
||||||
|
|> add_if_present("name", data["name"])
|
||||||
|
end)
|
||||||
|
|
||||||
|
Map.put(object, "attachment", attachments)
|
||||||
|
end
|
||||||
|
|
||||||
def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
|
def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
|
||||||
object
|
object
|
||||||
|> Map.put("attachment", [attachment])
|
|> Map.put("attachment", [attachment])
|
||||||
|
|
|
@ -1228,19 +1228,13 @@ test "it remaps video URLs as attachments if necessary" do
|
||||||
attachment = %{
|
attachment = %{
|
||||||
"type" => "Link",
|
"type" => "Link",
|
||||||
"mediaType" => "video/mp4",
|
"mediaType" => "video/mp4",
|
||||||
"href" =>
|
|
||||||
"https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
|
|
||||||
"mimeType" => "video/mp4",
|
|
||||||
"size" => 5_015_880,
|
|
||||||
"url" => [
|
"url" => [
|
||||||
%{
|
%{
|
||||||
"href" =>
|
"href" =>
|
||||||
"https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
|
"https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
|
||||||
"mediaType" => "video/mp4",
|
"mediaType" => "video/mp4"
|
||||||
"type" => "Link"
|
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
"width" => 480
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert object.data["url"] ==
|
assert object.data["url"] ==
|
||||||
|
@ -2067,11 +2061,7 @@ test "returns modified object when attachment is map" do
|
||||||
%{
|
%{
|
||||||
"mediaType" => "video/mp4",
|
"mediaType" => "video/mp4",
|
||||||
"url" => [
|
"url" => [
|
||||||
%{
|
%{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"}
|
||||||
"href" => "https://peertube.moe/stat-480.mp4",
|
|
||||||
"mediaType" => "video/mp4",
|
|
||||||
"type" => "Link"
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -2089,23 +2079,13 @@ test "returns modified object when attachment is list" do
|
||||||
%{
|
%{
|
||||||
"mediaType" => "video/mp4",
|
"mediaType" => "video/mp4",
|
||||||
"url" => [
|
"url" => [
|
||||||
%{
|
%{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
|
||||||
"href" => "https://pe.er/stat-480.mp4",
|
|
||||||
"mediaType" => "video/mp4",
|
|
||||||
"type" => "Link"
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
"href" => "https://pe.er/stat-480.mp4",
|
|
||||||
"mediaType" => "video/mp4",
|
"mediaType" => "video/mp4",
|
||||||
"mimeType" => "video/mp4",
|
|
||||||
"url" => [
|
"url" => [
|
||||||
%{
|
%{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"}
|
||||||
"href" => "https://pe.er/stat-480.mp4",
|
|
||||||
"mediaType" => "video/mp4",
|
|
||||||
"type" => "Link"
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue