Merge branch 'refactor/locked_user_field' into 'develop'
[akkoma] / lib / pleroma / web / pleroma_api / controllers / emoji_pack_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiPackController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Emoji.Pack
9
10 plug(Pleroma.Web.ApiSpec.CastAndValidate)
11
12 plug(
13 Pleroma.Web.Plugs.OAuthScopesPlug,
14 %{scopes: ["write"], admin: true}
15 when action in [
16 :import_from_filesystem,
17 :remote,
18 :download,
19 :create,
20 :update,
21 :delete
22 ]
23 )
24
25 @skip_plugs [
26 Pleroma.Web.Plugs.OAuthScopesPlug,
27 Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
28 ]
29 plug(:skip_plug, @skip_plugs when action in [:index, :archive, :show])
30
31 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaEmojiPackOperation
32
33 def remote(conn, params) do
34 with {:ok, packs} <-
35 Pack.list_remote(url: params.url, page_size: params.page_size, page: params.page) do
36 json(conn, packs)
37 else
38 {:error, :not_shareable} ->
39 conn
40 |> put_status(:internal_server_error)
41 |> json(%{error: "The requested instance does not support sharing emoji packs"})
42 end
43 end
44
45 def index(conn, params) do
46 emoji_path =
47 [:instance, :static_dir]
48 |> Pleroma.Config.get!()
49 |> Path.join("emoji")
50
51 with {:ok, packs, count} <- Pack.list_local(page: params.page, page_size: params.page_size) do
52 json(conn, %{packs: packs, count: count})
53 else
54 {:error, :create_dir, e} ->
55 conn
56 |> put_status(:internal_server_error)
57 |> json(%{error: "Failed to create the emoji pack directory at #{emoji_path}: #{e}"})
58
59 {:error, :ls, e} ->
60 conn
61 |> put_status(:internal_server_error)
62 |> json(%{
63 error: "Failed to get the contents of the emoji pack directory at #{emoji_path}: #{e}"
64 })
65 end
66 end
67
68 def show(conn, %{name: name, page: page, page_size: page_size}) do
69 name = String.trim(name)
70
71 with {:ok, pack} <- Pack.show(name: name, page: page, page_size: page_size) do
72 json(conn, pack)
73 else
74 {:error, :not_found} ->
75 conn
76 |> put_status(:not_found)
77 |> json(%{error: "Pack #{name} does not exist"})
78
79 {:error, :empty_values} ->
80 conn
81 |> put_status(:bad_request)
82 |> json(%{error: "pack name cannot be empty"})
83 end
84 end
85
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")
89 else
90 {:error, :cant_download} ->
91 conn
92 |> put_status(:forbidden)
93 |> json(%{
94 error:
95 "Pack #{name} cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
96 })
97
98 {:error, :not_found} ->
99 conn
100 |> put_status(:not_found)
101 |> json(%{error: "Pack #{name} does not exist"})
102 end
103 end
104
105 def download(%{body_params: %{url: url, name: name} = params} = conn, _) do
106 with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
107 json(conn, "ok")
108 else
109 {:error, :not_shareable} ->
110 conn
111 |> put_status(:internal_server_error)
112 |> json(%{error: "The requested instance does not support sharing emoji packs"})
113
114 {:error, :invalid_checksum} ->
115 conn
116 |> put_status(:internal_server_error)
117 |> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
118
119 {:error, e} ->
120 conn
121 |> put_status(:internal_server_error)
122 |> json(%{error: e})
123 end
124 end
125
126 def create(conn, %{name: name}) do
127 name = String.trim(name)
128
129 with {:ok, _pack} <- Pack.create(name) do
130 json(conn, "ok")
131 else
132 {:error, :eexist} ->
133 conn
134 |> put_status(:conflict)
135 |> json(%{error: "A pack named \"#{name}\" already exists"})
136
137 {:error, :empty_values} ->
138 conn
139 |> put_status(:bad_request)
140 |> json(%{error: "pack name cannot be empty"})
141
142 {:error, _} ->
143 render_error(
144 conn,
145 :internal_server_error,
146 "Unexpected error occurred while creating pack."
147 )
148 end
149 end
150
151 def delete(conn, %{name: name}) do
152 name = String.trim(name)
153
154 with {:ok, deleted} when deleted != [] <- Pack.delete(name) do
155 json(conn, "ok")
156 else
157 {:ok, []} ->
158 conn
159 |> put_status(:not_found)
160 |> json(%{error: "Pack #{name} does not exist"})
161
162 {:error, :empty_values} ->
163 conn
164 |> put_status(:bad_request)
165 |> json(%{error: "pack name cannot be empty"})
166
167 {:error, _, _} ->
168 conn
169 |> put_status(:internal_server_error)
170 |> json(%{error: "Couldn't delete the pack #{name}"})
171 end
172 end
173
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)
177 else
178 {:error, :incomplete} ->
179 conn
180 |> put_status(:bad_request)
181 |> json(%{error: "The fallback archive does not have all files specified in pack.json"})
182
183 {:error, _} ->
184 render_error(
185 conn,
186 :internal_server_error,
187 "Unexpected error occurred while updating pack metadata."
188 )
189 end
190 end
191
192 def import_from_filesystem(conn, _params) do
193 with {:ok, names} <- Pack.import_from_filesystem() do
194 json(conn, names)
195 else
196 {:error, :no_read_write} ->
197 conn
198 |> put_status(:internal_server_error)
199 |> json(%{error: "Error: emoji pack directory must be writable"})
200
201 {:error, _} ->
202 conn
203 |> put_status(:internal_server_error)
204 |> json(%{error: "Error accessing emoji pack directory"})
205 end
206 end
207 end