logo

pleroma

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

subscription_operation.ex (8519B)


  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.SubscriptionOperation 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. alias Pleroma.Web.ApiSpec.Schemas.PushSubscription
  11. def open_api_operation(action) do
  12. operation = String.to_existing_atom("#{action}_operation")
  13. apply(__MODULE__, operation, [])
  14. end
  15. def create_operation do
  16. %Operation{
  17. tags: ["Push subscriptions"],
  18. summary: "Subscribe to push notifications",
  19. description:
  20. "Add a Web Push API subscription to receive notifications. Each access token can have one push subscription. If you create a new subscription, the old subscription is deleted.",
  21. operationId: "SubscriptionController.create",
  22. security: [%{"oAuth" => ["push"]}],
  23. requestBody: Helpers.request_body("Parameters", create_request(), required: true),
  24. responses: %{
  25. 200 => Operation.response("Push subscription", "application/json", PushSubscription),
  26. 400 => Operation.response("Error", "application/json", ApiError),
  27. 403 => Operation.response("Error", "application/json", ApiError)
  28. }
  29. }
  30. end
  31. def show_operation do
  32. %Operation{
  33. tags: ["Push subscriptions"],
  34. summary: "Get current subscription",
  35. description: "View the PushSubscription currently associated with this access token.",
  36. operationId: "SubscriptionController.show",
  37. security: [%{"oAuth" => ["push"]}],
  38. responses: %{
  39. 200 => Operation.response("Push subscription", "application/json", PushSubscription),
  40. 403 => Operation.response("Error", "application/json", ApiError),
  41. 404 => Operation.response("Error", "application/json", ApiError)
  42. }
  43. }
  44. end
  45. def update_operation do
  46. %Operation{
  47. tags: ["Push subscriptions"],
  48. summary: "Change types of notifications",
  49. description:
  50. "Updates the current push subscription. Only the data part can be updated. To change fundamentals, a new subscription must be created instead.",
  51. operationId: "SubscriptionController.update",
  52. security: [%{"oAuth" => ["push"]}],
  53. requestBody: Helpers.request_body("Parameters", update_request(), required: true),
  54. responses: %{
  55. 200 => Operation.response("Push subscription", "application/json", PushSubscription),
  56. 403 => Operation.response("Error", "application/json", ApiError)
  57. }
  58. }
  59. end
  60. def delete_operation do
  61. %Operation{
  62. tags: ["Push subscriptions"],
  63. summary: "Remove current subscription",
  64. description: "Removes the current Web Push API subscription.",
  65. operationId: "SubscriptionController.delete",
  66. security: [%{"oAuth" => ["push"]}],
  67. responses: %{
  68. 200 => Operation.response("Empty object", "application/json", %Schema{type: :object}),
  69. 403 => Operation.response("Error", "application/json", ApiError),
  70. 404 => Operation.response("Error", "application/json", ApiError)
  71. }
  72. }
  73. end
  74. defp create_request do
  75. %Schema{
  76. title: "SubscriptionCreateRequest",
  77. description: "POST body for creating a push subscription",
  78. type: :object,
  79. properties: %{
  80. subscription: %Schema{
  81. type: :object,
  82. properties: %{
  83. endpoint: %Schema{
  84. type: :string,
  85. description: "Endpoint URL that is called when a notification event occurs."
  86. },
  87. keys: %Schema{
  88. type: :object,
  89. properties: %{
  90. p256dh: %Schema{
  91. type: :string,
  92. description:
  93. "User agent public key. Base64 encoded string of public key of ECDH key using `prime256v1` curve."
  94. },
  95. auth: %Schema{
  96. type: :string,
  97. description: "Auth secret. Base64 encoded string of 16 bytes of random data."
  98. }
  99. },
  100. required: [:p256dh, :auth]
  101. }
  102. },
  103. required: [:endpoint, :keys]
  104. },
  105. data: %Schema{
  106. nullable: true,
  107. type: :object,
  108. properties: %{
  109. alerts: %Schema{
  110. nullable: true,
  111. type: :object,
  112. properties: %{
  113. follow: %Schema{
  114. allOf: [BooleanLike],
  115. nullable: true,
  116. description: "Receive follow notifications?"
  117. },
  118. favourite: %Schema{
  119. allOf: [BooleanLike],
  120. nullable: true,
  121. description: "Receive favourite notifications?"
  122. },
  123. reblog: %Schema{
  124. allOf: [BooleanLike],
  125. nullable: true,
  126. description: "Receive reblog notifications?"
  127. },
  128. mention: %Schema{
  129. allOf: [BooleanLike],
  130. nullable: true,
  131. description: "Receive mention notifications?"
  132. },
  133. poll: %Schema{
  134. allOf: [BooleanLike],
  135. nullable: true,
  136. description: "Receive poll notifications?"
  137. },
  138. "pleroma:chat_mention": %Schema{
  139. allOf: [BooleanLike],
  140. nullable: true,
  141. description: "Receive chat notifications?"
  142. },
  143. "pleroma:emoji_reaction": %Schema{
  144. allOf: [BooleanLike],
  145. nullable: true,
  146. description: "Receive emoji reaction notifications?"
  147. }
  148. }
  149. }
  150. }
  151. }
  152. },
  153. required: [:subscription],
  154. example: %{
  155. "subscription" => %{
  156. "endpoint" => "https://example.com/example/1234",
  157. "keys" => %{
  158. "auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
  159. "p256dh" =>
  160. "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
  161. }
  162. },
  163. "data" => %{
  164. "alerts" => %{
  165. "follow" => true,
  166. "mention" => true,
  167. "poll" => false
  168. }
  169. }
  170. }
  171. }
  172. end
  173. defp update_request do
  174. %Schema{
  175. title: "SubscriptionUpdateRequest",
  176. type: :object,
  177. properties: %{
  178. data: %Schema{
  179. nullable: true,
  180. type: :object,
  181. properties: %{
  182. alerts: %Schema{
  183. nullable: true,
  184. type: :object,
  185. properties: %{
  186. follow: %Schema{
  187. allOf: [BooleanLike],
  188. nullable: true,
  189. description: "Receive follow notifications?"
  190. },
  191. favourite: %Schema{
  192. allOf: [BooleanLike],
  193. nullable: true,
  194. description: "Receive favourite notifications?"
  195. },
  196. reblog: %Schema{
  197. allOf: [BooleanLike],
  198. nullable: true,
  199. description: "Receive reblog notifications?"
  200. },
  201. mention: %Schema{
  202. allOf: [BooleanLike],
  203. nullable: true,
  204. description: "Receive mention notifications?"
  205. },
  206. poll: %Schema{
  207. allOf: [BooleanLike],
  208. nullable: true,
  209. description: "Receive poll notifications?"
  210. },
  211. "pleroma:chat_mention": %Schema{
  212. allOf: [BooleanLike],
  213. nullable: true,
  214. description: "Receive chat notifications?"
  215. },
  216. "pleroma:emoji_reaction": %Schema{
  217. allOf: [BooleanLike],
  218. nullable: true,
  219. description: "Receive emoji reaction notifications?"
  220. }
  221. }
  222. }
  223. }
  224. }
  225. },
  226. example: %{
  227. "data" => %{
  228. "alerts" => %{
  229. "follow" => true,
  230. "favourite" => true,
  231. "reblog" => true,
  232. "mention" => true,
  233. "poll" => true
  234. }
  235. }
  236. }
  237. }
  238. end
  239. end