1 | #pragma once |
2 | |
3 | /// @file |
4 | /// @brief Responses used by multiple endpoints. |
5 | |
6 | #if __has_include(<nlohmann/json_fwd.hpp>) |
7 | #include <nlohmann/json_fwd.hpp> |
8 | #else |
9 | #include <nlohmann/json.hpp> |
10 | #endif |
11 | |
12 | #include <string> |
13 | #include <vector> |
14 | |
15 | #include "mtx/events/collections.hpp" |
16 | |
17 | namespace mtx { |
18 | //! Namespace for the different types of responses. |
19 | namespace responses { |
20 | //! An event id returned by the API. |
21 | struct EventId |
22 | { |
23 | //! The event id. |
24 | mtx::identifiers::Event event_id; |
25 | |
26 | friend void from_json(const nlohmann::json &obj, EventId &response); |
27 | }; |
28 | |
29 | //! A room id returned by the API. |
30 | struct RoomId |
31 | { |
32 | //! The room id. |
33 | std::string room_id; |
34 | |
35 | friend void from_json(const nlohmann::json &obj, RoomId &response); |
36 | }; |
37 | |
38 | //! A filter id returned by the API. |
39 | struct FilterId |
40 | { |
41 | //! The filter id. |
42 | std::string filter_id; |
43 | |
44 | friend void from_json(const nlohmann::json &obj, FilterId &response); |
45 | }; |
46 | |
47 | //! A new room version as returned by the room_keys/version API |
48 | struct Version |
49 | { |
50 | //! Required: The backup version. This is an opaque string. |
51 | std::string version; |
52 | |
53 | friend void from_json(const nlohmann::json &obj, Version &response); |
54 | }; |
55 | |
56 | //! Some endpoints return this to indicate success in addition to the http code. |
57 | struct Success |
58 | { |
59 | //! Required. Whether the validation was successful or not. |
60 | bool success; |
61 | |
62 | friend void from_json(const nlohmann::json &obj, Success &response); |
63 | }; |
64 | |
65 | //! Some endpoints return this to indicate availability in addition to the http code (i.e. a |
66 | //! username). |
67 | struct Available |
68 | { |
69 | //! Required. A flag to indicate that the resource is available. |
70 | bool available; |
71 | |
72 | friend void from_json(const nlohmann::json &obj, Available &response); |
73 | }; |
74 | |
75 | //! Responses to the `/requestToken` endpoints |
76 | struct RequestToken |
77 | { |
78 | //! Required. The session ID. Session IDs are opaque strings that must consist entirely of the |
79 | //! characters [0-9a-zA-Z.=_-]. Their length must not exceed 255 characters and they must not be |
80 | //! empty. |
81 | std::string sid; |
82 | //! An optional field containing a URL where the client must submit the validation token to, |
83 | //! with identical parameters to the Identity Service API's POST /validate/email/submitToken |
84 | //! endpoint (without the requirement for an access token). The homeserver must send this token |
85 | //! to the user (if applicable), who should then be prompted to provide it to the client. |
86 | std::string submit_url; |
87 | |
88 | friend void from_json(const nlohmann::json &obj, RequestToken &response); |
89 | }; |
90 | |
91 | //! A simple list of aliases |
92 | struct Aliases |
93 | { |
94 | //! The aliases |
95 | std::vector<std::string> aliases; |
96 | |
97 | friend void from_json(const nlohmann::json &obj, Aliases &response); |
98 | }; |
99 | |
100 | //! Different helper for parsing responses. |
101 | namespace utils { |
102 | //! Multiple account_data events. |
103 | using RoomAccountDataEvents = std::vector<mtx::events::collections::RoomAccountDataEvents>; |
104 | //! Multiple TimelineEvents. |
105 | using TimelineEvents = std::vector<mtx::events::collections::TimelineEvents>; |
106 | //! Multiple StateEvents. |
107 | using StateEvents = std::vector<mtx::events::collections::StateEvents>; |
108 | //! Multiple StrippedEvents. |
109 | using StrippedEvents = std::vector<mtx::events::collections::StrippedEvents>; |
110 | //! Multiple DeviceEvents. |
111 | using DeviceEvents = std::vector<mtx::events::collections::DeviceEvents>; |
112 | //! Multiple EphemeralEvents. |
113 | using EphemeralEvents = std::vector<mtx::events::collections::EphemeralEvents>; |
114 | |
115 | namespace states = mtx::events::state; |
116 | namespace msgs = mtx::events::msg; |
117 | |
118 | void |
119 | log_error(std::exception &err, const nlohmann::json &event); |
120 | |
121 | void |
122 | log_error(const std::string &err, const nlohmann::json &event); |
123 | |
124 | //! Parse multiple account_data events. |
125 | void |
126 | parse_room_account_data_events(const nlohmann::json &events, RoomAccountDataEvents &container); |
127 | |
128 | void |
129 | compose_timeline_events(nlohmann::json &events, const TimelineEvents &container); |
130 | |
131 | //! Parse multiple timeline events. |
132 | void |
133 | parse_timeline_events(const nlohmann::json &events, TimelineEvents &container); |
134 | |
135 | //! Parse multiple state events. |
136 | void |
137 | parse_state_events(const nlohmann::json &events, StateEvents &container); |
138 | |
139 | //! Parse multiple stripped events. |
140 | void |
141 | parse_stripped_events(const nlohmann::json &events, StrippedEvents &container); |
142 | |
143 | //! Parse multiple device events. |
144 | void |
145 | parse_device_events(const nlohmann::json &events, DeviceEvents &container); |
146 | |
147 | //! Parse multiple ephemeral events. |
148 | void |
149 | parse_ephemeral_events(const nlohmann::json &events, EphemeralEvents &container); |
150 | } |
151 | |
152 | //! An array of state events |
153 | struct StateEvents |
154 | { |
155 | utils::StateEvents events; |
156 | |
157 | friend void from_json(const nlohmann::json &arr, StateEvents &response); |
158 | }; |
159 | } |
160 | } |
161 | |