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 @@ -98,6 +98,16 @@ virtual jsi::Value processThreadActivityStoreOperations( jsi::Runtime &rt, jsi::Array operations) override; + virtual jsi::Value + processDBStoreOperations(jsi::Runtime &rt, jsi::Object operations) override; + template + void appendDBStoreOps( + jsi::Runtime &rt, + jsi::Object &operations, + const char *key, + T &store, + std::shared_ptr>> + &destination); virtual jsi::Value initializeCryptoAccount(jsi::Runtime &rt) override; virtual jsi::Value getUserPublicKey(jsi::Runtime &rt) override; virtual jsi::Value 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 @@ -15,6 +15,7 @@ #include "JSIRust.h" #include "lib.rs.h" +#include #include namespace comm { @@ -406,6 +407,132 @@ rt, std::move(operations)); } +template +void CommCoreModule::appendDBStoreOps( + jsi::Runtime &rt, + jsi::Object &operations, + const char *key, + T &store, + std::shared_ptr>> + &destination) { + auto opsObject = operations.getProperty(rt, key); + if (opsObject.isObject()) { + auto ops = store.createOperations(rt, opsObject.asObject(rt).asArray(rt)); + std::move( + std::make_move_iterator(ops.begin()), + std::make_move_iterator(ops.end()), + std::back_inserter(*destination)); + } +} + +jsi::Value CommCoreModule::processDBStoreOperations( + jsi::Runtime &rt, + jsi::Object operations) { + std::string createOperationsError; + + auto storeOpsPtr = + std::make_shared>>(); + try { + this->appendDBStoreOps( + rt, operations, "draftStoreOperations", this->draftStore, storeOpsPtr); + this->appendDBStoreOps( + rt, + operations, + "threadStoreOperations", + this->threadStore, + storeOpsPtr); + this->appendDBStoreOps( + rt, + operations, + "messageStoreOperations", + this->messageStore, + storeOpsPtr); + this->appendDBStoreOps( + rt, + operations, + "reportStoreOperations", + this->reportStore, + storeOpsPtr); + this->appendDBStoreOps( + rt, operations, "userStoreOperations", this->userStore, storeOpsPtr); + this->appendDBStoreOps( + rt, + operations, + "keyserverStoreOperations", + this->keyserverStore, + storeOpsPtr); + this->appendDBStoreOps( + rt, + operations, + "communityStoreOperations", + this->communityStore, + storeOpsPtr); + this->appendDBStoreOps( + rt, + operations, + "integrityStoreOperations", + this->integrityStore, + storeOpsPtr); + this->appendDBStoreOps( + rt, + operations, + "syncedMetadataStoreOperations", + this->syncedMetadataStore, + storeOpsPtr); + this->appendDBStoreOps( + rt, + operations, + "auxUserStoreOperations", + this->auxUserStore, + storeOpsPtr); + this->appendDBStoreOps( + rt, + operations, + "threadActivityStoreOperations", + this->threadActivityStore, + storeOpsPtr); + } catch (std::runtime_error &e) { + createOperationsError = e.what(); + } + + return facebook::react::createPromiseAsJSIValue( + rt, + [=](jsi::Runtime &innerRt, + std::shared_ptr promise) { + taskType job = [=]() { + std::string error = createOperationsError; + + if (!error.size()) { + try { + DatabaseManager::getQueryExecutor().beginTransaction(); + for (const auto &operation : *storeOpsPtr) { + operation->execute(); + } + DatabaseManager::getQueryExecutor().captureBackupLogs(); + DatabaseManager::getQueryExecutor().commitTransaction(); + } catch (std::system_error &e) { + error = e.what(); + DatabaseManager::getQueryExecutor().rollbackTransaction(); + } + } + + if (!error.size()) { + ::triggerBackupFileUpload(); + } + + this->jsInvoker_->invokeAsync([=]() { + if (error.size()) { + promise->reject(error); + } else { + promise->resolve(jsi::Value::undefined()); + } + }); + }; + GlobalDBSingleton::instance.scheduleOrRunCancellable( + job, promise, this->jsInvoker_); + }); +} + void CommCoreModule::terminate(jsi::Runtime &rt) { TerminateApp::terminate(); } 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 @@ -78,6 +78,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_processThreadActivityStoreOperations(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->processThreadActivityStoreOperations(rt, args[0].asObject(rt).asArray(rt)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_processDBStoreOperations(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->processDBStoreOperations(rt, args[0].asObject(rt)); +} static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_initializeCryptoAccount(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->initializeCryptoAccount(rt); } @@ -232,6 +235,7 @@ methodMap_["processSyncedMetadataStoreOperations"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_processSyncedMetadataStoreOperations}; methodMap_["processAuxUserStoreOperations"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_processAuxUserStoreOperations}; methodMap_["processThreadActivityStoreOperations"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_processThreadActivityStoreOperations}; + methodMap_["processDBStoreOperations"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_processDBStoreOperations}; methodMap_["initializeCryptoAccount"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_initializeCryptoAccount}; methodMap_["getUserPublicKey"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getUserPublicKey}; methodMap_["getOneTimeKeys"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getOneTimeKeys}; 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 @@ -41,6 +41,7 @@ virtual jsi::Value processSyncedMetadataStoreOperations(jsi::Runtime &rt, jsi::Array operations) = 0; virtual jsi::Value processAuxUserStoreOperations(jsi::Runtime &rt, jsi::Array operations) = 0; virtual jsi::Value processThreadActivityStoreOperations(jsi::Runtime &rt, jsi::Array operations) = 0; + virtual jsi::Value processDBStoreOperations(jsi::Runtime &rt, jsi::Object operations) = 0; virtual jsi::Value initializeCryptoAccount(jsi::Runtime &rt) = 0; virtual jsi::Value getUserPublicKey(jsi::Runtime &rt) = 0; virtual jsi::Value getOneTimeKeys(jsi::Runtime &rt, double oneTimeKeysAmount) = 0; @@ -272,6 +273,14 @@ return bridging::callFromJs( rt, &T::processThreadActivityStoreOperations, jsInvoker_, instance_, std::move(operations)); } + jsi::Value processDBStoreOperations(jsi::Runtime &rt, jsi::Object operations) override { + static_assert( + bridging::getParameterCount(&T::processDBStoreOperations) == 2, + "Expected processDBStoreOperations(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::processDBStoreOperations, jsInvoker_, instance_, std::move(operations)); + } jsi::Value initializeCryptoAccount(jsi::Runtime &rt) override { static_assert( bridging::getParameterCount(&T::initializeCryptoAccount) == 1, diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -26,7 +26,10 @@ import type { ClientDBMessageInfo } from 'lib/types/message-types.js'; import type { SIWEBackupSecrets } from 'lib/types/siwe-types.js'; import type { ReceivedMessageToDevice } from 'lib/types/sqlite-types.js'; -import type { ClientDBStore } from 'lib/types/store-ops-types'; +import type { + ClientDBStore, + ClientDBStoreOperations, +} from 'lib/types/store-ops-types'; import type { ClientDBThreadInfo } from 'lib/types/thread-types.js'; type CommServicesAuthMetadata = { @@ -85,6 +88,7 @@ +processThreadActivityStoreOperations: ( operations: $ReadOnlyArray, ) => Promise; + +processDBStoreOperations: (operations: Object) => Promise; +initializeCryptoAccount: () => Promise; +getUserPublicKey: () => Promise; +getOneTimeKeys: (oneTimeKeysAmount: number) => Promise; @@ -193,6 +197,9 @@ siweBackupSecrets: SIWEBackupSecrets, ) => Promise; +getSIWEBackupSecrets: () => Promise; + +processDBStoreOperations: ( + operations: ClientDBStoreOperations, + ) => Promise; } export default (TurboModuleRegistry.getEnforcing(