total rebase
[anni] / lib / pleroma / web / api_spec / operations / poll_operation.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ApiSpec.PollOperation do
6   alias OpenApiSpex.Operation
7   alias OpenApiSpex.Schema
8   alias Pleroma.Web.ApiSpec.Schemas.ApiError
9   alias Pleroma.Web.ApiSpec.Schemas.FlakeID
10   alias Pleroma.Web.ApiSpec.Schemas.Poll
11
12   import Pleroma.Web.ApiSpec.Helpers
13
14   def open_api_operation(action) do
15     operation = String.to_existing_atom("#{action}_operation")
16     apply(__MODULE__, operation, [])
17   end
18
19   def show_operation do
20     %Operation{
21       tags: ["Polls"],
22       summary: "View a poll",
23       security: [%{"oAuth" => ["read:statuses"]}],
24       parameters: [id_param()],
25       operationId: "PollController.show",
26       responses: %{
27         200 => Operation.response("Poll", "application/json", Poll),
28         404 => Operation.response("Error", "application/json", ApiError)
29       }
30     }
31   end
32
33   def vote_operation do
34     %Operation{
35       tags: ["Polls"],
36       summary: "Vote on a poll",
37       parameters: [id_param()],
38       operationId: "PollController.vote",
39       requestBody: vote_request(),
40       security: [%{"oAuth" => ["write:statuses"]}],
41       responses: %{
42         200 => Operation.response("Poll", "application/json", Poll),
43         422 => Operation.response("Error", "application/json", ApiError),
44         404 => Operation.response("Error", "application/json", ApiError)
45       }
46     }
47   end
48
49   defp id_param do
50     Operation.parameter(:id, :path, FlakeID.schema(), "Poll ID",
51       example: "123",
52       required: true
53     )
54   end
55
56   defp vote_request do
57     request_body(
58       "Parameters",
59       %Schema{
60         type: :object,
61         properties: %{
62           choices: %Schema{
63             type: :array,
64             items: %Schema{type: :integer},
65             description: "Array of own votes containing index for each option (starting from 0)"
66           }
67         },
68         required: [:choices]
69       },
70       required: true,
71       example: %{
72         "choices" => [0, 1, 2]
73       }
74     )
75   end
76 end