support for with_relationships parameter
[akkoma] / lib / pleroma / web / api_spec / operations / chat_operation.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ApiSpec.ChatOperation do
6 alias OpenApiSpex.Operation
7 alias OpenApiSpex.Schema
8 alias Pleroma.Web.ApiSpec.Schemas.ApiError
9 alias Pleroma.Web.ApiSpec.Schemas.BooleanLike
10 alias Pleroma.Web.ApiSpec.Schemas.Chat
11 alias Pleroma.Web.ApiSpec.Schemas.ChatMessage
12
13 import Pleroma.Web.ApiSpec.Helpers
14
15 @spec open_api_operation(atom) :: Operation.t()
16 def open_api_operation(action) do
17 operation = String.to_existing_atom("#{action}_operation")
18 apply(__MODULE__, operation, [])
19 end
20
21 def mark_as_read_operation do
22 %Operation{
23 tags: ["chat"],
24 summary: "Mark all messages in the chat as read",
25 operationId: "ChatController.mark_as_read",
26 parameters: [Operation.parameter(:id, :path, :string, "The ID of the Chat")],
27 requestBody: request_body("Parameters", mark_as_read()),
28 responses: %{
29 200 =>
30 Operation.response(
31 "The updated chat",
32 "application/json",
33 Chat
34 )
35 },
36 security: [
37 %{
38 "oAuth" => ["write:chats"]
39 }
40 ]
41 }
42 end
43
44 def mark_message_as_read_operation do
45 %Operation{
46 tags: ["chat"],
47 summary: "Mark one message in the chat as read",
48 operationId: "ChatController.mark_message_as_read",
49 parameters: [
50 Operation.parameter(:id, :path, :string, "The ID of the Chat"),
51 Operation.parameter(:message_id, :path, :string, "The ID of the message")
52 ],
53 responses: %{
54 200 =>
55 Operation.response(
56 "The read ChatMessage",
57 "application/json",
58 ChatMessage
59 )
60 },
61 security: [
62 %{
63 "oAuth" => ["write:chats"]
64 }
65 ]
66 }
67 end
68
69 def show_operation do
70 %Operation{
71 tags: ["chat"],
72 summary: "Create a chat",
73 operationId: "ChatController.show",
74 parameters: [
75 Operation.parameter(
76 :id,
77 :path,
78 :string,
79 "The id of the chat",
80 required: true,
81 example: "1234"
82 )
83 ],
84 responses: %{
85 200 =>
86 Operation.response(
87 "The existing chat",
88 "application/json",
89 Chat
90 )
91 },
92 security: [
93 %{
94 "oAuth" => ["read"]
95 }
96 ]
97 }
98 end
99
100 def create_operation do
101 %Operation{
102 tags: ["chat"],
103 summary: "Create a chat",
104 operationId: "ChatController.create",
105 parameters: [
106 Operation.parameter(
107 :id,
108 :path,
109 :string,
110 "The account id of the recipient of this chat",
111 required: true,
112 example: "someflakeid"
113 )
114 ],
115 responses: %{
116 200 =>
117 Operation.response(
118 "The created or existing chat",
119 "application/json",
120 Chat
121 )
122 },
123 security: [
124 %{
125 "oAuth" => ["write:chats"]
126 }
127 ]
128 }
129 end
130
131 def index_operation do
132 %Operation{
133 tags: ["chat"],
134 summary: "Get a list of chats that you participated in",
135 operationId: "ChatController.index",
136 parameters: [
137 Operation.parameter(:with_muted, :query, BooleanLike, "Include chats from muted users")
138 | pagination_params()
139 ],
140 responses: %{
141 200 => Operation.response("The chats of the user", "application/json", chats_response())
142 },
143 security: [
144 %{
145 "oAuth" => ["read:chats"]
146 }
147 ]
148 }
149 end
150
151 def messages_operation do
152 %Operation{
153 tags: ["chat"],
154 summary: "Get the most recent messages of the chat",
155 operationId: "ChatController.messages",
156 parameters:
157 [Operation.parameter(:id, :path, :string, "The ID of the Chat")] ++
158 pagination_params(),
159 responses: %{
160 200 =>
161 Operation.response(
162 "The messages in the chat",
163 "application/json",
164 chat_messages_response()
165 ),
166 404 => Operation.response("Not Found", "application/json", ApiError)
167 },
168 security: [
169 %{
170 "oAuth" => ["read:chats"]
171 }
172 ]
173 }
174 end
175
176 def post_chat_message_operation do
177 %Operation{
178 tags: ["chat"],
179 summary: "Post a message to the chat",
180 operationId: "ChatController.post_chat_message",
181 parameters: [
182 Operation.parameter(:id, :path, :string, "The ID of the Chat")
183 ],
184 requestBody: request_body("Parameters", chat_message_create()),
185 responses: %{
186 200 =>
187 Operation.response(
188 "The newly created ChatMessage",
189 "application/json",
190 ChatMessage
191 ),
192 400 => Operation.response("Bad Request", "application/json", ApiError),
193 422 => Operation.response("MRF Rejection", "application/json", ApiError)
194 },
195 security: [
196 %{
197 "oAuth" => ["write:chats"]
198 }
199 ]
200 }
201 end
202
203 def delete_message_operation do
204 %Operation{
205 tags: ["chat"],
206 summary: "delete_message",
207 operationId: "ChatController.delete_message",
208 parameters: [
209 Operation.parameter(:id, :path, :string, "The ID of the Chat"),
210 Operation.parameter(:message_id, :path, :string, "The ID of the message")
211 ],
212 responses: %{
213 200 =>
214 Operation.response(
215 "The deleted ChatMessage",
216 "application/json",
217 ChatMessage
218 )
219 },
220 security: [
221 %{
222 "oAuth" => ["write:chats"]
223 }
224 ]
225 }
226 end
227
228 def chats_response do
229 %Schema{
230 title: "ChatsResponse",
231 description: "Response schema for multiple Chats",
232 type: :array,
233 items: Chat,
234 example: [
235 %{
236 "account" => %{
237 "pleroma" => %{
238 "is_admin" => false,
239 "is_confirmed" => true,
240 "hide_followers_count" => false,
241 "is_moderator" => false,
242 "hide_favorites" => true,
243 "ap_id" => "https://dontbulling.me/users/lain",
244 "hide_follows_count" => false,
245 "hide_follows" => false,
246 "background_image" => nil,
247 "skip_thread_containment" => false,
248 "hide_followers" => false,
249 "relationship" => %{},
250 "tags" => []
251 },
252 "avatar" =>
253 "https://dontbulling.me/media/065a4dd3c6740dab13ff9c71ec7d240bb9f8be9205c9e7467fb2202117da1e32.jpg",
254 "following_count" => 0,
255 "header_static" => "https://originalpatchou.li/images/banner.png",
256 "source" => %{
257 "sensitive" => false,
258 "note" => "lain",
259 "pleroma" => %{
260 "discoverable" => false,
261 "actor_type" => "Person"
262 },
263 "fields" => []
264 },
265 "statuses_count" => 1,
266 "locked" => false,
267 "created_at" => "2020-04-16T13:40:15.000Z",
268 "display_name" => "lain",
269 "fields" => [],
270 "acct" => "lain@dontbulling.me",
271 "id" => "9u6Qw6TAZANpqokMkK",
272 "emojis" => [],
273 "avatar_static" =>
274 "https://dontbulling.me/media/065a4dd3c6740dab13ff9c71ec7d240bb9f8be9205c9e7467fb2202117da1e32.jpg",
275 "username" => "lain",
276 "followers_count" => 0,
277 "header" => "https://originalpatchou.li/images/banner.png",
278 "bot" => false,
279 "note" => "lain",
280 "url" => "https://dontbulling.me/users/lain"
281 },
282 "id" => "1",
283 "unread" => 2
284 }
285 ]
286 }
287 end
288
289 def chat_messages_response do
290 %Schema{
291 title: "ChatMessagesResponse",
292 description: "Response schema for multiple ChatMessages",
293 type: :array,
294 items: ChatMessage,
295 example: [
296 %{
297 "emojis" => [
298 %{
299 "static_url" => "https://dontbulling.me/emoji/Firefox.gif",
300 "visible_in_picker" => false,
301 "shortcode" => "firefox",
302 "url" => "https://dontbulling.me/emoji/Firefox.gif"
303 }
304 ],
305 "created_at" => "2020-04-21T15:11:46.000Z",
306 "content" => "Check this out :firefox:",
307 "id" => "13",
308 "chat_id" => "1",
309 "account_id" => "someflakeid",
310 "unread" => false
311 },
312 %{
313 "account_id" => "someflakeid",
314 "content" => "Whats' up?",
315 "id" => "12",
316 "chat_id" => "1",
317 "emojis" => [],
318 "created_at" => "2020-04-21T15:06:45.000Z",
319 "unread" => false
320 }
321 ]
322 }
323 end
324
325 def chat_message_create do
326 %Schema{
327 title: "ChatMessageCreateRequest",
328 description: "POST body for creating an chat message",
329 type: :object,
330 properties: %{
331 content: %Schema{
332 type: :string,
333 description: "The content of your message. Optional if media_id is present"
334 },
335 media_id: %Schema{type: :string, description: "The id of an upload"}
336 },
337 example: %{
338 "content" => "Hey wanna buy feet pics?",
339 "media_id" => "134234"
340 }
341 }
342 end
343
344 def mark_as_read do
345 %Schema{
346 title: "MarkAsReadRequest",
347 description: "POST body for marking a number of chat messages as read",
348 type: :object,
349 required: [:last_read_id],
350 properties: %{
351 last_read_id: %Schema{
352 type: :string,
353 description: "The content of your message."
354 }
355 },
356 example: %{
357 "last_read_id" => "abcdef12456"
358 }
359 }
360 end
361 end