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