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
17namespace mtx {
18//! Namespace for the different types of responses.
19namespace responses {
20//! An event id returned by the API.
21struct 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.
30struct 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.
39struct 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
48struct 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.
57struct 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).
67struct 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
76struct 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
92struct 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.
101namespace utils {
102//! Multiple account_data events.
103using RoomAccountDataEvents = std::vector<mtx::events::collections::RoomAccountDataEvents>;
104//! Multiple TimelineEvents.
105using TimelineEvents = std::vector<mtx::events::collections::TimelineEvents>;
106//! Multiple StateEvents.
107using StateEvents = std::vector<mtx::events::collections::StateEvents>;
108//! Multiple StrippedEvents.
109using StrippedEvents = std::vector<mtx::events::collections::StrippedEvents>;
110//! Multiple DeviceEvents.
111using DeviceEvents = std::vector<mtx::events::collections::DeviceEvents>;
112//! Multiple EphemeralEvents.
113using EphemeralEvents = std::vector<mtx::events::collections::EphemeralEvents>;
114
115namespace states = mtx::events::state;
116namespace msgs = mtx::events::msg;
117
118void
119log_error(std::exception &err, const nlohmann::json &event);
120
121void
122log_error(const std::string &err, const nlohmann::json &event);
123
124//! Parse multiple account_data events.
125void
126parse_room_account_data_events(const nlohmann::json &events, RoomAccountDataEvents &container);
127
128void
129compose_timeline_events(nlohmann::json &events, const TimelineEvents &container);
130
131//! Parse multiple timeline events.
132void
133parse_timeline_events(const nlohmann::json &events, TimelineEvents &container);
134
135//! Parse multiple state events.
136void
137parse_state_events(const nlohmann::json &events, StateEvents &container);
138
139//! Parse multiple stripped events.
140void
141parse_stripped_events(const nlohmann::json &events, StrippedEvents &container);
142
143//! Parse multiple device events.
144void
145parse_device_events(const nlohmann::json &events, DeviceEvents &container);
146
147//! Parse multiple ephemeral events.
148void
149parse_ephemeral_events(const nlohmann::json &events, EphemeralEvents &container);
150}
151
152//! An array of state events
153struct StateEvents
154{
155 utils::StateEvents events;
156
157 friend void from_json(const nlohmann::json &arr, StateEvents &response);
158};
159}
160}
161