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)
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(conn, params) 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(conn, params) 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}"
64 def show(conn, %{name: name, page: page, page_size: page_size}) do
65 name = String.trim(name)
67 with {:ok, pack} <- Pack.show(name: name, page: page, page_size: page_size) do
72 |> put_status(:not_found)
73 |> json(%{error: "Pack #{name} does not exist"})
75 {:error, :empty_values} ->
77 |> put_status(:bad_request)
78 |> json(%{error: "pack name cannot be empty"})
83 "Failed to get the contents of the `#{name}` pack.",
88 |> put_status(:internal_server_error)
89 |> json(%{error: error_message})
93 def archive(conn, %{name: name}) do
94 with {:ok, archive} <- Pack.get_archive(name) do
95 send_download(conn, {:binary, archive}, filename: "#{name}.zip")
97 {:error, :cant_download} ->
99 |> put_status(:forbidden)
102 "Pack #{name} cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
107 |> put_status(:not_found)
108 |> json(%{error: "Pack #{name} does not exist"})
112 def download(%{body_params: %{url: url, name: name} = params} = conn, _) do
113 with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
116 {:error, :not_shareable} ->
118 |> put_status(:internal_server_error)
119 |> json(%{error: "The requested instance does not support sharing emoji packs"})
121 {:error, :invalid_checksum} ->
123 |> put_status(:internal_server_error)
124 |> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
128 |> put_status(:internal_server_error)
129 |> json(%{error: error})
133 def create(conn, %{name: name}) do
134 name = String.trim(name)
136 with {:ok, _pack} <- Pack.create(name) do
141 |> put_status(:conflict)
142 |> json(%{error: "A pack named \"#{name}\" already exists"})
144 {:error, :empty_values} ->
146 |> put_status(:bad_request)
147 |> json(%{error: "pack name cannot be empty"})
152 "Unexpected error occurred while creating pack.",
157 |> put_status(:internal_server_error)
158 |> json(%{error: error_message})
162 def delete(conn, %{name: name}) do
163 name = String.trim(name)
165 with {:ok, deleted} when deleted != [] <- Pack.delete(name) do
170 |> put_status(:not_found)
171 |> json(%{error: "Pack #{name} does not exist"})
173 {:error, :empty_values} ->
175 |> put_status(:bad_request)
176 |> json(%{error: "pack name cannot be empty"})
178 {:error, error, _} ->
179 error_message = add_posix_error("Couldn't delete the `#{name}` pack", error)
182 |> put_status(:internal_server_error)
183 |> json(%{error: error_message})
187 def update(%{body_params: %{metadata: metadata}} = conn, %{name: name}) do
188 with {:ok, pack} <- Pack.update_metadata(name, metadata) do
189 json(conn, pack.pack)
191 {:error, :incomplete} ->
193 |> put_status(:bad_request)
194 |> json(%{error: "The fallback archive does not have all files specified in pack.json"})
199 "Unexpected error occurred while updating pack metadata.",
204 |> put_status(:internal_server_error)
205 |> json(%{error: error_message})
209 def import_from_filesystem(conn, _params) do
210 with {:ok, names} <- Pack.import_from_filesystem() do
213 {:error, :no_read_write} ->
215 |> put_status(:internal_server_error)
216 |> json(%{error: "Error: emoji pack directory must be writable"})
220 |> put_status(:internal_server_error)
221 |> json(%{error: "Error accessing emoji pack directory"})
225 defp add_posix_error(msg, error) do
226 [msg, Pleroma.Utils.posix_error_message(error)]