1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.EmojiPackController do
6 use Pleroma.Web, :controller
8 alias Pleroma.Emoji.Pack
10 plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
13 Pleroma.Web.Plugs.OAuthScopesPlug,
14 %{scopes: ["admin:write"]}
16 :import_from_filesystem,
25 plug(:skip_auth when action in [:index, :archive, :show])
27 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaEmojiPackOperation
29 def remote(%{private: %{open_api_spex: %{params: params}}} = conn, _) do
31 Pack.list_remote(url: params.url, page_size: params.page_size, page: params.page) do
34 {:error, :not_shareable} ->
36 |> put_status(:internal_server_error)
37 |> json(%{error: "The requested instance does not support sharing emoji packs"})
41 def index(%{private: %{open_api_spex: %{params: params}}} = conn, _) do
43 [:instance, :static_dir]
44 |> Pleroma.Config.get!()
47 with {:ok, packs, count} <- Pack.list_local(page: params.page, page_size: params.page_size) do
48 json(conn, %{packs: packs, count: count})
50 {:error, :create_dir, e} ->
52 |> put_status(:internal_server_error)
53 |> json(%{error: "Failed to create the emoji pack directory at #{emoji_path}: #{e}"})
57 |> put_status(:internal_server_error)
59 error: "Failed to get the contents of the emoji pack directory at #{emoji_path}: #{e}"
65 %{private: %{open_api_spex: %{params: %{name: name, page: page, page_size: page_size}}}} =
69 name = String.trim(name)
71 with {:ok, pack} <- Pack.show(name: name, page: page, page_size: page_size) do
76 |> put_status(:not_found)
77 |> json(%{error: "Pack #{name} does not exist"})
79 {:error, :empty_values} ->
81 |> put_status(:bad_request)
82 |> json(%{error: "pack name cannot be empty"})
87 "Failed to get the contents of the `#{name}` pack.",
92 |> put_status(:internal_server_error)
93 |> json(%{error: error_message})
97 def archive(%{private: %{open_api_spex: %{params: %{name: name}}}} = conn, _) do
98 with {:ok, archive} <- Pack.get_archive(name) do
99 send_download(conn, {:binary, archive}, filename: "#{name}.zip")
101 {:error, :cant_download} ->
103 |> put_status(:forbidden)
106 "Pack #{name} cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
111 |> put_status(:not_found)
112 |> json(%{error: "Pack #{name} does not exist"})
117 %{private: %{open_api_spex: %{body_params: %{url: url, name: name} = params}}} = conn,
120 with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
123 {:error, :not_shareable} ->
125 |> put_status(:internal_server_error)
126 |> json(%{error: "The requested instance does not support sharing emoji packs"})
128 {:error, :invalid_checksum} ->
130 |> put_status(:internal_server_error)
131 |> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
135 |> put_status(:internal_server_error)
136 |> json(%{error: error})
140 def create(%{private: %{open_api_spex: %{params: %{name: name}}}} = conn, _) do
141 name = String.trim(name)
143 with {:ok, _pack} <- Pack.create(name) do
148 |> put_status(:conflict)
149 |> json(%{error: "A pack named \"#{name}\" already exists"})
151 {:error, :empty_values} ->
153 |> put_status(:bad_request)
154 |> json(%{error: "pack name cannot be empty"})
159 "Unexpected error occurred while creating pack.",
164 |> put_status(:internal_server_error)
165 |> json(%{error: error_message})
169 def delete(%{private: %{open_api_spex: %{params: %{name: name}}}} = conn, _) do
170 name = String.trim(name)
172 with {:ok, deleted} when deleted != [] <- Pack.delete(name) do
177 |> put_status(:not_found)
178 |> json(%{error: "Pack #{name} does not exist"})
180 {:error, :empty_values} ->
182 |> put_status(:bad_request)
183 |> json(%{error: "pack name cannot be empty"})
185 {:error, error, _} ->
186 error_message = add_posix_error("Couldn't delete the `#{name}` pack", error)
189 |> put_status(:internal_server_error)
190 |> json(%{error: error_message})
195 %{private: %{open_api_spex: %{body_params: %{metadata: metadata}, params: %{name: name}}}} =
199 with {:ok, pack} <- Pack.update_metadata(name, metadata) do
200 json(conn, pack.pack)
202 {:error, :incomplete} ->
204 |> put_status(:bad_request)
205 |> json(%{error: "The fallback archive does not have all files specified in pack.json"})
210 "Unexpected error occurred while updating pack metadata.",
215 |> put_status(:internal_server_error)
216 |> json(%{error: error_message})
220 def import_from_filesystem(conn, _params) do
221 with {:ok, names} <- Pack.import_from_filesystem() do
224 {:error, :no_read_write} ->
226 |> put_status(:internal_server_error)
227 |> json(%{error: "Error: emoji pack directory must be writable"})
231 |> put_status(:internal_server_error)
232 |> json(%{error: "Error accessing emoji pack directory"})
236 defp add_posix_error(msg, error) do
237 [msg, Pleroma.Utils.posix_error_message(error)]