2020-01-05 00:17:49 -08:00

168 lines
4.5 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/>.
*/
#ifndef MATRIX_EVENT_HPP
#define MATRIX_EVENT_HPP
#include "raii/rjp_ptr.hpp"
#include "raii/string_base.hpp"
#include "raii/rjp_string.hpp"
namespace matrix::sync{
/*
Base class for events returned by a sync
*/
class event
{
protected:
RJP_value* m_event;
RJP_value* m_type;
RJP_value* m_content;
public:
explicit event(RJP_value* ev);
event(const event&) = delete; //TODO
event(event&&)noexcept;
~event(void) = default;
event& operator=(const event&) = default;
raii::static_string type(void)const&;
raii::rjp_string type(void)&&;
const RJP_value* content(void)const;
RJP_value* content(void);
const RJP_value* raw(void)const;
RJP_value* raw(void);
};
/*
Base class for events associated with a room returned by a sync
*/
class room_event_base
{
protected:
const raii::static_string m_roomid;
public:
room_event_base(const room_event_base&) = delete;
constexpr room_event_base(room_event_base&&) = default;
constexpr room_event_base(const raii::string_base& roomid):
m_roomid(roomid.get(), roomid.length()){}
constexpr const raii::static_string& roomid(void)const{
return m_roomid;
}
};
/*
Class to represent a single event associated with a specific room.
*/
class room_event : public room_event_base, public event
{
protected:
RJP_value* m_id;
RJP_value* m_sender;
RJP_value* m_unsigned;
RJP_value* m_redacts;
int m_origin_server_ts;
public:
room_event(RJP_value* ev, const raii::string_base& roomid);
room_event(const room_event&) = delete; //TODO
room_event(room_event&&)noexcept;
raii::static_string eventid(void)const&;
raii::rjp_string eventid(void)&&;
raii::static_string sender(void)const&;
raii::rjp_string sender(void)&&;
int origin_server_ts(void)const;
const RJP_value* extra(void)const;
RJP_value* extra(void);
//only for m.room.redacts events
raii::static_string redacts(void)const&;
raii::rjp_string redacts(void)&&;
};
/*
Class to represent a single state event associated with a specific room
*/
class room_state_event : public room_event
{
protected:
RJP_value* m_state_key;
RJP_value* m_prev_content;
public:
room_state_event(RJP_value* ev, const raii::string_base& roomid);
room_state_event(const room_state_event&) = delete; //TODO
room_state_event(room_state_event&&)noexcept;
raii::static_string state_key(void)const&;
raii::rjp_string state_key(void)&&;
raii::static_string prev_content(void)const&;
raii::rjp_string prev_content(void)&&;
};
/*
Class to represent a single ephemeral event associated with a specific room
*/
class room_ephemeral_event : public room_event_base, public event
{
public:
room_ephemeral_event(RJP_value* ev, const raii::string_base& roomid);
room_ephemeral_event(const room_ephemeral_event&) = delete; //TODO
room_ephemeral_event(room_ephemeral_event&&) = default;
};
//Specialized event for room messages
class room_message_event : public room_event
{
protected:
RJP_value* m_body;
RJP_value* m_type;
public:
room_message_event(RJP_value* ev, const raii::string_base& roomid);
room_message_event(const room_message_event&) = delete;
room_message_event(room_message_event&&);
room_message_event(room_event&&)noexcept;
raii::static_string body(void)const&;
raii::rjp_string body(void)&&;
raii::static_string msgtype(void)const&;
raii::rjp_string msgtype(void)&&;
};
}
namespace matrix{
namespace detail{
template<class>
struct uninstantiated_false{
static constexpr bool value = false;
};
}
template<class To, class From, bool = std::is_rvalue_reference<From&&>::value>
To event_cast(From&&){
static_assert(detail::uninstantiated_false<From>::value);
}
template<>
matrix::sync::room_message_event event_cast<matrix::sync::room_message_event, matrix::sync::room_state_event, true>(matrix::sync::room_state_event&& from);
}
#endif