From 44163ad3a5e7e2e4c27baf12a86bec1fea97a7e2 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Fri, 12 Jan 2024 14:05:56 +0100 Subject: [PATCH] add receive confirmaton to tox friend messages --- solanaceae/tox_messages/components.hpp | 5 +++ solanaceae/tox_messages/components_id.inl | 1 + .../tox_messages/tox_message_manager.cpp | 36 +++++++++++++++++++ .../tox_messages/tox_message_manager.hpp | 1 + 4 files changed, 43 insertions(+) diff --git a/solanaceae/tox_messages/components.hpp b/solanaceae/tox_messages/components.hpp index cbea3bf..3bb5ca5 100644 --- a/solanaceae/tox_messages/components.hpp +++ b/solanaceae/tox_messages/components.hpp @@ -9,6 +9,11 @@ namespace Message::Components { +struct ToxFriendMessageID { + // only exposed for the read reciept event + uint32_t id = 0u; +}; + struct ToxGroupMessageID { uint32_t id = 0u; }; diff --git a/solanaceae/tox_messages/components_id.inl b/solanaceae/tox_messages/components_id.inl index 382166d..fc12848 100644 --- a/solanaceae/tox_messages/components_id.inl +++ b/solanaceae/tox_messages/components_id.inl @@ -12,6 +12,7 @@ constexpr entt::id_type entt::type_hash::value() noexcept { \ // cross compile(r) stable ids +DEFINE_COMP_ID(Message::Components::ToxFriendMessageID) DEFINE_COMP_ID(Message::Components::ToxGroupMessageID) DEFINE_COMP_ID(Message::Components::Transfer::ToxTransferFriend) DEFINE_COMP_ID(Message::Components::Transfer::FileID) diff --git a/solanaceae/tox_messages/tox_message_manager.cpp b/solanaceae/tox_messages/tox_message_manager.cpp index cc7b31e..a6c5806 100644 --- a/solanaceae/tox_messages/tox_message_manager.cpp +++ b/solanaceae/tox_messages/tox_message_manager.cpp @@ -105,6 +105,8 @@ bool ToxMessageManager::sendText(const Contact3 c, std::string_view message, boo if (!res.has_value()) { std::cerr << "TMM: failed to send friend message\n"; //return true; // not online? TODO: check for other errors + } else { + reg.emplace(new_msg_e, res.value()); } } else if ( _cr.any_of(c) @@ -207,6 +209,40 @@ bool ToxMessageManager::onToxEvent(const Tox_Event_Friend_Message* e) { return false; // TODO: return true? } +bool ToxMessageManager::onToxEvent(const Tox_Event_Friend_Read_Receipt* e) { + uint32_t friend_number = tox_event_friend_read_receipt_get_friend_number(e); + uint32_t msg_id = tox_event_friend_read_receipt_get_message_id(e); + + // get current time unix epoch utc + uint64_t ts = Message::getTimeMS(); + + const auto c = _tcm.getContactFriend(friend_number); + const auto self_c = c.get().self; + + auto* reg_ptr = _rmm.get(c); + if (reg_ptr == nullptr) { + std::cerr << "TMM error: cant find reg\n"; + return false; + } + + Message3Registry& reg = *reg_ptr; + + // find message by message id + // TODO: keep a short list of unconfirmed msg ids + // this iterates in reverse, so newest messages should be pretty front + for (const auto& [m, msg_id_comp] : reg.view().each()) { + if (msg_id_comp.id == msg_id) { + // found it + auto& rtr = reg.get_or_emplace(m); + // insert but dont overwrite + rtr.ts.try_emplace(c, ts); + break; + } + } + + return true; +} + bool ToxMessageManager::onToxEvent(const Tox_Event_Group_Message* e) { const uint32_t group_number = tox_event_group_message_get_group_number(e); const uint32_t peer_number = tox_event_group_message_get_peer_id(e); diff --git a/solanaceae/tox_messages/tox_message_manager.hpp b/solanaceae/tox_messages/tox_message_manager.hpp index ab4a917..325480a 100644 --- a/solanaceae/tox_messages/tox_message_manager.hpp +++ b/solanaceae/tox_messages/tox_message_manager.hpp @@ -23,6 +23,7 @@ class ToxMessageManager : public RegistryMessageModelEventI, public ToxEventI { protected: // tox events bool onToxEvent(const Tox_Event_Friend_Message* e) override; + bool onToxEvent(const Tox_Event_Friend_Read_Receipt* e) override; bool onToxEvent(const Tox_Event_Group_Message* e) override; bool onToxEvent(const Tox_Event_Group_Private_Message* e) override;