2020-10-12 17:00:50 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-05-18 15:38:22 +00:00
|
|
|
defmodule Pleroma.Web.PleromaAPI.EmojiPackController do
|
2019-08-10 21:39:21 +00:00
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
alias Pleroma.Emoji.Pack
|
2019-08-12 10:13:01 +00:00
|
|
|
|
2020-05-14 15:21:51 +00:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2019-10-06 14:12:17 +00:00
|
|
|
plug(
|
2020-06-24 06:57:27 +00:00
|
|
|
Pleroma.Web.Plugs.OAuthScopesPlug,
|
2019-12-06 17:33:47 +00:00
|
|
|
%{scopes: ["write"], admin: true}
|
2019-10-06 14:12:17 +00:00
|
|
|
when action in [
|
2020-04-30 14:50:57 +00:00
|
|
|
:import_from_filesystem,
|
2020-03-28 18:15:14 +00:00
|
|
|
:remote,
|
|
|
|
:download,
|
2019-10-06 14:12:17 +00:00
|
|
|
:create,
|
2020-03-28 18:15:14 +00:00
|
|
|
:update,
|
2020-08-19 03:50:20 +00:00
|
|
|
:delete
|
2019-10-06 14:12:17 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-06-24 06:57:27 +00:00
|
|
|
@skip_plugs [
|
|
|
|
Pleroma.Web.Plugs.OAuthScopesPlug,
|
2020-08-16 17:28:31 +00:00
|
|
|
Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
|
2020-06-24 06:57:27 +00:00
|
|
|
]
|
|
|
|
plug(:skip_plug, @skip_plugs when action in [:index, :archive, :show])
|
2020-05-14 15:21:51 +00:00
|
|
|
|
2020-05-18 15:38:22 +00:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaEmojiPackOperation
|
2019-10-06 14:12:17 +00:00
|
|
|
|
2020-06-27 11:33:49 +00:00
|
|
|
def remote(conn, params) do
|
|
|
|
with {:ok, packs} <-
|
|
|
|
Pack.list_remote(url: params.url, page_size: params.page_size, page: params.page) do
|
2020-03-28 10:34:32 +00:00
|
|
|
json(conn, packs)
|
2019-09-24 16:18:07 +00:00
|
|
|
else
|
2020-05-18 15:43:23 +00:00
|
|
|
{:error, :not_shareable} ->
|
2020-03-28 10:34:32 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: "The requested instance does not support sharing emoji packs"})
|
2019-09-24 16:18:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-18 11:32:21 +00:00
|
|
|
def index(conn, params) do
|
2020-03-28 10:34:32 +00:00
|
|
|
emoji_path =
|
2020-05-14 15:21:51 +00:00
|
|
|
[:instance, :static_dir]
|
|
|
|
|> Pleroma.Config.get!()
|
|
|
|
|> Path.join("emoji")
|
2020-03-28 10:34:32 +00:00
|
|
|
|
2020-06-19 07:17:24 +00:00
|
|
|
with {:ok, packs, count} <- Pack.list_local(page: params.page, page_size: params.page_size) do
|
|
|
|
json(conn, %{packs: packs, count: count})
|
2019-09-24 06:27:34 +00:00
|
|
|
else
|
2020-05-18 15:43:23 +00:00
|
|
|
{:error, :create_dir, e} ->
|
2019-09-24 06:27:34 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
2020-03-28 10:34:32 +00:00
|
|
|
|> json(%{error: "Failed to create the emoji pack directory at #{emoji_path}: #{e}"})
|
2019-09-24 06:27:34 +00:00
|
|
|
|
2020-05-18 15:43:23 +00:00
|
|
|
{:error, :ls, e} ->
|
2019-09-24 06:27:34 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{
|
2020-03-28 10:34:32 +00:00
|
|
|
error: "Failed to get the contents of the emoji pack directory at #{emoji_path}: #{e}"
|
2019-09-24 06:27:34 +00:00
|
|
|
})
|
2019-09-11 15:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-18 15:50:03 +00:00
|
|
|
def show(conn, %{name: name, page: page, page_size: page_size}) do
|
2020-03-28 10:34:32 +00:00
|
|
|
name = String.trim(name)
|
2019-09-11 15:48:51 +00:00
|
|
|
|
2020-06-18 15:50:03 +00:00
|
|
|
with {:ok, pack} <- Pack.show(name: name, page: page, page_size: page_size) do
|
2020-03-28 10:34:32 +00:00
|
|
|
json(conn, pack)
|
2019-09-11 15:48:51 +00:00
|
|
|
else
|
2020-11-13 12:13:14 +00:00
|
|
|
{:error, :enoent} ->
|
2020-03-28 10:34:32 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{error: "Pack #{name} does not exist"})
|
2019-08-12 10:13:01 +00:00
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "pack name cannot be empty"})
|
2020-11-13 12:13:14 +00:00
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
error_message =
|
|
|
|
add_posix_error(
|
|
|
|
"Failed to get the contents of the `#{name}` pack.",
|
|
|
|
error
|
|
|
|
)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: error_message})
|
2019-09-11 09:07:19 +00:00
|
|
|
end
|
2019-08-10 21:39:21 +00:00
|
|
|
end
|
|
|
|
|
2020-05-14 15:21:51 +00:00
|
|
|
def archive(conn, %{name: name}) do
|
2020-03-28 18:15:14 +00:00
|
|
|
with {:ok, archive} <- Pack.get_archive(name) do
|
2020-03-28 10:34:32 +00:00
|
|
|
send_download(conn, {:binary, archive}, filename: "#{name}.zip")
|
2019-09-11 16:00:48 +00:00
|
|
|
else
|
2020-05-18 15:43:23 +00:00
|
|
|
{:error, :cant_download} ->
|
2019-09-11 16:00:48 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:forbidden)
|
2019-09-11 16:39:47 +00:00
|
|
|
|> json(%{
|
2020-03-28 10:34:32 +00: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 16:39:47 +00:00
|
|
|
})
|
2019-08-10 21:39:21 +00:00
|
|
|
|
2020-11-13 12:13:14 +00:00
|
|
|
{:error, :enoent} ->
|
2019-08-10 21:39:21 +00:00
|
|
|
conn
|
2019-09-11 16:00:48 +00:00
|
|
|
|> put_status(:not_found)
|
2019-09-11 16:39:47 +00:00
|
|
|
|> json(%{error: "Pack #{name} does not exist"})
|
2019-08-10 21:39:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 15:21:51 +00:00
|
|
|
def download(%{body_params: %{url: url, name: name} = params} = conn, _) do
|
2020-05-18 15:43:23 +00:00
|
|
|
with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
|
2020-03-28 10:34:32 +00:00
|
|
|
json(conn, "ok")
|
2019-09-11 15:59:31 +00:00
|
|
|
else
|
2020-05-18 15:43:23 +00:00
|
|
|
{:error, :not_shareable} ->
|
2020-03-28 10:34:32 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: "The requested instance does not support sharing emoji packs"})
|
|
|
|
|
2020-05-27 19:34:37 +00:00
|
|
|
{:error, :invalid_checksum} ->
|
2020-03-28 10:34:32 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
|
|
|
|
|
2020-11-13 12:13:14 +00:00
|
|
|
{:error, error} ->
|
2020-03-28 10:34:32 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
2020-11-13 12:13:14 +00:00
|
|
|
|> json(%{error: error})
|
2019-08-10 21:39:21 +00:00
|
|
|
end
|
|
|
|
end
|
2019-08-12 15:03:59 +00:00
|
|
|
|
2020-05-14 15:21:51 +00:00
|
|
|
def create(conn, %{name: name}) do
|
2020-03-28 10:34:32 +00:00
|
|
|
name = String.trim(name)
|
2019-08-28 16:29:01 +00:00
|
|
|
|
2020-05-18 15:43:23 +00:00
|
|
|
with {:ok, _pack} <- Pack.create(name) do
|
2020-03-28 10:34:32 +00:00
|
|
|
json(conn, "ok")
|
|
|
|
else
|
|
|
|
{:error, :eexist} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:conflict)
|
|
|
|
|> json(%{error: "A pack named \"#{name}\" already exists"})
|
2019-08-28 16:29:01 +00:00
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "pack name cannot be empty"})
|
2019-08-28 16:29:01 +00:00
|
|
|
|
2020-11-13 12:13:14 +00:00
|
|
|
{:error, error} ->
|
|
|
|
error_message =
|
|
|
|
add_posix_error(
|
|
|
|
"Unexpected error occurred while creating pack.",
|
|
|
|
error
|
|
|
|
)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: error_message})
|
2019-08-28 16:29:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-14 15:21:51 +00:00
|
|
|
def delete(conn, %{name: name}) do
|
2020-03-28 10:34:32 +00: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 15:03:59 +00:00
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "pack name cannot be empty"})
|
2019-08-12 15:03:59 +00:00
|
|
|
|
2020-11-13 12:13:14 +00:00
|
|
|
{:error, error, _} ->
|
|
|
|
error_message = add_posix_error("Couldn't delete the pack #{name}", error)
|
|
|
|
|
2019-09-11 16:39:47 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
2020-11-13 12:13:14 +00:00
|
|
|
|> json(%{error: error_message})
|
2019-08-12 15:03:59 +00:00
|
|
|
end
|
|
|
|
end
|
2019-08-15 16:55:58 +00:00
|
|
|
|
2020-05-14 15:21:51 +00:00
|
|
|
def update(%{body_params: %{metadata: metadata}} = conn, %{name: name}) do
|
2020-03-28 18:15:14 +00:00
|
|
|
with {:ok, pack} <- Pack.update_metadata(name, metadata) do
|
2020-03-28 10:34:32 +00:00
|
|
|
json(conn, pack.pack)
|
2019-09-11 15:32:54 +00:00
|
|
|
else
|
2020-05-18 15:43:23 +00:00
|
|
|
{:error, :incomplete} ->
|
2019-09-11 15:32:54 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2019-09-11 16:39:47 +00:00
|
|
|
|> json(%{error: "The fallback archive does not have all files specified in pack.json"})
|
2020-02-06 15:01:12 +00:00
|
|
|
|
2020-11-13 12:13:14 +00:00
|
|
|
{:error, error} ->
|
|
|
|
error_message =
|
|
|
|
add_posix_error(
|
|
|
|
"Unexpected error occurred while updating pack metadata.",
|
|
|
|
error
|
|
|
|
)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: error_message})
|
2019-09-11 16:39:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-28 18:15:14 +00:00
|
|
|
def import_from_filesystem(conn, _params) do
|
2020-03-28 10:34:32 +00:00
|
|
|
with {:ok, names} <- Pack.import_from_filesystem() do
|
|
|
|
json(conn, names)
|
2019-09-11 15:32:54 +00:00
|
|
|
else
|
2020-03-30 06:09:27 +00:00
|
|
|
{:error, :no_read_write} ->
|
2020-01-29 10:51:17 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
2020-01-30 14:09:41 +00:00
|
|
|
|> json(%{error: "Error: emoji pack directory must be writable"})
|
2020-01-29 10:51:17 +00:00
|
|
|
|
2019-09-10 18:16:30 +00:00
|
|
|
{:error, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
2019-09-11 16:39:47 +00:00
|
|
|
|> json(%{error: "Error accessing emoji pack directory"})
|
2019-09-11 15:32:54 +00:00
|
|
|
end
|
|
|
|
end
|
2020-11-13 12:13:14 +00:00
|
|
|
|
|
|
|
defp add_posix_error(msg, error) do
|
|
|
|
[msg, Pleroma.Utils.posix_error_message(error)]
|
|
|
|
|> Enum.join(" ")
|
|
|
|
|> String.trim()
|
|
|
|
end
|
2019-08-10 21:39:21 +00:00
|
|
|
end
|