Add a way to create emoji packs via an endpoint
authorEkaterina Vaartis <vaartis@cock.li>
Wed, 28 Aug 2019 16:29:01 +0000 (19:29 +0300)
committerEkaterina Vaartis <vaartis@cock.li>
Wed, 18 Sep 2019 21:16:33 +0000 (00:16 +0300)
lib/pleroma/web/emoji_api/emoji_api_controller.ex
lib/pleroma/web/router.ex
test/web/emoji_api_controller_test.exs

index 87ae0e092ea969dc63c4a69f6e3412e74998f524..0bd9cd2074e6cee800351bc7353a426e4fc87224 100644 (file)
@@ -211,6 +211,27 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
     end
   end
 
+  def create(conn, %{"name" => name}) do
+    pack_dir = Path.join(@emoji_dir_path, name)
+
+    unless File.exists?(pack_dir) do
+      File.mkdir_p!(pack_dir)
+
+      pack_file_p = Path.join(pack_dir, "pack.json")
+
+      File.write!(
+        pack_file_p,
+        Jason.encode!(%{pack: %{}, files: %{}})
+      )
+
+      conn |> text("ok")
+    else
+      conn
+      |> put_status(:conflict)
+      |> text("A pack named \"#{name}\" already exists")
+    end
+  end
+
   def delete(conn, %{"name" => name}) do
     pack_dir = Path.join(@emoji_dir_path, name)
 
index acd6f740ba8823fe92a0b035eb2363edc586f196..a21fefc70f45af73bbb494c75af736382d34b887 100644 (file)
@@ -220,6 +220,7 @@ defmodule Pleroma.Web.Router do
 
       post("/update_file/:pack_name", EmojiAPIController, :update_file)
       post("/update_metadata/:pack_name", EmojiAPIController, :update_metadata)
+      post("/create/:name", EmojiAPIController, :create)
       delete("/delete/:name", EmojiAPIController, :delete)
       post("/download_from", EmojiAPIController, :download_from)
     end
index c1aece69140fb838b597e34095b4e71a8643491e..fa194a26c3c9d258b89f09dd746480a412bdf37b 100644 (file)
@@ -331,4 +331,38 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIControllerTest do
 
     refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
   end
+
+  test "creating and deleting a pack" do
+    on_exit(fn ->
+      File.rm_rf!("#{@emoji_dir_path}/test_created")
+    end)
+
+    admin = insert(:user, info: %{is_admin: true})
+
+    conn = build_conn() |> assign(:user, admin)
+
+    assert conn
+           |> put_req_header("content-type", "application/json")
+           |> post(
+             emoji_api_path(
+               conn,
+               :create,
+               "test_created"
+             )
+           )
+           |> text_response(200) == "ok"
+
+    assert File.exists?("#{@emoji_dir_path}/test_created/pack.json")
+
+    assert Jason.decode!(File.read!("#{@emoji_dir_path}/test_created/pack.json")) == %{
+             "pack" => %{},
+             "files" => %{}
+           }
+
+    assert conn
+           |> delete(emoji_api_path(conn, :delete, "test_created"))
+           |> response(200) == "ok"
+
+    refute File.exists?("#{@emoji_dir_path}/test_created/pack.json")
+  end
 end