From f5131540dc9bbf8038e6625f4524ca01b52abbbf Mon Sep 17 00:00:00 2001 From: Ekaterina Vaartis Date: Wed, 28 Aug 2019 19:29:01 +0300 Subject: [PATCH] Add a way to create emoji packs via an endpoint --- .../web/emoji_api/emoji_api_controller.ex | 21 ++++++++++++ lib/pleroma/web/router.ex | 1 + test/web/emoji_api_controller_test.exs | 34 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/lib/pleroma/web/emoji_api/emoji_api_controller.ex b/lib/pleroma/web/emoji_api/emoji_api_controller.ex index 87ae0e092..0bd9cd207 100644 --- a/lib/pleroma/web/emoji_api/emoji_api_controller.ex +++ b/lib/pleroma/web/emoji_api/emoji_api_controller.ex @@ -211,6 +211,27 @@ def download_from(conn, %{"instance_address" => address, "pack_name" => name} = 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) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index acd6f740b..a21fefc70 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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 diff --git a/test/web/emoji_api_controller_test.exs b/test/web/emoji_api_controller_test.exs index c1aece691..fa194a26c 100644 --- a/test/web/emoji_api_controller_test.exs +++ b/test/web/emoji_api_controller_test.exs @@ -331,4 +331,38 @@ test "updating pack files" 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