emoji pagination for pack show action

This commit is contained in:
Alexander Strizhakov 2020-06-18 18:50:03 +03:00
parent 3becdafd33
commit 4975ed86bc
No known key found for this signature in database
GPG Key ID: 022896A53AEF1381
9 changed files with 104 additions and 42 deletions

View 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. * 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` ## `GET /api/pleroma/emoji/packs`
### Lists local custom emoji packs ### Lists local custom emoji packs
* Method `GET` * Method `GET`
* Authentication: not required * 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 * Response: JSON, "ok" and 200 status and the JSON hashmap of pack name to pack contents
## `GET /api/pleroma/emoji/packs/:name` ## `GET /api/pleroma/emoji/packs/:name`
### Get pack.json for the pack ### Get pack.json for the pack
* Method `GET` * Method `GET`
* Authentication: not required * 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 * 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` ## `GET /api/pleroma/emoji/packs/:name/archive`

View File

@ -26,10 +26,27 @@ def create(name) do
end end
end end
@spec show(String.t()) :: {:ok, t()} | {:error, atom()} defp paginate(entities, 1, page_size), do: Enum.take(entities, page_size)
def show(name) do
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]), with :ok <- validate_not_empty([name]),
{:ok, pack} <- load_pack(name) do {: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)} {:ok, validate_pack(pack)}
end end
end end
@ -132,17 +149,7 @@ def list_local(opts) do
end end
end) end)
|> Enum.reject(&is_nil/1) |> Enum.reject(&is_nil/1)
|> paginate(opts[:page], opts[:page_size])
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
|> Map.new(fn pack -> {pack.name, validate_pack(pack)} end) |> Map.new(fn pack -> {pack.name, validate_pack(pack)} end)
{:ok, packs} {:ok, packs}
@ -307,7 +314,9 @@ defp downloadable?(pack) do
# Otherwise, they'd have to download it from external-src # Otherwise, they'd have to download it from external-src
pack.pack["share-files"] && pack.pack["share-files"] &&
Enum.all?(pack.files, fn {_, file} -> Enum.all?(pack.files, fn {_, file} ->
File.exists?(Path.join(pack.path, file)) pack.path
|> Path.join(file)
|> File.exists?()
end) end)
end end

View File

