summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Suprunenko <suprunenko.s@gmail.com>2020-07-31 21:00:19 +0200
committerSergey Suprunenko <suprunenko.s@gmail.com>2020-11-29 18:10:57 +0100
commit019c211353b70764ae2e70f2f486c9790b460a61 (patch)
treea7012e5d9b0e595d0e3cf05e4f00654d226faea8
parent3000f3ff7c09057da51a07a0f51bb34bc1e4818d (diff)
Try to use custom filename in attachment_links
-rw-r--r--lib/pleroma/upload.ex2
-rw-r--r--lib/pleroma/web/common_api/utils.ex2
-rw-r--r--test/pleroma/upload_test.exs6
-rw-r--r--test/pleroma/web/mastodon_api/controllers/status_controller_test.exs46
4 files changed, 47 insertions, 9 deletions
diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex
index 47279dc0b..c7de08634 100644
--- a/lib/pleroma/upload.ex
+++ b/lib/pleroma/upload.ex
@@ -91,7 +91,7 @@ defmodule Pleroma.Upload do
}
],
"name" => description,
- "filename" => Map.get(opts, :filename) || upload.name
+ "filename" => Map.get(opts, :filename)
}}
else
{:description_limit, _} ->
diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex
index 1c74ea787..385414c41 100644
--- a/lib/pleroma/web/common_api/utils.ex
+++ b/lib/pleroma/web/common_api/utils.ex
@@ -255,7 +255,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
end
defp build_attachment_link(%{"url" => [%{"href" => href} | _]} = attachment) do
- name = attachment["name"] || URI.decode(Path.basename(href))
+ name = attachment["filename"] || attachment["name"] || URI.decode(Path.basename(href))
href = MediaProxy.url(href)
"<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
end
diff --git a/test/pleroma/upload_test.exs b/test/pleroma/upload_test.exs
index fc5b0acc4..21bdb6ee8 100644
--- a/test/pleroma/upload_test.exs
+++ b/test/pleroma/upload_test.exs
@@ -62,7 +62,7 @@ defmodule Pleroma.UploadTest do
"type" => "Link"
}
],
- "filename" => "image.jpg"
+ "filename" => nil
}}
Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
@@ -139,7 +139,7 @@ defmodule Pleroma.UploadTest do
assert data["filename"] == filename
end
- test "saves default filename if opts don't have one" do
+ test "sets filename to nil if opts don't have one" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
desc = "sample file"
@@ -154,7 +154,7 @@ defmodule Pleroma.UploadTest do
{:ok, data} = Upload.store(file, description: desc)
assert data["name"] == desc
- assert data["filename"] == filename
+ refute data["filename"]
end
@tag capture_log: true
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 3dce0de27..12df4fce2 100644
--- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
+++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
@@ -126,6 +126,38 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
)
end
+ test "posting a status with an attachment", %{user: user, conn: conn} do
+ clear_config([:instance, :attachment_links], true)
+ filename = "an_image.jpg"
+ custom_filename = "look at this.jpg"
+
+ file = %Plug.Upload{
+ content_type: "image/jpg",
+ path: Path.absname("test/fixtures/image.jpg"),
+ filename: filename
+ }
+
+ {:ok, upload} =
+ ActivityPub.upload(file,
+ actor: user.ap_id,
+ description: "test image",
+ filename: custom_filename
+ )
+
+ response =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/v1/statuses", %{
+ "status" => "cofe",
+ "spoiler_text" => "2hu",
+ "media_ids" => [to_string(upload.id)]
+ })
+ |> json_response_and_validate_schema(200)
+
+ assert String.ends_with?(response["content"], "#{filename}\">#{custom_filename}</a>")
+ assert length(response["media_attachments"]) == 1
+ end
+
test "it fails to create a status if `expires_in` is less or equal than an hour", %{
conn: conn
} do
@@ -166,22 +198,28 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
end
test "posting an undefined status with an attachment", %{user: user, conn: conn} do
+ clear_config([:instance, :attachment_links], true)
+ filename = "an_image.jpg"
+ description = "test image"
+
file = %Plug.Upload{
content_type: "image/jpeg",
path: Path.absname("test/fixtures/image.jpg"),
- filename: "an_image.jpg"
+ filename: filename
}
- {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
+ {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id, description: description)
- conn =
+ response =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/statuses", %{
"media_ids" => [to_string(upload.id)]
})
+ |> json_response_and_validate_schema(200)
- assert json_response_and_validate_schema(conn, 200)
+ assert String.ends_with?(response["content"], "#{filename}\">#{description}</a>")
+ assert length(response["media_attachments"]) == 1
end
test "replying to a status", %{user: user, conn: conn} do