Merge branch 'develop' into feature/gen-magic
[akkoma] / lib / pleroma / emoji / pack.ex
index 03aed33bbe7e6b07b1eab4b1a67c40a9f7badf38..8f1989ada53a41346e1fd76a72b3828739737643 100644 (file)
@@ -65,71 +65,73 @@ defmodule Pleroma.Emoji.Pack do
     end
   end
 
-  @spec add_file(String.t(), String.t(), Path.t(), Plug.Upload.t()) ::
+  @spec unpack_zip_emojies(list(tuple())) :: list(map())
+  defp unpack_zip_emojies(zip_files) do
+    Enum.reduce(zip_files, [], fn
+      {_, path, s, _, _, _}, acc when elem(s, 2) == :regular ->
+        with(
+          filename <- Path.basename(path),
+          shortcode <- Path.basename(filename, Path.extname(filename)),
+          false <- Emoji.exist?(shortcode)
+        ) do
+          [%{path: path, filename: path, shortcode: shortcode} | acc]
+        else
+          _ -> acc
+        end
+
+      _, acc ->
+        acc
+    end)
+  end
+
+  @spec add_file(t(), String.t(), Path.t(), Plug.Upload.t()) ::
           {:ok, t()}
           | {:error, File.posix() | atom()}
   def add_file(%Pack{} = pack, _, _, %Plug.Upload{content_type: "application/zip"} = file) do
