First
[anni] / lib / pleroma / web / api_spec / operations / marker_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.MarkerOperation 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: ["Markers"],
18       summary: "Get saved timeline position",
19       security: [%{"oAuth" => ["read:statuses"]}],
20       operationId: "MarkerController.index",
21       parameters: [
22         Operation.parameter(
23           :timeline,
24           :query,
25           %Schema{
26             type: :array,
27             items: %Schema{type: :string, enum: ["home", "notifications"]}
28           },
29           "Array of markers to fetch. If not provided, an empty object will be returned."
30         )
31       ],
32       responses: %{
33         200 => Operation.response("Marker", "application/json", response()),
34         403 => Operation.response("Error", "application/json", api_error())
35       }
36     }
37   end
38
39   def upsert_operation do
40     %Operation{
41       tags: ["Markers"],
42       summary: "Save position in timeline",
43       operationId: "MarkerController.upsert",
44       requestBody: Helpers.request_body("Parameters", upsert_request(), required: true),
45       security: [%{"oAuth" => ["follow", "write:blocks"]}],
46       responses: %{
47         200 => Operation.response("Marker", "application/json", response()),
48         403 => Operation.response("Error", "application/json", api_error())
49       }
50     }
51   end
52
53   defp marker do
54     %Schema{
55       title: "Marker",
56       description: "Schema for a marker",
57       type: :object,
58       properties: %{
59         last_read_id: %Schema{type: :string},
60         version: %Schema{type: :integer},
61         updated_at: %Schema{type: :string},
62         pleroma: %Schema{
63           type: :object,
64           properties: %{
65             unread_count: %Schema{type: :integer}
66           }
67         }
68       },
69       example: %{
70         "last_read_id" => "35098814",
71         "version" => 361,
72         "updated_at" => "2019-11-26T22:37:25.239Z",
73         "pleroma" => %{"unread_count" => 5}
74       }
75     }
76   end
77
78   defp response do
79     %Schema{
80       title: "MarkersResponse",
81       description: "Response schema for markers",
82       type: :object,
83       properties: %{
84         notifications: %Schema{allOf: [marker()], nullable: true},
85         home: %Schema{allOf: [marker()], nullable: true}
86       },
87       items: %Schema{type: :string},
88       example: %{
89         "notifications" => %{
90           "last_read_id" => "35098814",
91           "version" => 361,
92           "updated_at" => "2019-11-26T22:37:25.239Z",
93           "pleroma" => %{"unread_count" => 0}
94         },
95         "home" => %{
96           "last_read_id" => "103206604258487607",
97           "version" => 468,
98           "updated_at" => "2019-11-26T22:37:25.235Z",
99           "pleroma" => %{"unread_count" => 10}
100         }
101       }
102     }
103   end
104
105   defp upsert_request do
106     %Schema{
107       title: "MarkersUpsertRequest",
108       description: "Request schema for marker upsert",
109       type: :object,
110       properties: %{
111         notifications: %Schema{
112           type: :object,
113           nullable: true,
114           properties: %{
115             last_read_id: %Schema{nullable: true, type: :string}
116           }
117         },
118         home: %Schema{
119           type: :object,
120           nullable: true,
121           properties: %{
122             last_read_id: %Schema{nullable: true, type: :string}
123           }
124         }
125       },
126       example: %{
127         "home" => %{
128           "last_read_id" => "103194548672408537",
129           "version" => 462,
130           "updated_at" => "2019-11-24T19:39:39.337Z"
131         }
132       }
133     }
134   end
135
136   defp api_error do
137     %Schema{
138       type: :object,
139       properties: %{error: %Schema{type: :string}}
140     }
141   end
142 end