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,9 @@ jsi::String backupLogDataKey) override; virtual jsi::Value retrieveBackupKeys(jsi::Runtime &rt, jsi::String backupSecret) override; + virtual jsi::Value getAllReceivedMessageToDevice(jsi::Runtime &rt) override; + virtual jsi::Value + removeReceivedMessagesToDevice(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 @@ -1910,4 +1910,85 @@ ::retrieveBackupKeys(rust::string(backupSecretStr), currentID); }); } + +jsi::Value CommCoreModule::getAllReceivedMessageToDevice(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() + .getAllReceivedMessageToDevice(); + + } 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 ReceivedMessageToDevice &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); + jsiMessages.setValueAtIndex(innerRt, writeIdx++, jsiMsg); + } + + promise->resolve(std::move(jsiMessages)); + }); + }; + GlobalDBSingleton::instance.scheduleOrRunCancellable( + job, promise, this->jsInvoker_); + }); +} + +jsi::Value CommCoreModule::removeReceivedMessagesToDevice( + 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, + [this, + msgIDsCPP](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [this, promise, msgIDsCPP]() { + std::string error; + try { + DatabaseManager::getQueryExecutor().removeReceivedMessagesToDevice( + msgIDsCPP); + } 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 @@ -196,6 +196,12 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_retrieveBackupKeys(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->retrieveBackupKeys(rt, args[0].asString(rt)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getAllReceivedMessageToDevice(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getAllReceivedMessageToDevice(rt); +} +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_removeReceivedMessagesToDevice(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->removeReceivedMessagesToDevice(rt, args[0].asObject(rt).asArray(rt)); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -258,6 +264,8 @@ methodMap_["restoreBackup"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackup}; methodMap_["restoreBackupData"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackupData}; methodMap_["retrieveBackupKeys"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_retrieveBackupKeys}; + methodMap_["getAllReceivedMessageToDevice"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getAllReceivedMessageToDevice}; + methodMap_["removeReceivedMessagesToDevice"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_removeReceivedMessagesToDevice}; } 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 @@ -79,6 +79,8 @@ virtual jsi::Value restoreBackup(jsi::Runtime &rt, jsi::String backupSecret) = 0; virtual jsi::Value restoreBackupData(jsi::Runtime &rt, jsi::String backupID, jsi::String backupDataKey, jsi::String backupLogDataKey) = 0; virtual jsi::Value retrieveBackupKeys(jsi::Runtime &rt, jsi::String backupSecret) = 0; + virtual jsi::Value getAllReceivedMessageToDevice(jsi::Runtime &rt) = 0; + virtual jsi::Value removeReceivedMessagesToDevice(jsi::Runtime &rt, jsi::Array ids) = 0; }; @@ -572,6 +574,22 @@ return bridging::callFromJs( rt, &T::retrieveBackupKeys, jsInvoker_, instance_, std::move(backupSecret)); } + jsi::Value getAllReceivedMessageToDevice(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::getAllReceivedMessageToDevice) == 1, + "Expected getAllReceivedMessageToDevice(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::getAllReceivedMessageToDevice, jsInvoker_, instance_); + } + jsi::Value removeReceivedMessagesToDevice(jsi::Runtime &rt, jsi::Array ids) override { + static_assert( + bridging::getParameterCount(&T::removeReceivedMessagesToDevice) == 2, + "Expected removeReceivedMessagesToDevice(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::removeReceivedMessagesToDevice, 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 @@ -24,6 +24,7 @@ } from 'lib/types/crypto-types.js'; import type { ClientDBDraftStoreOperation } from 'lib/types/draft-types.js'; import type { ClientDBMessageInfo } from 'lib/types/message-types.js'; +import type { ReceivedMessageToDevice } from 'lib/types/sqlite-types.js'; import type { ClientDBStore } from 'lib/types/store-ops-types'; import type { ClientDBThreadInfo } from 'lib/types/thread-types.js'; @@ -161,6 +162,10 @@ backupLogDataKey: string, ) => Promise; +retrieveBackupKeys: (backupSecret: string) => Promise; + +getAllReceivedMessageToDevice: () => Promise; + +removeReceivedMessagesToDevice: ( + ids: $ReadOnlyArray, + ) => Promise; } export interface CoreModuleSpec extends Spec {