diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h @@ -280,6 +280,8 @@ jsi::String threadID, double limit, double offset) override; + virtual jsi::Value + getInboundP2PMessagesByID(jsi::Runtime &rt, jsi::Array ids) override; public: CommCoreModule(std::shared_ptr jsInvoker); diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp @@ -3213,4 +3213,58 @@ }); } +jsi::Value +CommCoreModule::getInboundP2PMessagesByID(jsi::Runtime &rt, jsi::Array ids) { + std::vector msgIDsCPP{}; + for (auto idx = 0; idx < ids.size(rt); idx++) { + std::string msgID = ids.getValueAtIndex(rt, idx).asString(rt).utf8(rt); + msgIDsCPP.push_back(msgID); + } + + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=, &innerRt]() { + std::string error; + std::vector messages; + + try { + messages = + DatabaseManager::getQueryExecutor().getInboundP2PMessagesByID( + msgIDsCPP); + } catch (std::system_error &e) { + error = e.what(); + } + auto messagesPtr = std::make_shared>( + std::move(messages)); + + this->jsInvoker_->invokeAsync( + [&innerRt, messagesPtr, error, promise]() { + if (error.size()) { + promise->reject(error); + return; + } + + jsi::Array jsiMessages = + jsi::Array(innerRt, messagesPtr->size()); + size_t writeIdx = 0; + for (const InboundP2PMessage &msg : *messagesPtr) { + jsi::Object jsiMsg = jsi::Object(innerRt); + jsiMsg.setProperty(innerRt, "messageID", msg.message_id); + jsiMsg.setProperty( + innerRt, "senderDeviceID", msg.sender_device_id); + jsiMsg.setProperty(innerRt, "plaintext", msg.plaintext); + jsiMsg.setProperty(innerRt, "status", msg.status); + jsiMsg.setProperty( + innerRt, "senderUserID", msg.sender_user_id); + jsiMessages.setValueAtIndex(innerRt, writeIdx++, jsiMsg); + } + + promise->resolve(std::move(jsiMessages)); + }); + }; + GlobalDBSingleton::instance.scheduleOrRunCancellable( + job, promise, this->jsInvoker_); + }); +} + } // namespace comm diff --git a/native/cpp/CommonCpp/_generated/commJSI-generated.cpp b/native/cpp/CommonCpp/_generated/commJSI-generated.cpp --- a/native/cpp/CommonCpp/_generated/commJSI-generated.cpp +++ b/native/cpp/CommonCpp/_generated/commJSI-generated.cpp @@ -244,6 +244,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_fetchMessages(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->fetchMessages(rt, args[0].asString(rt), args[1].asNumber(), args[2].asNumber()); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getInboundP2PMessagesByID(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getInboundP2PMessagesByID(rt, args[0].asObject(rt).asArray(rt)); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -322,6 +325,7 @@ methodMap_["getRelatedMessages"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getRelatedMessages}; methodMap_["searchMessages"] = MethodMetadata {4, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_searchMessages}; methodMap_["fetchMessages"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_fetchMessages}; + methodMap_["getInboundP2PMessagesByID"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getInboundP2PMessagesByID}; } diff --git a/native/cpp/CommonCpp/_generated/commJSI.h b/native/cpp/CommonCpp/_generated/commJSI.h --- a/native/cpp/CommonCpp/_generated/commJSI.h +++ b/native/cpp/CommonCpp/_generated/commJSI.h @@ -95,6 +95,7 @@ virtual jsi::Value getRelatedMessages(jsi::Runtime &rt, jsi::String messageID) = 0; virtual jsi::Value searchMessages(jsi::Runtime &rt, jsi::String query, jsi::String threadID, std::optional timestampCursor, std::optional messageIDCursor) = 0; virtual jsi::Value fetchMessages(jsi::Runtime &rt, jsi::String threadID, double limit, double offset) = 0; + virtual jsi::Value getInboundP2PMessagesByID(jsi::Runtime &rt, jsi::Array ids) = 0; }; @@ -716,6 +717,14 @@ return bridging::callFromJs( rt, &T::fetchMessages, jsInvoker_, instance_, std::move(threadID), std::move(limit), std::move(offset)); } + jsi::Value getInboundP2PMessagesByID(jsi::Runtime &rt, jsi::Array ids) override { + static_assert( + bridging::getParameterCount(&T::getInboundP2PMessagesByID) == 2, + "Expected getInboundP2PMessagesByID(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getInboundP2PMessagesByID, jsInvoker_, instance_, std::move(ids)); + } private: T *instance_; diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -210,6 +210,9 @@ limit: number, offset: number, ) => Promise>; + +getInboundP2PMessagesByID: ( + ids: $ReadOnlyArray, + ) => Promise>; } export interface CoreModuleSpec extends Spec {