[#2456] OpenAPI: added `embed_relationships` param definition.
[akkoma] / lib / pleroma / web / api_spec / operations / search_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.SearchOperation do
6 alias OpenApiSpex.Operation
7 alias OpenApiSpex.Schema
8 alias Pleroma.Web.ApiSpec.AccountOperation
9 alias Pleroma.Web.ApiSpec.Schemas.Account
10 alias Pleroma.Web.ApiSpec.Schemas.BooleanLike
11 alias Pleroma.Web.ApiSpec.Schemas.FlakeID
12 alias Pleroma.Web.ApiSpec.Schemas.Status
13 alias Pleroma.Web.ApiSpec.Schemas.Tag
14
15 import Pleroma.Web.ApiSpec.Helpers
16
17 def open_api_operation(action) do
18 operation = String.to_existing_atom("#{action}_operation")
19 apply(__MODULE__, operation, [])
20 end
21
22 def account_search_operation do
23 %Operation{
24 tags: ["Search"],
25 summary: "Search for matching accounts by username or display name",
26 operationId: "SearchController.account_search",
27 parameters:
28 [
29 Operation.parameter(:q, :query, %Schema{type: :string}, "What to search for",
30 required: true
31 ),
32 Operation.parameter(
33 :limit,
34 :query,
35 %Schema{type: :integer, default: 40},
36 "Maximum number of results"
37 ),
38 Operation.parameter(
39 :resolve,
40 :query,
41 %Schema{allOf: [BooleanLike], default: false},
42 "Attempt WebFinger lookup. Use this when `q` is an exact address."
43 ),
44 Operation.parameter(
45 :following,
46 :query,
47 %Schema{allOf: [BooleanLike], default: false},
48 "Only include accounts that the user is following"
49 )
50 ] ++ [embed_relationships_param()],
51 responses: %{
52 200 =>
53 Operation.response(
54 "Array of Account",
55 "application/json",
56 AccountOperation.array_of_accounts()
57 )
58 }
59 }
60 end
61
62 def search_operation do
63 %Operation{
64 tags: ["Search"],
65 summary: "Search results",
66 security: [%{"oAuth" => ["read:search"]}],
67 operationId: "SearchController.search",
68 deprecated: true,
69 parameters:
70 [
71 Operation.parameter(
72 :account_id,
73 :query,
74 FlakeID,
75 "If provided, statuses returned will be authored only by this account"
76 ),
77 Operation.parameter(
78 :type,
79 :query,
80 %Schema{type: :string, enum: ["accounts", "hashtags", "statuses"]},
81 "Search type"
82 ),
83 Operation.parameter(:q, :query, %Schema{type: :string}, "The search query",
84 required: true
85 ),
86 Operation.parameter(
87 :resolve,
88 :query,
89 %Schema{allOf: [BooleanLike], default: false},
90 "Attempt WebFinger lookup"
91 ),
92 Operation.parameter(
93 :following,
94 :query,
95 %Schema{allOf: [BooleanLike], default: false},
96 "Only include accounts that the user is following"
97 ),
98 Operation.parameter(
99 :offset,
100 :query,
101 %Schema{type: :integer},
102 "Offset"
103 )
104 ] ++ pagination_params() ++ [embed_relationships_param()],
105 responses: %{
106 200 => Operation.response("Results", "application/json", results())
107 }
108 }
109 end
110
111 def search2_operation do
112 %Operation{
113 tags: ["Search"],
114 summary: "Search results",
115 security: [%{"oAuth" => ["read:search"]}],
116 operationId: "SearchController.search2",
117 parameters: [
118 Operation.parameter(
119 :account_id,
120 :query,
121 FlakeID,
122 "If provided, statuses returned will be authored only by this account"
123 ),
124 Operation.parameter(
125 :type,
126 :query,
127 %Schema{type: :string, enum: ["accounts", "hashtags", "statuses"]},
128 "Search type"
129 ),
130 Operation.parameter(:q, :query, %Schema{type: :string}, "What to search for",
131 required: true
132 ),
133 Operation.parameter(
134 :resolve,
135 :query,
136 %Schema{allOf: [BooleanLike], default: false},
137 "Attempt WebFinger lookup"
138 ),
139 Operation.parameter(
140 :following,
141 :query,
142 %Schema{allOf: [BooleanLike], default: false},
143 "Only include accounts that the user is following"
144 )
145 | pagination_params()
146 ],
147 responses: %{
148 200 => Operation.response("Results", "application/json", results2())
149 }
150 }
151 end
152
153 defp results2 do
154 %Schema{
155 title: "SearchResults",
156 type: :object,
157 properties: %{
158 accounts: %Schema{
159 type: :array,
160 items: Account,
161 description: "Accounts which match the given query"
162 },
163 statuses: %Schema{
164 type: :array,
165 items: Status,
166 description: "Statuses which match the given query"
167 },
168 hashtags: %Schema{
169 type: :array,
170 items: Tag,
171 description: "Hashtags which match the given query"
172 }
173 },
174 example: %{
175 "accounts" => [Account.schema().example],
176 "statuses" => [Status.schema().example],
177 "hashtags" => [Tag.schema().example]
178 }
179 }
180 end
181
182 defp results do
183 %Schema{
184 title: "SearchResults",
185 type: :object,
186 properties: %{
187 accounts: %Schema{
188 type: :array,
189 items: Account,
190 description: "Accounts which match the given query"
191 },
192 statuses: %Schema{
193 type: :array,
194 items: Status,
195 description: "Statuses which match the given query"
196 },
197 hashtags: %Schema{
198 type: :array,
199 items: %Schema{type: :string},
200 description: "Hashtags which match the given query"
201 }
202 },
203 example: %{
204 "accounts" => [Account.schema().example],
205 "statuses" => [Status.schema().example],
206 "hashtags" => ["cofe"]
207 }
208 }
209 end
210 end