CommonAPI: Allow URLs into media_ids
This commit is contained in:
parent
84ec0fbeaa
commit
1d49a440b2
@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
- MastodonAPI: Allow to pass arbitrary URLs when creating a status
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
@ -453,7 +453,7 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do
|
|||||||
nullable: true,
|
nullable: true,
|
||||||
type: :array,
|
type: :array,
|
||||||
items: %Schema{type: :string},
|
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(),
|
poll: poll_params(),
|
||||||
in_reply_to_id: %Schema{
|
in_reply_to_id: %Schema{
|
||||||
|
@ -23,6 +23,19 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
|||||||
require Logger
|
require Logger
|
||||||
require Pleroma.Constants
|
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
|
def attachments_from_ids(%{media_ids: ids, descriptions: desc}) do
|
||||||
attachments_from_ids_descs(ids, desc)
|
attachments_from_ids_descs(ids, desc)
|
||||||
end
|
end
|
||||||
@ -36,11 +49,15 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
|||||||
def attachments_from_ids_no_descs([]), do: []
|
def attachments_from_ids_no_descs([]), do: []
|
||||||
|
|
||||||
def attachments_from_ids_no_descs(ids) do
|
def attachments_from_ids_no_descs(ids) do
|
||||||
Enum.map(ids, fn media_id ->
|
Enum.map(ids, fn
|
||||||
case Repo.get(Object, media_id) do
|
"http" <> _ = id ->
|
||||||
%Object{data: data} -> data
|
raw_url_to_attachment(id)
|
||||||
_ -> nil
|
|
||||||
end
|
media_id ->
|
||||||
|
case Repo.get(Object, media_id) do
|
||||||
|
%Object{data: data} -> data
|
||||||
|
_ -> nil
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|> Enum.reject(&is_nil/1)
|
|> Enum.reject(&is_nil/1)
|
||||||
end
|
end
|
||||||
@ -50,10 +67,14 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
|||||||
def attachments_from_ids_descs(ids, descs_str) do
|
def attachments_from_ids_descs(ids, descs_str) do
|
||||||
{_, descs} = Jason.decode(descs_str)
|
{_, descs} = Jason.decode(descs_str)
|
||||||
|
|
||||||
Enum.map(ids, fn media_id ->
|
Enum.map(ids, fn
|
||||||
with %Object{data: data} <- Repo.get(Object, media_id) do
|
"http" <> _ = media_id ->
|
||||||
Map.put(data, "name", descs[media_id])
|
raw_url_to_attachment(media_id, descs[media_id])
|
||||||
end
|
|
||||||
|
media_id ->
|
||||||
|
with %Object{data: data} <- Repo.get(Object, media_id) do
|
||||||
|
Map.put(data, "name", descs[media_id])
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|> Enum.reject(&is_nil/1)
|
|> Enum.reject(&is_nil/1)
|
||||||
end
|
end
|
||||||
|
@ -655,6 +655,16 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
|
|||||||
assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
|
assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
|
||||||
end
|
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
|
test "returns [] when not pass media_ids" do
|
||||||
assert Utils.attachments_from_ids(%{}) == []
|
assert Utils.attachments_from_ids(%{}) == []
|
||||||
end
|
end
|
||||||
|
@ -182,6 +182,18 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
|
|||||||
assert json_response_and_validate_schema(conn, 200)
|
assert json_response_and_validate_schema(conn, 200)
|
||||||
end
|
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
|
test "replying to a status", %{user: user, conn: conn} do
|
||||||
{:ok, replied_to} = CommonAPI.post(user, %{status: "cofe"})
|
{:ok, replied_to} = CommonAPI.post(user, %{status: "cofe"})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user