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 @@ -237,6 +237,12 @@ virtual jsi::Value markPrekeysAsPublished(jsi::Runtime &rt) override; virtual jsi::Value getRelatedMessages(jsi::Runtime &rt, jsi::String messageID) override; + virtual jsi::Value searchMessages( + jsi::Runtime &rt, + jsi::String query, + jsi::String threadID, + std::optional timestampCursor, + std::optional messageIDCursor) 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 @@ -2698,4 +2698,59 @@ }); } +jsi::Value CommCoreModule::searchMessages( + jsi::Runtime &rt, + jsi::String query, + jsi::String threadID, + std::optional timestampCursor, + std::optional messageIDCursor) { + std::string queryStr = query.utf8(rt); + std::string threadIDStr = threadID.utf8(rt); + + std::optional timestampCursorCpp; + if (timestampCursor) { + timestampCursorCpp = timestampCursor->utf8(rt); + } + + std::optional messageIDCursorCpp; + if (messageIDCursor) { + messageIDCursorCpp = messageIDCursor->utf8(rt); + } + + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=, &innerRt]() { + std::string error; + std::shared_ptr> messages; + try { + messages = std::make_shared>( + DatabaseManager::getQueryExecutor().searchMessages( + queryStr, + threadIDStr, + timestampCursorCpp, + messageIDCursorCpp)); + } catch (std::system_error &e) { + error = e.what(); + } + + this->jsInvoker_->invokeAsync([&innerRt, + error, + promise, + messages, + messageStore = this->messageStore]() { + if (error.size()) { + promise->reject(error); + return; + } + + jsi::Array jsiMessages = + messageStore.parseDBDataStore(innerRt, messages); + 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 @@ -214,6 +214,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getRelatedMessages(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->getRelatedMessages(rt, args[0].asString(rt)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_searchMessages(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->searchMessages(rt, args[0].asString(rt), args[1].asString(rt), args[2].isNull() || args[2].isUndefined() ? std::nullopt : std::make_optional(args[2].asString(rt)), args[3].isNull() || args[3].isUndefined() ? std::nullopt : std::make_optional(args[3].asString(rt))); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -282,6 +285,7 @@ methodMap_["getSyncedDatabaseVersion"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getSyncedDatabaseVersion}; methodMap_["markPrekeysAsPublished"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_markPrekeysAsPublished}; methodMap_["getRelatedMessages"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getRelatedMessages}; + methodMap_["searchMessages"] = MethodMetadata {4, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_searchMessages}; } 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 @@ -85,6 +85,7 @@ virtual jsi::Value getSyncedDatabaseVersion(jsi::Runtime &rt) = 0; virtual jsi::Value markPrekeysAsPublished(jsi::Runtime &rt) = 0; 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; }; @@ -626,6 +627,14 @@ return bridging::callFromJs( rt, &T::getRelatedMessages, jsInvoker_, instance_, std::move(messageID)); } + jsi::Value searchMessages(jsi::Runtime &rt, jsi::String query, jsi::String threadID, std::optional timestampCursor, std::optional messageIDCursor) override { + static_assert( + bridging::getParameterCount(&T::searchMessages) == 5, + "Expected searchMessages(...) to have 5 parameters"); + + return bridging::callFromJs( + rt, &T::searchMessages, jsInvoker_, instance_, std::move(query), std::move(threadID), std::move(timestampCursor), std::move(messageIDCursor)); + } 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 @@ -169,6 +169,12 @@ +getSyncedDatabaseVersion: () => Promise; +markPrekeysAsPublished: () => Promise; +getRelatedMessages: (messageID: string) => Promise; + +searchMessages: ( + query: string, + threadID: string, + timestampCursor: ?string, + messageIDCursor: ?string, + ) => Promise; } export interface CoreModuleSpec extends Spec {