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