diff --git a/src/client/presencemanager.cpp b/src/client/presencemanager.cpp index e735fead1b027b0a8021df794d0fee7d5dfee2dc..395a9009037e2244d4adef0619162a2e43736c53 100644 --- a/src/client/presencemanager.cpp +++ b/src/client/presencemanager.cpp @@ -65,7 +65,7 @@ subscribeBuddy(const std::string& accountID, const std::string& uri, bool flag) pres->subscribeClient(uri, flag); } } else if (auto ringaccount = ring::Manager::instance().getAccount<ring::RingAccount>(accountID)) { - ringaccount->trackBuddyPresence(uri); + ringaccount->trackBuddyPresence(uri, flag); } else RING_ERR("Could not find account %s", accountID.c_str()); } diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp index 39a923f4ab3405f8a1156a4a7f356896f0a7d37a..80ec20b816551aaaac929e5ce87a2b7ef4e39e9c 100644 --- a/src/ringdht/ringaccount.cpp +++ b/src/ringdht/ringaccount.cpp @@ -1943,7 +1943,7 @@ RingAccount::loadBootstrap() const } void -RingAccount::trackBuddyPresence(const std::string& buddy_id) +RingAccount::trackBuddyPresence(const std::string& buddy_id, bool track) { std::string buddyUri; @@ -1955,9 +1955,19 @@ RingAccount::trackBuddyPresence(const std::string& buddy_id) return; } auto h = dht::InfoHash(buddyUri); - auto buddy = trackedBuddies_.emplace(h, BuddyInfo {h}); - if (buddy.second) { - trackPresence(buddy.first->first, buddy.first->second); + std::lock_guard<std::mutex> lock(buddyInfoMtx); + if (track) { + auto buddy = trackedBuddies_.emplace(h, BuddyInfo {h}); + if (buddy.second) { + trackPresence(buddy.first->first, buddy.first->second); + } + } else { + auto buddy = trackedBuddies_.find(h); + if (buddy != trackedBuddies_.end()) { + if (dht_.isRunning()) + dht_.cancelListen(h, std::move(buddy->second.listenToken)); + trackedBuddies_.erase(buddy); + } } } @@ -1970,7 +1980,7 @@ RingAccount::trackPresence(const dht::InfoHash& h, BuddyInfo& buddy) buddy.listenToken = dht_.listen<DeviceAnnouncement>(h, [this, h](DeviceAnnouncement&& dev, bool expired){ bool wasConnected, isConnected; { - std::lock_guard<std::recursive_mutex> lock(buddyInfoMtx); + std::lock_guard<std::mutex> lock(buddyInfoMtx); auto buddy = trackedBuddies_.find(h); if (buddy == trackedBuddies_.end()) return true; @@ -1995,7 +2005,7 @@ RingAccount::trackPresence(const dht::InfoHash& h, BuddyInfo& buddy) std::map<std::string, bool> RingAccount::getTrackedBuddyPresence() { - std::lock_guard<std::recursive_mutex> lock(buddyInfoMtx); + std::lock_guard<std::mutex> lock(buddyInfoMtx); std::map<std::string, bool> presence_info; for (const auto& buddy_info_p : trackedBuddies_) presence_info.emplace(buddy_info_p.first.toString(), not buddy_info_p.second.devices.empty()); diff --git a/src/ringdht/ringaccount.h b/src/ringdht/ringaccount.h index 550f54975b82c52ee5f3562af0d0016712536230..ea8d3da485e5d6abf6a20fa83ebb565c0bb1bccc 100644 --- a/src/ringdht/ringaccount.h +++ b/src/ringdht/ringaccount.h @@ -156,7 +156,7 @@ class RingAccount : public SIPAccountBase { * * @param buddy_id The buddy id. */ - void trackBuddyPresence(const std::string& buddy_id); + void trackBuddyPresence(const std::string& buddy_id, bool track); /** * Tells for each tracked account id if it has been seen online so far @@ -553,7 +553,7 @@ class RingAccount : public SIPAccountBase { std::map<dht::InfoHash, KnownDevice> knownDevices_; /* tracked buddies presence */ - std::recursive_mutex buddyInfoMtx; + std::mutex buddyInfoMtx; std::map<dht::InfoHash, BuddyInfo> trackedBuddies_; void loadAccount(const std::string& archive_password = {}, const std::string& archive_pin = {}, const std::string& archive_path = {});