2019-09-11 19:43:00 +00:00
|
|
|
defmodule Pleroma.Web.PleromaAPI.EmojiAPIController 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
|
|
|
|
2019-10-06 14:12:17 +00:00
|
|
|
plug(
|
2020-03-28 10:34:32 +00:00
|
|
|
Pleroma.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 [
|
|
|
|
:create,
|
|
|
|
:delete,
|
2020-03-28 10:34:32 +00:00
|
|
|
:download_from,
|
2019-10-06 14:12:17 +00:00
|
|
|
:import_from_fs,
|
|
|
|
:update_file,
|
|
|
|
:update_metadata
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-04-21 13:29:19 +00:00
|
|
|
plug(
|
|
|
|
:skip_plug,
|
2020-03-28 10:34:32 +00:00
|
|
|
[Pleroma.Plugs.OAuthScopesPlug, Pleroma.Plugs.ExpectPublicOrAuthenticatedCheckPlug]
|
2020-04-21 13:29:19 +00:00
|
|
|
when action in [:download_shared, :list_packs, :list_from]
|
|
|
|
)
|
2019-10-06 14:12:17 +00:00
|
|
|
|
2019-09-24 16:18:07 +00:00
|
|
|
@doc """
|
|
|
|
Lists packs from the remote instance.
|
|
|
|
|
|
|
|
Since JS cannot ask remote instances for their packs due to CPS, it has to
|
|
|
|
be done by the server
|
|
|
|
"""
|
|
|
|
def list_from(conn, %{"instance_address" => address}) do
|
2020-03-28 10:34:32 +00:00
|
|
|
with {:ok, packs} <- Pack.list_remote_packs(address) do
|
|
|
|
json(conn, packs)
|
2019-09-24 16:18:07 +00:00
|
|
|
else
|
2020-03-28 10:34:32 +00:00
|
|
|
{:shareable, _} ->
|
|
|
|
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
|
|
|
|
|
2019-09-10 18:34:57 +00:00
|
|
|
@doc """
|
|
|
|
Lists the packs available on the instance as JSON.
|
|
|
|
|
2019-12-15 19:32:42 +00:00
|
|
|
The information is public and does not require authentication. The format is
|
2019-09-10 18:34:57 +00:00
|
|
|
a map of "pack directory name" to pack.json contents.
|
|
|
|
"""
|
2019-08-10 21:39:21 +00:00
|
|
|
def list_packs(conn, _params) do
|
2020-03-28 10:34:32 +00:00
|
|
|
emoji_path =
|
|
|
|
Path.join(
|
|
|
|
Pleroma.Config.get!([:instance, :static_dir]),
|
|
|
|
"emoji"
|
|
|
|
)
|
|
|
|
|
|
|
|
with {:ok, packs} <- Pack.list_local_packs() do
|
|
|
|
json(conn, packs)
|
2019-09-24 06:27:34 +00:00
|
|
|
else
|
|
|
|
{:create_dir, {:error, e}} ->
|
|
|
|
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
|
|
|
|
|
|
|
{:ls, {:error, e}} ->
|
|
|
|
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-03-28 10:34:32 +00:00
|
|
|
def show(conn, %{"name" => name}) do
|
|
|
|
name = String.trim(name)
|
2019-09-11 15:48:51 +00:00
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
with {:ok, pack} <- Pack.show(name) do
|
|
|
|
json(conn, pack)
|
2019-09-11 15:48:51 +00:00
|
|
|
else
|
2020-03-28 10:34:32 +00:00
|
|
|
{:loaded, _} ->
|
|
|
|
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"})
|
2019-09-11 09:07:19 +00:00
|
|
|
end
|
2019-08-10 21:39:21 +00:00
|
|
|
end
|
|
|
|
|
2019-09-10 18:34:57 +00:00
|
|
|
@doc """
|
|
|
|
An endpoint for other instances (via admin UI) or users (via browser)
|
|
|
|
to download packs that the instance shares.
|
|
|
|
"""
|
2019-08-10 21:39:21 +00:00
|
|
|
def download_shared(conn, %{"name" => name}) do
|
2020-03-28 10:34:32 +00:00
|
|
|
with {:ok, archive} <- Pack.download(name) do
|
|
|
|
send_download(conn, {:binary, archive}, filename: "#{name}.zip")
|
2019-09-11 16:00:48 +00:00
|
|
|
else
|
|
|
|
{:can_download?, _} ->
|
|
|
|
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
|
|
|
|
2019-09-11 16:00:48 +00:00
|
|
|
{:exists?, _} ->
|
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
|
|
|
|
|
2019-09-10 18:34:57 +00:00
|
|
|
@doc """
|
2020-04-21 13:29:19 +00:00
|
|
|
An admin endpoint to request downloading and storing a pack named `pack_name` from the instance
|
2019-09-10 18:34:57 +00:00
|
|
|
`instance_address`.
|
|
|
|
|
|
|
|
If the requested instance's admin chose to share the pack, it will be downloaded
|
|
|
|
from that instance, otherwise it will be downloaded from the fallback source, if there is one.
|
|
|
|
"""
|
2020-03-28 10:34:32 +00:00
|
|
|
def download_from(conn, %{"instance_address" => address, "pack_name" => name} = params) do
|
|
|
|
with :ok <- Pack.download_from_source(name, address, params["as"]) do
|
|
|
|
json(conn, "ok")
|
2019-09-11 15:59:31 +00:00
|
|
|
else
|
2020-03-28 10:34:32 +00:00
|
|
|
{:shareable, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: "The requested instance does not support sharing emoji packs"})
|
|
|
|
|
|
|
|
{:checksum, _} ->
|
|
|
|
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 21:39:21 +00:00
|
|
|
end
|
|
|
|
end
|
2019-08-12 15:03:59 +00:00
|
|
|
|
2019-09-10 18:34:57 +00:00
|
|
|
@doc """
|
|
|
|
Creates an empty pack named `name` which then can be updated via the admin UI.
|
|
|
|
"""
|
2019-08-28 16:29:01 +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-03-28 10:34:32 +00:00
|
|
|
with :ok <- Pack.create(name) do
|
|
|
|
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-03-28 10:34:32 +00:00
|
|
|
{:error, _} ->
|
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
:internal_server_error,
|
|
|
|
"Unexpected error occurred while creating pack."
|
|
|
|
)
|
2019-08-28 16:29:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-10 18:34:57 +00:00
|
|
|
@doc """
|
|
|
|
Deletes the pack `name` and all it's files.
|
|
|
|
"""
|
2019-08-12 15:03:59 +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-02-25 14:34:56 +00:00
|
|
|
{:error, _, _} ->
|
2019-09-11 16:39:47 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: "Couldn't delete the pack #{name}"})
|
2019-08-12 15:03:59 +00:00
|
|
|
end
|
|
|
|
end
|
2019-08-15 16:55:58 +00:00
|
|
|
|
2019-09-10 18:34:57 +00:00
|
|
|
@doc """
|
|
|
|
An endpoint to update `pack_names`'s metadata.
|
|
|
|
|
|
|
|
`new_data` is the new metadata for the pack, that will replace the old metadata.
|
|
|
|
"""
|
2019-08-18 19:05:38 +00:00
|
|
|
def update_metadata(conn, %{"pack_name" => name, "new_data" => new_data}) do
|
2020-03-28 10:34:32 +00:00
|
|
|
with {:ok, pack} <- Pack.update_metadata(name, new_data) do
|
|
|
|
json(conn, pack.pack)
|
2019-09-11 15:32:54 +00:00
|
|
|
else
|
|
|
|
{:has_all_files?, _} ->
|
|
|
|
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-03-28 10:34:32 +00:00
|
|
|
{:error, _} ->
|
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
:internal_server_error,
|
|
|
|
"Unexpected error occurred while updating pack metadata."
|
|
|
|
)
|
2019-09-11 16:39:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-10 18:34:57 +00:00
|
|
|
@doc """
|
|
|
|
Updates a file in a pack.
|
|
|
|
|
|
|
|
Updating can mean three things:
|
|
|
|
|
|
|
|
- `add` adds an emoji named `shortcode` to the pack `pack_name`,
|
|
|
|
that means that the emoji file needs to be uploaded with the request
|
|
|
|
(thus requiring it to be a multipart request) and be named `file`.
|
|
|
|
There can also be an optional `filename` that will be the new emoji file name
|
|
|
|
(if it's not there, the name will be taken from the uploaded file).
|
|
|
|
- `update` changes emoji shortcode (from `shortcode` to `new_shortcode` or moves the file
|
|
|
|
(from the current filename to `new_filename`)
|
|
|
|
- `remove` removes the emoji named `shortcode` and it's associated file
|
|
|
|
"""
|
2019-09-11 16:39:47 +00:00
|
|
|
|
|
|
|
# Add
|
2019-08-18 19:05:38 +00:00
|
|
|
def update_file(
|
|
|
|
conn,
|
2020-02-06 15:01:12 +00:00
|
|
|
%{"pack_name" => pack_name, "action" => "add"} = params
|
2019-08-18 19:05:38 +00:00
|
|
|
) do
|
2020-03-28 10:34:32 +00:00
|
|
|
filename = params["filename"] || get_filename(params["file"])
|
|
|
|
shortcode = params["shortcode"] || Path.basename(filename, Path.extname(filename))
|
|
|
|
|
|
|
|
with {:ok, pack} <- Pack.add_file(pack_name, shortcode, filename, params["file"]) do
|
|
|
|
json(conn, pack.files)
|
2019-09-11 16:39:47 +00:00
|
|
|
else
|
2020-03-28 10:34:32 +00:00
|
|
|
{:exists, _} ->
|
2019-09-11 16:39:47 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:conflict)
|
|
|
|
|> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"})
|
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
{:loaded, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "pack \"#{pack_name}\" is not found"})
|
|
|
|
|
|
|
|
{:error, :empty_values} ->
|
2019-09-11 16:39:47 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2020-03-28 10:34:32 +00: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 16:39:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove
|
|
|
|
def update_file(conn, %{
|
|
|
|
"pack_name" => pack_name,
|
|
|
|
"action" => "remove",
|
|
|
|
"shortcode" => shortcode
|
|
|
|
}) do
|
2020-03-28 10:34:32 +00:00
|
|
|
with {:ok, pack} <- Pack.remove_file(pack_name, shortcode) do
|
|
|
|
json(conn, pack.files)
|
|
|
|
else
|
|
|
|
{:exists, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "Emoji \"#{shortcode}\" does not exist"})
|
2019-09-11 16:39:47 +00:00
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
{:loaded, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "pack \"#{pack_name}\" is not found"})
|
2019-09-11 16:39:47 +00:00
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "pack name or shortcode cannot be empty"})
|
2019-09-11 16:39:47 +00:00
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
{:error, _} ->
|
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
:internal_server_error,
|
|
|
|
"Unexpected error occurred while removing file from pack."
|
|
|
|
)
|
2019-09-11 16:39:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update
|
|
|
|
def update_file(
|
|
|
|
conn,
|
2020-03-28 10:34:32 +00:00
|
|
|
%{"pack_name" => name, "action" => "update", "shortcode" => shortcode} = params
|
2019-09-11 16:39:47 +00:00
|
|
|
) do
|
2020-03-28 10:34:32 +00:00
|
|
|
new_shortcode = params["new_shortcode"]
|
|
|
|
new_filename = params["new_filename"]
|
|
|
|
force = params["force"] == true
|
|
|
|
|
|
|
|
with {:ok, pack} <- Pack.update_file(name, shortcode, new_shortcode, new_filename, force) do
|
|
|
|
json(conn, pack.files)
|
2019-09-11 16:39:47 +00:00
|
|
|
else
|
2020-03-28 10:34:32 +00:00
|
|
|
{:exists, _} ->
|
2019-09-11 16:39:47 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "Emoji \"#{shortcode}\" does not exist"})
|
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
{:not_used, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:conflict)
|
|
|
|
|> json(%{
|
|
|
|
error:
|
|
|
|
"New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:loaded, _} ->
|
2019-09-11 16:39:47 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2020-03-28 10:34:32 +00:00
|
|
|
|> json(%{error: "pack \"#{name}\" is not found"})
|
2019-09-11 16:39:47 +00:00
|
|
|
|
2020-03-28 10:34:32 +00:00
|
|
|
{:error, :empty_values} ->
|
2019-09-11 16:39:47 +00:00
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
2020-03-28 10:34:32 +00:00
|
|
|
|> json(%{error: "new_shortcode or new_filename cannot be empty"})
|
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
:internal_server_error,
|
|
|
|
"Unexpected error occurred while updating file in pack."
|
|
|
|
)
|
2019-08-18 19:05:38 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-10 18:16:30 +00:00
|
|
|
|
2019-09-11 16:39:47 +00:00
|
|
|
def update_file(conn, %{"action" => action}) do
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "Unknown action: #{action}"})
|
|
|
|
end
|
|
|
|
|
2019-09-10 18:34:57 +00:00
|
|
|
@doc """
|
|
|
|
Imports emoji from the filesystem.
|
|
|
|
|
|
|
|
Importing means checking all the directories in the
|
|
|
|
`$instance_static/emoji/` for directories which do not have
|
|
|
|
`pack.json`. If one has an emoji.txt file, that file will be used
|
|
|
|
to create a `pack.json` file with it's contents. If the directory has
|
|
|
|
neither, all the files with specific configured extenstions will be
|
|
|
|
assumed to be emojis and stored in the new `pack.json` file.
|
|
|
|
"""
|
2020-03-28 10:34:32 +00:00
|
|
|
|
2019-09-10 18:16:30 +00:00
|
|
|
def import_from_fs(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-28 10:34:32 +00:00
|
|
|
{:error, :not_writable} ->
|
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
|
2019-09-10 18:16:30 +00:00
|
|
|
|
2020-03-28 10:34:32 +00: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 21:39:21 +00:00
|
|
|
end
|