| 1 | #include "mtx/responses/sync.hpp" |
| 2 | #include "mtx/log.hpp" |
| 3 | #include "mtx/responses/common.hpp" |
| 4 | |
| 5 | #include <nlohmann/json.hpp> |
| 6 | |
| 7 | #include <variant> |
| 8 | |
| 9 | using json = nlohmann::json; |
| 10 | |
| 11 | namespace mtx { |
| 12 | namespace responses { |
| 13 | |
| 14 | void |
| 15 | from_json(const json &obj, AccountData &account_data) |
| 16 | { |
| 17 | if (auto it = obj.find("events"); it != obj.end() && it->is_array()) |
| 18 | utils::parse_room_account_data_events(*it, account_data.events); |
| 19 | } |
| 20 | |
| 21 | void |
| 22 | from_json(const json &obj, State &state) |
| 23 | { |
| 24 | if (auto it = obj.find("events"); it != obj.end() && it->is_array()) |
| 25 | utils::parse_state_events(*it, state.events); |
| 26 | } |
| 27 | |
| 28 | void |
| 29 | from_json(const json &obj, Timeline &timeline) |
| 30 | { |
| 31 | timeline.prev_batch = obj.value("prev_batch", std::string{}); |
| 32 | timeline.limited = obj.value("limited", false); |
| 33 | |
| 34 | utils::parse_timeline_events(obj.at("events"), timeline.events); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | from_json(const json &obj, UnreadNotifications ¬ifications) |
| 39 | { |
| 40 | if (auto it = obj.find("highlight_count"); it != obj.end()) |
| 41 | notifications.highlight_count = it->get<uint64_t>(); |
| 42 | |
| 43 | if (auto it = obj.find("notification_count"); it != obj.end()) |
| 44 | notifications.notification_count = it->get<uint64_t>(); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | from_json(const json &obj, Ephemeral &ephemeral) |
| 49 | { |
| 50 | if (auto it = obj.find("events"); it != obj.end() && it->is_array()) |
| 51 | utils::parse_ephemeral_events(*it, ephemeral.events); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | from_json(const json &obj, JoinedRoom &room) |
| 56 | { |
| 57 | if (auto it = obj.find("state"); it != obj.end()) |
| 58 | room.state = it->get<State>(); |
| 59 | |
| 60 | if (auto it = obj.find("timeline"); it != obj.end()) |
| 61 | room.timeline = it->get<Timeline>(); |
| 62 | |
| 63 | if (auto it = obj.find("unread_notifications"); it != obj.end()) |
| 64 | room.unread_notifications = it->get<UnreadNotifications>(); |
| 65 | |
| 66 | if (auto it = obj.find("ephemeral"); it != obj.end()) |
| 67 | room.ephemeral = it->get<Ephemeral>(); |
| 68 | |
| 69 | if (auto it = obj.find("account_data"); it != obj.end()) |
| 70 | room.account_data = it->get<AccountData>(); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | from_json(const json &obj, LeftRoom &room) |
| 75 | { |
| 76 | if (auto it = obj.find("state"); it != obj.end()) |
| 77 | room.state = it->get<State>(); |
| 78 | |
| 79 | if (auto it = obj.find("timeline"); it != obj.end()) |
| 80 | room.timeline = it->get<Timeline>(); |
| 81 | } |
| 82 | |
| 83 | std::string |
| 84 | InvitedRoom::name() const |
| 85 | { |
| 86 | using Name = mtx::events::StrippedEvent<mtx::events::state::Name>; |
| 87 | using Member = mtx::events::StrippedEvent<mtx::events::state::Member>; |
| 88 | |
| 89 | std::string room_name; |
| 90 | std::string member_name; |
| 91 | |
| 92 | for (const auto &event : invite_state) { |
| 93 | if (auto name = std::get_if<Name>(&event); name != nullptr) { |
| 94 | room_name = name->content.name; |
| 95 | } else if (auto avatar = std::get_if<Member>(&event); avatar != nullptr) { |
| 96 | if (member_name.empty()) |
| 97 | member_name = avatar->content.display_name; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (room_name.empty()) |
| 102 | return member_name; |
| 103 | |
| 104 | return room_name; |
| 105 | } |
| 106 | |
| 107 | std::string |
| 108 | InvitedRoom::avatar() const |
| 109 | { |
| 110 | using Avatar = mtx::events::StrippedEvent<mtx::events::state::Avatar>; |
| 111 | using Member = mtx::events::StrippedEvent<mtx::events::state::Member>; |
| 112 | |
| 113 | std::string room_avatar; |
| 114 | std::string member_avatar; |
| 115 | |
| 116 | for (const auto &event : invite_state) { |
| 117 | if (auto avatar = std::get_if<Avatar>(&event); avatar != nullptr) { |
| 118 | room_avatar = avatar->content.url; |
| 119 | } else if (auto member = std::get_if<Member>(&event); member != nullptr) { |
| 120 | // Pick the first avatar. |
| 121 | if (member_avatar.empty()) |
| 122 | member_avatar = member->content.avatar_url; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (room_avatar.empty()) |
| 127 | return member_avatar; |
| 128 | |
| 129 | return room_avatar; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | from_json(const json &obj, InvitedRoom &room) |
| 134 | { |
| 135 | if (auto state = obj.find("invite_state"); state != obj.end()) |
| 136 | if (auto events = state->find("events"); events != state->end()) |
| 137 | utils::parse_stripped_events(*events, room.invite_state); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | from_json(const json &obj, KnockedRoom &room) |
| 142 | { |
| 143 | if (auto state = obj.find("knock_state"); state != obj.end()) |
| 144 | if (auto events = state->find("events"); events != state->end()) |
| 145 | utils::parse_stripped_events(*events, room.knock_state); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | from_json(const json &obj, Rooms &rooms) |
| 150 | { |
| 151 | if (auto entries = obj.find("join"); entries != obj.end()) { |
| 152 | for (const auto &r : entries->items()) { |
| 153 | if (r.key().size() < 256) { |
| 154 | rooms.join.emplace_hint(rooms.join.end(), r.key(), r.value().get<JoinedRoom>()); |
| 155 | } else { |
| 156 | mtx::utils::log::log()->warn("Skipping roomid which exceeds 255 bytes."); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (auto entries = obj.find("leave"); entries != obj.end()) { |
| 162 | for (const auto &r : entries->items()) { |
| 163 | if (r.key().size() < 256) { |
| 164 | rooms.leave.emplace_hint(rooms.leave.end(), r.key(), r.value().get<LeftRoom>()); |
| 165 | } else { |
| 166 | mtx::utils::log::log()->warn("Skipping roomid which exceeds 255 bytes."); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (auto entries = obj.find("invite"); entries != obj.end()) { |
| 172 | for (const auto &r : entries->items()) { |
| 173 | if (r.key().size() < 256) { |
| 174 | rooms.invite.emplace_hint( |
| 175 | rooms.invite.end(), r.key(), r.value().get<InvitedRoom>()); |
| 176 | } else { |
| 177 | mtx::utils::log::log()->warn("Skipping roomid which exceeds 255 bytes."); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (auto entries = obj.find("knock"); entries != obj.end()) { |
| 183 | for (const auto &r : entries->items()) { |
| 184 | if (r.key().size() < 256) { |
| 185 | rooms.knock.emplace_hint(rooms.knock.end(), r.key(), r.value().get<KnockedRoom>()); |
| 186 | } else { |
| 187 | mtx::utils::log::log()->warn("Skipping roomid which exceeds 255 bytes."); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | from_json(const json &obj, DeviceLists &device_lists) |
| 195 | { |
| 196 | if (obj.count("changed") != 0) { |
| 197 | device_lists.changed = obj.at("changed").get<std::vector<std::string>>(); |
| 198 | |
| 199 | std::erase_if(device_lists.changed, [](const std::string &user) { |
| 200 | if (user.size() > 255) { |
| 201 | mtx::utils::log::log()->warn("Invalid userid in device list changed."); |
| 202 | return true; |
| 203 | } else |
| 204 | return false; |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | if (obj.count("left") != 0) { |
| 209 | device_lists.left = obj.at("left").get<std::vector<std::string>>(); |
| 210 | |
| 211 | std::erase_if(device_lists.left, [](const std::string &user) { |
| 212 | if (user.size() > 255) { |
| 213 | mtx::utils::log::log()->warn("Invalid userid in device list left."); |
| 214 | return true; |
| 215 | } else |
| 216 | return false; |
| 217 | }); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void |
| 222 | from_json(const json &obj, ToDevice &to_device) |
| 223 | { |
| 224 | if (obj.count("events") != 0) |
| 225 | utils::parse_device_events(obj.at("events"), to_device.events); |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | from_json(const json &obj, Sync &response) |
| 230 | { |
| 231 | if (auto it = obj.find("rooms"); it != obj.end()) |
| 232 | response.rooms = it->get<Rooms>(); |
| 233 | |
| 234 | if (auto it = obj.find("device_lists"); it != obj.end()) |
| 235 | response.device_lists = it->get<DeviceLists>(); |
| 236 | |
| 237 | if (auto it = obj.find("to_device"); it != obj.end()) |
| 238 | response.to_device = it->get<ToDevice>(); |
| 239 | |
| 240 | if (auto it = obj.find("device_one_time_keys_count"); it != obj.end()) |
| 241 | response.device_one_time_keys_count = it->get<std::map<std::string, uint16_t>>(); |
| 242 | |
| 243 | if (auto fallback_keys = obj.find("device_unused_fallback_key_types"); |
| 244 | fallback_keys != obj.end() && fallback_keys->is_array()) |
| 245 | response.device_unused_fallback_key_types = fallback_keys->get<std::vector<std::string>>(); |
| 246 | |
| 247 | if (obj.count("presence") != 0 && obj.at("presence").contains("events")) { |
| 248 | const auto &events = obj.at("presence").at("events"); |
| 249 | response.presence.reserve(events.size()); |
| 250 | for (const auto &e : events) { |
| 251 | try { |
| 252 | response.presence.push_back( |
| 253 | e.get<mtx::events::Event<mtx::events::presence::Presence>>()); |
| 254 | } catch (std::exception &ex) { |
| 255 | mtx::utils::log::log()->warn( |
| 256 | "Error parsing presence event: {}, {}", ex.what(), e.dump(2)); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (auto it = obj.find("account_data"); it != obj.end()) |
| 262 | response.account_data = it->get<AccountData>(); |
| 263 | |
| 264 | response.next_batch = obj.at("next_batch").get<std::string>(); |
| 265 | } |
| 266 | } |
| 267 | } |