Merge branch 'openapi/subscriptions' into 'develop'
[akkoma] / lib / pleroma / web / api_spec / operations / filter_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.FilterOperation do
6 alias OpenApiSpex.Operation
7 alias OpenApiSpex.Schema
8 alias Pleroma.Web.ApiSpec.Helpers
9
10 def open_api_operation(action) do
11 operation = String.to_existing_atom("#{action}_operation")
12 apply(__MODULE__, operation, [])
13 end
14
15 def index_operation do
16 %Operation{
17 tags: ["apps"],
18 summary: "View all filters",
19 operationId: "FilterController.index",
20 security: [%{"oAuth" => ["read:filters"]}],
21 responses: %{
22 200 => Operation.response("Filters", "application/json", array_of_filters())
23 }
24 }
25 end
26
27 def create_operation do
28 %Operation{
29 tags: ["apps"],
30 summary: "Create a filter",
31 operationId: "FilterController.create",
32 requestBody: Helpers.request_body("Parameters", create_request(), required: true),
33 security: [%{"oAuth" => ["write:filters"]}],
34 responses: %{200 => Operation.response("Filter", "application/json", filter())}
35 }
36 end
37
38 def show_operation do
39 %Operation{
40 tags: ["apps"],
41 summary: "View all filters",
42 parameters: [id_param()],
43 operationId: "FilterController.show",
44 security: [%{"oAuth" => ["read:filters"]}],
45 responses: %{
46 200 => Operation.response("Filter", "application/json", filter())
47 }
48 }
49 end
50
51 def update_operation do
52 %Operation{
53 tags: ["apps"],
54 summary: "Update a filter",
55 parameters: [id_param()],
56 operationId: "FilterController.update",
57 requestBody: Helpers.request_body("Parameters", update_request(), required: true),
58 security: [%{"oAuth" => ["write:filters"]}],
59 responses: %{
60 200 => Operation.response("Filter", "application/json", filter())
61 }
62 }
63 end
64
65 def delete_operation do
66 %Operation{
67 tags: ["apps"],
68 summary: "Remove a filter",
69 parameters: [id_param()],
70 operationId: "FilterController.delete",
71 security: [%{"oAuth" => ["write:filters"]}],
72 responses: %{
73 200 =>
74 Operation.response("Filter", "application/json", %Schema{
75 type: :object,
76 description: "Empty object"
77 })
78 }
79 }
80 end
81
82 defp id_param do
83 Operation.parameter(:id, :path, :string, "Filter ID", example: "123", required: true)
84 end
85
86 defp filter do
87 %Schema{
88 title: "Filter",
89 type: :object,
90 properties: %{
91 id: %Schema{type: :string},
92 phrase: %Schema{type: :string, description: "The text to be filtered"},
93 context: %Schema{
94 type: :array,
95 items: %Schema{type: :string, enum: ["home", "notifications", "public", "thread"]},
96 description: "The contexts in which the filter should be applied."
97 },
98 expires_at: %Schema{
99 type: :string,
100 format: :"date-time",
101 description:
102 "When the filter should no longer be applied. String (ISO 8601 Datetime), or null if the filter does not expire.",
103 nullable: true
104 },
105 irreversible: %Schema{
106 type: :boolean,
107 description:
108 "Should matching entities in home and notifications be dropped by the server?"
109 },
110 whole_word: %Schema{
111 type: :boolean,
112 description: "Should the filter consider word boundaries?"
113 }
114 },
115 example: %{
116 "id" => "5580",
117 "phrase" => "@twitter.com",
118 "context" => [
119 "home",
120 "notifications",
121 "public",
122 "thread"
123 ],
124 "whole_word" => false,
125 "expires_at" => nil,
126 "irreversible" => true
127 }
128 }
129 end
130
131 defp array_of_filters do
132 %Schema{
133 title: "ArrayOfFilters",
134 description: "Array of Filters",
135 type: :array,
136 items: filter(),
137 example: [
138 %{
139 "id" => "5580",
140 "phrase" => "@twitter.com",
141 "context" => [
142 "home",
143 "notifications",
144 "public",
145 "thread"
146 ],
147 "whole_word" => false,
148 "expires_at" => nil,
149 "irreversible" => true
150 },
151 %{
152 "id" => "6191",
153 "phrase" => ":eurovision2019:",
154 "context" => [
155 "home"
156 ],
157 "whole_word" => true,
158 "expires_at" => "2019-05-21T13:47:31.333Z",
159 "irreversible" => false
160 }
161 ]
162 }
163 end
164
165 defp create_request do
166 %Schema{
167 title: "FilterCreateRequest",
168 allOf: [
169 update_request(),
170 %Schema{
171 type: :object,
172 properties: %{
173 irreversible: %Schema{
174 type: :bolean,
175 description:
176 "Should the server irreversibly drop matching entities from home and notifications?",
177 default: false
178 }
179 }
180 }
181 ],
182 example: %{
183 "phrase" => "knights",
184 "context" => ["home"]
185 }
186 }
187 end
188
189 defp update_request do
190 %Schema{
191 title: "FilterUpdateRequest",
192 type: :object,
193 properties: %{
194 phrase: %Schema{type: :string, description: "The text to be filtered"},
195 context: %Schema{
196 type: :array,
197 items: %Schema{type: :string, enum: ["home", "notifications", "public", "thread"]},
198 description:
199 "Array of enumerable strings `home`, `notifications`, `public`, `thread`. At least one context must be specified."
200 },
201 irreversible: %Schema{
202 type: :bolean,
203 description:
204 "Should the server irreversibly drop matching entities from home and notifications?"
205 },
206 whole_word: %Schema{
207 type: :bolean,
208 description: "Consider word boundaries?",
209 default: true
210 }
211 # TODO: probably should implement filter expiration
212 # expires_in: %Schema{
213 # type: :string,
214 # format: :"date-time",
215 # description:
216 # "ISO 8601 Datetime for when the filter expires. Otherwise,
217 # null for a filter that doesn't expire."
218 # }
219 },
220 required: [:phrase, :context],
221 example: %{
222 "phrase" => "knights",
223 "context" => ["home"]
224 }
225 }
226 end
227 end