defmodule Pleroma.Emoji.Pack do
- @derive {Jason.Encoder, only: [:files, :pack]}
+ @derive {Jason.Encoder, only: [:files, :pack, :files_count]}
defstruct files: %{},
+ files_count: 0,
pack_file: nil,
path: nil,
pack: %{},
@type t() :: %__MODULE__{
files: %{String.t() => Path.t()},
+ files_count: non_neg_integer(),
pack_file: Path.t(),
path: Path.t(),
pack: map(),
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.chunk_every(page_size)
+ |> Enum.at(page - 1)
+ 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
end
end
- @spec list_local(keyword()) :: {:ok, map()}
+ @spec list_local(keyword()) :: {:ok, map(), non_neg_integer()}
def list_local(opts) do
with {:ok, results} <- list_packs_dir() do
- packs =
+ all_packs =
results
|> Enum.map(fn name ->
case load_pack(name) do
|> 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
+ all_packs
+ |> paginate(opts[:page], opts[:page_size])
|> Map.new(fn pack -> {pack.name, validate_pack(pack)} end)
- {:ok, packs}
+ {:ok, packs, length(all_packs)}
end
end
|> Map.put(:path, Path.dirname(pack_file))
|> Map.put(:name, name)
- {:ok, pack}
+ files_count =
+ pack.files
+ |> Map.keys()
+ |> length()
+
+ {:ok, Map.put(pack, :files_count, files_count)}
else
{:error, :not_found}
end
# 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
# with the API so it should be sufficient
with {:create_dir, :ok} <- {:create_dir, File.mkdir_p(emoji_path)},
{:ls, {:ok, results}} <- {:ls, File.ls(emoji_path)} do
- {:ok, results}
+ {:ok, Enum.sort(results)}
else
{:create_dir, {:error, e}} -> {:error, :create_dir, e}
{:ls, {:error, e}} -> {:error, :ls, e}