aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>2020-03-19 19:10:03 +0100
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>2020-03-19 19:10:03 +0100
commitc1fd4f665335ba67336bd1b2fab2d9df5e247e08 (patch)
treeb302e2b7a69ad9284e18d08e7ef5ed6d8ef1758b /lib
parentf9d622d25a744f58fbaf8370ad4435597bb15bf0 (diff)
downloadpleroma-c1fd4f665335ba67336bd1b2fab2d9df5e247e08.tar.gz
transmogrifier.ex: rework fix_attachment for better IR
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/web/activity_pub/transmogrifier.ex45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex
index db848f657..df5ca0239 100644
--- a/lib/pleroma/web/activity_pub/transmogrifier.ex
+++ b/lib/pleroma/web/activity_pub/transmogrifier.ex
@@ -202,6 +202,51 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|> Map.put("conversation", context)
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
object
|> Map.put("attachment", [attachment])