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