matrix_thing/src/matrix/sync_response.cpp
2019-10-02 08:46:58 -07:00

109 lines
3.9 KiB
C++

/**
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 <http://www.gnu.org/licenses/>.
*/
#include "matrix/sync_response.hpp"
namespace matrix::sync{
//Room event response
raii::static_string room_event_response::roomid(void)const{
return raii::static_string(rjp_member_name(m_room), rjp_member_name_length(m_room));
}
RJP_value* room_event_response::_find_event_list(const char* mname)const{
RJP_search_res res = rjp_search_member(m_room, mname, 0);
if(!res.value)
return nullptr;
res = rjp_search_member(res.value, "events", 0);
if(!res.value)
return nullptr;
return rjp_get_element(res.value);
}
room_event_list room_event_response::account_events(void){
return room_event_list(_find_event_list("account_data"), roomid());
}
room_ephem_event_list room_event_response::ephemeral_events(void){
return room_ephem_event_list(_find_event_list("ephemeral"), roomid());
}
room_state_event_list room_event_response::state_events(void){
return room_state_event_list(_find_event_list("state"), roomid());
}
room_state_event_list room_event_response::timeline_events(void){
return room_state_event_list(_find_event_list("timeline"), roomid());
}
RJP_value* room_event_response::notifications(void){
return rjp_search_member(m_room, "unread_notifications", 0).value;
}
RJP_value* room_event_response::summary(void){
return rjp_search_member(m_room, "summary", 0).value;
}
//Sync response
response::response(const raii::string_base& s):
m_root(rjp_parse(s)){}
response::response(RJP_value* root):
m_root(root){}
room_list response::room_join_events(void)const{
return _find_room_list("join");
}
room_list response::room_invite_events(void)const{
return _find_room_list("invite");
}
room_list response::room_leave_events(void)const{
return _find_room_list("leave");
}
device_list response::device_lists(void)const{
return rjp_search_member(m_root.get(), "device_lists", 0).value;
}
raii::static_string response::next_batch(void)const&{
RJP_value* nb = rjp_search_member(m_root.get(), "next_batch", 0).value;
if(!nb) return {};
return raii::static_string(rjp_value_string(nb), rjp_value_string_length(nb));
}
raii::rjp_string response::next_batch(void)&&{
return raii::rjp_string(rjp_search_member(m_root.get(), "next_batch", 0).value);
}
event_list response::to_device_events(void)const{
return rjp_search_member(rjp_search_member(m_root.get(), "to_device", 0).value, "events", 0).value;
}
event_list response::presence_events(void)const{
return rjp_search_member(rjp_search_member(m_root.get(), "presence", 0).value, "events", 0).value;
}
RJP_value* response::device_one_time_keys_count(void)const{
return rjp_search_member(m_root.get(), "device_one_time_keys_count", 0).value;
}
RJP_value* response::raw_handle(void){
return m_root.get();
}
const RJP_value* response::raw_handle(void)const{
return m_root.get();
}
raii::rjp_string response::raw_str(void)const{
return raii::rjp_string(rjp_to_json(m_root.get()));
}
RJP_value* response::_find_room_list(const char* segment)const{
RJP_search_res res = rjp_search_member(m_root.get(), "rooms", 0);
if(!res.value)
return nullptr;
res = rjp_search_member(res.value, segment, 0);
if(!res.value)
return nullptr;
return rjp_get_member(res.value);
}
}