summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/pleroma_api/controllers/emoji_pack_controller.ex
blob: 657f4632451a192a0bf34dccb1bfe928d5e3c5d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
defmodule Pleroma.Web.PleromaAPI.EmojiPackController do
  use Pleroma.Web, :controller

  alias Pleroma.Emoji.Pack

  plug(Pleroma.Web.ApiSpec.CastAndValidate)

  plug(
    Pleroma.Plugs.OAuthScopesPlug,
    %{scopes: ["write"], admin: true}
    when action in [
           :import_from_filesystem,
           :remote,
           :download,
           :create,
           :update,
           :delete,
           :add_file,
           :update_file,
           :delete_file
         ]
  )

  @skip_plugs [Pleroma.Plugs.OAuthScopesPlug, Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug]
  plug(:skip_plug, @skip_plugs when action in [:index, :show, :archive])

  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaEmojiPackOperation

  def remote(conn, %{url: url}) do
    with {:ok, packs} <- Pack.list_remote(url) do
      json(conn, packs)
    else
      {:error, :not_shareable} ->
        conn
        |> put_status(:internal_server_error)
        |> json(%{error: "The requested instance does not support sharing emoji packs"})
    end
  end

  def index(conn, params) do
    emoji_path =
      [:instance, :static_dir]
      |> Pleroma.Config.get!()
      |> Path.join("emoji")

    with {:ok, packs, count} <- Pack.list_local(page: params.page, page_size: params.page_size) do
      json(conn, %{packs: packs, count: count})
    else
      {:error, :create_dir, e} ->
        conn
        |> put_status(:internal_server_error)
        |> json(%{error: "Failed to create the emoji pack directory at #{emoji_path}: #{e}"})

      {:error, :ls, e} ->
        conn
        |> put_status(:internal_server_error)
        |> json(%{
          error: "Failed to get the contents of the emoji pack directory at #{emoji_path}: #{e}"
        })
    end
  end

  def show(conn, %{name: name, page: page, page_size: page_size}) do
    name = String.trim(name)

    with {:ok, pack} <- Pack.show(name: name, page: page, page_size: page_size) do
      json(conn, pack)
    else
      {:error, :not_found} ->
        conn
        |> put_status(:not_found)
        |> json(%{error: "Pack #{name} does not exist"})

      {:error, :empty_values} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "pack name cannot be empty"})
    end
  end

  def archive(conn, %{name: name}) do
    with {:ok, archive} <- Pack.get_archive(name) do
      send_download(conn, {:binary, archive}, filename: "#{name}.zip")
    else
      {:error, :cant_download} ->
        conn
        |> put_status(:forbidden)
        |> json(%{
          error:
            "Pack #{name} cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
        })

      {:error, :not_found} ->
        conn
        |> put_status(:not_found)
        |> json(%{error: "Pack #{name} does not exist"})
    end
  end

  def download(%{body_params: %{url: url, name: name} = params} = conn, _) do
    with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
      json(conn, "ok")
    else
      {:error, :not_shareable} ->
        conn
        |> put_status(:internal_server_error)
        |> json(%{error: "The requested instance does not support sharing emoji packs"})

      {:error, :invalid_checksum} ->
        conn
        |> put_status(:internal_server_error)
        |> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})

      {:error, e} ->
        conn
        |> put_status(:internal_server_error)
        |> json(%{error: e})
    end
  end

  def create(conn, %{name: name}) do
    name = String.trim(name)

    with {:ok, _pack} <- Pack.create(name) do
      json(conn, "ok")
    else
      {:error, :eexist} ->
        conn
        |> put_status(:conflict)
        |> json(%{error: "A pack named \"#{name}\" already exists"})

      {:error, :empty_values} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "pack name cannot be empty"})

      {:error, _} ->
        render_error(
          conn,
          :internal_server_error,
          "Unexpected error occurred while creating pack."
        )
    end
  end

  def delete(conn, %{name: name}) do
    name = String.trim(name)

    with {:ok, deleted} when deleted != [] <- Pack.delete(name) do
      json(conn, "ok")
    else
      {:ok, []} ->
        conn
        |> put_status(:not_found)
        |> json(%{error: "Pack #{name} does not exist"})

      {:error, :empty_values} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "pack name cannot be empty"})

      {:error, _, _} ->
        conn
        |> put_status(:internal_server_error)
        |> json(%{error: "Couldn't delete the pack #{name}"})
    end
  end

  def update(%{body_params: %{metadata: metadata}} = conn, %{name: name}) do
    with {:ok, pack} <- Pack.update_metadata(name, metadata) do
      json(conn, pack.pack)
    else
      {:error, :incomplete} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "The fallback archive does not have all files specified in pack.json"})

      {:error, _} ->
        render_error(
          conn,
          :internal_server_error,
          "Unexpected error occurred while updating pack metadata."
        )
    end
  end

  def add_file(%{body_params: params} = conn, %{name: name}) do
    filename = params[:filename] || get_filename(params[:file])
    shortcode = params[:shortcode] || Path.basename(filename, Path.extname(filename))

    with {:ok, pack} <- Pack.add_file(name, shortcode, filename, params[:file]) do
      json(conn, pack.files)
    else
      {:error, :already_exists} ->
        conn
        |> put_status(:conflict)
        |> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"})

      {:error, :not_found} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "pack \"#{name}\" is not found"})

      {:error, :empty_values} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "pack name, shortcode or filename cannot be empty"})

      {:error, _} ->
        render_error(
          conn,
          :internal_server_error,
          "Unexpected error occurred while adding file to pack."
        )
    end
  end

  def update_file(%{body_params: %{shortcode: shortcode} = params} = conn, %{name: name}) do
    new_shortcode = params[:new_shortcode]
    new_filename = params[:new_filename]
    force = params[:force]

    with {:ok, pack} <- Pack.update_file(name, shortcode, new_shortcode, new_filename, force) do
      json(conn, pack.files)
    else
      {:error, :doesnt_exist} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "Emoji \"#{shortcode}\" does not exist"})

      {:error, :already_exists} ->
        conn
        |> put_status(:conflict)
        |> json(%{
          error:
            "New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
        })

      {:error, :not_found} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "pack \"#{name}\" is not found"})

      {:error, :empty_values} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "new_shortcode or new_filename cannot be empty"})

      {:error, _} ->
        render_error(
          conn,
          :internal_server_error,
          "Unexpected error occurred while updating file in pack."
        )
    end
  end

  def delete_file(conn, %{name: name, shortcode: shortcode}) do
    with {:ok, pack} <- Pack.delete_file(name, shortcode) do
      json(conn, pack.files)
    else
      {:error, :doesnt_exist} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "Emoji \"#{shortcode}\" does not exist"})

      {:error, :not_found} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "pack \"#{name}\" is not found"})

      {:error, :empty_values} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: "pack name or shortcode cannot be empty"})

      {:error, _} ->
        render_error(
          conn,
          :internal_server_error,
          "Unexpected error occurred while removing file from pack."
        )
    end
  end

  def import_from_filesystem(conn, _params) do
    with {:ok, names} <- Pack.import_from_filesystem() do
      json(conn, names)
    else
      {:error, :no_read_write} ->
        conn
        |> put_status(:internal_server_error)
        |> json(%{error: "Error: emoji pack directory must be writable"})

      {:error, _} ->
        conn
        |> put_status(:internal_server_error)
        |> json(%{error: "Error accessing emoji pack directory"})
    end
  end

  defp get_filename(%Plug.Upload{filename: filename}), do: filename
  defp get_filename(url) when is_binary(url), do: Path.basename(url)
end