Merge remote-tracking branch 'remotes/origin/develop' into restricted-relations-embedding
[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 Operation.parameter(:q, :query, %Schema{type: :string}, "What to search for",
29 required: true
30 ),
31 Operation.parameter(
32 :limit,
33 :query,
34 %Schema{type: :integer, default: 40},
35 "Maximum number of results"
36 ),
37 Operation.parameter(
38 :resolve,
39 :query,
40 %Schema{allOf: [BooleanLike], default: false},
41 "Attempt WebFinger lookup. Use this when `q` is an exact address."
42 ),
43 Operation.parameter(
44 :following,
45 :query,
46 %Schema{allOf: [BooleanLike], default: false},
47 "Only include accounts that the user is following"
48 )
49 ],
50 responses: %{
51 200 =>
52 Operation.response(
53 "Array of Account",
54 "application/json",
55 AccountOperation.array_of_accounts()
56 )
57 }
58 }
59 end
60
61 def search_operation do
62 %Operation{
63 tags: ["Search"],
64 summary: "Search results",
65 security: [%{"oAuth" => ["read:search"]}],
66 operationId: "SearchController.search",
67 deprecated: true,
68 parameters: [
69 Operation.parameter(
70 :account_id,
71 :query,
72 FlakeID,
73 "If provided, statuses returned will be authored only by this account"
74 ),
75 Operation.parameter(
76 :type,
77 :query,
78 %Schema{type: :string, enum: ["accounts", "hashtags", "statuses"]},
79 "Search type"
80 ),
81 Operation.parameter(:q, :query, %Schema{type: :string}, "The search query", required: true),
82 Operation.parameter(
83 :resolve,
84 :query,
85 %Schema{allOf: [BooleanLike], default: false},
86 "Attempt WebFinger lookup"
87 ),
88 Operation.parameter(
89 :following,
90 :query,
91 %Schema{allOf: [BooleanLike], default: false},
92 "Only include accounts that the user is following"
93 ),
94 Operation.parameter(
95 :offset,
96 :query,
97 %Schema{type: :integer},
98 "Offset"
99 )
100 | pagination_params()
101 ],
102 responses: %{
103 200 => Operation.response("Results", "application/json", results())
104 }
105 }
106 end
107
108 def search2_operation do
109 %Operation{
110 tags: ["Search"],
111 summary: "Search results",
112 security: [%{"oAuth" => ["read:search"]}],
113 operationId: "SearchController.search2",
114 parameters: [
115 Operation.parameter(
116 :account_id,
117 :query,
118 FlakeID,
119 "If provided, statuses returned will be authored only by this account"
120 ),
121 Operation.parameter(
122 :type,
123 :query,
124 %Schema{type: :string, enum: ["accounts", "hashtags", "statuses"]},
125 "Search type"
126 ),
127 Operation.parameter(:q, :query, %Schema{type: :string}, "What to search for",
128 required: true
129 ),
130 Operation.parameter(
131 :resolve,
132 :query,
133 %Schema{allOf: [BooleanLike], default: false},
134 "Attempt WebFinger lookup"
135 ),
136 Operation.parameter(
137 :following,
138 :query,
139 %Schema{allOf: [BooleanLike], default: false},
140 "Only include accounts that the user is following"
141 )
142 | pagination_params()
143 ],
144 responses: %{
145 200 => Operation.response("Results", "application/json", results2())
146 }
147 }
148 end
149
150 defp results2 do
151 %Schema{
152 title: "SearchResults",
153 type: :object,
154 properties: %{
155 accounts: %Schema{
156 type: :array,
157 items: Account,
158 description: "Accounts which match the given query"
159 },
160 statuses: %Schema{
161 type: :array,
162 items: Status,
163 description: "Statuses which match the given query"
164 },
165 hashtags: %Schema{
166 type: :array,
167 items: Tag,
168 description: "Hashtags which match the given query"
169 }
170 },
171 example: %{
172 "accounts" => [Account.schema().example],
173 "statuses" => [Status.schema().example],
174 "hashtags" => [Tag.schema().example]
175 }
176 }
177 end
178
179 defp results do
180 %Schema{
181 title: "SearchResults",
182 type: :object,
183 properties: %{
184 accounts: %Schema{
185 type: :array,
186 items: Account,
187 description: "Accounts which match the given query"
188 },
189 statuses: %Schema{
190 type: :array,
191 items: Status,
192 description: "Statuses which match the given query"
193 },
194 hashtags: %Schema{
195 type: :array,
196 items: %Schema{type: :string},
197 description: "Hashtags which match the given query"
198 }
199 },
200 example: %{
201 "accounts" => [Account.schema().example],
202 "statuses" => [Status.schema().example],
203 "hashtags" => ["cofe"]
204 }
205 }
206 end
207 end