logo

pleroma

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

list_operation.ex (5690B)


  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.ListOperation do
  5. alias OpenApiSpex.Operation
  6. alias OpenApiSpex.Schema
  7. alias Pleroma.Web.ApiSpec.Schemas.Account
  8. alias Pleroma.Web.ApiSpec.Schemas.ApiError
  9. alias Pleroma.Web.ApiSpec.Schemas.FlakeID
  10. alias Pleroma.Web.ApiSpec.Schemas.List
  11. import Pleroma.Web.ApiSpec.Helpers
  12. def open_api_operation(action) do
  13. operation = String.to_existing_atom("#{action}_operation")
  14. apply(__MODULE__, operation, [])
  15. end
  16. def index_operation do
  17. %Operation{
  18. tags: ["Lists"],
  19. summary: "Retrieve a list of lists",
  20. description: "Fetch all lists that the user owns",
  21. security: [%{"oAuth" => ["read:lists"]}],
  22. operationId: "ListController.index",
  23. responses: %{
  24. 200 => Operation.response("Array of List", "application/json", array_of_lists())
  25. }
  26. }
  27. end
  28. def create_operation do
  29. %Operation{
  30. tags: ["Lists"],
  31. summary: "Create a list",
  32. description: "Fetch the list with the given ID. Used for verifying the title of a list.",
  33. operationId: "ListController.create",
  34. requestBody: create_update_request(),
  35. security: [%{"oAuth" => ["write:lists"]}],
  36. responses: %{
  37. 200 => Operation.response("List", "application/json", List),
  38. 400 => Operation.response("Error", "application/json", ApiError),
  39. 404 => Operation.response("Error", "application/json", ApiError)
  40. }
  41. }
  42. end
  43. def show_operation do
  44. %Operation{
  45. tags: ["Lists"],
  46. summary: "Retrieve a list",
  47. description: "Fetch the list with the given ID. Used for verifying the title of a list.",
  48. operationId: "ListController.show",
  49. parameters: [id_param()],
  50. security: [%{"oAuth" => ["read:lists"]}],
  51. responses: %{
  52. 200 => Operation.response("List", "application/json", List),
  53. 404 => Operation.response("Error", "application/json", ApiError)
  54. }
  55. }
  56. end
  57. def update_operation do
  58. %Operation{
  59. tags: ["Lists"],
  60. summary: "Update a list",
  61. description: "Change the title of a list",
  62. operationId: "ListController.update",
  63. parameters: [id_param()],
  64. requestBody: create_update_request(),
  65. security: [%{"oAuth" => ["write:lists"]}],
  66. responses: %{
  67. 200 => Operation.response("List", "application/json", List),
  68. 422 => Operation.response("Error", "application/json", ApiError)
  69. }
  70. }
  71. end
  72. def delete_operation do
  73. %Operation{
  74. tags: ["Lists"],
  75. summary: "Delete a list",
  76. operationId: "ListController.delete",
  77. parameters: [id_param()],
  78. security: [%{"oAuth" => ["write:lists"]}],
  79. responses: %{
  80. 200 => Operation.response("Empty object", "application/json", %Schema{type: :object})
  81. }
  82. }
  83. end
  84. def list_accounts_operation do
  85. %Operation{
  86. tags: ["Lists"],
  87. summary: "Retrieve accounts in list",
  88. operationId: "ListController.list_accounts",
  89. parameters: [id_param()],
  90. security: [%{"oAuth" => ["read:lists"]}],
  91. responses: %{
  92. 200 =>
  93. Operation.response("Array of Account", "application/json", %Schema{
  94. type: :array,
  95. items: Account
  96. })
  97. }
  98. }
  99. end
  100. def add_to_list_operation do
  101. %Operation{
  102. tags: ["Lists"],
  103. summary: "Add accounts to list",
  104. description: "Add accounts to the given list.",
  105. operationId: "ListController.add_to_list",
  106. parameters: [id_param()],
  107. requestBody: add_remove_accounts_request(true),
  108. security: [%{"oAuth" => ["write:lists"]}],
  109. responses: %{
  110. 200 => Operation.response("Empty object", "application/json", %Schema{type: :object})
  111. }
  112. }
  113. end
  114. def remove_from_list_operation do
  115. %Operation{
  116. tags: ["Lists"],
  117. summary: "Remove accounts from list",
  118. operationId: "ListController.remove_from_list",
  119. parameters: [
  120. id_param(),
  121. Operation.parameter(
  122. :account_ids,
  123. :query,
  124. %Schema{type: :array, items: %Schema{type: :string}},
  125. "Array of account IDs"
  126. )
  127. ],
  128. requestBody: add_remove_accounts_request(false),
  129. security: [%{"oAuth" => ["write:lists"]}],
  130. responses: %{
  131. 200 => Operation.response("Empty object", "application/json", %Schema{type: :object})
  132. }
  133. }
  134. end
  135. defp array_of_lists do
  136. %Schema{
  137. title: "ArrayOfLists",
  138. description: "Response schema for lists",
  139. type: :array,
  140. items: List,
  141. example: [
  142. %{"id" => "123", "title" => "my list"},
  143. %{"id" => "1337", "title" => "another list"}
  144. ]
  145. }
  146. end
  147. defp id_param do
  148. Operation.parameter(:id, :path, :string, "List ID",
  149. example: "123",
  150. required: true
  151. )
  152. end
  153. defp create_update_request do
  154. request_body(
  155. "Parameters",
  156. %Schema{
  157. description: "POST body for creating or updating a List",
  158. type: :object,
  159. properties: %{
  160. title: %Schema{type: :string, description: "List title"}
  161. },
  162. required: [:title]
  163. },
  164. required: true
  165. )
  166. end
  167. defp add_remove_accounts_request(required) when is_boolean(required) do
  168. request_body(
  169. "Parameters",
  170. %Schema{
  171. description: "POST body for adding/removing accounts to/from a List",
  172. type: :object,
  173. properties: %{
  174. account_ids: %Schema{type: :array, description: "Array of account IDs", items: FlakeID}
  175. }
  176. },
  177. required: required
  178. )
  179. end
  180. end