continue transfer object refactor, re-enabling avatar receiving
Some checks failed
ContinuousDelivery / linux-ubuntu (push) Has been cancelled
ContinuousDelivery / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android-23]) (push) Has been cancelled
ContinuousDelivery / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android-23]) (push) Has been cancelled
ContinuousDelivery / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android-23]) (push) Has been cancelled
ContinuousDelivery / windows (push) Has been cancelled
ContinuousDelivery / windows-asan (push) Has been cancelled
ContinuousDelivery / dumpsyms (push) Has been cancelled
ContinuousDelivery / release (push) Has been cancelled
ContinuousIntegration / linux (push) Has been cancelled
ContinuousIntegration / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android-23]) (push) Has been cancelled
ContinuousIntegration / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android-23]) (push) Has been cancelled
ContinuousIntegration / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android-23]) (push) Has been cancelled
ContinuousIntegration / macos (push) Has been cancelled
ContinuousIntegration / windows (push) Has been cancelled

This commit is contained in:
Green Sky
2025-05-04 19:11:28 +02:00
parent 7021e092b1
commit 2654cd1b19
12 changed files with 390 additions and 69 deletions

View File

@@ -1,6 +1,6 @@
#include "./tox_avatar_manager.hpp"
// TODO: this whole thing needs a rewrite and is currently disabled
// TODO: this whole thing needs a rewrite, seperating tcs and rng uid os
#include <solanaceae/util/config_model.hpp>
@@ -33,11 +33,13 @@ namespace Components {
ToxAvatarManager::ToxAvatarManager(
ObjectStore2& os,
ContactStore4I& cs,
ConfigModelI& conf
) : _os(os), _os_sr(_os.newSubRef(this)), _cs(cs), _conf(conf) {
ConfigModelI& conf,
ToxContactModel2& tcm
) : _os(os), _os_sr(_os.newSubRef(this)), _cs(cs), _conf(conf), _tcm(tcm), _sb_tcs(os) {
_os_sr
.subscribe(ObjectStore_Event::object_construct)
.subscribe(ObjectStore_Event::object_update)
.subscribe(ObjectStore_Event::object_destroy)
;
if (!_conf.has_string("ToxAvatarManager", "save_path")) {
@@ -45,20 +47,21 @@ ToxAvatarManager::ToxAvatarManager(
_conf.set("ToxAvatarManager", "save_path", std::string_view{"tmp_avatar_dir"});
}
//_conf.set("TransferAutoAccept", "autoaccept_limit", int64_t(50l*1024l*1024l)); // sane default
const std::string_view avatar_save_path {_conf.get_string("ToxAvatarManager", "save_path").value()};
// make sure it exists
std::filesystem::create_directories(avatar_save_path);
// TODO: instead listen for new contacts, and attach
{ // scan tox contacts for cached avatars
// old sts says pubkey.png
_cs.registry().view<Contact::Components::ToxFriendPersistent>().each([this](auto c, const Contact::Components::ToxFriendPersistent& tox_pers) {
// try
addAvatarFileToContact(c, tox_pers.key);
});
_cs.registry().view<Contact::Components::ToxGroupPersistent>().each([this](auto c, const Contact::Components::ToxGroupPersistent& tox_pers) {
// try
addAvatarFileToContact(c, tox_pers.chat_id);
});
@@ -88,30 +91,70 @@ void ToxAvatarManager::iterate(void) {
std::string ToxAvatarManager::getAvatarPath(const ToxKey& key) const {
const std::string_view avatar_save_path {_conf.get_string("ToxAvatarManager", "save_path").value()};
const auto pub_key_string = bin2hex({key.data.cbegin(), key.data.cend()});
const auto file_path = std::filesystem::path(avatar_save_path) / (pub_key_string + ".png");
const auto file_path = std::filesystem::path(avatar_save_path) / getAvatarFileName(key);
return file_path.generic_u8string();
}
std::string ToxAvatarManager::getAvatarFileName(const ToxKey& key) const {
const auto pub_key_string = bin2hex({key.data.cbegin(), key.data.cend()});
return pub_key_string + ".png"; // TODO: remove png?
}
void ToxAvatarManager::addAvatarFileToContact(const Contact4 c, const ToxKey& key) {
const auto file_path = getAvatarPath(key);
if (std::filesystem::is_regular_file(file_path)) {
// avatar file png file exists
_cs.registry().emplace_or_replace<Contact::Components::AvatarFile>(c, file_path);
_cs.registry().emplace_or_replace<Contact::Components::TagAvatarInvalidate>(c);
_cs.throwEventUpdate(c);
if (!std::filesystem::is_regular_file(file_path)) {
return;
}
//std::cout << "TAM: found '" << file_path << "'\n";
// TODO: use guid instead
auto o = _sb_tcs.newObject(ByteSpan{key.data}, false);
o.emplace_or_replace<ObjComp::F::SingleInfoLocal>(file_path);
o.emplace_or_replace<ObjComp::F::TagLocalHaveAll>();
// for file size
o.emplace_or_replace<ObjComp::F::SingleInfo>(
getAvatarFileName(key),
std::filesystem::file_size(file_path)
);
_os.throwEventConstruct(o);
// avatar file "png" exists
//_cs.registry().emplace_or_replace<Contact::Components::AvatarFile>(c, file_path);
_cs.registry().emplace_or_replace<Contact::Components::AvatarObj>(c, o);
_cs.registry().emplace_or_replace<Contact::Components::TagAvatarInvalidate>(c);
_cs.throwEventUpdate(c);
}
void ToxAvatarManager::clearAvatarFromContact(const Contact4 c) {
auto& cr = _cs.registry();
if (cr.all_of<Contact::Components::AvatarFile>(c)) {
std::filesystem::remove(cr.get<Contact::Components::AvatarFile>(c).file_path);
cr.remove<Contact::Components::AvatarFile>(c);
if (cr.any_of<Contact::Components::AvatarFile, Contact::Components::AvatarObj>(c)) {
if (cr.all_of<Contact::Components::AvatarFile>(c)) {
std::filesystem::remove(cr.get<Contact::Components::AvatarFile>(c).file_path);
} else if (cr.all_of<Contact::Components::AvatarObj>(c)) {
auto o = _os.objectHandle(cr.get<Contact::Components::AvatarObj>(c).obj);
if (o) {
if (o.all_of<ObjComp::F::SingleInfoLocal>()) {
std::filesystem::remove(o.get<ObjComp::F::SingleInfoLocal>().file_path);
}
// TODO: make destruction more ergonomic
//_sb_tcs.destroy() ??
_os.throwEventDestroy(o);
o.destroy();
}
}
cr.remove<
Contact::Components::AvatarFile,
Contact::Components::AvatarObj
>(c);
cr.emplace_or_replace<Contact::Components::TagAvatarInvalidate>(c);
_cs.throwEventUpdate(c);
std::cout << "TAM: cleared avatar from " << entt::to_integral(c) << "\n";
}
}
@@ -131,13 +174,12 @@ void ToxAvatarManager::checkObj(ObjectHandle o) {
return;
}
// TODO: non tox code path?
if (!o.all_of<
ObjComp::Tox::TagIncomming,
//ObjComp::Ephemeral::Backend,
ObjComp::F::SingleInfo,
ObjComp::Tox::FileKind
// TODO: mesage? how do we know where a file is from??
//Message::Components::ContactFrom // should always be there, just making sure
ObjComp::Tox::FileKind,
ObjComp::Ephemeral::ToxContact
>()) {
return;
}
@@ -159,10 +201,14 @@ void ToxAvatarManager::checkObj(ObjectHandle o) {
return; // too large
}
#if 0 // TODO: make avatars work again !!!!!
auto contact = o.get<ObjComp::Ephemeral::ToxContact>().c;
if (!static_cast<bool>(contact)) {
std::cerr << "TAM error: failed to attribute object to contact\n";
}
// TCS-2.2.10
if (file_info.file_name.empty() || file_info.file_size == 0) {
std::cout << "TAM: no filename or filesize, deleting avatar\n";
// reset
clearAvatarFromContact(contact);
// TODO: cancel
@@ -175,30 +221,38 @@ void ToxAvatarManager::checkObj(ObjectHandle o) {
return;
}
std::string file_path;
if (_cr.all_of<Contact::Components::ToxFriendPersistent>(contact)) {
file_path = getAvatarPath(_cr.get<Contact::Components::ToxFriendPersistent>(contact).key);
} else if (_cr.all_of<Contact::Components::ToxGroupPersistent>(contact)) {
file_path = getAvatarPath(_cr.get<Contact::Components::ToxGroupPersistent>(contact).chat_id);
} else {
std::cerr << "TAM error: cant get toxkey for contact\n";
// TODO: mark handled?
return;
}
if (o.all_of<ObjComp::F::TagLocalHaveAll>()) {
std::cout << "TAM: full avatar received\n";
if (_cr.all_of<Contact::Components::ToxFriendPersistent>(contact)) {
addAvatarFileToContact(contact, _cr.get<Contact::Components::ToxFriendPersistent>(contact).key);
} else if (_cr.all_of<Contact::Components::ToxGroupPersistent>(contact)) {
addAvatarFileToContact(contact, _cr.get<Contact::Components::ToxGroupPersistent>(contact).chat_id);
} else {
std::cerr << "TAM error: cant get toxkey for contact\n";
}
// old code no longer works, we already have the object (right here lol)
contact.emplace_or_replace<Contact::Components::AvatarObj>(o);
contact.emplace_or_replace<Contact::Components::TagAvatarInvalidate>();
o.emplace_or_replace<Components::TagAvatarImageHandled>();
} else {
} else if (!o.all_of<
ObjComp::Ephemeral::BackendMeta, // hmm
ObjComp::Ephemeral::BackendFile2,
ObjComp::Ephemeral::BackendAtomic // to be safe
>()) {
std::string file_path;
if (contact.all_of<Contact::Components::ToxFriendPersistent>()) {
file_path = getAvatarPath(contact.get<Contact::Components::ToxFriendPersistent>().key);
} else if (contact.all_of<Contact::Components::ToxGroupPersistent>()) {
file_path = getAvatarPath(contact.get<Contact::Components::ToxGroupPersistent>().chat_id);
} else {
std::cerr << "TAM error: cant get toxkey for contact\n";
// TODO: mark handled?
return;
}
// already has avatar, delete old (TODO: or check hash)
if (contact.all_of<Contact::Components::AvatarObj>()) {
clearAvatarFromContact(contact);
}
// crude
// TODO: queue/async instead
// check file id for existing hash
if (std::filesystem::is_regular_file(file_path)) {
//const auto& supposed_file_hash = h.get<Message::Components::Transfer::FileID>().id;
@@ -209,12 +263,18 @@ void ToxAvatarManager::checkObj(ObjectHandle o) {
std::filesystem::remove(file_path); // hack, hard replace existing file
}
std::cout << "TAM: accepted avatar ft\n";
if (!_sb_tcs.attach(o)) {
std::cerr << "TAM error: failed to attach backend??\n";
return;
}
// if not already on disk
_accept_queue.push_back(AcceptEntry{o, file_path});
o.emplace_or_replace<ObjComp::F::SingleInfoLocal>(file_path);
// ... do we do anything here?
// like set "accepted" tag comp or something
std::cout << "TAM: accepted avatar ft\n";
}
#endif
}
bool ToxAvatarManager::onEvent(const ObjectStore::Events::ObjectConstruct& e) {
@@ -227,3 +287,42 @@ bool ToxAvatarManager::onEvent(const ObjectStore::Events::ObjectUpdate& e) {
return false;
}
bool ToxAvatarManager::onEvent(const ObjectStore::Events::ObjectDestory& e) {
// TODO: avatar contact comp instead?
// TODO: generic contact comp? (hard, very usecase dep)
#if 0
if (!e.e.all_of<ObjComp::Ephemeral::File::Sender>()) {
// cant be reasonable be attributed to a contact
return false;
}
// TODO: remove obj from contact
#endif
if (!e.e.all_of<ObjComp::Ephemeral::ToxContact>()) {
// cant be reasonable be attributed to a contact
return false;
}
auto c = e.e.get<ObjComp::Ephemeral::ToxContact>().c;
if (!static_cast<bool>(c)) {
// meh
return false;
}
if (!c.all_of<Contact::Components::AvatarObj>()) {
// funny
return false;
}
if (c.get<Contact::Components::AvatarObj>().obj != e.e) {
// maybe got replace?
// TODO: make error and do proper cleanup
return false;
}
c.remove<Contact::Components::AvatarObj>();
c.emplace_or_replace<Contact::Components::TagAvatarInvalidate>();
// TODO: throw contact update!!!
return false;
}