diff --git a/include/mtx/responses/common.hpp b/include/mtx/responses/common.hpp index 2cda8c5d78207ad34eb8caf0955be2bf152dd9f8..becc9831e9a61e3144fee6d1e013a7908e405567 100644 --- a/include/mtx/responses/common.hpp +++ b/include/mtx/responses/common.hpp @@ -43,6 +43,165 @@ using TimelineEvents = std::vector<mtx::events::collections::TimelineEven using StateEvents = std::vector<mtx::events::collections::StateEvents>; using StrippedEvents = std::vector<mtx::events::collections::StrippedEvents>; +namespace states = mtx::events::state; +namespace msgs = mtx::events::msg; + +struct TimelineEventVisitor : public boost::static_visitor<json> +{ + json operator()(const events::StateEvent<states::Aliases> &aliasEv) const + { + json j; + mtx::events::to_json(j, aliasEv); + return j; + } + json operator()(const events::StateEvent<states::Avatar> &avatarEv) const + { + json j; + mtx::events::to_json(j, avatarEv); + return j; + }; + json operator()(const events::StateEvent<states::CanonicalAlias> &aliasEv) const + { + json j; + mtx::events::to_json(j, aliasEv); + return j; + }; + json operator()(const events::StateEvent<states::Create> &createEv) const + { + json j; + mtx::events::to_json(j, createEv); + return j; + }; + json operator()(const events::StateEvent<states::Encryption> &encEv) const + { + json j; + mtx::events::to_json(j, encEv); + return j; + }; + json operator()(const events::StateEvent<states::GuestAccess> &guestEv) const + { + json j; + mtx::events::to_json(j, guestEv); + return j; + }; + json operator()(const events::StateEvent<states::HistoryVisibility> &histEv) const + { + json j; + mtx::events::to_json(j, histEv); + return j; + }; + json operator()(const events::StateEvent<states::JoinRules> &joinEv) const + { + json j; + mtx::events::to_json(j, joinEv); + return j; + }; + json operator()(const events::StateEvent<states::Member> &membEv) const + { + json j; + mtx::events::to_json(j, membEv); + return j; + }; + json operator()(const events::StateEvent<states::Name> &nameEv) const + { + json j; + mtx::events::to_json(j, nameEv); + return j; + }; + json operator()(const events::StateEvent<states::PinnedEvents> &pinEv) const + { + json j; + mtx::events::to_json(j, pinEv); + return j; + }; + json operator()(const events::StateEvent<states::PowerLevels> &powEv) const + { + json j; + mtx::events::to_json(j, powEv); + return j; + }; + json operator()(const events::StateEvent<states::Tombstone> &tombEv) const + { + json j; + mtx::events::to_json(j, tombEv); + return j; + }; + json operator()(const events::StateEvent<states::Topic> &topicEv) const + { + json j; + mtx::events::to_json(j, topicEv); + return j; + }; + json operator()(const events::EncryptedEvent<msgs::Encrypted> &encEv) const + { + json j; + mtx::events::to_json(j, encEv); + return j; + }; + json operator()(const events::RedactionEvent<msgs::Redaction> &redEv) const + { + json j; + mtx::events::to_json(j, redEv); + return j; + }; + json operator()(const events::Sticker &stickEv) const + { + json j; + mtx::events::to_json(j, stickEv); + return j; + }; + json operator()(const events::RoomEvent<msgs::Redacted> &redEv) const + { + json j; + mtx::events::to_json(j, redEv); + return j; + }; + json operator()(const events::RoomEvent<msgs::Audio> &audioEv) const + { + json j; + mtx::events::to_json(j, audioEv); + return j; + }; + json operator()(const events::RoomEvent<msgs::Emote> &emoteEv) const + { + json j; + mtx::events::to_json(j, emoteEv); + return j; + }; + json operator()(const events::RoomEvent<msgs::File> &fileEv) const + { + json j; + mtx::events::to_json(j, fileEv); + return j; + }; + json operator()(const events::RoomEvent<msgs::Image> &imageEv) const + { + json j; + mtx::events::to_json(j, imageEv); + return j; + }; + // TODO: json operator()(const events::RoomEvent<msgs::Location> &locEv) const { json j; + // mtx::events::to_json(j, locEv); return j;}; + json operator()(const events::RoomEvent<msgs::Notice> ¬iceEv) const + { + json j; + mtx::events::to_json(j, noticeEv); + return j; + }; + json operator()(const events::RoomEvent<msgs::Text> &textEv) const + { + json j; + mtx::events::to_json(j, textEv); + return j; + }; + json operator()(const events::RoomEvent<msgs::Video> &videoEv) const + { + json j; + mtx::events::to_json(j, videoEv); + return j; + }; +}; + void log_error(json::exception &err, const json &event); @@ -52,6 +211,9 @@ log_error(std::string err, const json &event); void parse_room_account_data_events(const json &events, RoomAccountDataEvents &container); +void +compose_timeline_events(json &events, const TimelineEvents &container); + void parse_timeline_events(const json &events, TimelineEvents &container); diff --git a/include/mtx/responses/notifications.hpp b/include/mtx/responses/notifications.hpp index d69853812077783920f7c54d742c865d8e37a0b8..7c7804a1b1cc44c38127214f5927bcb8d51e4164 100644 --- a/include/mtx/responses/notifications.hpp +++ b/include/mtx/responses/notifications.hpp @@ -26,6 +26,8 @@ struct Notification void from_json(const nlohmann::json &obj, Notification &res); +void +to_json(nlohmann::json &obj, const Notification &res); //! Response from the `GET /_matrix/client/r0/notifications` endpoint. // @@ -44,5 +46,7 @@ struct Notifications void from_json(const nlohmann::json &obj, Notifications &res); +void +to_json(nlohmann::json &obj, const Notifications &res); } } diff --git a/lib/structs/responses/common.cpp b/lib/structs/responses/common.cpp index 67ce418b7f9831cdb874b2e2ffd7ccfe2f9d891f..b5164f5154e65745c5f6de2e7377ec9aabc0a32a 100644 --- a/lib/structs/responses/common.cpp +++ b/lib/structs/responses/common.cpp @@ -104,6 +104,14 @@ parse_room_account_data_events( } } +void +compose_timeline_events(json &events, + const std::vector<mtx::events::collections::TimelineEvents> &container) +{ + const auto c = container.at(0); + events = boost::apply_visitor(TimelineEventVisitor(), c); +} + void parse_timeline_events(const json &events, std::vector<mtx::events::collections::TimelineEvents> &container) diff --git a/lib/structs/responses/notifications.cpp b/lib/structs/responses/notifications.cpp index 9bd214b0231fa859bad1fdc3e285712788122dde..5a132fe474263b417915fba31e7bced85dcec70a 100644 --- a/lib/structs/responses/notifications.cpp +++ b/lib/structs/responses/notifications.cpp @@ -34,11 +34,47 @@ from_json(const json &obj, Notification &res) res.event = tmp.at(0); } +void +to_json(json &obj, const Notification &res) +{ + obj["actions"] = res.actions; + obj["read"] = res.read; + obj["room_id"] = res.room_id; + obj["ts"] = res.ts; + + // HACK to work around the fact that there isn't + // a method to parse a timeline event from a json object. + // + // TODO: Create method that retrieves a TimelineEvents variant from a json object. + // Ideally with an optional type to indicate failure. + std::vector<events::collections::TimelineEvents> tmp; + tmp.reserve(1); + + json arr; + tmp.push_back(res.event); + + utils::compose_timeline_events(arr, tmp); + + if (!tmp.empty()) { + obj["event"] = arr; + } + + if (!res.profile_tag.empty()) { + obj["profile_tag"] = res.profile_tag; + } +} + void from_json(const json &obj, Notifications &res) { // res.next_token = obj.at("next_token").get<std::string>(); res.notifications = obj.at("notifications").get<std::vector<Notification>>(); } + +void +to_json(json &obj, const Notifications ¬if) +{ + obj["notifications"] = notif.notifications; +} } // namespace responses } // namespace mtx