emoji packs pagination

This commit is contained in:
Alexander Strizhakov 2020-06-18 14:32:21 +03:00
parent d772361e62
commit 3becdafd33
No known key found for this signature in database
GPG Key ID: 022896A53AEF1381
4 changed files with 53 additions and 6 deletions

View File

@ -16,7 +16,7 @@ defmodule Pleroma.Emoji.Pack do
alias Pleroma.Emoji
@spec create(String.t()) :: :ok | {:error, File.posix()} | {:error, :empty_values}
@spec create(String.t()) :: {:ok, t()} | {:error, File.posix()} | {:error, :empty_values}
def create(name) do
with :ok <- validate_not_empty([name]),
dir <- Path.join(emoji_path(), name),
@ -120,8 +120,8 @@ def list_remote(url) do
end
end
@spec list_local() :: {:ok, map()}
def list_local do
@spec list_local(keyword()) :: {:ok, map()}
def list_local(opts) do
with {:ok, results} <- list_packs_dir() do
packs =
results
@ -132,6 +132,17 @@ def list_local 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
|> Map.new(fn pack -> {pack.name, validate_pack(pack)} end)
{:ok, packs}
@ -146,7 +157,7 @@ def get_archive(name) do
end
end
@spec download(String.t(), String.t(), String.t()) :: :ok | {:error, atom()}
@spec download(String.t(), String.t(), String.t()) :: {:ok, t()} | {:error, atom()}
def download(name, url, as) do
uri = url |> String.trim() |> URI.parse()

View File

@ -33,6 +33,20 @@ def index_operation do
tags: ["Emoji Packs"],
summary: "Lists local custom emoji packs",
operationId: "PleromaAPI.EmojiPackController.index",
parameters: [
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 => emoji_packs_response()
}

View File

@ -37,13 +37,13 @@ def remote(conn, %{url: url}) do
end
end
def index(conn, _params) do
def index(conn, params) do
emoji_path =
[:instance, :static_dir]
|> Pleroma.Config.get!()
|> Path.join("emoji")
with {:ok, packs} <- Pack.list_local() do
with {:ok, packs} <- Pack.list_local(page: params.page, page_size: params.page_size) do
json(conn, packs)
else
{:error, :create_dir, e} ->

View File

@ -39,6 +39,28 @@ test "GET /api/pleroma/emoji/packs", %{conn: conn} do
non_shared = resp["test_pack_nonshared"]
assert non_shared["pack"]["share-files"] == false
assert non_shared["pack"]["can-download"] == false
resp =
conn
|> get("/api/pleroma/emoji/packs?page_size=1")
|> json_response_and_validate_schema(200)
[pack1] = Map.keys(resp)
resp =
conn
|> get("/api/pleroma/emoji/packs?page_size=1&page=2")
|> json_response_and_validate_schema(200)
[pack2] = Map.keys(resp)
resp =
conn
|> get("/api/pleroma/emoji/packs?page_size=1&page=3")
|> json_response_and_validate_schema(200)
[pack3] = Map.keys(resp)
assert [pack1, pack2, pack3] |> Enum.uniq() |> length() == 3
end
describe "GET /api/pleroma/emoji/packs/remote" do