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 @@ -231,6 +231,8 @@ jsi::String deviceID) override; virtual jsi::Value getSyncedDatabaseVersion(jsi::Runtime &rt) override; virtual jsi::Value markPrekeysAsPublished(jsi::Runtime &rt) override; + virtual jsi::Value + getLatestMessageEdit(jsi::Runtime &rt, jsi::String messageID) 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 @@ -2593,4 +2593,50 @@ }); } +jsi::Value +CommCoreModule::getLatestMessageEdit(jsi::Runtime &rt, jsi::String messageID) { + std::string messageIDStr = messageID.utf8(rt); + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=, &innerRt]() { + std::string error; + std::shared_ptr messageEntity; + try { + auto messageEntityResult = + DatabaseManager::getQueryExecutor().getLatestMessageEdit( + messageIDStr); + if (messageEntityResult.has_value()) { + messageEntity = std::make_shared( + std::move(messageEntityResult.value())); + } + } catch (std::system_error &e) { + error = e.what(); + } + + this->jsInvoker_->invokeAsync([&innerRt, + error, + promise, + messageEntity, + messageStore = this->messageStore]() { + if (error.size()) { + promise->reject(error); + return; + } else if (!messageEntity) { + promise->resolve(jsi::Value::undefined()); + return; + } + + auto vector = std::make_shared>(); + vector->push_back(std::move(*messageEntity)); + jsi::Array jsiMessages = + messageStore.parseDBDataStore(innerRt, vector); + promise->resolve( + jsiMessages.getValueAtIndex(innerRt, 0).asObject(innerRt)); + }); + }; + 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 @@ -208,6 +208,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_markPrekeysAsPublished(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->markPrekeysAsPublished(rt); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getLatestMessageEdit(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getLatestMessageEdit(rt, args[0].asString(rt)); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -274,6 +277,7 @@ methodMap_["removeOutboundP2PMessagesOlderThan"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_removeOutboundP2PMessagesOlderThan}; methodMap_["getSyncedDatabaseVersion"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getSyncedDatabaseVersion}; methodMap_["markPrekeysAsPublished"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_markPrekeysAsPublished}; + methodMap_["getLatestMessageEdit"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getLatestMessageEdit}; } 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 @@ -83,6 +83,7 @@ virtual jsi::Value removeOutboundP2PMessagesOlderThan(jsi::Runtime &rt, jsi::String messageID, jsi::String deviceID) = 0; virtual jsi::Value getSyncedDatabaseVersion(jsi::Runtime &rt) = 0; virtual jsi::Value markPrekeysAsPublished(jsi::Runtime &rt) = 0; + virtual jsi::Value getLatestMessageEdit(jsi::Runtime &rt, jsi::String messageID) = 0; }; @@ -608,6 +609,14 @@ return bridging::callFromJs( rt, &T::markPrekeysAsPublished, jsInvoker_, instance_); } + jsi::Value getLatestMessageEdit(jsi::Runtime &rt, jsi::String messageID) override { + static_assert( + bridging::getParameterCount(&T::getLatestMessageEdit) == 2, + "Expected getLatestMessageEdit(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getLatestMessageEdit, jsInvoker_, instance_, std::move(messageID)); + } 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 @@ -165,6 +165,7 @@ ) => Promise; +getSyncedDatabaseVersion: () => Promise; +markPrekeysAsPublished: () => Promise; + +getLatestMessageEdit: (messageID: string) => Promise; } export interface CoreModuleSpec extends Spec {