logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

pleroma_emoji_pack_operation.ex (10386B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ApiSpec.PleromaEmojiPackOperation do
  5. alias OpenApiSpex.Operation
  6. alias OpenApiSpex.Schema
  7. alias Pleroma.Web.ApiSpec.Schemas.ApiError
  8. import Pleroma.Web.ApiSpec.Helpers
  9. def open_api_operation(action) do
  10. operation = String.to_existing_atom("#{action}_operation")
  11. apply(__MODULE__, operation, [])
  12. end
  13. def remote_operation do
  14. %Operation{
  15. tags: ["Emoji pack administration"],
  16. summary: "Make request to another instance for emoji packs list",
  17. security: [%{"oAuth" => ["admin:write"]}],
  18. parameters: [
  19. url_param(),
  20. Operation.parameter(
  21. :page,
  22. :query,
  23. %Schema{type: :integer, default: 1},
  24. "Page"
  25. ),
  26. Operation.parameter(
  27. :page_size,
  28. :query,
  29. %Schema{type: :integer, default: 30},
  30. "Number of emoji to return"
  31. )
  32. ],
  33. operationId: "PleromaAPI.EmojiPackController.remote",
  34. responses: %{
  35. 200 => emoji_packs_response(),
  36. 500 => Operation.response("Error", "application/json", ApiError)
  37. }
  38. }
  39. end
  40. def index_operation do
  41. %Operation{
  42. tags: ["Emoji packs"],
  43. summary: "Lists local custom emoji packs",
  44. operationId: "PleromaAPI.EmojiPackController.index",
  45. parameters: [
  46. Operation.parameter(
  47. :page,
  48. :query,
  49. %Schema{type: :integer, default: 1},
  50. "Page"
  51. ),
  52. Operation.parameter(
  53. :page_size,
  54. :query,
  55. %Schema{type: :integer, default: 50},
  56. "Number of emoji packs to return"
  57. )
  58. ],
  59. responses: %{
  60. 200 => emoji_packs_response()
  61. }
  62. }
  63. end
  64. def show_operation do
  65. %Operation{
  66. tags: ["Emoji packs"],
  67. summary: "Show emoji pack",
  68. operationId: "PleromaAPI.EmojiPackController.show",
  69. parameters: [
  70. name_param(),
  71. Operation.parameter(
  72. :page,
  73. :query,
  74. %Schema{type: :integer, default: 1},
  75. "Page"
  76. ),
  77. Operation.parameter(
  78. :page_size,
  79. :query,
  80. %Schema{type: :integer, default: 30},
  81. "Number of emoji to return"
  82. )
  83. ],
  84. responses: %{
  85. 200 => Operation.response("Emoji Pack", "application/json", emoji_pack()),
  86. 400 => Operation.response("Bad Request", "application/json", ApiError),
  87. 404 => Operation.response("Not Found", "application/json", ApiError)
  88. }
  89. }
  90. end
  91. def archive_operation do
  92. %Operation{
  93. tags: ["Emoji packs"],
  94. summary: "Requests a local pack archive from the instance",
  95. operationId: "PleromaAPI.EmojiPackController.archive",
  96. parameters: [name_param()],
  97. responses: %{
  98. 200 =>
  99. Operation.response("Archive file", "application/octet-stream", %Schema{
  100. type: :string,
  101. format: :binary
  102. }),
  103. 403 => Operation.response("Forbidden", "application/json", ApiError),
  104. 404 => Operation.response("Not Found", "application/json", ApiError)
  105. }
  106. }
  107. end
  108. def download_operation do
  109. %Operation{
  110. tags: ["Emoji pack administration"],
  111. summary: "Download pack from another instance",
  112. operationId: "PleromaAPI.EmojiPackController.download",
  113. security: [%{"oAuth" => ["admin:write"]}],
  114. requestBody: request_body("Parameters", download_request(), required: true),
  115. responses: %{
  116. 200 => ok_response(),
  117. 500 => Operation.response("Error", "application/json", ApiError)
  118. }
  119. }
  120. end
  121. defp download_request do
  122. %Schema{
  123. type: :object,
  124. required: [:url, :name],
  125. properties: %{
  126. url: %Schema{
  127. type: :string,
  128. format: :uri,
  129. description: "URL of the instance to download from"
  130. },
  131. name: %Schema{type: :string, format: :uri, description: "Pack Name"},
  132. as: %Schema{type: :string, format: :uri, description: "Save as"}
  133. }
  134. }
  135. end
  136. def create_operation do
  137. %Operation{
  138. tags: ["Emoji pack administration"],
  139. summary: "Create an empty pack",
  140. operationId: "PleromaAPI.EmojiPackController.create",
  141. security: [%{"oAuth" => ["admin:write"]}],
  142. parameters: [name_param()],
  143. responses: %{
  144. 200 => ok_response(),
  145. 400 => Operation.response("Not Found", "application/json", ApiError),
  146. 409 => Operation.response("Conflict", "application/json", ApiError),
  147. 500 => Operation.response("Error", "application/json", ApiError)
  148. }
  149. }
  150. end
  151. def delete_operation do
  152. %Operation{
  153. tags: ["Emoji pack administration"],
  154. summary: "Delete a custom emoji pack",
  155. operationId: "PleromaAPI.EmojiPackController.delete",
  156. security: [%{"oAuth" => ["admin:write"]}],
  157. parameters: [name_param()],
  158. responses: %{
  159. 200 => ok_response(),
  160. 400 => Operation.response("Bad Request", "application/json", ApiError),
  161. 404 => Operation.response("Not Found", "application/json", ApiError),
  162. 500 => Operation.response("Error", "application/json", ApiError)
  163. }
  164. }
  165. end
  166. def update_operation do
  167. %Operation{
  168. tags: ["Emoji pack administration"],
  169. summary: "Updates (replaces) pack metadata",
  170. operationId: "PleromaAPI.EmojiPackController.update",
  171. security: [%{"oAuth" => ["admin:write"]}],
  172. requestBody: request_body("Parameters", update_request(), required: true),
  173. parameters: [name_param()],
  174. responses: %{
  175. 200 => Operation.response("Metadata", "application/json", metadata()),
  176. 400 => Operation.response("Bad Request", "application/json", ApiError),
  177. 500 => Operation.response("Error", "application/json", ApiError)
  178. }
  179. }
  180. end
  181. def import_from_filesystem_operation do
  182. %Operation{
  183. tags: ["Emoji pack administration"],
  184. summary: "Imports packs from filesystem",
  185. operationId: "PleromaAPI.EmojiPackController.import",
  186. security: [%{"oAuth" => ["admin:write"]}],
  187. responses: %{
  188. 200 =>
  189. Operation.response("Array of imported pack names", "application/json", %Schema{
  190. type: :array,
  191. items: %Schema{type: :string}
  192. })
  193. }
  194. }
  195. end
  196. defp name_param do
  197. Operation.parameter(:name, :query, :string, "Pack Name", example: "cofe", required: true)
  198. end
  199. defp url_param do
  200. Operation.parameter(
  201. :url,
  202. :query,
  203. %Schema{type: :string, format: :uri},
  204. "URL of the instance",
  205. required: true
  206. )
  207. end
  208. defp ok_response do
  209. Operation.response("Ok", "application/json", %Schema{type: :string, example: "ok"})
  210. end
  211. defp emoji_packs_response do
  212. Operation.response(
  213. "Emoji packs and the count",
  214. "application/json",
  215. %Schema{
  216. type: :object,
  217. properties: %{
  218. packs: %Schema{
  219. type: :object,
  220. description: "Object with pack names as keys and pack contents as values",
  221. additionalProperties: %Schema{
  222. emoji_pack()
  223. | extensions: %{"x-additionalPropertiesName": "Pack name"}
  224. }
  225. },
  226. count: %Schema{
  227. type: :integer,
  228. description: "Number of emoji packs"
  229. }
  230. },
  231. example: %{
  232. "packs" => %{
  233. "emojos" => emoji_pack().example
  234. },
  235. "count" => 1
  236. }
  237. }
  238. )
  239. end
  240. defp emoji_pack do
  241. %Schema{
  242. title: "EmojiPack",
  243. type: :object,
  244. properties: %{
  245. files: files_object(),
  246. pack: %Schema{
  247. type: :object,
  248. properties: %{
  249. license: %Schema{type: :string},
  250. homepage: %Schema{type: :string, format: :uri},
  251. description: %Schema{type: :string},
  252. "can-download": %Schema{type: :boolean},
  253. "share-files": %Schema{type: :boolean},
  254. "download-sha256": %Schema{type: :string}
  255. }
  256. }
  257. },
  258. example: %{
  259. "files" => %{"emacs" => "emacs.png", "guix" => "guix.png"},
  260. "pack" => %{
  261. "license" => "Test license",
  262. "homepage" => "https://pleroma.social",
  263. "description" => "Test description",
  264. "can-download" => true,
  265. "share-files" => true,
  266. "download-sha256" => "57482F30674FD3DE821FF48C81C00DA4D4AF1F300209253684ABA7075E5FC238"
  267. }
  268. }
  269. }
  270. end
  271. defp files_object do
  272. %Schema{
  273. type: :object,
  274. additionalProperties: %Schema{
  275. type: :string,
  276. description: "Filename",
  277. extensions: %{"x-additionalPropertiesName": "Emoji name"}
  278. },
  279. description: "Object with emoji names as keys and filenames as values"
  280. }
  281. end
  282. defp update_request do
  283. %Schema{
  284. type: :object,
  285. properties: %{
  286. metadata: %Schema{
  287. type: :object,
  288. description: "Metadata to replace the old one",
  289. properties: %{
  290. license: %Schema{type: :string},
  291. homepage: %Schema{type: :string, format: :uri},
  292. description: %Schema{type: :string},
  293. "fallback-src": %Schema{
  294. type: :string,
  295. format: :uri,
  296. description: "Fallback url to download pack from"
  297. },
  298. "fallback-src-sha256": %Schema{
  299. type: :string,
  300. description: "SHA256 encoded for fallback pack archive"
  301. },
  302. "share-files": %Schema{type: :boolean, description: "Is pack allowed for sharing?"}
  303. }
  304. }
  305. }
  306. }
  307. end
  308. defp metadata do
  309. %Schema{
  310. type: :object,
  311. properties: %{
  312. license: %Schema{type: :string},
  313. homepage: %Schema{type: :string, format: :uri},
  314. description: %Schema{type: :string},
  315. "fallback-src": %Schema{
  316. type: :string,
  317. format: :uri,
  318. description: "Fallback url to download pack from"
  319. },
  320. "fallback-src-sha256": %Schema{
  321. type: :string,
  322. description: "SHA256 encoded for fallback pack archive"
  323. },
  324. "share-files": %Schema{type: :boolean, description: "Is pack allowed for sharing?"}
  325. }
  326. }
  327. end
  328. end