/** This file is a part of rexy's matrix client Copyright (C) 2019 rexy712 This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ #ifndef SYNC_RESPONSE_HPP #define SYNC_RESPONSE_HPP #include "raii/rjp_string.hpp" #include "raii/static_string.hpp" #include "raii/rjp_iterator.hpp" #include "raii/rjp_ptr.hpp" #include "matrix/events.hpp" #include "matrix/iterable.hpp" namespace matrix::sync{ /////////////////////////////////////////////////////////////////////////////////////////////////// /* Class representing the "rooms" section of a sync response */ class room_event_response { private: RJP_value* m_room; public: constexpr room_event_response(RJP_value* room):m_room(room){} raii::static_string roomid(void)const; room_event_list account_events(void); room_ephem_event_list ephemeral_events(void); room_state_event_list state_events(void); room_state_event_list timeline_events(void); RJP_value* notifications(void); RJP_value* summary(void); private: RJP_value* _find_event_list(const char* mname)const; }; /* Class used to iterate over an array of rooms returned by a sync */ class room_iterator { private: raii::rjp_object_iterator m_room; public: constexpr room_iterator(RJP_value* r):m_room(r){} constexpr bool operator==(const room_iterator& r){ return r.m_room == m_room; } constexpr bool operator!=(const room_iterator& r){ return r.m_room != m_room; } room_iterator& operator++(void){ ++m_room; return *this; } constexpr room_event_response operator*(void){ return room_event_response(*m_room); } }; /* Class representing a list of rooms */ class room_list { public: using iterator = room_iterator; using const_iterator = const iterator; private: iterator m_room; public: constexpr room_list(RJP_value* room):m_room(room){} constexpr room_list(const room_list& r):m_room(r.m_room){} constexpr iterator& begin(void){ return m_room; } constexpr const_iterator& begin(void)const{ return m_room; } constexpr const_iterator end(void)const{ return const_iterator(nullptr); } }; class device_list { private: RJP_value* m_root; public: device_list(RJP_value* root): m_root(root){} event_list left(void)const{ return rjp_get_element(rjp_search_member(m_root, "left", 0).value); } event_list changed(void)const{ return rjp_get_element(rjp_search_member(m_root, "changed", 0).value); } }; /////////////////////////////////////////////////////////////////////////////////////////////////// /* Class returned by a sync. Makes for theoretically easier processing than a pure raw response */ class response { private: raii::rjp_ptr m_root = nullptr; public: constexpr response(void) = default; response(const raii::string_base& src); response(RJP_value* root); room_list room_join_events(void)const; room_list room_invite_events(void)const; room_list room_leave_events(void)const; device_list device_lists(void)const; raii::static_string next_batch(void)const&; raii::rjp_string next_batch(void)&&; event_list to_device_events(void)const; event_list presence_events(void)const; event_list account_data_events(void)const; //TODO room_list groups_join_events(void)const; room_list groups_leave_events(void)const; room_list group_invite_events(void)const; RJP_value* device_one_time_keys_count(void)const; RJP_value* raw_handle(void); const RJP_value* raw_handle(void)const; raii::rjp_string raw_str(void)const; private: RJP_value* _find_room_list(const char* segment)const; }; } #endif