-    with {:ok, zip_items} <- :zip.table(to_charlist(file.path)) do
-      emojies =
-        for {_, path, s, _, _, _} <- zip_items, elem(s, 2) == :regular do
-          filename = Path.basename(path)
-          shortcode = Path.basename(filename, Path.extname(filename))
-
-          %{
-            path: path,
-            filename: path,
-            shortcode: shortcode,
-            exist: not is_nil(Pleroma.Emoji.get(shortcode))
-          }
-        end
-        |> Enum.group_by(& &1[:exist])
-
-      case Map.get(emojies, false, []) do
-        [_ | _] = new_emojies ->
-          {:ok, tmp_dir} = Pleroma.Utils.tmp_dir("emoji")
-
-          try do
-            {:ok, _emoji_files} =
-              :zip.unzip(
-                to_charlist(file.path),
-                [
-                  {:file_list, Enum.map(new_emojies, & &1[:path])},
-                  {:cwd, tmp_dir}
-                ]
+    with {:ok, zip_files} <- :zip.table(to_charlist(file.path)),
+         [_ | _] = emojies <- unpack_zip_emojies(zip_files),
+         {:ok, tmp_dir} <- Pleroma.Utils.tmp_dir("emoji") do
+      try do
+        {:ok, _emoji_files} =
+          :zip.unzip(
+            to_charlist(file.path),
+            [{:file_list, Enum.map(emojies, & &1[:path])}, {:cwd, tmp_dir}]
+          )
+
+        {_, updated_pack} =
+          Enum.map_reduce(emojies, pack, fn item, emoji_pack ->
+            emoji_file = %Plug.Upload{
+              filename: item[:filename],
+              path: Path.join(tmp_dir, item[:path])
+            }
+
+            {:ok, updated_pack} =
+              do_add_file(
+                emoji_pack,
+                item[:shortcode],
+                to_string(item[:filename]),
+                emoji_file
               )
 
-            {_, updated_pack} =
-              Enum.map_reduce(new_emojies, pack, fn item, emoji_pack ->
-                emoji_file = %Plug.Upload{
-                  filename: item[:filename],
-                  path: Path.join(tmp_dir, item[:path])
-                }
-
-                {:ok, updated_pack} =
-                  do_add_file(
-                    emoji_pack,
-                    item[:shortcode],
-                    to_string(item[:filename]),
-                    emoji_file
-                  )
-
-                {item, updated_pack}
-              end)
-
-            Emoji.reload()
-
-            {:ok, updated_pack}
-          after
-            File.rm_rf(tmp_dir)
-          end
+            {item, updated_pack}
+          end)
+
+        Emoji.reload()
 
-        _ ->
-          {:ok, pack}
+        {:ok, updated_pack}
+      after
+        File.rm_rf(tmp_dir)
       end
+    else
+      {:error, _} = error ->
+        error
+
+      _ ->
+        {:ok, pack}
     end
   end
 
-  def add_file(%Pack{} = pack, shortcode, filename, file) do
+  def add_file(%Pack{} = pack, shortcode, filename, %Plug.Upload{} = file) do
     with :ok <- validate_not_empty([shortcode, filename]),
          :ok <- validate_emoji_not_exists(shortcode),
          {:ok, updated_pack} <- do_add_file(pack, shortcode, filename, file) do
@@ -139,12 +141,10 @@ defmodule Pleroma.Emoji.Pack do
   end
 
   defp do_add_file(pack, shortcode, filename, file) do
-    with :ok <- save_file(file, pack, filename),
-         {:ok, updated_pack} <-
-           pack
-           |> put_emoji(shortcode, filename)
-           |> save_pack() do
-      {:ok, updated_pack}
+    with :ok <- save_file(file, pack, filename) do
+      pack
+      |> put_emoji(shortcode, filename)
+      |> save_pack()
     end
   end
 
@@ -198,13 +198,13 @@ defmodule Pleroma.Emoji.Pack do
     end
   end
 
-  @spec list_remote(String.t()) :: {:ok, map()} | {:error, atom()}
-  def list_remote(url) do
-    uri = url |> String.trim() |> URI.parse()
+  @spec list_remote(keyword()) :: {:ok, map()} | {:error, atom()}
+  def list_remote(opts) do
+    uri = opts[:url] |> String.trim() |> URI.parse()
 
     with :ok <- validate_shareable_packs_available(uri) do
       uri
-      |> URI.merge("/api/pleroma/emoji/packs")
+      |> URI.merge("/api/pleroma/emoji/packs?page=#{opts[:page]}&page_size=#{opts[:page_size]}")
       |> http_get()
     end
   end
@@ -244,7 +244,8 @@ defmodule Pleroma.Emoji.Pack do
     uri = url |> String.trim() |> URI.parse()
 
     with :ok <- validate_shareable_packs_available(uri),
-         {:ok, remote_pack} <- uri |> URI.merge("/api/pleroma/emoji/packs/#{name}") |> http_get(),
+         {:ok, remote_pack} <-
+           uri |> URI.merge("/api/pleroma/emoji/pack?name=#{name}") |> http_get(),
          {:ok, %{sha: sha, url: url} = pack_info} <- fetch_pack_info(remote_pack, uri, name),
          {:ok, archive} <- download_archive(url, sha),
          pack <- copy_as(remote_pack, as || name),
@@ -312,9 +313,10 @@ defmodule Pleroma.Emoji.Pack do
   defp validate_emoji_not_exists(_shortcode, true), do: :ok
 
   defp validate_emoji_not_exists(shortcode, _) do
-    case Emoji.get(shortcode) do
-      nil -> :ok
-      _ -> {:error, :already_exists}
+    if Emoji.exist?(shortcode) do
+      {:error, :already_exists}
+    else
+      :ok
     end
   end
 
@@ -466,7 +468,7 @@ defmodule Pleroma.Emoji.Pack do
 
   defp put_emoji(pack, shortcode, filename) do
     files = Map.put(pack.files, shortcode, filename)
-    %{pack | files: files}
+    %{pack | files: files, files_count: length(Map.keys(files))}
   end
 
   defp delete_emoji(pack, shortcode) do
@@ -522,7 +524,7 @@ defmodule Pleroma.Emoji.Pack do
   defp http_get(%URI{} = url), do: url |> to_string() |> http_get()
 
   defp http_get(url) do
-    with {:ok, %{body: body}} <- url |> Pleroma.HTTP.get() do
+    with {:ok, %{body: body}} <- Pleroma.HTTP.get(url, [], pool: :default) do
       Jason.decode(body)
     end
   end
@@ -571,7 +573,7 @@ defmodule Pleroma.Emoji.Pack do
         {:ok,
          %{
            sha: sha,
-           url: URI.merge(uri, "/api/pleroma/emoji/packs/#{name}/archive") |> to_string()
+           url: URI.merge(uri, "/api/pleroma/emoji/packs/archive?name=#{name}") |> to_string()
          }}
 
       %{"fallback-src" => src, "fallback-src-sha256" => sha} when is_binary(src) ->