From 95b580b573aecf2186e852659977c0ba98cf7ae3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Wed, 7 Sep 2022 16:34:59 -0400
Subject: [PATCH] nodejs: add binding for MessagesFound

Change-Id: Ifc2d7853ba58311356a4c33e80987424f3bff283
---
 bin/nodejs/callback.h | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/bin/nodejs/callback.h b/bin/nodejs/callback.h
index c93f010def..d5749bb58e 100644
--- a/bin/nodejs/callback.h
+++ b/bin/nodejs/callback.h
@@ -28,6 +28,7 @@ Persistent<Function> incomingMessageCb;
 Persistent<Function> incomingCallCb;
 Persistent<Function> incomingCallWithMediaCb;
 Persistent<Function> conversationLoadedCb;
+Persistent<Function> messagesFoundCb;
 Persistent<Function> messageReceivedCb;
 Persistent<Function> conversationProfileUpdatedCb;
 Persistent<Function> conversationRequestReceivedCb;
@@ -87,6 +88,8 @@ getPresistentCb(std::string_view signal)
         return &incomingCallWithMediaCb;
     else if (signal == "ConversationLoaded")
         return &conversationLoadedCb;
+    else if (signal == "MessagesFound")
+        return &messagesFoundCb;
     else if (signal == "MessageReceived")
         return &messageReceivedCb;
     else if (signal == "ConversationProfileUpdated")
@@ -111,7 +114,6 @@ getPresistentCb(std::string_view signal)
         return &conferenceRemovedCb;
     else if (signal == "OnConferenceInfosUpdated")
         return &onConferenceInfosUpdatedCb;
-
     else
         return nullptr;
 }
@@ -564,6 +566,23 @@ conversationLoaded(uint32_t id,
     uv_async_send(&signalAsync);
 }
 
+void
+messagesFound(uint32_t id, const std::string& accountId, const std::string&  conversationId, const std::vector<std::map<std::string, std::string>>& messages)
+{
+    std::lock_guard<std::mutex> lock(pendingSignalsLock);
+    pendingSignals.emplace([id, accountId, conversationId, messages]() {
+        Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), messagesFoundCb);
+        if (!func.IsEmpty()) {
+            SWIGV8_VALUE callback_args[] = {SWIGV8_INTEGER_NEW_UNS(id),
+                                            V8_STRING_NEW_LOCAL(accountId),
+                                            V8_STRING_NEW_LOCAL(conversationId),
+                                            stringMapVecToJsMapArray(messages)};
+            func->Call(SWIGV8_CURRENT_CONTEXT(), SWIGV8_NULL(), 4, callback_args);
+        }
+    });
+    uv_async_send(&signalAsync);
+}
+
 void
 messageReceived(const std::string& accountId,
                 const std::string& conversationId,
-- 
GitLab