/**
This file is a part of rexy's matrix client
Copyright (C) 2019-2020 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
#include "raii/rjp_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){}
rexy::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:
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;
}
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:
room_list(RJP_value* room):m_room(room){}
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;
}
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{
RJP_array_iterator it;
RJP_value* ev = rjp_search_member(m_root, "left");
rjp_init_array_iterator(&it, ev);
return rjp_array_iterator_current(&it);
}
event_list changed(void)const{
RJP_array_iterator it;
RJP_value* ev = rjp_search_member(m_root, "changed");
rjp_init_array_iterator(&it, ev);
return rjp_array_iterator_current(&it);
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/*
Class returned by a sync. Makes for theoretically easier processing than a pure raw response
*/
class client_response
{
private:
raii::rjp_ptr m_root = nullptr;
public:
constexpr client_response(void) = default;
client_response(const rexy::string_base& src);
client_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;
rexy::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