1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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)
13 Pleroma.Web.Plugs.OAuthScopesPlug,
14 %{scopes: ["admin:write"]}
16 :import_from_filesystem,
26 Pleroma.Web.Plugs.OAuthScopesPlug,
27 Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
29 plug(:skip_plug, @skip_plugs when action in [:index, :archive, :show])
31 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaEmojiPackOperation
33 def remote(conn, params) do
35 Pack.list_remote(url: params.url, page_size: params.page_size, page: params.page) do
38 {:error, :not_shareable} ->
40 |> put_status(:internal_server_error)
41 |> json(%{error: "The requested instance does not support sharing emoji packs"})
45 def index(conn, params) do
47 [:instance, :static_dir]
48 |> Pleroma.Config.get!()
51 with {:ok, packs, count} <- Pack.list_local(page: params.page, page_size: params.page_size) do
52 json(conn, %{packs: packs, count: count})
54 {:error, :create_dir, e} ->
56 |> put_status(:internal_server_error)
57 |> json(%{error: "Failed to create the emoji pack directory at #{emoji_path}: #{e}"})
61 |> put_status(:internal_server_error)
63 error: "Failed to get the contents of the emoji pack directory at #{emoji_path}: #{e}"
68 def show(conn, %{name: name, page: page, page_size: page_size}) do
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(conn, %{name: name}) 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"})
116 def download(%{body_params: %{url: url, name: name} = params} = conn, _) do
117 with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
120 {:error, :not_shareable} ->
122 |> put_status(:internal_server_error)
123 |> json(%{error: "The requested instance does not support sharing emoji packs"})
125 {:error, :invalid_checksum} ->
127 |> put_status(:internal_server_error)
128 |> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
132 |> put_status(:internal_server_error)
133 |> json(%{error: error})
137 def create(conn, %{name: name}) do
138 name = String.trim(name)
140 with {:ok, _pack} <- Pack.create(name) do
145 |> put_status(:conflict)
146 |> json(%{error: "A pack named \"#{name}\" already exists"})
148 {:error, :empty_values} ->
150 |> put_status(:bad_request)
151 |> json(%{error: "pack name cannot be empty"})
156 "Unexpected error occurred while creating pack.",
161 |> put_status(:internal_server_error)
162 |> json(%{error: error_message})
166 def delete(conn, %{name: name}) do
167 name = String.trim(name)
169 with {:ok, deleted} when deleted != [] <- Pack.delete(name) do
174 |> put_status(:not_found)
175 |> json(%{error: "Pack #{name} does not exist"})
177 {:error, :empty_values} ->
179 |> put_status(:bad_request)
180 |> json(%{error: "pack name cannot be empty"})
182 {:error, error, _} ->
183 error_message = add_posix_error("Couldn't delete the `#{name}` pack", error)
186 |> put_status(:internal_server_error)
187 |> json(%{error: error_message})
191 def update(%{body_params: %{metadata: metadata}} = conn, %{name: name}) do
192 with {:ok, pack} <- Pack.update_metadata(name, metadata) do
193 json(conn, pack.pack)
195 {:error, :incomplete} ->
197 |> put_status(:bad_request)
198 |> json(%{error: "The fallback archive does not have all files specified in pack.json"})
203 "Unexpected error occurred while updating pack metadata.",
208 |> put_status(:internal_server_error)
209 |> json(%{error: error_message})
213 def import_from_filesystem(conn, _params) do
214 with {:ok, names} <- Pack.import_from_filesystem() do
217 {:error, :no_read_write} ->
219 |> put_status(:internal_server_error)
220 |> json(%{error: "Error: emoji pack directory must be writable"})
224 |> put_status(:internal_server_error)
225 |> json(%{error: "Error accessing emoji pack directory"})
229 defp add_posix_error(msg, error) do
230 [msg, Pleroma.Utils.posix_error_message(error)]