diff options
author | Haelwenn (lanodan) Monnier <contact@hacktivis.me> | 2021-08-28 03:53:56 +0200 |
---|---|---|
committer | Haelwenn (lanodan) Monnier <contact@hacktivis.me> | 2021-08-28 05:41:34 +0200 |
commit | 1d49a440b2ce9e4911954cf09182aebcd4821601 (patch) | |
tree | d2a5c19b498ae1857cd965ed1eed36111d925513 /lib/pleroma/web/common_api | |
parent | 84ec0fbeaadc8bdbce256212258a932530088346 (diff) | |
download | pleroma-features/attachment_direct.tar.gz |
CommonAPI: Allow URLs into media_idsfeatures/attachment_direct
Diffstat (limited to 'lib/pleroma/web/common_api')
-rw-r--r-- | lib/pleroma/web/common_api/utils.ex | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index b6feaf32a..f2359c93a 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -23,6 +23,19 @@ defmodule Pleroma.Web.CommonAPI.Utils do require Logger require Pleroma.Constants + defp raw_url_to_attachment(url, desc \\ nil) do + %{ + "type" => "Document", + "name" => desc, + "url" => [ + %{ + "type" => "Link", + "href" => url + } + ] + } + end + def attachments_from_ids(%{media_ids: ids, descriptions: desc}) do attachments_from_ids_descs(ids, desc) end @@ -36,11 +49,15 @@ defmodule Pleroma.Web.CommonAPI.Utils do def attachments_from_ids_no_descs([]), do: [] def attachments_from_ids_no_descs(ids) do - Enum.map(ids, fn media_id -> - case Repo.get(Object, media_id) do - %Object{data: data} -> data - _ -> nil - end + Enum.map(ids, fn + "http" <> _ = id -> + raw_url_to_attachment(id) + + media_id -> + case Repo.get(Object, media_id) do + %Object{data: data} -> data + _ -> nil + end end) |> Enum.reject(&is_nil/1) end @@ -50,10 +67,14 @@ defmodule Pleroma.Web.CommonAPI.Utils do def attachments_from_ids_descs(ids, descs_str) do {_, descs} = Jason.decode(descs_str) - Enum.map(ids, fn media_id -> - with %Object{data: data} <- Repo.get(Object, media_id) do - Map.put(data, "name", descs[media_id]) - end + Enum.map(ids, fn + "http" <> _ = media_id -> + raw_url_to_attachment(media_id, descs[media_id]) + + media_id -> + with %Object{data: data} <- Repo.get(Object, media_id) do + Map.put(data, "name", descs[media_id]) + end end) |> Enum.reject(&is_nil/1) end |