logo

pleroma

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

filter_operation.ex (6965B)


  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.FilterOperation do
  5. alias OpenApiSpex.Operation
  6. alias OpenApiSpex.Schema
  7. alias Pleroma.Web.ApiSpec.Helpers
  8. alias Pleroma.Web.ApiSpec.Schemas.ApiError
  9. alias Pleroma.Web.ApiSpec.Schemas.BooleanLike
  10. def open_api_operation(action) do
  11. operation = String.to_existing_atom("#{action}_operation")
  12. apply(__MODULE__, operation, [])
  13. end
  14. def index_operation do
  15. %Operation{
  16. tags: ["Filters"],
  17. summary: "All filters",
  18. operationId: "FilterController.index",
  19. security: [%{"oAuth" => ["read:filters"]}],
  20. responses: %{
  21. 200 => Operation.response("Filters", "application/json", array_of_filters()),
  22. 403 => Operation.response("Error", "application/json", ApiError)
  23. }
  24. }
  25. end
  26. def create_operation do
  27. %Operation{
  28. tags: ["Filters"],
  29. summary: "Create a filter",
  30. operationId: "FilterController.create",
  31. requestBody: Helpers.request_body("Parameters", create_request(), required: true),
  32. security: [%{"oAuth" => ["write:filters"]}],
  33. responses: %{
  34. 200 => Operation.response("Filter", "application/json", filter()),
  35. 403 => Operation.response("Error", "application/json", ApiError)
  36. }
  37. }
  38. end
  39. def show_operation do
  40. %Operation{
  41. tags: ["Filters"],
  42. summary: "Filter",
  43. parameters: [id_param()],
  44. operationId: "FilterController.show",
  45. security: [%{"oAuth" => ["read:filters"]}],
  46. responses: %{
  47. 200 => Operation.response("Filter", "application/json", filter()),
  48. 403 => Operation.response("Error", "application/json", ApiError),
  49. 404 => Operation.response("Error", "application/json", ApiError)
  50. }
  51. }
  52. end
  53. def update_operation do
  54. %Operation{
  55. tags: ["Filters"],
  56. summary: "Update a filter",
  57. parameters: [id_param()],
  58. operationId: "FilterController.update",
  59. requestBody: Helpers.request_body("Parameters", update_request(), required: true),
  60. security: [%{"oAuth" => ["write:filters"]}],
  61. responses: %{
  62. 200 => Operation.response("Filter", "application/json", filter()),
  63. 403 => Operation.response("Error", "application/json", ApiError)
  64. }
  65. }
  66. end
  67. def delete_operation do
  68. %Operation{
  69. tags: ["Filters"],
  70. summary: "Remove a filter",
  71. parameters: [id_param()],
  72. operationId: "FilterController.delete",
  73. security: [%{"oAuth" => ["write:filters"]}],
  74. responses: %{
  75. 200 =>
  76. Operation.response("Filter", "application/json", %Schema{
  77. type: :object,
  78. description: "Empty object"
  79. }),
  80. 403 => Operation.response("Error", "application/json", ApiError)
  81. }
  82. }
  83. end
  84. defp id_param do
  85. Operation.parameter(:id, :path, :string, "Filter ID", example: "123", required: true)
  86. end
  87. defp filter do
  88. %Schema{
  89. title: "Filter",
  90. type: :object,
  91. properties: %{
  92. id: %Schema{type: :string},
  93. phrase: %Schema{type: :string, description: "The text to be filtered"},
  94. context: %Schema{
  95. type: :array,
  96. items: %Schema{type: :string, enum: ["home", "notifications", "public", "thread"]},
  97. description: "The contexts in which the filter should be applied."
  98. },
  99. expires_at: %Schema{
  100. type: :string,
  101. format: :"date-time",
  102. description:
  103. "When the filter should no longer be applied. String (ISO 8601 Datetime), or null if the filter does not expire.",
  104. nullable: true
  105. },
  106. irreversible: %Schema{
  107. type: :boolean,
  108. description:
  109. "Should matching entities in home and notifications be dropped by the server?"
  110. },
  111. whole_word: %Schema{
  112. type: :boolean,
  113. description: "Should the filter consider word boundaries?"
  114. }
  115. },
  116. example: %{
  117. "id" => "5580",
  118. "phrase" => "@twitter.com",
  119. "context" => [
  120. "home",
  121. "notifications",
  122. "public",
  123. "thread"
  124. ],
  125. "whole_word" => false,
  126. "expires_at" => nil,
  127. "irreversible" => true
  128. }
  129. }
  130. end
  131. defp array_of_filters do
  132. %Schema{
  133. title: "ArrayOfFilters",
  134. description: "Array of Filters",
  135. type: :array,
  136. items: filter(),
  137. example: [
  138. %{
  139. "id" => "5580",
  140. "phrase" => "@twitter.com",
  141. "context" => [
  142. "home",
  143. "notifications",
  144. "public",
  145. "thread"
  146. ],
  147. "whole_word" => false,
  148. "expires_at" => nil,
  149. "irreversible" => true
  150. },
  151. %{
  152. "id" => "6191",
  153. "phrase" => ":eurovision2019:",
  154. "context" => [
  155. "home"
  156. ],
  157. "whole_word" => true,
  158. "expires_at" => "2019-05-21T13:47:31.333Z",
  159. "irreversible" => false
  160. }
  161. ]
  162. }
  163. end
  164. defp create_request do
  165. %Schema{
  166. title: "FilterCreateRequest",
  167. allOf: [
  168. update_request(),
  169. %Schema{
  170. type: :object,
  171. properties: %{
  172. irreversible: %Schema{
  173. allOf: [BooleanLike],
  174. description:
  175. "Should the server irreversibly drop matching entities from home and notifications?",
  176. default: false
  177. }
  178. }
  179. }
  180. ],
  181. example: %{
  182. "phrase" => "knights",
  183. "context" => ["home"]
  184. }
  185. }
  186. end
  187. defp update_request do
  188. %Schema{
  189. title: "FilterUpdateRequest",
  190. type: :object,
  191. properties: %{
  192. phrase: %Schema{type: :string, description: "The text to be filtered"},
  193. context: %Schema{
  194. type: :array,
  195. items: %Schema{type: :string, enum: ["home", "notifications", "public", "thread"]},
  196. description:
  197. "Array of enumerable strings `home`, `notifications`, `public`, `thread`. At least one context must be specified."
  198. },
  199. irreversible: %Schema{
  200. allOf: [BooleanLike],
  201. nullable: true,
  202. description:
  203. "Should the server irreversibly drop matching entities from home and notifications?"
  204. },
  205. whole_word: %Schema{
  206. allOf: [BooleanLike],
  207. nullable: true,
  208. description: "Consider word boundaries?",
  209. default: true
  210. },
  211. expires_in: %Schema{
  212. nullable: true,
  213. type: :integer,
  214. description:
  215. "Number of seconds from now the filter should expire. Otherwise, null for a filter that doesn't expire."
  216. }
  217. },
  218. required: [:phrase, :context],
  219. example: %{
  220. "phrase" => "knights",
  221. "context" => ["home"]
  222. }
  223. }
  224. end
  225. end