Pleroma.Upload: Set default upload name / description based on config.
authorlain <lain@soykaf.club>
Tue, 18 Aug 2020 11:21:30 +0000 (13:21 +0200)
committerlain <lain@soykaf.club>
Tue, 18 Aug 2020 11:21:30 +0000 (13:21 +0200)
config/config.exs
lib/pleroma/upload.ex
test/web/activity_pub/activity_pub_test.exs

index a7c9e54b163b57bf3d9a03700016927ae743caf6..1ed3157c3d82b0b84f77e4e62dfb78dd9a0199de 100644 (file)
@@ -72,7 +72,8 @@ config :pleroma, Pleroma.Upload,
       pool: :upload
     ]
   ],
-  filename_display_max_length: 30
+  filename_display_max_length: 30,
+  default_description: nil
 
 config :pleroma, Pleroma.Uploaders.Local, uploads: "uploads"
 
index 0fa6b89dc1c6e53e2dfb62689c049a5a41f19aed..015c875938967e3fed944f7eb21b46a85274cb96 100644 (file)
@@ -56,6 +56,15 @@ defmodule Pleroma.Upload do
         }
   defstruct [:id, :name, :tempfile, :content_type, :path]
 
+  defp get_description(opts, upload) do
+    case {opts[:description], Pleroma.Config.get([Pleroma.Upload, :default_description])} do
+      {description, _} when is_binary(description) -> description
+      {_, :filename} -> upload.name
+      {_, str} when is_binary(str) -> str
+      _ -> ""
+    end
+  end
+
   @spec store(source, options :: [option()]) :: {:ok, Map.t()} | {:error, any()}
   def store(upload, opts \\ []) do
     opts = get_opts(opts)
@@ -63,7 +72,7 @@ defmodule Pleroma.Upload do
     with {:ok, upload} <- prepare_upload(upload, opts),
          upload = %__MODULE__{upload | path: upload.path || "#{upload.id}/#{upload.name}"},
          {:ok, upload} <- Pleroma.Upload.Filter.filter(opts.filters, upload),
-         description = Map.get(opts, :description) || upload.name,
+         description = get_description(opts, upload),
          {_, true} <-
            {:description_limit,
             String.length(description) <= Pleroma.Config.get([:instance, :description_limit])},
index d6eab7337c7022f1fb7056d596a8fc9e45ad9ced..03f968aafa3be3a4f7a3d068decd70e36f0ac763 100644 (file)
@@ -990,13 +990,39 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   end
 
   describe "uploading files" do
-    test "copies the file to the configured folder" do
-      file = %Plug.Upload{
+    setup do
+      test_file = %Plug.Upload{
         content_type: "image/jpg",
         path: Path.absname("test/fixtures/image.jpg"),
         filename: "an_image.jpg"
       }
 
+      %{test_file: test_file}
+    end
+
+    test "sets a description if given", %{test_file: file} do
+      {:ok, %Object{} = object} = ActivityPub.upload(file, description: "a cool file")
+      assert object.data["name"] == "a cool file"
+    end
+
+    test "it sets the default description depending on the configuration", %{test_file: file} do
+      clear_config([Pleroma.Upload, :default_description])
+
+      Pleroma.Config.put([Pleroma.Upload, :default_description], nil)
+      {:ok, %Object{} = object} = ActivityPub.upload(file)
+      assert object.data["name"] == ""
+
+      Pleroma.Config.put([Pleroma.Upload, :default_description], :filename)
+      {:ok, %Object{} = object} = ActivityPub.upload(file)
+      assert object.data["name"] == "an_image.jpg"
+
+      Pleroma.Config.put([Pleroma.Upload, :default_description], "unnamed attachment")
+      {:ok, %Object{} = object} = ActivityPub.upload(file)
+      assert object.data["name"] == "unnamed attachment"
+    end
+
+    test "copies the file to the configured folder", %{test_file: file} do
+      clear_config([Pleroma.Upload, :default_description], :filename)
       {:ok, %Object{} = object} = ActivityPub.upload(file)
       assert object.data["name"] == "an_image.jpg"
     end