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