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 chats_response do
170 %Schema{
171 title: "ChatsResponse",
172 description: "Response schema for multiple Chats",
173 type: :array,
174 items: Chat,
175 example: [
176 %{
177 "account" => %{
178 "pleroma" => %{
179 "is_admin" => false,
180 "confirmation_pending" => false,
181 "hide_followers_count" => false,
182 "is_moderator" => false,
183 "hide_favorites" => true,
184 "ap_id" => "https://dontbulling.me/users/lain",
185 "hide_follows_count" => false,
186 "hide_follows" => false,
187 "background_image" => nil,
188 "skip_thread_containment" => false,
189 "hide_followers" => false,
190 "relationship" => %{},
191 "tags" => []
192 },
193 "avatar" =>
194 "https://dontbulling.me/media/065a4dd3c6740dab13ff9c71ec7d240bb9f8be9205c9e7467fb2202117da1e32.jpg",
195 "following_count" => 0,
196 "header_static" => "https://originalpatchou.li/images/banner.png",
197 "source" => %{
198 "sensitive" => false,
199 "note" => "lain",
200 "pleroma" => %{
201 "discoverable" => false,
202 "actor_type" => "Person"
203 },
204 "fields" => []
205 },
206 "statuses_count" => 1,
207 "locked" => false,
208 "created_at" => "2020-04-16T13:40:15.000Z",
209 "display_name" => "lain",
210 "fields" => [],
211 "acct" => "lain@dontbulling.me",
212 "id" => "9u6Qw6TAZANpqokMkK",
213 "emojis" => [],
214 "avatar_static" =>
215 "https://dontbulling.me/media/065a4dd3c6740dab13ff9c71ec7d240bb9f8be9205c9e7467fb2202117da1e32.jpg",
216 "username" => "lain",
217 "followers_count" => 0,
218 "header" => "https://originalpatchou.li/images/banner.png",
219 "bot" => false,
220 "note" => "lain",
221 "url" => "https://dontbulling.me/users/lain"
222 },
223 "id" => "1",
224 "unread" => 2
225 }
226 ]
227 }
228 end
229
230 def chat_messages_response do
231 %Schema{
232 title: "ChatMessagesResponse",
233 description: "Response schema for multiple ChatMessages",
234 type: :array,
235 items: ChatMessage,
236 example: [
237 %{
238 "emojis" => [
239 %{
240 "static_url" => "https://dontbulling.me/emoji/Firefox.gif",
241 "visible_in_picker" => false,
242 "shortcode" => "firefox",
243 "url" => "https://dontbulling.me/emoji/Firefox.gif"
244 }
245 ],
246 "created_at" => "2020-04-21T15:11:46.000Z",
247 "content" => "Check this out :firefox:",
248 "id" => "13",
249 "chat_id" => "1",
250 "actor_id" => "someflakeid"
251 },
252 %{
253 "actor_id" => "someflakeid",
254 "content" => "Whats' up?",
255 "id" => "12",
256 "chat_id" => "1",
257 "emojis" => [],
258 "created_at" => "2020-04-21T15:06:45.000Z"
259 }
260 ]
261 }
262 end
263
264 def chat_message_create do
265 %Schema{
266 title: "ChatMessageCreateRequest",
267 description: "POST body for creating an chat message",
268 type: :object,
269 properties: %{
270 content: %Schema{type: :string, description: "The content of your message"},
271 media_id: %Schema{type: :string, description: "The id of an upload"}
272 },
273 required: [:content],
274 example: %{
275 "content" => "Hey wanna buy feet pics?",
276 "media_id" => "134234"
277 }
278 }
279 end
280 end