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 @@ -199,6 +199,15 @@ virtual jsi::Value getAllInboundP2PMessage(jsi::Runtime &rt) override; virtual jsi::Value removeInboundP2PMessages(jsi::Runtime &rt, jsi::Array ids) override; + virtual jsi::Value getAllOutboundP2PMessage(jsi::Runtime &rt) override; + virtual jsi::Value markOutboundP2PMessageAsSent( + jsi::Runtime &rt, + jsi::String messageID, + jsi::String deviceID) override; + virtual jsi::Value removeOutboundP2PMessagesOlderThan( + jsi::Runtime &rt, + jsi::String messageID, + jsi::String deviceID) 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 @@ -2218,4 +2218,111 @@ }); } +jsi::Value CommCoreModule::getAllOutboundP2PMessage(jsi::Runtime &rt) { + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=, &innerRt]() { + std::string error; + std::vector messages; + + try { + messages = + DatabaseManager::getQueryExecutor().getAllOutboundP2PMessages(); + + } 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 OutboundP2PMessage &msg : *messagesPtr) { + jsi::Object jsiMsg = jsi::Object(innerRt); + jsiMsg.setProperty(innerRt, "messageID", msg.message_id); + jsiMsg.setProperty(innerRt, "deviceID", msg.device_id); + jsiMsg.setProperty(innerRt, "userID", msg.user_id); + jsiMsg.setProperty(innerRt, "timestamp", msg.timestamp); + jsiMsg.setProperty(innerRt, "plaintext", msg.plaintext); + jsiMsg.setProperty(innerRt, "ciphertext", msg.ciphertext); + jsiMsg.setProperty(innerRt, "status", msg.status); + jsiMessages.setValueAtIndex(innerRt, writeIdx++, jsiMsg); + } + + promise->resolve(std::move(jsiMessages)); + }); + }; + GlobalDBSingleton::instance.scheduleOrRunCancellable( + job, promise, this->jsInvoker_); + }); +} + +jsi::Value CommCoreModule::markOutboundP2PMessageAsSent( + jsi::Runtime &rt, + jsi::String messageID, + jsi::String deviceID) { + auto messageIDCpp{messageID.utf8(rt)}; + auto deviceIDCpp{deviceID.utf8(rt)}; + + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=]() { + std::string error; + try { + DatabaseManager::getQueryExecutor().markOutboundP2PMessageAsSent( + messageIDCpp, deviceIDCpp); + } catch (std::system_error &e) { + error = e.what(); + } + this->jsInvoker_->invokeAsync([error, promise]() { + if (error.size()) { + promise->reject(error); + } else { + promise->resolve(jsi::Value::undefined()); + } + }); + }; + GlobalDBSingleton::instance.scheduleOrRunCancellable( + job, promise, this->jsInvoker_); + }); +} + +jsi::Value CommCoreModule::removeOutboundP2PMessagesOlderThan( + jsi::Runtime &rt, + jsi::String messageID, + jsi::String deviceID) { + auto messageIDCpp{messageID.utf8(rt)}; + auto deviceIDCpp{deviceID.utf8(rt)}; + + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=]() { + std::string error; + try { + DatabaseManager::getQueryExecutor() + .removeOutboundP2PMessagesOlderThan(messageIDCpp, deviceIDCpp); + } catch (std::system_error &e) { + error = e.what(); + } + this->jsInvoker_->invokeAsync([error, promise]() { + if (error.size()) { + promise->reject(error); + } else { + promise->resolve(jsi::Value::undefined()); + } + }); + }; + 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 @@ -187,6 +187,15 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_removeInboundP2PMessages(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->removeInboundP2PMessages(rt, args[0].asObject(rt).asArray(rt)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getAllOutboundP2PMessage(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getAllOutboundP2PMessage(rt); +} +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_markOutboundP2PMessageAsSent(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->markOutboundP2PMessageAsSent(rt, args[0].asString(rt), args[1].asString(rt)); +} +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_removeOutboundP2PMessagesOlderThan(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->removeOutboundP2PMessagesOlderThan(rt, args[0].asString(rt), args[1].asString(rt)); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -246,6 +255,9 @@ methodMap_["getSIWEBackupSecrets"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getSIWEBackupSecrets}; methodMap_["getAllInboundP2PMessage"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getAllInboundP2PMessage}; methodMap_["removeInboundP2PMessages"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_removeInboundP2PMessages}; + methodMap_["getAllOutboundP2PMessage"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getAllOutboundP2PMessage}; + methodMap_["markOutboundP2PMessageAsSent"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_markOutboundP2PMessageAsSent}; + methodMap_["removeOutboundP2PMessagesOlderThan"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_removeOutboundP2PMessagesOlderThan}; } 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 @@ -76,6 +76,9 @@ virtual jsi::Value getSIWEBackupSecrets(jsi::Runtime &rt) = 0; virtual jsi::Value getAllInboundP2PMessage(jsi::Runtime &rt) = 0; virtual jsi::Value removeInboundP2PMessages(jsi::Runtime &rt, jsi::Array ids) = 0; + virtual jsi::Value getAllOutboundP2PMessage(jsi::Runtime &rt) = 0; + virtual jsi::Value markOutboundP2PMessageAsSent(jsi::Runtime &rt, jsi::String messageID, jsi::String deviceID) = 0; + virtual jsi::Value removeOutboundP2PMessagesOlderThan(jsi::Runtime &rt, jsi::String messageID, jsi::String deviceID) = 0; }; @@ -545,6 +548,30 @@ return bridging::callFromJs( rt, &T::removeInboundP2PMessages, jsInvoker_, instance_, std::move(ids)); } + jsi::Value getAllOutboundP2PMessage(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::getAllOutboundP2PMessage) == 1, + "Expected getAllOutboundP2PMessage(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::getAllOutboundP2PMessage, jsInvoker_, instance_); + } + jsi::Value markOutboundP2PMessageAsSent(jsi::Runtime &rt, jsi::String messageID, jsi::String deviceID) override { + static_assert( + bridging::getParameterCount(&T::markOutboundP2PMessageAsSent) == 3, + "Expected markOutboundP2PMessageAsSent(...) to have 3 parameters"); + + return bridging::callFromJs( + rt, &T::markOutboundP2PMessageAsSent, jsInvoker_, instance_, std::move(messageID), std::move(deviceID)); + } + jsi::Value removeOutboundP2PMessagesOlderThan(jsi::Runtime &rt, jsi::String messageID, jsi::String deviceID) override { + static_assert( + bridging::getParameterCount(&T::removeOutboundP2PMessagesOlderThan) == 3, + "Expected removeOutboundP2PMessagesOlderThan(...) to have 3 parameters"); + + return bridging::callFromJs( + rt, &T::removeOutboundP2PMessagesOlderThan, jsInvoker_, instance_, std::move(messageID), std::move(deviceID)); + } 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 @@ -17,7 +17,10 @@ } from 'lib/types/crypto-types.js'; import type { ClientDBMessageInfo } from 'lib/types/message-types.js'; import type { SIWEBackupSecrets } from 'lib/types/siwe-types.js'; -import type { InboundP2PMessage } from 'lib/types/sqlite-types.js'; +import type { + InboundP2PMessage, + OutboundP2PMessage, +} from 'lib/types/sqlite-types.js'; import type { ClientDBStore, ClientDBStoreOperations, @@ -140,6 +143,15 @@ +getSIWEBackupSecrets: () => Promise; +getAllInboundP2PMessage: () => Promise; +removeInboundP2PMessages: (ids: $ReadOnlyArray) => Promise; + +getAllOutboundP2PMessage: () => Promise; + +markOutboundP2PMessageAsSent: ( + messageID: string, + deviceID: string, + ) => Promise; + +removeOutboundP2PMessagesOlderThan: ( + messageID: string, + deviceID: string, + ) => Promise; } export interface CoreModuleSpec extends Spec {