// DragonX Wallet - HushChat in-memory message store (implementation). #include "chat_store.h" #include namespace dragonx::chat { std::string ChatStore::dedupKey(const ChatMessage& message) { return message.txid + ":" + std::to_string(message.payload_position); } bool ChatStore::append(const ChatMessage& message) { if (!seen_.insert(dedupKey(message)).second) return false; messages_.push_back(message); return true; } std::vector ChatStore::conversation(const std::string& conversationId) const { std::vector out; for (const auto& message : messages_) { if (message.conversation_id == conversationId) out.push_back(message); } // Return chronological (oldest→newest). messages_ is in scan/insertion order, which is NOT // time-ordered (a full scan harvests txids in std::set/unordered_map order) — that would mis-order the // rendered thread AND let the reply-target pin (first-seen peer address) latch onto a non-establishing // message (B2). timestamp is the block/tx time (peer can't set it); tie-break txid + payload_position // for determinism. std::stable_sort(out.begin(), out.end(), [](const ChatMessage& a, const ChatMessage& b) { if (a.timestamp != b.timestamp) return a.timestamp < b.timestamp; if (a.txid != b.txid) return a.txid < b.txid; return a.payload_position < b.payload_position; }); return out; } const ChatMessage* ChatStore::updateDelivery(const std::string& txid, ChatDelivery delivery) { for (auto& message : messages_) { if (message.txid == txid) { message.delivery = delivery; return &message; } } return nullptr; } std::vector ChatStore::conversationIds() const { std::vector ids; std::unordered_set seenIds; for (const auto& message : messages_) { if (seenIds.insert(message.conversation_id).second) ids.push_back(message.conversation_id); } return ids; } void ChatStore::clear() { messages_.clear(); seen_.clear(); } } // namespace dragonx::chat