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