adding overall count for packs and files

This commit is contained in:
Alexander Strizhakov 2020-06-19 10:17:24 +03:00
parent 4975ed86bc
commit 3e3f9253e6
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
4 changed files with 56 additions and 20 deletions

View file

@ -458,7 +458,17 @@ The status posting endpoint takes an additional parameter, `in_reply_to_conversa
* Params: * Params:
* `page`: page number for packs (default 1) * `page`: page number for packs (default 1)
* `page_size`: page size for packs (default 50) * `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: `packs` key with JSON hashmap of pack name to pack contents and `count` key for count of packs.
```json
{
"packs": {
"pack_name": {...}, // pack contents
...
},
"count": 0 // packs count
}
```
## `GET /api/pleroma/emoji/packs/:name` ## `GET /api/pleroma/emoji/packs/:name`
@ -469,7 +479,15 @@ The status posting endpoint takes an additional parameter, `in_reply_to_conversa
* Params: * Params:
* `page`: page number for files (default 1) * `page`: page number for files (default 1)
* `page_size`: page size for files (default 50) * `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`, `files_count` and `pack` keys with 200 status or 404 if the pack does not exist.
```json
{
"files": {...},
"files_count": 0, // emoji count in pack
"pack": {...}
}
```
## `GET /api/pleroma/emoji/packs/:name/archive` ## `GET /api/pleroma/emoji/packs/:name/archive`
### Requests a local pack archive from the instance ### Requests a local pack archive from the instance

View file

@ -1,6 +1,7 @@
defmodule Pleroma.Emoji.Pack do defmodule Pleroma.Emoji.Pack do
@derive {Jason.Encoder, only: [:files, :pack]} @derive {Jason.Encoder, only: [:files, :pack, :files_count]}
defstruct files: %{}, defstruct files: %{},
files_count: 0,
pack_file: nil, pack_file: nil,
path: nil, path: nil,
pack: %{}, pack: %{},
@ -8,6 +9,7 @@ defmodule Pleroma.Emoji.Pack do
@type t() :: %__MODULE__{ @type t() :: %__MODULE__{
files: %{String.t() => Path.t()}, files: %{String.t() => Path.t()},
files_count: non_neg_integer(),
pack_file: Path.t(), pack_file: Path.t(),
path: Path.t(), path: Path.t(),
pack: map(), pack: map(),
@ -137,10 +139,10 @@ defmodule Pleroma.Emoji.Pack do
end end
end end
@spec list_local(keyword()) :: {:ok, map()} @spec list_local(keyword()) :: {:ok, map(), non_neg_integer()}
def list_local(opts) do def list_local(opts) do
with {:ok, results} <- list_packs_dir() do with {:ok, results} <- list_packs_dir() do
packs = all_packs =
results results
|> Enum.map(fn name -> |> Enum.map(fn name ->
case load_pack(name) do case load_pack(name) do
@ -149,10 +151,13 @@ defmodule Pleroma.Emoji.Pack do
end end
end) end)
|> Enum.reject(&is_nil/1) |> Enum.reject(&is_nil/1)
packs =
all_packs
|> paginate(opts[:page], opts[:page_size]) |> paginate(opts[:page], opts[:page_size])
|> Map.new(fn pack -> {pack.name, validate_pack(pack)} end) |> Map.new(fn pack -> {pack.name, validate_pack(pack)} end)
{:ok, packs} {:ok, packs, length(all_packs)}
end end
end end
@ -215,7 +220,12 @@ defmodule Pleroma.Emoji.Pack do
|> Map.put(:path, Path.dirname(pack_file)) |> Map.put(:path, Path.dirname(pack_file))
|> Map.put(:name, name) |> Map.put(:name, name)
{:ok, pack} files_count =
pack.files
|> Map.keys()
|> length()
{:ok, Map.put(pack, :files_count, files_count)}
else else
{:error, :not_found} {:error, :not_found}
end end

View file

@ -43,8 +43,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackController do
|> Pleroma.Config.get!() |> Pleroma.Config.get!()
|> Path.join("emoji") |> Path.join("emoji")
with {:ok, packs} <- Pack.list_local(page: params.page, page_size: params.page_size) do with {:ok, packs, count} <- Pack.list_local(page: params.page, page_size: params.page_size) do
json(conn, packs) json(conn, %{packs: packs, count: count})
else else
{:error, :create_dir, e} -> {:error, :create_dir, e} ->
conn conn

View file

@ -30,13 +30,14 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
test "GET /api/pleroma/emoji/packs", %{conn: conn} do 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"] assert resp["count"] == 3
shared = resp["packs"]["test_pack"]
assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.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"]
non_shared = resp["test_pack_nonshared"] non_shared = resp["packs"]["test_pack_nonshared"]
assert non_shared["pack"]["share-files"] == false assert non_shared["pack"]["share-files"] == false
assert non_shared["pack"]["can-download"] == false assert non_shared["pack"]["can-download"] == false
@ -45,21 +46,24 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> get("/api/pleroma/emoji/packs?page_size=1") |> get("/api/pleroma/emoji/packs?page_size=1")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
[pack1] = Map.keys(resp) assert resp["count"] == 3
[pack1] = Map.keys(resp["packs"])
resp = resp =
conn conn
|> get("/api/pleroma/emoji/packs?page_size=1&page=2") |> get("/api/pleroma/emoji/packs?page_size=1&page=2")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
[pack2] = Map.keys(resp) assert resp["count"] == 3
[pack2] = Map.keys(resp["packs"])
resp = resp =
conn conn
|> get("/api/pleroma/emoji/packs?page_size=1&page=3") |> get("/api/pleroma/emoji/packs?page_size=1&page=3")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
[pack3] = Map.keys(resp) assert resp["count"] == 3
[pack3] = Map.keys(resp["packs"])
assert [pack1, pack2, pack3] |> Enum.uniq() |> length() == 3 assert [pack1, pack2, pack3] |> Enum.uniq() |> length() == 3
end end
@ -683,7 +687,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{ assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
"pack" => %{}, "pack" => %{},
"files" => %{} "files" => %{},
"files_count" => 0
} }
assert admin_conn assert admin_conn
@ -741,14 +746,14 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest 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)
refute Map.has_key?(resp, "test_pack_for_import") refute Map.has_key?(resp["packs"], "test_pack_for_import")
assert admin_conn assert admin_conn
|> get("/api/pleroma/emoji/packs/import") |> get("/api/pleroma/emoji/packs/import")
|> json_response_and_validate_schema(200) == ["test_pack_for_import"] |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
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)
assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"} assert resp["packs"]["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
File.rm!("#{@emoji_path}/test_pack_for_import/pack.json") File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json") refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
@ -768,7 +773,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest 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)
assert resp["test_pack_for_import"]["files"] == %{ assert resp["packs"]["test_pack_for_import"]["files"] == %{
"blank" => "blank.png", "blank" => "blank.png",
"blank2" => "blank.png", "blank2" => "blank.png",
"foo" => "blank.png" "foo" => "blank.png"
@ -779,6 +784,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
test "shows pack.json", %{conn: conn} do test "shows pack.json", %{conn: conn} do
assert %{ assert %{
"files" => files, "files" => files,
"files_count" => 2,
"pack" => %{ "pack" => %{
"can-download" => true, "can-download" => true,
"description" => "Test description", "description" => "Test description",
@ -795,7 +801,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"} assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"}
assert %{ assert %{
"files" => files "files" => files,
"files_count" => 2
} = } =
conn conn
|> get("/api/pleroma/emoji/packs/test_pack?page_size=1") |> get("/api/pleroma/emoji/packs/test_pack?page_size=1")
@ -804,7 +811,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
assert files |> Map.keys() |> length() == 1 assert files |> Map.keys() |> length() == 1
assert %{ assert %{
"files" => files "files" => files,
"files_count" => 2
} = } =
conn conn
|> get("/api/pleroma/emoji/packs/test_pack?page_size=1&page=2") |> get("/api/pleroma/emoji/packs/test_pack?page_size=1&page=2")