From 3f27cd70693a70a737648a28ab3915d7d90e6022 Mon Sep 17 00:00:00 2001
From: Adrien Beraud <adrien.beraud@savoirfairelinux.com>
Date: Wed, 6 Nov 2024 16:50:08 -0500
Subject: [PATCH] proxy client: resubscribe on token change

---
 include/opendht/dht_proxy_client.h |  8 +-------
 src/dht_proxy_client.cpp           | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h
index c85ef9c8..f7dd0fea 100644
--- a/include/opendht/dht_proxy_client.h
+++ b/include/opendht/dht_proxy_client.h
@@ -67,13 +67,7 @@ public:
 
     void setHeaderFields(http::Request& request);
 
-    virtual void setPushNotificationToken(const std::string& token) override {
-#ifdef OPENDHT_PUSH_NOTIFICATIONS
-        deviceKey_ = token;
-#else
-        (void) token;
-#endif
-    }
+    virtual void setPushNotificationToken(const std::string& token) override;
 
     virtual void setPushNotificationTopic(const std::string& topic) override {
 #ifdef OPENDHT_PUSH_NOTIFICATIONS
diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp
index 21dd6730..6a1e6c54 100644
--- a/src/dht_proxy_client.cpp
+++ b/src/dht_proxy_client.cpp
@@ -1394,4 +1394,24 @@ DhtProxyClient::fillBody(bool resubscribe)
 }
 #endif // OPENDHT_PUSH_NOTIFICATIONS
 
+void
+DhtProxyClient::setPushNotificationToken([[maybe_unused]] const std::string& token) {
+#ifdef OPENDHT_PUSH_NOTIFICATIONS
+    std::unique_lock<std::mutex> l(lockCurrentProxyInfos_);
+    if (deviceKey_ != token) {
+        deviceKey_ = token;
+        if (statusIpv4_ == NodeStatus::Connected || statusIpv6_ == NodeStatus::Connected) {
+            if (logger_)
+                logger_->d("[proxy:client] [push] token changed, resubscribing");
+            for (auto& search : searches_) {
+                for (auto& listener : search.second.listeners) {
+                    resubscribe(search.first, listener.first, listener.second);
+                }
+            }
+        }
+    }
+#endif
+}
+
+
 } // namespace dht
-- 
GitLab