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