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 create_operation do
42 %Operation{
43 tags: ["chat"],
44 summary: "Create a chat",
45 operationId: "ChatController.create",
46 parameters: [
47 Operation.parameter(
48 :id,
49 :path,
50 :string,
51 "The account id of the recipient of this chat",
52 required: true,
53 example: "someflakeid"
54 )
55 ],
56 responses: %{
57 200 =>
58 Operation.response(
59 "The created or existing chat",
60 "application/json",
61 Chat
62 )
63 },
64 security: [
65 %{
66 "oAuth" => ["write"]
67 }
68 ]
69 }
70 end
71
72 def index_operation do
73 %Operation{
74 tags: ["chat"],
75 summary: "Get a list of chats that you participated in",
76 operationId: "ChatController.index",
77 parameters: pagination_params(),
78 responses: %{
79 200 => Operation.response("The chats of the user", "application/json", chats_response())
80 },
81 security: [
82 %{
83 "oAuth" => ["read"]
84 }
85 ]
86 }
87 end
88
89 def messages_operation do
90 %Operation{
91 tags: ["chat"],
92 summary: "Get the most recent messages of the chat",
93 operationId: "ChatController.messages",
94 parameters:
95 [Operation.parameter(:id, :path, :string, "The ID of the Chat")] ++
96 pagination_params(),
97 responses: %{
98 200 =>
99 Operation.response(
100 "The messages in the chat",
101 "application/json",
102 chat_messages_response()
103 )
104 },
105 security: [
106 %{
107 "oAuth" => ["read"]
108 }
109 ]
110 }
111 end
112
113 def post_chat_message_operation do
114 %Operation{
115 tags: ["chat"],
116 summary: "Post a message to the chat",
117 operationId: "ChatController.post_chat_message",
118 parameters: [
119 Operation.parameter(:id, :path, :string, "The ID of the Chat")
120 ],
121 requestBody: request_body("Parameters", chat_message_create(), required: true),
122 responses: %{
123 200 =>
124 Operation.response(
125 "The newly created ChatMessage",
126 "application/json",
127 ChatMessage
128 )
129 },
130 security: [
131 %{
132 "oAuth" => ["write"]
133 }
134 ]
135 }
136 end
137
138 def chats_response do
139 %Schema{
140 title: "ChatsResponse",
141 description: "Response schema for multiple Chats",
142 type: :array,
143 items: Chat,
144 example: [
145 %{
146 "account" => %{
147 "pleroma" => %{
148 "is_admin" => false,
149 "confirmation_pending" => false,
150 "hide_followers_count" => false,
151 "is_moderator" => false,
152 "hide_favorites" => true,
153 "ap_id" => "https://dontbulling.me/users/lain",
154 "hide_follows_count" => false,
155 "hide_follows" => false,
156 "background_image" => nil,
157 "skip_thread_containment" => false,
158 "hide_followers" => false,
159 "relationship" => %{},
160 "tags" => []
161 },
162 "avatar" =>
163 "https://dontbulling.me/media/065a4dd3c6740dab13ff9c71ec7d240bb9f8be9205c9e7467fb2202117da1e32.jpg",
164 "following_count" => 0,
165 "header_static" => "https://originalpatchou.li/images/banner.png",
166 "source" => %{
167 "sensitive" => false,
168 "note" => "lain",
169 "pleroma" => %{
170 "discoverable" => false,
171 "actor_type" => "Person"
172 },
173 "fields" => []
174 },
175 "statuses_count" => 1,
176 "locked" => false,
177 "created_at" => "2020-04-16T13:40:15.000Z",
178 "display_name" => "lain",
179 "fields" => [],
180 "acct" => "lain@dontbulling.me",
181 "id" => "9u6Qw6TAZANpqokMkK",
182 "emojis" => [],
183 "avatar_static" =>
184 "https://dontbulling.me/media/065a4dd3c6740dab13ff9c71ec7d240bb9f8be9205c9e7467fb2202117da1e32.jpg",
185 "username" => "lain",
186 "followers_count" => 0,
187 "header" => "https://originalpatchou.li/images/banner.png",
188 "bot" => false,
189 "note" => "lain",
190 "url" => "https://dontbulling.me/users/lain"
191 },
192 "id" => "1",
193 "unread" => 2
194 }
195 ]
196 }
197 end
198
199 def chat_messages_response do
200 %Schema{
201 title: "ChatMessagesResponse",
202 description: "Response schema for multiple ChatMessages",
203 type: :array,
204 items: ChatMessage,
205 example: [
206 %{
207 "emojis" => [
208 %{
209 "static_url" => "https://dontbulling.me/emoji/Firefox.gif",
210 "visible_in_picker" => false,
211 "shortcode" => "firefox",
212 "url" => "https://dontbulling.me/emoji/Firefox.gif"
213 }
214 ],
215 "created_at" => "2020-04-21T15:11:46.000Z",
216 "content" => "Check this out :firefox:",
217 "id" => "13",
218 "chat_id" => "1",
219 "actor_id" => "someflakeid"
220 },
221 %{
222 "actor_id" => "someflakeid",
223 "content" => "Whats' up?",
224 "id" => "12",
225 "chat_id" => "1",
226 "emojis" => [],
227 "created_at" => "2020-04-21T15:06:45.000Z"
228 }
229 ]
230 }
231 end
232
233 def chat_message_create do
234 %Schema{
235 title: "ChatMessageCreateRequest",
236 description: "POST body for creating an chat message",
237 type: :object,
238 properties: %{
239 content: %Schema{type: :string, description: "The content of your message"}
240 },
241 required: [:content],
242 example: %{
243 "content" => "Hey wanna buy feet pics?"
244 }
245 }
246 end
247 end