emoji pagination for pack show action
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Thu, 18 Jun 2020 15:50:03 +0000 (18:50 +0300)
committerAlexander Strizhakov <alex.strizhakov@gmail.com>
Thu, 18 Jun 2020 15:50:03 +0000 (18:50 +0300)
docs/API/pleroma_api.md
lib/pleroma/emoji/pack.ex
lib/pleroma/web/api_spec/operations/pleroma_emoji_pack_operation.ex
lib/pleroma/web/pleroma_api/controllers/emoji_pack_controller.ex
test/instance_static/emoji/test_pack/blank2.png [new file with mode: 0644]
test/instance_static/emoji/test_pack/pack.json
test/instance_static/emoji/test_pack_nonshared/nonshared.zip
test/instance_static/emoji/test_pack_nonshared/pack.json
test/web/pleroma_api/controllers/emoji_pack_controller_test.exs

index 70d4755b7ce2362bd6357529ae42a122a447bfbb..d8d3ba85f50cb7893d398d58b448136f6515ca0d 100644 (file)
@@ -450,17 +450,25 @@ The status posting endpoint takes an additional parameter, `in_reply_to_conversa
 * Response: JSON, list with updated files for updated pack (hashmap -> shortcode => filename) with status 200, either error status with error message.
 
 ## `GET /api/pleroma/emoji/packs`
+
 ### Lists local custom emoji packs
+
 * Method `GET`
 * Authentication: not required
-* Params: None
+* Params:
+  * `page`: page number for packs (default 1)
+  * `page_size`: page size for packs (default 50)
 * Response: JSON, "ok" and 200 status and the JSON hashmap of pack name to pack contents
 
 ## `GET /api/pleroma/emoji/packs/:name`
+
 ### Get pack.json for the pack
+
 * Method `GET`
 * Authentication: not required
-* Params: None
+* Params:
+  * `page`: page number for files (default 1)
+  * `page_size`: page size for files (default 50)
 * Response: JSON, pack json with `files` and `pack` keys with 200 status or 404 if the pack does not exist
 
 ## `GET /api/pleroma/emoji/packs/:name/archive`
index 5660c4c9d8e39a20a910d7765afd62ad7cca8f28..c033572c17c3819869816916a4b76792bea646a2 100644 (file)
@@ -26,10 +26,27 @@ defmodule Pleroma.Emoji.Pack do
     end
   end
 
-  @spec show(String.t()) :: {:ok, t()} | {:error, atom()}
-  def show(name) do
+  defp paginate(entities, 1, page_size), do: Enum.take(entities, page_size)
+
+  defp paginate(entities, page, page_size) do
+    entities
+    |> Enum.take(page * page_size)
+    |> Enum.take(-page_size)
+  end
+
+  @spec show(keyword()) :: {:ok, t()} | {:error, atom()}
+  def show(opts) do
+    name = opts[:name]
+
     with :ok <- validate_not_empty([name]),
          {:ok, pack} <- load_pack(name) do
+      shortcodes =
+        pack.files
+        |> Map.keys()
+        |> paginate(opts[:page], opts[:page_size])
+
+      pack = Map.put(pack, :files, Map.take(pack.files, shortcodes))
+
       {:ok, validate_pack(pack)}
     end
   end
@@ -132,17 +149,7 @@ defmodule Pleroma.Emoji.Pack do
           end
         end)
         |> Enum.reject(&is_nil/1)
-
-      packs =
-        case opts[:page] do
-          1 ->
-            Enum.take(packs, opts[:page_size])
-
-          _ ->
-            packs
-            |> Enum.take(opts[:page] * opts[:page_size])
-            |> Enum.take(-opts[:page_size])
-        end
+        |> paginate(opts[:page], opts[:page_size])
         |> Map.new(fn pack -> {pack.name, validate_pack(pack)} end)
 
       {:ok, packs}
@@ -307,7 +314,9 @@ defmodule Pleroma.Emoji.Pack do
     # Otherwise, they'd have to download it from external-src
     pack.pack["share-files"] &&
       Enum.all?(pack.files, fn {_, file} ->
-        File.exists?(Path.join(pack.path, file))
+        pack.path
+        |> Path.join(file)
+        |> File.exists?()
       end)
   end
 
index 0d842382b149fad14281f5e30395484db49fda3e..e8abe654d5bb6ff85882d5a6f77d1b7cb2b4914b 100644 (file)
@@ -58,7 +58,21 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiPackOperation do
       tags: ["Emoji Packs"],
       summary: "Show emoji pack",
       operationId: "PleromaAPI.EmojiPackController.show",
-      parameters: [name_param()],
+      parameters: [
+        name_param(),
+        Operation.parameter(
+          :page,
+          :query,
+          %Schema{type: :integer, default: 1},
+          "Page"
+        ),
+        Operation.parameter(
+          :page_size,
+          :query,
+          %Schema{type: :integer, default: 50},
+          "Number of statuses to return"
+        )
+      ],
       responses: %{
         200 => Operation.response("Emoji Pack", "application/json", emoji_pack()),
         400 => Operation.response("Bad Request", "application/json", ApiError),
index 5654b3fbe59f85e24d4849e431c1a5b6cb637215..078fb88dd00e62417afb1ffb489f0771cfc73095 100644 (file)
@@ -60,10 +60,10 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackController do
     end
   end
 
-  def show(conn, %{name: name}) do
+  def show(conn, %{name: name, page: page, page_size: page_size}) do
     name = String.trim(name)
 
-    with {:ok, pack} <- Pack.show(name) do
+    with {:ok, pack} <- Pack.show(name: name, page: page, page_size: page_size) do
       json(conn, pack)
     else
       {:error, :not_found} ->
diff --git a/test/instance_static/emoji/test_pack/blank2.png b/test/instance_static/emoji/test_pack/blank2.png
new file mode 100644 (file)
index 0000000..8f50fa0
Binary files /dev/null and b/test/instance_static/emoji/test_pack/blank2.png differ
index 481891b08f22f1c1f8e761353be3c6ea55e5f3fd..5b33fbb32615d60f8a07e37d52e31b44b174e7a1 100644 (file)
@@ -1,6 +1,7 @@
 {
     "files": {
-        "blank": "blank.png"
+        "blank": "blank.png",
+        "blank2": "blank2.png"
     },
     "pack": {
         "description": "Test description",
index 148446c642ea24b494bc3e25ccd772faaf2f2a13..59bff37f0895f17fecf4285015c5dcd18fcc8b35 100644 (file)
Binary files a/test/instance_static/emoji/test_pack_nonshared/nonshared.zip and b/test/instance_static/emoji/test_pack_nonshared/nonshared.zip differ
index 93d643a5f0a9b79e1b6376cc8c561f64a58a1c54..09f6274d17ac926e2b1dd170b04d2dff4e1263f6 100644 (file)
@@ -4,7 +4,7 @@
         "homepage": "https://pleroma.social",
         "description": "Test description",
         "fallback-src": "https://nonshared-pack",
-        "fallback-src-sha256": "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF",
+        "fallback-src-sha256": "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D",
         "share-files": false
     },
     "files": {
index aafca6359dab0a4e058ad961ac38c307632b99c3..f6239cae56260c123e23155f8d5ad78ec49a0b5e 100644 (file)
@@ -31,7 +31,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
     resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
 
     shared = resp["test_pack"]
-    assert shared["files"] == %{"blank" => "blank.png"}
+    assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"}
     assert Map.has_key?(shared["pack"], "download-sha256")
     assert shared["pack"]["can-download"]
     assert shared["pack"]["share-files"]
@@ -354,7 +354,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
         Map.put(
           new_data,
           "fallback-src-sha256",
-          "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
+          "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
         )
 
       assert ctx[:admin_conn]
@@ -420,7 +420,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
       assert admin_conn
              |> put_req_header("content-type", "multipart/form-data")
              |> post("/api/pleroma/emoji/packs/test_pack/files", %{
-               shortcode: "blank2",
+               shortcode: "blank3",
                filename: "dir/blank.png",
                file: %Plug.Upload{
                  filename: "blank.png",
@@ -429,7 +429,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
              })
              |> json_response_and_validate_schema(200) == %{
                "blank" => "blank.png",
-               "blank2" => "dir/blank.png"
+               "blank2" => "blank2.png",
+               "blank3" => "dir/blank.png"
              }
 
       assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
@@ -453,7 +454,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
       assert admin_conn
              |> put_req_header("content-type", "multipart/form-data")
              |> post("/api/pleroma/emoji/packs/test_pack/files", %{
-               shortcode: "blank2",
+               shortcode: "blank3",
                filename: "dir/blank.png",
                file: %Plug.Upload{
                  filename: "blank.png",
@@ -462,7 +463,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
              })
              |> json_response_and_validate_schema(200) == %{
                "blank" => "blank.png",
-               "blank2" => "dir/blank.png"
+               "blank2" => "blank2.png",
+               "blank3" => "dir/blank.png"
              }
 
       assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
@@ -470,14 +472,15 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
       assert admin_conn
              |> put_req_header("content-type", "multipart/form-data")
              |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
-               shortcode: "blank2",
-               new_shortcode: "blank3",
+               shortcode: "blank3",
+               new_shortcode: "blank4",
                new_filename: "dir_2/blank_3.png",
                force: true
              })
              |> json_response_and_validate_schema(200) == %{
                "blank" => "blank.png",
-               "blank3" => "dir_2/blank_3.png"
+               "blank2" => "blank2.png",
+               "blank4" => "dir_2/blank_3.png"
              }
 
       assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
@@ -503,7 +506,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
       assert admin_conn
              |> put_req_header("content-type", "multipart/form-data")
              |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
-               shortcode: "blank2",
+               shortcode: "blank3",
                filename: "dir/blank.png",
                file: %Plug.Upload{
                  filename: "blank.png",
@@ -557,7 +560,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
              })
              |> json_response_and_validate_schema(200) == %{
                "blank" => "blank.png",
-               "blank4" => "dir/blank.png"
+               "blank4" => "dir/blank.png",
+               "blank2" => "blank2.png"
              }
 
       assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
@@ -571,7 +575,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
              })
              |> json_response_and_validate_schema(200) == %{
                "blank3" => "dir_2/blank_3.png",
-               "blank" => "blank.png"
+               "blank" => "blank.png",
+               "blank2" => "blank2.png"
              }
 
       refute File.exists?("#{@emoji_path}/test_pack/dir/")
@@ -579,7 +584,10 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
 
       assert admin_conn
              |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
-             |> json_response_and_validate_schema(200) == %{"blank" => "blank.png"}
+             |> json_response_and_validate_schema(200) == %{
+               "blank" => "blank.png",
+               "blank2" => "blank2.png"
+             }
 
       refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
 
@@ -603,7 +611,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
              })
              |> json_response_and_validate_schema(200) == %{
                "blank_url" => "blank_url.png",
-               "blank" => "blank.png"
+               "blank" => "blank.png",
+               "blank2" => "blank2.png"
              }
 
       assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
@@ -624,15 +633,16 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
              })
              |> json_response_and_validate_schema(200) == %{
                "shortcode" => "shortcode.png",
-               "blank" => "blank.png"
+               "blank" => "blank.png",
+               "blank2" => "blank2.png"
              }
     end
 
     test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
       assert admin_conn
-             |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank2")
+             |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
              |> json_response_and_validate_schema(:bad_request) == %{
-               "error" => "Emoji \"blank2\" does not exist"
+               "error" => "Emoji \"blank3\" does not exist"
              }
     end
 
@@ -640,12 +650,12 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
       assert admin_conn
              |> put_req_header("content-type", "multipart/form-data")
              |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
-               shortcode: "blank2",
-               new_shortcode: "blank3",
+               shortcode: "blank3",
+               new_shortcode: "blank4",
                new_filename: "dir_2/blank_3.png"
              })
              |> json_response_and_validate_schema(:bad_request) == %{
-               "error" => "Emoji \"blank2\" does not exist"
+               "error" => "Emoji \"blank3\" does not exist"
              }
     end
 
@@ -768,7 +778,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
   describe "GET /api/pleroma/emoji/packs/:name" do
     test "shows pack.json", %{conn: conn} do
       assert %{
-               "files" => %{"blank" => "blank.png"},
+               "files" => files,
                "pack" => %{
                  "can-download" => true,
                  "description" => "Test description",
@@ -781,6 +791,26 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
                conn
                |> get("/api/pleroma/emoji/packs/test_pack")
                |> json_response_and_validate_schema(200)
+
+      assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"}
+
+      assert %{
+               "files" => files
+             } =
+               conn
+               |> get("/api/pleroma/emoji/packs/test_pack?page_size=1")
+               |> json_response_and_validate_schema(200)
+
+      assert files |> Map.keys() |> length() == 1
+
+      assert %{
+               "files" => files
+             } =
+               conn
+               |> get("/api/pleroma/emoji/packs/test_pack?page_size=1&page=2")
+               |> json_response_and_validate_schema(200)
+
+      assert files |> Map.keys() |> length() == 1
     end
 
     test "non existing pack", %{conn: conn} do