contact4 refactor
This commit is contained in:
parent
e55fb46027
commit
2a3828b30d
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <solanaceae/contact/contact_model3.hpp>
|
||||
#include <solanaceae/contact/fwd.hpp>
|
||||
|
||||
#include "./message.hpp"
|
||||
|
||||
@ -8,6 +8,10 @@
|
||||
|
||||
#include <entt/container/dense_map.hpp>
|
||||
|
||||
#include <entt/entity/entity.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include <entt/entity/handle.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@ -15,11 +19,11 @@
|
||||
namespace Message::Components {
|
||||
|
||||
struct ContactFrom {
|
||||
Contact3 c{entt::null};
|
||||
Contact4 c{entt::null};
|
||||
};
|
||||
|
||||
struct ContactTo {
|
||||
Contact3 c{entt::null};
|
||||
Contact4 c{entt::null};
|
||||
};
|
||||
|
||||
// best guess as to how this should be displayed in the global order
|
||||
@ -48,18 +52,18 @@ namespace Message::Components {
|
||||
struct ReceivedBy {
|
||||
// Due to a lack of info with some protocols,
|
||||
// this is often the timestamp we heard they already have the message.
|
||||
entt::dense_map<Contact3, uint64_t> ts;
|
||||
entt::dense_map<Contact4, uint64_t> ts;
|
||||
};
|
||||
|
||||
struct ReadBy {
|
||||
entt::dense_map<Contact3, uint64_t> ts;
|
||||
entt::dense_map<Contact4, uint64_t> ts;
|
||||
};
|
||||
|
||||
// similar to ReceivedBy, but only set when they sent the message
|
||||
// (efficent protocols have 1 contact in here)
|
||||
struct SyncedBy {
|
||||
// ts is not updated once set
|
||||
entt::dense_map<Contact3, uint64_t> ts;
|
||||
entt::dense_map<Contact4, uint64_t> ts;
|
||||
};
|
||||
|
||||
struct MessageText {
|
||||
|
@ -5,8 +5,8 @@
|
||||
#include <solanaceae/util/time.hpp>
|
||||
#include <solanaceae/message3/components.hpp>
|
||||
#include <solanaceae/contact/components.hpp>
|
||||
#include <solanaceae/contact/contact_store_i.hpp>
|
||||
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
@ -14,11 +14,11 @@
|
||||
#include <cassert>
|
||||
|
||||
MessageCommandDispatcher::MessageCommandDispatcher(
|
||||
Contact3Registry& cr,
|
||||
ContactStore4I& cs,
|
||||
RegistryMessageModelI& rmm,
|
||||
ConfigModelI& conf
|
||||
) :
|
||||
_cr(cr), _rmm(rmm), _conf(conf), _program_started_at(getTimeMS())
|
||||
_cs(cs), _rmm(rmm), _conf(conf), _program_started_at(getTimeMS())
|
||||
{
|
||||
// overwrite default admin and moderator to false
|
||||
_conf.set("MessageCommandDispatcher", "admin", false);
|
||||
@ -177,13 +177,14 @@ bool MessageCommandDispatcher::helpCommand(std::string_view params, Message3Hand
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MessageCommandDispatcher::hasPermission(const Command& cmd, const Contact3 contact) {
|
||||
if (!_cr.all_of<Contact::Components::ID>(contact)) {
|
||||
bool MessageCommandDispatcher::hasPermission(const Command& cmd, const Contact4 contact) {
|
||||
const auto& reg = _cs.registry();
|
||||
if (!reg.all_of<Contact::Components::ID>(contact)) {
|
||||
std::cerr << "MCD error: contact without ID\n";
|
||||
return false; // default to false
|
||||
}
|
||||
|
||||
const auto id_str = bin2hex(_cr.get<Contact::Components::ID>(contact).data);
|
||||
const auto id_str = bin2hex(reg.get<Contact::Components::ID>(contact).data);
|
||||
std::cout << "MCD: perm check for id '" << id_str << "'\n";
|
||||
|
||||
// TODO: blacklist here
|
||||
@ -253,14 +254,16 @@ bool MessageCommandDispatcher::onEvent(const Message::Events::MessageConstruct&
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto& reg = _cs.registry();
|
||||
|
||||
// skip unrelyable synced
|
||||
if (e.e.all_of<Message::Components::SyncedBy>()) {
|
||||
const auto& list = e.e.get<Message::Components::SyncedBy>().ts;
|
||||
if (
|
||||
std::find_if(
|
||||
list.cbegin(), list.cend(),
|
||||
[this](const auto&& it) {
|
||||
return _cr.any_of<
|
||||
[this, ®](const auto&& it) {
|
||||
return reg.any_of<
|
||||
Contact::Components::TagSelfStrong,
|
||||
Contact::Components::TagSelfWeak // trust weak self
|
||||
>(it.first);
|
||||
@ -273,7 +276,7 @@ bool MessageCommandDispatcher::onEvent(const Message::Events::MessageConstruct&
|
||||
}
|
||||
}
|
||||
|
||||
const bool is_private = _cr.any_of<Contact::Components::TagSelfWeak, Contact::Components::TagSelfStrong>(e.e.get<Message::Components::ContactTo>().c);
|
||||
const bool is_private = reg.any_of<Contact::Components::TagSelfWeak, Contact::Components::TagSelfStrong>(e.e.get<Message::Components::ContactTo>().c);
|
||||
|
||||
if (is_private) {
|
||||
// check for command prefix
|
||||
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <solanaceae/contact/fwd.hpp>
|
||||
#include <solanaceae/message3/registry_message_model.hpp>
|
||||
#include <solanaceae/contact/contact_model3.hpp>
|
||||
|
||||
#include <deque>
|
||||
#include <string>
|
||||
@ -13,7 +13,7 @@
|
||||
struct ConfigModelI;
|
||||
|
||||
class MessageCommandDispatcher : public RegistryMessageModelEventI {
|
||||
Contact3Registry& _cr;
|
||||
ContactStore4I& _cs;
|
||||
RegistryMessageModelI& _rmm;
|
||||
ConfigModelI& _conf;
|
||||
|
||||
@ -48,7 +48,7 @@ class MessageCommandDispatcher : public RegistryMessageModelEventI {
|
||||
std::unordered_map<std::string, Command> _command_map;
|
||||
|
||||
struct QueuedMessage {
|
||||
Contact3 to;
|
||||
Contact4 to;
|
||||
std::string message;
|
||||
};
|
||||
std::deque<QueuedMessage> _message_queue;
|
||||
@ -56,7 +56,7 @@ class MessageCommandDispatcher : public RegistryMessageModelEventI {
|
||||
uint64_t _program_started_at {0};
|
||||
|
||||
public:
|
||||
MessageCommandDispatcher(Contact3Registry& cr, RegistryMessageModelI& rmm, ConfigModelI& conf);
|
||||
MessageCommandDispatcher(ContactStore4I& cs, RegistryMessageModelI& rmm, ConfigModelI& conf);
|
||||
~MessageCommandDispatcher(void);
|
||||
|
||||
float iterate(float time_delta);
|
||||
@ -78,7 +78,7 @@ class MessageCommandDispatcher : public RegistryMessageModelEventI {
|
||||
// generates a help
|
||||
bool helpCommand(std::string_view params, Message3Handle m);
|
||||
|
||||
bool hasPermission(const Command& cmd, const Contact3 contact);
|
||||
bool hasPermission(const Command& cmd, const Contact4 contact);
|
||||
|
||||
protected: // mm
|
||||
bool onEvent(const Message::Events::MessageConstruct& e) override;
|
||||
|
@ -1,8 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <solanaceae/contact/contact_model3.hpp>
|
||||
#include <solanaceae/contact/fwd.hpp>
|
||||
#include <solanaceae/object_store/fwd.hpp>
|
||||
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include <entt/entity/handle.hpp>
|
||||
|
||||
// TODO: move, rename, do something?, change in favor of tox?
|
||||
//enum class FileKind : uint32_t {
|
||||
//file = 0u,
|
||||
@ -15,11 +18,11 @@ struct MessageModel3I {
|
||||
|
||||
// return true if a handler was found for the contact
|
||||
|
||||
virtual bool sendText(const Contact3 c, std::string_view message, bool action = false) { (void)c,(void)message,(void)action; return false; }
|
||||
virtual bool sendText(const Contact4 c, std::string_view message, bool action = false) { (void)c,(void)message,(void)action; return false; }
|
||||
|
||||
//virtual bool sendFile(const Contact& c, std::string_view file_name, std::unique_ptr<FileI> file) { (void)c,(void)message,(void)action; return false; }
|
||||
virtual bool sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) { (void)c,(void)file_name,(void)file_path; return false; }
|
||||
virtual bool sendFileObj(const Contact3 c, ObjectHandle o) { (void)c,(void)o; return false; } // ideal for forwarding
|
||||
virtual bool sendFilePath(const Contact4 c, std::string_view file_name, std::string_view file_path) { (void)c,(void)file_name,(void)file_path; return false; }
|
||||
virtual bool sendFileObj(const Contact4 c, ObjectHandle o) { (void)c,(void)o; return false; } // ideal for forwarding
|
||||
|
||||
// we want this back :)
|
||||
//virtual bool sendFileMem(const Contact& c, std::string_view file_name, const std::vector<uint8_t>& file) = 0;
|
||||
|
@ -64,12 +64,12 @@ using RegistryMessageModelEventProviderI = EventProviderI<RegistryMessageModelEv
|
||||
|
||||
class RegistryMessageModelI : public RegistryMessageModelEventProviderI, public MessageModel3I {
|
||||
public:
|
||||
static constexpr const char* version {"3"};
|
||||
static constexpr const char* version {"4"};
|
||||
|
||||
// rmm interface
|
||||
public:
|
||||
virtual Message3Registry* get(Contact3 c) = 0;
|
||||
virtual Message3Registry* get(Contact3 c) const = 0;
|
||||
virtual Message3Registry* get(Contact4 c) = 0;
|
||||
virtual Message3Registry* get(Contact4 c) const = 0;
|
||||
|
||||
public: // dispatcher
|
||||
// !!! remember to manually throw these externally
|
||||
@ -81,9 +81,9 @@ class RegistryMessageModelI : public RegistryMessageModelEventProviderI, public
|
||||
void throwEventUpdate(Message3Handle h) { throwEventUpdate(*h.registry(), h.entity()); }
|
||||
void throwEventDestroy(Message3Handle h) { throwEventDestroy(*h.registry(), h.entity()); }
|
||||
|
||||
virtual void throwEventConstruct(const Contact3 c, Message3 e) = 0;
|
||||
virtual void throwEventUpdate(const Contact3 c, Message3 e) = 0;
|
||||
virtual void throwEventDestroy(const Contact3 c, Message3 e) = 0;
|
||||
virtual void throwEventConstruct(const Contact4 c, Message3 e) = 0;
|
||||
virtual void throwEventUpdate(const Contact4 c, Message3 e) = 0;
|
||||
virtual void throwEventDestroy(const Contact4 c, Message3 e) = 0;
|
||||
};
|
||||
|
||||
template<>
|
||||
|
@ -1,19 +1,21 @@
|
||||
#include "./registry_message_model_impl.hpp"
|
||||
|
||||
#include <solanaceae/contact/components.hpp>
|
||||
#include <solanaceae/contact/contact_store_i.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Message3Registry* RegistryMessageModelImpl::get(Contact3 c) {
|
||||
if (_cr.valid(c) && !_cr.all_of<Contact::Components::TagBig>(c)) {
|
||||
Message3Registry* RegistryMessageModelImpl::get(Contact4 c) {
|
||||
const auto& reg = _cs.registry();
|
||||
if (reg.valid(c) && !reg.all_of<Contact::Components::TagBig>(c)) {
|
||||
// TODO: loop upwards
|
||||
if (!_cr.all_of<Contact::Components::Parent>(c)) {
|
||||
if (!reg.all_of<Contact::Components::Parent>(c)) {
|
||||
return nullptr;
|
||||
}
|
||||
c = _cr.get<Contact::Components::Parent>(c).parent;
|
||||
c = reg.get<Contact::Components::Parent>(c).parent;
|
||||
}
|
||||
|
||||
if (!_cr.valid(c)) {
|
||||
if (!reg.valid(c)) {
|
||||
// TODO: throw error
|
||||
return nullptr;
|
||||
}
|
||||
@ -24,20 +26,21 @@ Message3Registry* RegistryMessageModelImpl::get(Contact3 c) {
|
||||
}
|
||||
|
||||
auto& reg_sh = _contact_messages[c] = std::make_unique<Message3Registry>();
|
||||
reg_sh->ctx().emplace<Contact3>(c);
|
||||
reg_sh->ctx().emplace<Contact4>(c);
|
||||
return reg_sh.get();
|
||||
}
|
||||
|
||||
Message3Registry* RegistryMessageModelImpl::get(Contact3 c) const {
|
||||
if (_cr.valid(c) && !_cr.all_of<Contact::Components::TagBig>(c)) {
|
||||
Message3Registry* RegistryMessageModelImpl::get(Contact4 c) const {
|
||||
const auto& reg = _cs.registry();
|
||||
if (reg.valid(c) && !reg.all_of<Contact::Components::TagBig>(c)) {
|
||||
// TODO: loop upwards
|
||||
if (!_cr.all_of<Contact::Components::Parent>(c)) {
|
||||
if (!reg.all_of<Contact::Components::Parent>(c)) {
|
||||
return nullptr;
|
||||
}
|
||||
c = _cr.get<Contact::Components::Parent>(c).parent;
|
||||
c = reg.get<Contact::Components::Parent>(c).parent;
|
||||
}
|
||||
|
||||
if (!_cr.valid(c)) {
|
||||
if (!reg.valid(c)) {
|
||||
// TODO: throw error
|
||||
return nullptr;
|
||||
}
|
||||
@ -90,25 +93,25 @@ void RegistryMessageModelImpl::throwEventDestroy(Message3Registry& reg, Message3
|
||||
);
|
||||
}
|
||||
|
||||
void RegistryMessageModelImpl::throwEventConstruct(const Contact3 c, Message3 e) {
|
||||
void RegistryMessageModelImpl::throwEventConstruct(const Contact4 c, Message3 e) {
|
||||
if (auto* reg_ptr = get(c); reg_ptr) {
|
||||
throwEventConstruct(*reg_ptr, e);
|
||||
}
|
||||
}
|
||||
|
||||
void RegistryMessageModelImpl::throwEventUpdate(const Contact3 c, Message3 e) {
|
||||
void RegistryMessageModelImpl::throwEventUpdate(const Contact4 c, Message3 e) {
|
||||
if (auto* reg_ptr = get(c); reg_ptr) {
|
||||
throwEventUpdate(*reg_ptr, e);
|
||||
}
|
||||
}
|
||||
|
||||
void RegistryMessageModelImpl::throwEventDestroy(const Contact3 c, Message3 e) {
|
||||
void RegistryMessageModelImpl::throwEventDestroy(const Contact4 c, Message3 e) {
|
||||
if (auto* reg_ptr = get(c); reg_ptr) {
|
||||
throwEventDestroy(*reg_ptr, e);
|
||||
}
|
||||
}
|
||||
|
||||
bool RegistryMessageModelImpl::sendText(const Contact3 c, std::string_view message, bool action) {
|
||||
bool RegistryMessageModelImpl::sendText(const Contact4 c, std::string_view message, bool action) {
|
||||
std::cout << "RMM debug: event send text\n";
|
||||
|
||||
// manual, bc its not an "event"
|
||||
@ -123,7 +126,7 @@ bool RegistryMessageModelImpl::sendText(const Contact3 c, std::string_view messa
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RegistryMessageModelImpl::sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) {
|
||||
bool RegistryMessageModelImpl::sendFilePath(const Contact4 c, std::string_view file_name, std::string_view file_path) {
|
||||
std::cout << "RMM debug: event send file path\n";
|
||||
|
||||
// manual, bc its not an "event"
|
||||
|
@ -2,30 +2,28 @@
|
||||
|
||||
#include "./registry_message_model.hpp"
|
||||
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include <entt/entity/handle.hpp>
|
||||
#include <entt/container/dense_map.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class RegistryMessageModelImpl : public RegistryMessageModelI {
|
||||
protected:
|
||||
Contact3Registry& _cr;
|
||||
ContactStore4I& _cs;
|
||||
|
||||
entt::dense_map<Contact3, std::unique_ptr<Message3Registry>> _contact_messages;
|
||||
entt::dense_map<Contact4, std::unique_ptr<Message3Registry>> _contact_messages;
|
||||
|
||||
bool _update_in_progess {false};
|
||||
std::vector<Message3Handle> _update_queue {};
|
||||
|
||||
public:
|
||||
RegistryMessageModelImpl(Contact3Registry& cr) : _cr(cr) {}
|
||||
RegistryMessageModelImpl(ContactStore4I& cs) : _cs(cs) {}
|
||||
virtual ~RegistryMessageModelImpl(void) {}
|
||||
|
||||
// TODO: iterate?
|
||||
|
||||
public:
|
||||
Message3Registry* get(Contact3 c) override;
|
||||
Message3Registry* get(Contact3 c) const override;
|
||||
Message3Registry* get(Contact4 c) override;
|
||||
Message3Registry* get(Contact4 c) const override;
|
||||
|
||||
public: // dispatcher
|
||||
// !!! remember to manually throw these externally
|
||||
@ -33,12 +31,12 @@ class RegistryMessageModelImpl : public RegistryMessageModelI {
|
||||
void throwEventUpdate(Message3Registry& reg, Message3 e) override;
|
||||
void throwEventDestroy(Message3Registry& reg, Message3 e) override;
|
||||
|
||||
void throwEventConstruct(const Contact3 c, Message3 e) override;
|
||||
void throwEventUpdate(const Contact3 c, Message3 e) override;
|
||||
void throwEventDestroy(const Contact3 c, Message3 e) override;
|
||||
void throwEventConstruct(const Contact4 c, Message3 e) override;
|
||||
void throwEventUpdate(const Contact4 c, Message3 e) override;
|
||||
void throwEventDestroy(const Contact4 c, Message3 e) override;
|
||||
|
||||
public: // mm3
|
||||
bool sendText(const Contact3 c, std::string_view message, bool action = false) override;
|
||||
bool sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) override;
|
||||
bool sendText(const Contact4 c, std::string_view message, bool action = false) override;
|
||||
bool sendFilePath(const Contact4 c, std::string_view file_name, std::string_view file_path) override;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user