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