1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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: ["write"], admin: true}
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
74 {:error, :not_found} ->
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"})
86 def archive(conn, %{name: name}) do
87 with {:ok, archive} <- Pack.get_archive(name) do
88 send_download(conn, {:binary, archive}, filename: "#{name}.zip")
90 {:error, :cant_download} ->
92 |> put_status(:forbidden)
95 "Pack #{name} cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
98 {:error, :not_found} ->
100 |> put_status(:not_found)
101 |> json(%{error: "Pack #{name} does not exist"})
105 def download(%{body_params: %{url: url, name: name} = params} = conn, _) do
106 with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
109 {:error, :not_shareable} ->
111 |> put_status(:internal_server_error)
112 |> json(%{error: "The requested instance does not support sharing emoji packs"})
114 {:error, :invalid_checksum} ->
116 |> put_status(:internal_server_error)
117 |> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
121 |> put_status(:internal_server_error)
126 def create(conn, %{name: name}) do
127 name = String.trim(name)
129 with {:ok, _pack} <- Pack.create(name) do
134 |> put_status(:conflict)
135 |> json(%{error: "A pack named \"#{name}\" already exists"})
137 {:error, :empty_values} ->
139 |> put_status(:bad_request)
140 |> json(%{error: "pack name cannot be empty"})
145 :internal_server_error,
146 "Unexpected error occurred while creating pack."
151 def delete(conn, %{name: name}) do
152 name = String.trim(name)
154 with {:ok, deleted} when deleted != [] <- Pack.delete(name) do
159 |> put_status(:not_found)
160 |> json(%{error: "Pack #{name} does not exist"})
162 {:error, :empty_values} ->
164 |> put_status(:bad_request)
165 |> json(%{error: "pack name cannot be empty"})
169 |> put_status(:internal_server_error)
170 |> json(%{error: "Couldn't delete the pack #{name}"})
174 def update(%{body_params: %{metadata: metadata}} = conn, %{name: name}) do
175 with {:ok, pack} <- Pack.update_metadata(name, metadata) do
176 json(conn, pack.pack)
178 {:error, :incomplete} ->
180 |> put_status(:bad_request)
181 |> json(%{error: "The fallback archive does not have all files specified in pack.json"})
186 :internal_server_error,
187 "Unexpected error occurred while updating pack metadata."
192 def import_from_filesystem(conn, _params) do
193 with {:ok, names} <- Pack.import_from_filesystem() do
196 {:error, :no_read_write} ->
198 |> put_status(:internal_server_error)
199 |> json(%{error: "Error: emoji pack directory must be writable"})
203 |> put_status(:internal_server_error)
204 |> json(%{error: "Error accessing emoji pack directory"})