Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 line
5.8KB

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