2020-05-18 17:38:22 +02:00
|
|
|
defmodule Pleroma.Web.PleromaAPI.EmojiPackController do
|
2019-08-10 23:39:21 +02:00
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
alias Pleroma.Emoji.Pack
|
2019-08-12 12:13:01 +02:00
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2019-10-06 16:12:17 +02:00
|
|
|
plug(
|
2020-03-28 11:34:32 +01:00
|
|
|
Pleroma.Plugs.OAuthScopesPlug,
|
2019-12-06 18:33:47 +01:00
|
|
|
%{scopes: ["write"], admin: true}
|
2019-10-06 16:12:17 +02:00
|
|
|
when action in [
|
2020-04-30 16:50:57 +02:00
|
|
|
:import_from_filesystem,
|
2020-03-28 19:15:14 +01:00
|
|
|
:remote,
|
|
|
|
:download,
|
2019-10-06 16:12:17 +02:00
|
|
|
:create,
|
2020-03-28 19:15:14 +01:00
|
|
|
:update,
|
2019-10-06 16:12:17 +02:00
|
|
|
:delete,
|
2020-03-28 19:15:14 +01:00
|
|
|
:add_file,
|
2019-10-06 16:12:17 +02:00
|
|
|
:update_file,
|
2020-03-28 19:15:14 +01:00
|
|
|
:delete_file
|
2019-10-06 16:12:17 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
@skip_plugs [Pleroma.Plugs.OAuthScopesPlug, Pleroma.Plugs.ExpectPublicOrAuthenticatedCheckPlug]
|
|
|
|
plug(:skip_plug, @skip_plugs when action in [:archive, :show, :list])
|
|
|
|
|
2020-05-18 17:38:22 +02:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaEmojiPackOperation
|
2019-10-06 16:12:17 +02:00
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def remote(conn, %{url: url}) do
|
2020-03-28 19:15:14 +01:00
|
|
|
with {:ok, packs} <- Pack.list_remote(url) do
|
2020-03-28 11:34:32 +01:00
|
|
|
json(conn, packs)
|
2019-09-24 18:18:07 +02:00
|
|
|
else
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :not_shareable} ->
|
2020-03-28 11:34:32 +01:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: "The requested instance does not support sharing emoji packs"})
|
2019-09-24 18:18:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def index(conn, _params) do
|
2020-03-28 11:34:32 +01:00
|
|
|
emoji_path =
|
2020-05-14 17:21:51 +02:00
|
|
|
[:instance, :static_dir]
|
|
|
|
|> Pleroma.Config.get!()
|
|
|
|
|> Path.join("emoji")
|
2020-03-28 11:34:32 +01:00
|
|
|
|
2020-03-28 19:15:14 +01:00
|
|
|
with {:ok, packs} <- Pack.list_local() do
|
2020-03-28 11:34:32 +01:00
|
|
|
json(conn, packs)
|
2019-09-24 08:27:34 +02:00
|
|
|
else
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :create_dir, e} ->
|
2019-09-24 08:27:34 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
2020-03-28 11:34:32 +01:00
|
|
|
|> json(%{error: "Failed to create the emoji pack directory at #{emoji_path}: #{e}"})
|
2019-09-24 08:27:34 +02:00
|
|
|
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :ls, e} ->
|
2019-09-24 08:27:34 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{
|
2020-03-28 11:34:32 +01:00
|
|
|
error: "Failed to get the contents of the emoji pack directory at #{emoji_path}: #{e}"
|
2019-09-24 08:27:34 +02:00
|
|
|
})
|
2019-09-11 17:48:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def show(conn, %{name: name}) do
|
2020-03-28 11:34:32 +01:00
|
|
|
name = String.trim(name)
|
2019-09-11 17:48:51 +02:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
with {:ok, pack} <- Pack.show(name) do
|
|
|
|
json(conn, pack)
|
2019-09-11 17:48:51 +02:00
|
|
|
else
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :not_found} ->
|
2020-03-28 11:34:32 +01:00
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{error: "Pack #{name} does not exist"})
|
2019-08-12 12:13:01 +02:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "pack name cannot be empty"})
|
2019-09-11 11:07:19 +02:00
|
|
|
end
|
2019-08-10 23:39:21 +02:00
|
|
|
end
|
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def archive(conn, %{name: name}) do
|
2020-03-28 19:15:14 +01:00
|
|
|
with {:ok, archive} <- Pack.get_archive(name) do
|
2020-03-28 11:34:32 +01:00
|
|
|
send_download(conn, {:binary, archive}, filename: "#{name}.zip")
|
2019-09-11 18:00:48 +02:00
|
|
|
else
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :cant_download} ->
|
2019-09-11 18:00:48 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:forbidden)
|
2019-09-11 18:39:47 +02:00
|
|
|
|> json(%{
|
2020-03-28 11:34:32 +01:00
|
|
|
error:
|
|
|
|
"Pack #{name} cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
|
2019-09-11 18:39:47 +02:00
|
|
|
})
|
2019-08-10 23:39:21 +02:00
|
|
|
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :not_found} ->
|
2019-08-10 23:39:21 +02:00
|
|
|
conn
|
2019-09-11 18:00:48 +02:00
|
|
|
|> put_status(:not_found)
|
2019-09-11 18:39:47 +02:00
|
|
|
|> json(%{error: "Pack #{name} does not exist"})
|
2019-08-10 23:39:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def download(%{body_params: %{url: url, name: name} = params} = conn, _) do
|
2020-05-18 17:43:23 +02:00
|
|
|
with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
|
2020-03-28 11:34:32 +01:00
|
|
|
json(conn, "ok")
|
2019-09-11 17:59:31 +02:00
|
|
|
else
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :not_shareable} ->
|
2020-03-28 11:34:32 +01:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: "The requested instance does not support sharing emoji packs"})
|
|
|
|
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :imvalid_checksum} ->
|
2020-03-28 11:34:32 +01:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
|
|
|
|
|
|
|
|
{:error, e} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: e})
|
2019-08-10 23:39:21 +02:00
|
|
|
end
|
|
|
|
end
|
2019-08-12 17:03:59 +02:00
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def create(conn, %{name: name}) do
|
2020-03-28 11:34:32 +01:00
|
|
|
name = String.trim(name)
|
2019-08-28 18:29:01 +02:00
|
|
|
|
2020-05-18 17:43:23 +02:00
|
|
|
with {:ok, _pack} <- Pack.create(name) do
|
2020-03-28 11:34:32 +01:00
|
|
|
json(conn, "ok")
|
|
|
|
else
|
|
|
|
{:error, :eexist} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:conflict)
|
|
|
|
|> json(%{error: "A pack named \"#{name}\" already exists"})
|
2019-08-28 18:29:01 +02:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "pack name cannot be empty"})
|
2019-08-28 18:29:01 +02:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
{:error, _} ->
|
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
:internal_server_error,
|
|
|
|
"Unexpected error occurred while creating pack."
|
|
|
|
)
|
2019-08-28 18:29:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def delete(conn, %{name: name}) do
|
2020-03-28 11:34:32 +01:00
|
|
|
name = String.trim(name)
|
|
|
|
|
|
|
|
with {:ok, deleted} when deleted != [] <- Pack.delete(name) do
|
|
|
|
json(conn, "ok")
|
|
|
|
else
|
|
|
|
{:ok, []} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{error: "Pack #{name} does not exist"})
|
2019-08-12 17:03:59 +02:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "pack name cannot be empty"})
|
2019-08-12 17:03:59 +02:00
|
|
|
|
2020-02-25 15:34:56 +01:00
|
|
|
{:error, _, _} ->
|
2019-09-11 18:39:47 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: "Couldn't delete the pack #{name}"})
|
2019-08-12 17:03:59 +02:00
|
|
|
end
|
|
|
|
end
|
2019-08-15 18:55:58 +02:00
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def update(%{body_params: %{metadata: metadata}} = conn, %{name: name}) do
|
2020-03-28 19:15:14 +01:00
|
|
|
with {:ok, pack} <- Pack.update_metadata(name, metadata) do
|
2020-03-28 11:34:32 +01:00
|
|
|
json(conn, pack.pack)
|
2019-09-11 17:32:54 +02:00
|
|
|
else
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :incomplete} ->
|
2019-09-11 17:32:54 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2019-09-11 18:39:47 +02:00
|
|
|
|> json(%{error: "The fallback archive does not have all files specified in pack.json"})
|
2020-02-06 16:01:12 +01:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
{:error, _} ->
|
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
:internal_server_error,
|
|
|
|
"Unexpected error occurred while updating pack metadata."
|
|
|
|
)
|
2019-09-11 18:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def add_file(%{body_params: params} = conn, %{name: name}) do
|
|
|
|
filename = params[:filename] || get_filename(params[:file])
|
|
|
|
shortcode = params[:shortcode] || Path.basename(filename, Path.extname(filename))
|
2020-03-28 11:34:32 +01:00
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
with {:ok, pack} <- Pack.add_file(name, shortcode, filename, params[:file]) do
|
2020-03-28 11:34:32 +01:00
|
|
|
json(conn, pack.files)
|
2019-09-11 18:39:47 +02:00
|
|
|
else
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :already_exists} ->
|
2019-09-11 18:39:47 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:conflict)
|
|
|
|
|> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"})
|
|
|
|
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :not_found} ->
|
2020-03-28 11:34:32 +01:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2020-03-28 19:15:14 +01:00
|
|
|
|> json(%{error: "pack \"#{name}\" is not found"})
|
2020-03-28 11:34:32 +01:00
|
|
|
|
|
|
|
{:error, :empty_values} ->
|
2019-09-11 18:39:47 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2020-03-28 11:34:32 +01:00
|
|
|
|> json(%{error: "pack name, shortcode or filename cannot be empty"})
|
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
:internal_server_error,
|
|
|
|
"Unexpected error occurred while adding file to pack."
|
|
|
|
)
|
2019-09-11 18:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def update_file(%{body_params: %{shortcode: shortcode} = params} = conn, %{name: name}) do
|
|
|
|
new_shortcode = params[:new_shortcode]
|
|
|
|
new_filename = params[:new_filename]
|
|
|
|
force = params[:force]
|
2020-03-28 19:15:14 +01:00
|
|
|
|
|
|
|
with {:ok, pack} <- Pack.update_file(name, shortcode, new_shortcode, new_filename, force) do
|
2020-03-28 11:34:32 +01:00
|
|
|
json(conn, pack.files)
|
|
|
|
else
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :doesnt_exist} ->
|
2020-03-28 11:34:32 +01:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "Emoji \"#{shortcode}\" does not exist"})
|
2019-09-11 18:39:47 +02:00
|
|
|
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :already_exists} ->
|
2020-03-28 19:15:14 +01:00
|
|
|
conn
|
|
|
|
|> put_status(:conflict)
|
|
|
|
|> json(%{
|
|
|
|
error:
|
|
|
|
"New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
|
|
|
|
})
|
|
|
|
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :not_found} ->
|
2020-03-28 11:34:32 +01:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2020-03-28 19:15:14 +01:00
|
|
|
|> json(%{error: "pack \"#{name}\" is not found"})
|
2019-09-11 18:39:47 +02:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2020-03-28 19:15:14 +01:00
|
|
|
|> json(%{error: "new_shortcode or new_filename cannot be empty"})
|
2019-09-11 18:39:47 +02:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
{:error, _} ->
|
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
:internal_server_error,
|
2020-03-28 19:15:14 +01:00
|
|
|
"Unexpected error occurred while updating file in pack."
|
2020-03-28 11:34:32 +01:00
|
|
|
)
|
2019-09-11 18:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 17:21:51 +02:00
|
|
|
def delete_file(conn, %{name: name, shortcode: shortcode}) do
|
2020-03-28 19:15:14 +01:00
|
|
|
with {:ok, pack} <- Pack.delete_file(name, shortcode) do
|
2020-03-28 11:34:32 +01:00
|
|
|
json(conn, pack.files)
|
2019-09-11 18:39:47 +02:00
|
|
|
else
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :doesnt_exist} ->
|
2019-09-11 18:39:47 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "Emoji \"#{shortcode}\" does not exist"})
|
|
|
|
|
2020-05-18 17:43:23 +02:00
|
|
|
{:error, :not_found} ->
|
2019-09-11 18:39:47 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2020-03-28 11:34:32 +01:00
|
|
|
|> json(%{error: "pack \"#{name}\" is not found"})
|
2019-09-11 18:39:47 +02:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
{:error, :empty_values} ->
|
2019-09-11 18:39:47 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2020-03-28 19:15:14 +01:00
|
|
|
|> json(%{error: "pack name or shortcode cannot be empty"})
|
2020-03-28 11:34:32 +01:00
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
:internal_server_error,
|
2020-03-28 19:15:14 +01:00
|
|
|
"Unexpected error occurred while removing file from pack."
|
2020-03-28 11:34:32 +01:00
|
|
|
)
|
2019-08-18 21:05:38 +02:00
|
|
|
end
|
|
|
|
end
|
2019-09-10 20:16:30 +02:00
|
|
|
|
2020-03-28 19:15:14 +01:00
|
|
|
def import_from_filesystem(conn, _params) do
|
2020-03-28 11:34:32 +01:00
|
|
|
with {:ok, names} <- Pack.import_from_filesystem() do
|
|
|
|
json(conn, names)
|
2019-09-11 17:32:54 +02:00
|
|
|
else
|
2020-03-30 08:09:27 +02:00
|
|
|
{:error, :no_read_write} ->
|
2020-01-29 11:51:17 +01:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
2020-01-30 15:09:41 +01:00
|
|
|
|> json(%{error: "Error: emoji pack directory must be writable"})
|
2020-01-29 11:51:17 +01:00
|
|
|
|
2019-09-10 20:16:30 +02:00
|
|
|
{:error, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
2019-09-11 18:39:47 +02:00
|
|
|
|> json(%{error: "Error accessing emoji pack directory"})
|
2019-09-11 17:32:54 +02:00
|
|
|
end
|
|
|
|
end
|
2019-09-10 20:16:30 +02:00
|
|
|
|
2020-03-28 11:34:32 +01:00
|
|
|
defp get_filename(%Plug.Upload{filename: filename}), do: filename
|
|
|
|
defp get_filename(url) when is_binary(url), do: Path.basename(url)
|
2019-08-10 23:39:21 +02:00
|
|
|
end
|