port to contact4, does not implement create/leave yet

This commit is contained in:
Green Sky
2025-03-06 18:56:42 +01:00
parent dd7eb2702f
commit 24bbf2a338
8 changed files with 423 additions and 301 deletions

View File

@ -1,7 +1,5 @@
#pragma once
#include <solanaceae/contact/contact_model3.hpp>
#include <cstdint>
namespace Message::Components {

View File

@ -3,6 +3,7 @@
#include <solanaceae/util/time.hpp>
#include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/contact/contact_store_i.hpp>
#include <solanaceae/contact/components.hpp>
#include <solanaceae/tox_contacts/components.hpp>
@ -17,14 +18,14 @@
ToxMessageManager::ToxMessageManager(
RegistryMessageModelI& rmm,
Contact3Registry& cr,
ContactStore4I& cs,
ToxContactModel2& tcm,
ToxI& t,
ToxEventProviderI& tep
) :
_rmm(rmm),
_rmm_sr(_rmm.newSubRef(this)),
_cr(cr),
_cs(cs),
_tcm(tcm),
_t(t),
_tep_sr(tep.newSubRef(this))
@ -52,8 +53,9 @@ ToxMessageManager::ToxMessageManager(
ToxMessageManager::~ToxMessageManager(void) {
}
bool ToxMessageManager::sendText(const Contact3 c, std::string_view message, bool action) {
if (!_cr.valid(c)) {
bool ToxMessageManager::sendText(const Contact4 c, std::string_view message, bool action) {
const auto& cr = _cs.registry();
if (!cr.valid(c)) {
return false;
}
@ -61,12 +63,12 @@ bool ToxMessageManager::sendText(const Contact3 c, std::string_view message, boo
return false; // TODO: empty messages allowed?
}
if (_cr.all_of<Contact::Components::TagSelfStrong>(c)) {
if (cr.all_of<Contact::Components::TagSelfStrong>(c)) {
return false; // message to self? not with tox
}
// testing for persistent is enough
if (!_cr.any_of<
if (!cr.any_of<
Contact::Components::ToxFriendPersistent,
// TODO: conf
Contact::Components::ToxGroupPersistent,
@ -82,11 +84,11 @@ bool ToxMessageManager::sendText(const Contact3 c, std::string_view message, boo
Message3Registry& reg = *reg_ptr;
if (!_cr.all_of<Contact::Components::Self>(c)) {
if (!cr.all_of<Contact::Components::Self>(c)) {
std::cerr << "TMM error: cant get self\n";
return false;
}
const Contact3 c_self = _cr.get<Contact::Components::Self>(c).self;
const Contact4 c_self = cr.get<Contact::Components::Self>(c).self;
// get current time unix epoch utc
uint64_t ts = getTimeMS();
@ -114,8 +116,8 @@ bool ToxMessageManager::sendText(const Contact3 c, std::string_view message, boo
reg.emplace<Message::Components::ReceivedBy>(new_msg_e).ts[c_self] = ts;
if (_cr.any_of<Contact::Components::ToxFriendEphemeral>(c)) {
const uint32_t friend_number = _cr.get<Contact::Components::ToxFriendEphemeral>(c).friend_number;
if (cr.any_of<Contact::Components::ToxFriendEphemeral>(c)) {
const uint32_t friend_number = cr.get<Contact::Components::ToxFriendEphemeral>(c).friend_number;
auto [res, _] = _t.toxFriendSendMessage(
friend_number,
@ -132,16 +134,16 @@ bool ToxMessageManager::sendText(const Contact3 c, std::string_view message, boo
} else {
reg.emplace<Message::Components::ToxFriendMessageID>(new_msg_e, res.value());
}
} else if (_cr.any_of<Contact::Components::ToxFriendPersistent>(c)) {
} else if (cr.any_of<Contact::Components::ToxFriendPersistent>(c)) {
// here we just assume friend not online (no ephemeral id)
// set manually, so it can still be synced
const uint32_t msg_id = randombytes_random();
reg.emplace<Message::Components::ToxFriendMessageID>(new_msg_e, msg_id);
std::cerr << "TMM: failed to send friend message, offline and not in tox profile\n";
} else if (
_cr.any_of<Contact::Components::ToxGroupEphemeral>(c)
cr.any_of<Contact::Components::ToxGroupEphemeral>(c)
) {
const uint32_t group_number = _cr.get<Contact::Components::ToxGroupEphemeral>(c).group_number;
const uint32_t group_number = cr.get<Contact::Components::ToxGroupEphemeral>(c).group_number;
auto [message_id_opt, _] = _t.toxGroupSendMessage(
group_number,
@ -164,7 +166,7 @@ bool ToxMessageManager::sendText(const Contact3 c, std::string_view message, boo
}
} else if (
// non online group
_cr.any_of<Contact::Components::ToxGroupPersistent>(c)
cr.any_of<Contact::Components::ToxGroupPersistent>(c)
) {
// create msg_id
const uint32_t msg_id = randombytes_random();
@ -173,9 +175,9 @@ bool ToxMessageManager::sendText(const Contact3 c, std::string_view message, boo
// TODO: generalize?
reg.emplace<Message::Components::SyncedBy>(new_msg_e).ts.emplace(c_self, ts);
} else if (
_cr.any_of<Contact::Components::ToxGroupPeerEphemeral>(c)
cr.any_of<Contact::Components::ToxGroupPeerEphemeral>(c)
) {
const auto& numbers = _cr.get<Contact::Components::ToxGroupPeerEphemeral>(c);
const auto& numbers = cr.get<Contact::Components::ToxGroupPeerEphemeral>(c);
auto [message_id_opt, _] = _t.toxGroupSendPrivateMessage(
numbers.group_number,

View File

@ -11,19 +11,20 @@ class ToxMessageManager : public RegistryMessageModelEventI, public ToxEventI {
protected:
RegistryMessageModelI& _rmm;
RegistryMessageModelI::SubscriptionReference _rmm_sr;
Contact3Registry& _cr;
ContactStore4I& _cs;
ToxContactModel2& _tcm;
ToxI& _t;
ToxEventProviderI::SubscriptionReference _tep_sr;
public:
ToxMessageManager(RegistryMessageModelI& rmm, Contact3Registry& cr, ToxContactModel2& tcm, ToxI& t, ToxEventProviderI& tep);
ToxMessageManager(RegistryMessageModelI& rmm, ContactStore4I& cs, ToxContactModel2& tcm, ToxI& t, ToxEventProviderI& tep);
virtual ~ToxMessageManager(void);
public: // mm3
bool sendText(const Contact3 c, std::string_view message, bool action = false) override;
bool sendText(const Contact4 c, std::string_view message, bool action = false) override;
protected: // tox events
// TODO: add friend request message handling
bool onToxEvent(const Tox_Event_Friend_Message* e) override;
bool onToxEvent(const Tox_Event_Friend_Read_Receipt* e) override;

View File

@ -3,6 +3,7 @@
#include <solanaceae/util/time.hpp>
#include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/contact/contact_store_i.hpp>
#include <solanaceae/file/file2_std.hpp>
@ -11,8 +12,6 @@
#include <solanaceae/message3/components.hpp>
#include "./msg_components.hpp"
#include "./obj_components.hpp"
#include "solanaceae/object_store/meta_components.hpp"
#include "solanaceae/util/span.hpp"
#include <sodium.h>
@ -81,12 +80,12 @@ ObjectHandle ToxTransferManager::toxFriendLookupReceiving(const uint32_t friend_
ToxTransferManager::ToxTransferManager(
RegistryMessageModelI& rmm,
Contact3Registry& cr,
ContactStore4I& cs,
ToxContactModel2& tcm,
ToxI& t,
ToxEventProviderI& tep,
ObjectStore2& os
) : _rmm(rmm), _rmm_sr(_rmm.newSubRef(this)), _cr(cr), _tcm(tcm), _t(t), _tep_sr(tep.newSubRef(this)), _os(os), _os_sr(_os.newSubRef(this)), _ftb(os) {
) : _rmm(rmm), _rmm_sr(_rmm.newSubRef(this)), _cs(cs), _tcm(tcm), _t(t), _tep_sr(tep.newSubRef(this)), _os(os), _os_sr(_os.newSubRef(this)), _ftb(os) {
_tep_sr
.subscribe(Tox_Event_Type::TOX_EVENT_FRIEND_CONNECTION_STATUS)
.subscribe(Tox_Event_Type::TOX_EVENT_FILE_RECV)
@ -110,10 +109,11 @@ void ToxTransferManager::iterate(void) {
// TODO: time out transfers
}
Message3Handle ToxTransferManager::toxSendFilePath(const Contact3 c, uint32_t file_kind, std::string_view file_name, std::string_view file_path, std::vector<uint8_t> file_id) {
Message3Handle ToxTransferManager::toxSendFilePath(const Contact4 c, uint32_t file_kind, std::string_view file_name, std::string_view file_path, std::vector<uint8_t> file_id) {
const auto& cr = _cs.registry();
if (
// TODO: add support of offline queuing
!_cr.all_of<Contact::Components::ToxFriendEphemeral>(c)
!cr.all_of<Contact::Components::ToxFriendEphemeral>(c)
) {
std::cerr << "TTM error: unsupported contact type\n";
return {};
@ -142,8 +142,8 @@ Message3Handle ToxTransferManager::toxSendFilePath(const Contact3 c, uint32_t fi
}
assert(file_id.size() == 32);
const auto c_self = _cr.get<Contact::Components::Self>(c).self;
if (!_cr.valid(c_self)) {
const auto c_self = cr.get<Contact::Components::Self>(c).self;
if (!cr.valid(c_self)) {
std::cerr << "TTM error: failed to get self!\n";
return {};
}
@ -173,7 +173,7 @@ Message3Handle ToxTransferManager::toxSendFilePath(const Contact3 c, uint32_t fi
msg.emplace<Message::Components::ReceivedBy>().ts.try_emplace(c_self, ts);
msg.emplace<Message::Components::MessageFileObject>(o);
const auto friend_number = _cr.get<Contact::Components::ToxFriendEphemeral>(c).friend_number;
const auto friend_number = cr.get<Contact::Components::ToxFriendEphemeral>(c).friend_number;
const auto&& [transfer_id, err] = _t.toxFileSend(friend_number, file_kind, file_impl->_file_size, file_id, file_name);
if (err == TOX_ERR_FILE_SEND_OK) {
assert(transfer_id.has_value());
@ -383,10 +383,11 @@ bool ToxTransferManager::accept(ObjectHandle transfer, std::string_view file_pat
return true;
}
bool ToxTransferManager::sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) {
bool ToxTransferManager::sendFilePath(const Contact4 c, std::string_view file_name, std::string_view file_path) {
const auto& cr = _cs.registry();
if (
// TODO: add support of offline queuing
!_cr.all_of<Contact::Components::ToxFriendEphemeral>(c)
!cr.all_of<Contact::Components::ToxFriendEphemeral>(c)
) {
// TODO: add support for persistant friend filesends
return false;
@ -504,7 +505,9 @@ bool ToxTransferManager::onToxEvent(const Tox_Event_File_Recv* e) {
// get current time unix epoch utc
uint64_t ts = getTimeMS();
auto self_c = _cr.get<Contact::Components::Self>(c).self;
const auto& cr = _cs.registry();
auto self_c = cr.get<Contact::Components::Self>(c).self;
o = _ftb.newObject(ByteSpan{f_id_opt.value()}, false);
@ -628,7 +631,7 @@ bool ToxTransferManager::onToxEvent(const Tox_Event_File_Recv_Chunk* e) {
auto c = _tcm.getContactFriend(friend_number);
if (static_cast<bool>(c)) {
auto self_c = _cr.get<Contact::Components::Self>(c).self;
auto self_c = c.get<Contact::Components::Self>().self;
auto& rb = msg.get_or_emplace<Message::Components::ReceivedBy>().ts;
rb.try_emplace(self_c, ts); // on completion
}

View File

@ -7,8 +7,9 @@
#include "./backends/tox_ft_filesystem.hpp"
// switch to fwd or remove
#include <solanaceae/file/file2.hpp>
//#include <solanaceae/file/file2.hpp>
// fwd
struct File2I;
#include <entt/container/dense_map.hpp>
@ -20,12 +21,12 @@ struct ToxI;
class ToxTransferManager : public RegistryMessageModelEventI, public ObjectStoreEventI, public ToxEventI {
public:
static constexpr const char* version {"2"};
static constexpr const char* version {"3"};
protected:
RegistryMessageModelI& _rmm;
RegistryMessageModelI::SubscriptionReference _rmm_sr;
Contact3Registry& _cr;
ContactStore4I& _cs;
ToxContactModel2& _tcm;
ToxI& _t;
ToxEventProviderI::SubscriptionReference _tep_sr;
@ -48,7 +49,7 @@ class ToxTransferManager : public RegistryMessageModelEventI, public ObjectStore
public:
ToxTransferManager(
RegistryMessageModelI& rmm,
Contact3Registry& cr,
ContactStore4I& cs,
ToxContactModel2& tcm,
ToxI& t,
ToxEventProviderI& tep,
@ -59,7 +60,7 @@ class ToxTransferManager : public RegistryMessageModelEventI, public ObjectStore
virtual void iterate(void);
public: // TODO: private?
Message3Handle toxSendFilePath(const Contact3 c, uint32_t file_kind, std::string_view file_name, std::string_view file_path, std::vector<uint8_t> file_id = {});
Message3Handle toxSendFilePath(const Contact4 c, uint32_t file_kind, std::string_view file_name, std::string_view file_path, std::vector<uint8_t> file_id = {});
bool resume(ObjectHandle transfer);
bool pause(ObjectHandle transfer);
@ -72,7 +73,7 @@ class ToxTransferManager : public RegistryMessageModelEventI, public ObjectStore
bool accept(ObjectHandle transfer, std::string_view file_path, bool path_is_file);
protected: // (r)mm
bool sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) override;
bool sendFilePath(const Contact4 c, std::string_view file_name, std::string_view file_path) override;
protected: // os
//bool onEvent(const ObjectStore::Events::ObjectConstruct&) override;