From 2d5edba9dc54d426829d7f20d7487e7cbda036a9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 18 May 2018 15:59:38 -0400
Subject: [PATCH] ringaccount: fail message if no device is found

Change-Id: I04466980b941dd943637b1a0002997de7a77114f
Reviewed-by: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
---
 src/ringdht/p2p.cpp         |  8 ++++----
 src/ringdht/ringaccount.cpp | 40 ++++++++++++++++++++-----------------
 src/ringdht/ringaccount.h   |  3 ++-
 3 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/src/ringdht/p2p.cpp b/src/ringdht/p2p.cpp
index 049acdf488..6f6e096331 100644
--- a/src/ringdht/p2p.cpp
+++ b/src/ringdht/p2p.cpp
@@ -743,9 +743,9 @@ DhtPeerConnector::requestConnection(const std::string& peer_id,
                 });
         },
 
-        [this, peer_h, connect_cb](bool found) {
+        [this, peer_h, connect_cb](const std::shared_ptr<RingAccount>& account, bool found) {
             if (!found) {
-                RING_WARN() << pimpl_->account << "[CNX] aborted, no devices for " << peer_h;
+                RING_WARN() << account << "[CNX] aborted, no devices for " << peer_h;
                 connect_cb(nullptr);
             }
         });
@@ -775,9 +775,9 @@ DhtPeerConnector::closeConnection(const std::string& peer_id, const DRing::DataT
             pimpl_->ctrl << makeMsg<CtrlMsgType::CANCEL>(dev_h, tid);
         },
 
-        [this, peer_h](bool found) {
+        [peer_h](const std::shared_ptr<RingAccount>& account, bool found) {
             if (!found) {
-                RING_WARN() << pimpl_->account << "[CNX] aborted, no devices for " << peer_h;
+                RING_WARN() << account << "[CNX] aborted, no devices for " << peer_h;
             }
         });
 }
diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index cc44b0343c..110b9c4b64 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -490,7 +490,7 @@ RingAccount::startOutgoingCall(const std::shared_ptr<SIPCall>& call, const std::
             });
             return false;
         });
-    }, [=](bool ok){
+    }, [wCall](const std::shared_ptr<RingAccount>& sthis, bool ok){
         if (not ok) {
             if (auto call = wCall.lock()) {
                 RING_WARN("[call:%s] no devices found", call->getCallId().c_str());
@@ -1956,20 +1956,18 @@ RingAccount::trackBuddyPresence(const std::string& buddy_id)
         buddy_info.updateInfo = Manager::instance().scheduleTask([h,weak_this]() {
             if (auto shared_this = weak_this.lock()) {
                 /* ::forEachDevice call will update buddy info accordingly. */
-                shared_this->forEachDevice(h, {}, [h, weak_this] (bool /* ok */) {
-                    if (auto shared_this = weak_this.lock()) {
-                        std::lock_guard<std::recursive_mutex> lock(shared_this->buddyInfoMtx);
-                        auto buddy_info_it = shared_this->trackedBuddies_.find(h);
-                        if (buddy_info_it == shared_this->trackedBuddies_.end()) return;
-
-                        auto& buddy_info = buddy_info_it->second;
-                        if (buddy_info.updateInfo) {
-                            auto cb = buddy_info.updateInfo;
-                            Manager::instance().scheduleTask(
-                                std::move(cb),
-                                std::chrono::steady_clock::now() + DeviceAnnouncement::TYPE.expiration
-                            );
-                        }
+                shared_this->forEachDevice(h, {}, [h] (const std::shared_ptr<RingAccount>& shared_this, bool /* ok */) {
+                    std::lock_guard<std::recursive_mutex> lock(shared_this->buddyInfoMtx);
+                    auto buddy_info_it = shared_this->trackedBuddies_.find(h);
+                    if (buddy_info_it == shared_this->trackedBuddies_.end()) return;
+
+                    auto& buddy_info = buddy_info_it->second;
+                    if (buddy_info.updateInfo) {
+                        auto cb = buddy_info.updateInfo;
+                        Manager::instance().scheduleTask(
+                            std::move(cb),
+                            std::chrono::steady_clock::now() + DeviceAnnouncement::TYPE.expiration
+                        );
                     }
                 });
             }
@@ -2092,8 +2090,10 @@ RingAccount::doRegister_()
         if (not config.proxy_server.empty())
             RING_WARN("[Account %s] using proxy server %s", getAccountID().c_str(), config.proxy_server.c_str());
 
-        if (not deviceKey_.empty())
+        if (not deviceKey_.empty()) {
+            RING_WARN("[Account %s] using push notifications", getAccountID().c_str());
             dht_.setPushNotificationToken(deviceKey_);
+        }
 
         dht_.run((in_port_t)dhtPortUsed_, config);
 
@@ -3233,7 +3233,7 @@ void
 RingAccount::forEachDevice(const dht::InfoHash& to,
                            std::function<void(const std::shared_ptr<RingAccount>&,
                                               const dht::InfoHash&)> op,
-                           std::function<void(bool)> end)
+                           std::function<void(const std::shared_ptr<RingAccount>&, bool)> end)
 {
     auto shared = std::static_pointer_cast<RingAccount>(shared_from_this());
     auto treatedDevices = std::make_shared<std::set<dht::InfoHash>>();
@@ -3261,7 +3261,7 @@ RingAccount::forEachDevice(const dht::InfoHash& to,
         }
         RING_DBG("[Account %s] found %lu devices for %s",
                  getAccountID().c_str(), treatedDevices->size(), to.to_c_str());
-        if (end) end(not treatedDevices->empty());
+        if (end) end(shared, not treatedDevices->empty());
     });
 }
 
@@ -3357,6 +3357,10 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string,
             });
 
         RING_DBG("[Account %s] [message %" PRIx64 "] sending message for device %s", shared->getAccountID().c_str(), token, dev.toString().c_str());
+    }, [token](const std::shared_ptr<RingAccount>& shared, bool ok) {
+        if (not ok) {
+            shared->messageEngine_.onMessageSent(token, false);
+        }
     });
 
     // Timeout cleanup
diff --git a/src/ringdht/ringaccount.h b/src/ringdht/ringaccount.h
index 455b97d29e..5d9c8907ab 100644
--- a/src/ringdht/ringaccount.h
+++ b/src/ringdht/ringaccount.h
@@ -352,7 +352,8 @@ class RingAccount : public SIPAccountBase {
         void forEachDevice(const dht::InfoHash& to,
                            std::function<void(const std::shared_ptr<RingAccount>&,
                                               const dht::InfoHash&)> op,
-                           std::function<void(bool)> end = {});
+                           std::function<void(const std::shared_ptr<RingAccount>&,
+                                              bool)> end = {});
 
         /**
          * Inform that a potential peer device have been found.
-- 
GitLab