From 91ae0671b0d31276d4fa6495c28db1c3a0bd67eb Mon Sep 17 00:00:00 2001 From: Green Sky Date: Thu, 31 Jul 2025 18:58:49 +0200 Subject: [PATCH] add api helpers for contact creation workaround until i settle on a more generic api --- .../tox_contacts/tox_contact_model2.cpp | 67 +++++++++++++++++++ .../tox_contacts/tox_contact_model2.hpp | 18 +++++ 2 files changed, 85 insertions(+) diff --git a/solanaceae/tox_contacts/tox_contact_model2.cpp b/solanaceae/tox_contacts/tox_contact_model2.cpp index 3a4d8f1..067f2fb 100644 --- a/solanaceae/tox_contacts/tox_contact_model2.cpp +++ b/solanaceae/tox_contacts/tox_contact_model2.cpp @@ -1,6 +1,7 @@ #include "./tox_contact_model2.hpp" #include +#include #include #include @@ -172,6 +173,72 @@ bool ToxContactModel2::leave(Contact4 c, std::string_view reason) { return false; } +std::tuple ToxContactModel2::createContactFriend( + std::string_view tox_id, + std::string_view message +) { + auto [id_opt, err_r] = _t.toxFriendAdd( + hex2bin(tox_id), + message + ); + + if (!id_opt.has_value()) { + return std::make_tuple(ContactHandle4{}, err_r); + } + + auto contact = getContactFriend(id_opt.value()); + return std::make_tuple(contact, err_r); +} + +std::tuple ToxContactModel2::createContactGroupJoin( + std::string_view chat_id, + std::string_view self_name, + std::string_view password +) { + auto [id_opt, err_r] = _t.toxGroupJoin( + hex2bin(chat_id), + self_name, + password + ); + + if (!id_opt.has_value()) { + return std::make_tuple(ContactHandle4{}, err_r); + } + + auto contact = getContactGroup(id_opt.value()); + return std::make_tuple(contact, err_r); +} + +std::tuple ToxContactModel2::createContactGroupNew( + Tox_Group_Privacy_State privacy_state, + std::string_view name, + std::string_view self_name, + std::string_view password +) { + Tox_Err_Group_Set_Password err_pw = Tox_Err_Group_Set_Password::TOX_ERR_GROUP_SET_PASSWORD_OK; + + auto [new_id_opt, err_r] = _t.toxGroupNew( + privacy_state, + name, + self_name + ); + if (!new_id_opt.has_value() || err_r != TOX_ERR_GROUP_NEW_OK) { + return std::make_tuple(ContactHandle4{}, err_r, err_pw); + } + + auto contact = getContactGroup(new_id_opt.value()); + if (!static_cast(contact)) { + return std::make_tuple(contact, err_r, err_pw); + } + + if (!password.empty()) { + // TODO: error handling, delete new group if pw error? + err_pw = _t.toxGroupSetPassword(new_id_opt.value(), password); + } + + return std::make_tuple(contact, err_r, err_pw); +} + ContactHandle4 ToxContactModel2::getContactFriend(uint32_t friend_number) { auto& cr = _cs.registry(); Contact4 c {entt::null}; diff --git a/solanaceae/tox_contacts/tox_contact_model2.hpp b/solanaceae/tox_contacts/tox_contact_model2.hpp index 72e4e12..fe01998 100644 --- a/solanaceae/tox_contacts/tox_contact_model2.hpp +++ b/solanaceae/tox_contacts/tox_contact_model2.hpp @@ -35,6 +35,24 @@ class ToxContactModel2 : public ContactModel4I, public ToxEventI { bool leave(Contact4 c, std::string_view reason) override; + public: // api until i have a better idea + std::tuple createContactFriend( + std::string_view tox_id, + std::string_view message + ); + + std::tuple createContactGroupJoin( + std::string_view chat_id, + std::string_view self_name, + std::string_view password + ); + std::tuple createContactGroupNew( + Tox_Group_Privacy_State privacy_state, + std::string_view name, + std::string_view self_name, + std::string_view password + ); + public: // util for tox code // also creates if non existant ContactHandle4 getContactFriend(uint32_t friend_number);