@ -58,7 +58,21 @@ def show_operation do
tags: ["Emoji Packs"], tags: ["Emoji Packs"],
summary: "Show emoji pack", summary: "Show emoji pack",
operationId: "PleromaAPI.EmojiPackController.show", 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: %{ responses: %{
200 => Operation.response("Emoji Pack", "application/json", emoji_pack()), 200 => Operation.response("Emoji Pack", "application/json", emoji_pack()),
400 => Operation.response("Bad Request", "application/json", ApiError), 400 => Operation.response("Bad Request", "application/json", ApiError),

View File

@ -60,10 +60,10 @@ def index(conn, params) do
end end
end end
def show(conn, %{name: name}) do def show(conn, %{name: name, page: page, page_size: page_size}) do
name = String.trim(name) 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) json(conn, pack)
else else
{:error, :not_found} -> {:error, :not_found} ->

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 B

View File

@ -1,6 +1,7 @@
{ {
"files": { "files": {
"blank": "blank.png" "blank": "blank.png",
"blank2": "blank2.png"
}, },
"pack": { "pack": {
"description": "Test description", "description": "Test description",

View File

@ -4,7 +4,7 @@
"homepage": "https://pleroma.social", "homepage": "https://pleroma.social",
"description": "Test description", "description": "Test description",
"fallback-src": "https://nonshared-pack", "fallback-src": "https://nonshared-pack",
"fallback-src-sha256": "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF", "fallback-src-sha256": "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D",
"share-files": false "share-files": false
}, },
"files": { "files": {

View File

@ -31,7 +31,7 @@ test "GET /api/pleroma/emoji/packs", %{conn: conn} do
resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200) resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
shared = resp["test_pack"] 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 Map.has_key?(shared["pack"], "download-sha256")
assert shared["pack"]["can-download"] assert shared["pack"]["can-download"]
assert shared["pack"]["share-files"] assert shared["pack"]["share-files"]
@ -354,7 +354,7 @@ test "for a pack with a fallback source", ctx do
Map.put( Map.put(
new_data, new_data,
"fallback-src-sha256", "fallback-src-sha256",
"74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF" "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
) )
assert ctx[:admin_conn] assert ctx[:admin_conn]
@ -420,7 +420,7 @@ test "don't rewrite old emoji", %{admin_conn: admin_conn} do
assert admin_conn assert admin_conn
|> put_req_header("content-type", "multipart/form-data") |> put_req_header("content-type", "multipart/form-data")
|> post("/api/pleroma/emoji/packs/test_pack/files", %{ |> post("/api/pleroma/emoji/packs/test_pack/files", %{
shortcode: "blank2", shortcode: "blank3",
filename: "dir/blank.png", filename: "dir/blank.png",
file: %Plug.Upload{ file: %Plug.Upload{
filename: "blank.png", filename: "blank.png",
@ -429,7 +429,8 @@ test "don't rewrite old emoji", %{admin_conn: admin_conn} do
}) })
|> json_response_and_validate_schema(200) == %{ |> json_response_and_validate_schema(200) == %{
"blank" => "blank.png", "blank" => "blank.png",
"blank2" => "dir/blank.png" "blank2" => "blank2.png",
"blank3" => "dir/blank.png"
} }
assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png") assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
@ -453,7 +454,7 @@ test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
assert admin_conn assert admin_conn
|> put_req_header("content-type", "multipart/form-data") |> put_req_header("content-type", "multipart/form-data")
|> post("/api/pleroma/emoji/packs/test_pack/files", %{ |> post("/api/pleroma/emoji/packs/test_pack/files", %{
shortcode: "blank2", shortcode: "blank3",
filename: "dir/blank.png", filename: "dir/blank.png",
file: %Plug.Upload{ file: %Plug.Upload{
filename: "blank.png", filename: "blank.png",
@ -462,7 +463,8 @@ test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
}) })
|> json_response_and_validate_schema(200) == %{ |> json_response_and_validate_schema(200) == %{
"blank" => "blank.png", "blank" => "blank.png",
"blank2" => "dir/blank.png" "blank2" => "blank2.png",
"blank3" => "dir/blank.png"
} }
assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png") assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
@ -470,14 +472,15 @@ test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
assert admin_conn assert admin_conn
|> put_req_header("content-type", "multipart/form-data") |> put_req_header("content-type", "multipart/form-data")
|> patch("/api/pleroma/emoji/packs/test_pack/files", %{ |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
shortcode: "blank2", shortcode: "blank3",
new_shortcode: "blank3", new_shortcode: "blank4",
new_filename: "dir_2/blank_3.png", new_filename: "dir_2/blank_3.png",
force: true force: true
}) })
|> json_response_and_validate_schema(200) == %{ |> json_response_and_validate_schema(200) == %{
"blank" => "blank.png", "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") assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
@ -503,7 +506,7 @@ test "add file with not loaded pack", %{admin_conn: admin_conn} do
assert admin_conn assert admin_conn
|> put_req_header("content-type", "multipart/form-data") |> put_req_header("content-type", "multipart/form-data")
|> post("/api/pleroma/emoji/packs/not_loaded/files", %{ |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
shortcode: "blank2", shortcode: "blank3",
filename: "dir/blank.png", filename: "dir/blank.png",
file: %Plug.Upload{ file: %Plug.Upload{
filename: "blank.png", filename: "blank.png",
@ -557,7 +560,8 @@ test "new with shortcode as file with update", %{admin_conn: admin_conn} do
}) })
|> json_response_and_validate_schema(200) == %{ |> json_response_and_validate_schema(200) == %{
"blank" => "blank.png", "blank" => "blank.png",
"blank4" => "dir/blank.png" "blank4" => "dir/blank.png",
"blank2" => "blank2.png"
} }
assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png") assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
@ -571,7 +575,8 @@ test "new with shortcode as file with update", %{admin_conn: admin_conn} do
}) })
|> json_response_and_validate_schema(200) == %{ |> json_response_and_validate_schema(200) == %{
"blank3" => "dir_2/blank_3.png", "blank3" => "dir_2/blank_3.png",
"blank" => "blank.png" "blank" => "blank.png",
"blank2" => "blank2.png"
} }
refute File.exists?("#{@emoji_path}/test_pack/dir/") refute File.exists?("#{@emoji_path}/test_pack/dir/")
@ -579,7 +584,10 @@ test "new with shortcode as file with update", %{admin_conn: admin_conn} do
assert admin_conn assert admin_conn
|> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3") |> 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/") refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
@ -603,7 +611,8 @@ test "new with shortcode from url", %{admin_conn: admin_conn} do
}) })
|> json_response_and_validate_schema(200) == %{ |> json_response_and_validate_schema(200) == %{
"blank_url" => "blank_url.png", "blank_url" => "blank_url.png",
"blank" => "blank.png" "blank" => "blank.png",
"blank2" => "blank2.png"
} }
assert File.exists?("#{@emoji_path}/test_pack/blank_url.png") assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
@ -624,15 +633,16 @@ test "new without shortcode", %{admin_conn: admin_conn} do
}) })
|> json_response_and_validate_schema(200) == %{ |> json_response_and_validate_schema(200) == %{
"shortcode" => "shortcode.png", "shortcode" => "shortcode.png",
"blank" => "blank.png" "blank" => "blank.png",
"blank2" => "blank2.png"
} }
end end
test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
assert admin_conn 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) == %{ |> json_response_and_validate_schema(:bad_request) == %{
"error" => "Emoji \"blank2\" does not exist" "error" => "Emoji \"blank3\" does not exist"
} }
end end
@ -640,12 +650,12 @@ test "update non existing emoji", %{admin_conn: admin_conn} do
assert admin_conn assert admin_conn
|> put_req_header("content-type", "multipart/form-data") |> put_req_header("content-type", "multipart/form-data")
|> patch("/api/pleroma/emoji/packs/test_pack/files", %{ |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
shortcode: "blank2", shortcode: "blank3",
new_shortcode: "blank3", new_shortcode: "blank4",
new_filename: "dir_2/blank_3.png" new_filename: "dir_2/blank_3.png"
}) })
|> json_response_and_validate_schema(:bad_request) == %{ |> json_response_and_validate_schema(:bad_request) == %{
"error" => "Emoji \"blank2\" does not exist" "error" => "Emoji \"blank3\" does not exist"
} }
end end
@ -768,7 +778,7 @@ test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
describe "GET /api/pleroma/emoji/packs/:name" do describe "GET /api/pleroma/emoji/packs/:name" do
test "shows pack.json", %{conn: conn} do test "shows pack.json", %{conn: conn} do
assert %{ assert %{
"files" => %{"blank" => "blank.png"}, "files" => files,
"pack" => %{ "pack" => %{
"can-download" => true, "can-download" => true,
"description" => "Test description", "description" => "Test description",
@ -781,6 +791,26 @@ test "shows pack.json", %{conn: conn} do
conn conn
|> get("/api/pleroma/emoji/packs/test_pack") |> get("/api/pleroma/emoji/packs/test_pack")
|> json_response_and_validate_schema(200) |> 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 end
test "non existing pack", %{conn: conn} do test "non existing pack", %{conn: conn} do