summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>2021-08-28 03:53:56 +0200
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>2021-08-28 05:41:34 +0200
commit1d49a440b2ce9e4911954cf09182aebcd4821601 (patch)
treed2a5c19b498ae1857cd965ed1eed36111d925513
parent84ec0fbeaadc8bdbce256212258a932530088346 (diff)
CommonAPI: Allow URLs into media_idsfeatures/attachment_direct
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/pleroma/web/api_spec/operations/status_operation.ex2
-rw-r--r--lib/pleroma/web/common_api/utils.ex39
-rw-r--r--test/pleroma/web/common_api/utils_test.exs10
-rw-r--r--test/pleroma/web/mastodon_api/controllers/status_controller_test.exs12
5 files changed, 54 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 231cac990..f7c16393f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## Unreleased
### Changed
+- MastodonAPI: Allow to pass arbitrary URLs when creating a status
### Added
diff --git a/lib/pleroma/web/api_spec/operations/status_operation.ex b/lib/pleroma/web/api_spec/operations/status_operation.ex
index 802fbef3e..bed0d1eac 100644
--- a/lib/pleroma/web/api_spec/operations/status_operation.ex
+++ b/lib/pleroma/web/api_spec/operations/status_operation.ex
@@ -453,7 +453,7 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do
nullable: true,
type: :array,
items: %Schema{type: :string},
- description: "Array of Attachment ids to be attached as media."
+ description: "Array of Attachment ids or URLs to be attached as media."
},
poll: poll_params(),
in_reply_to_id: %Schema{
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
diff --git a/test/pleroma/web/common_api/utils_test.exs b/test/pleroma/web/common_api/utils_test.exs
index d8fec3520..a12332cad 100644
--- a/test/pleroma/web/common_api/utils_test.exs
+++ b/test/pleroma/web/common_api/utils_test.exs
@@ -655,6 +655,16 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
end
+ test "returns attachment object with raw URL" do
+ assert Utils.attachments_from_ids(%{media_ids: ["https://example.org/corndog.jpeg"]}) == [
+ %{
+ "name" => nil,
+ "type" => "Document",
+ "url" => [%{"href" => "https://example.org/corndog.jpeg", "type" => "Link"}]
+ }
+ ]
+ end
+
test "returns [] when not pass media_ids" do
assert Utils.attachments_from_ids(%{}) == []
end
diff --git a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
index ed66d370a..73dfd40f1 100644
--- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
+++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
@@ -182,6 +182,18 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
assert json_response_and_validate_schema(conn, 200)
end
+ test "posting an undefined status with arbitrary URL as attachment", %{conn: conn} do
+ assert response =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/v1/statuses", %{
+ "media_ids" => ["https://example.org/corndog.jpeg"]
+ })
+ |> json_response_and_validate_schema(200)
+
+ assert [%{"url" => "https://example.org/corndog.jpeg"}] = response["media_attachments"]
+ end
+
test "replying to a status", %{user: user, conn: conn} do
{:ok, replied_to} = CommonAPI.post(user, %{status: "cofe"})