Chats: Add API specs.
[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 Pleroma.Web.ApiSpec.Helpers
8 alias Pleroma.Web.ApiSpec.Schemas.ChatMessageCreateRequest
9 alias Pleroma.Web.ApiSpec.Schemas.ChatMessageResponse
10 alias Pleroma.Web.ApiSpec.Schemas.ChatMessagesResponse
11 alias Pleroma.Web.ApiSpec.Schemas.ChatResponse
12 alias Pleroma.Web.ApiSpec.Schemas.ChatsResponse
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 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("The created or existing chat", "application/json", ChatResponse)
37 },
38 security: [
39 %{
40 "oAuth" => ["write"]
41 }
42 ]
43 }
44 end
45
46 def index_operation do
47 %Operation{
48 tags: ["chat"],
49 summary: "Get a list of chats that you participated in",
50 parameters: [
51 Operation.parameter(:limit, :query, :integer, "How many results to return", example: 20),
52 Operation.parameter(:min_id, :query, :string, "Return only chats after this id"),
53 Operation.parameter(:max_id, :query, :string, "Return only chats before this id")
54 ],
55 responses: %{
56 200 => Operation.response("The chats of the user", "application/json", ChatsResponse)
57 },
58 security: [
59 %{
60 "oAuth" => ["read"]
61 }
62 ]
63 }
64 end
65
66 def messages_operation do
67 %Operation{
68 tags: ["chat"],
69 summary: "Get the most recent messages of the chat",
70 parameters: [
71 Operation.parameter(:id, :path, :string, "The ID of the Chat"),
72 Operation.parameter(:limit, :query, :integer, "How many results to return", example: 20),
73 Operation.parameter(:min_id, :query, :string, "Return only messages after this id"),
74 Operation.parameter(:max_id, :query, :string, "Return only messages before this id")
75 ],
76 responses: %{
77 200 =>
78 Operation.response("The messages in the chat", "application/json", ChatMessagesResponse)
79 },
80 security: [
81 %{
82 "oAuth" => ["read"]
83 }
84 ]
85 }
86 end
87
88 def post_chat_message_operation do
89 %Operation{
90 tags: ["chat"],
91 summary: "Post a message to the chat",
92 parameters: [
93 Operation.parameter(:id, :path, :string, "The ID of the Chat")
94 ],
95 requestBody: Helpers.request_body("Parameters", ChatMessageCreateRequest, required: true),
96 responses: %{
97 200 =>
98 Operation.response(
99 "The newly created ChatMessage",
100 "application/json",
101 ChatMessageResponse
102 )
103 },
104 security: [
105 %{
106 "oAuth" => ["write"]
107 }
108 ]
109 }
110 end
111 end