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