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