diff --git a/lib/types/draft-types.js b/lib/types/draft-types.js --- a/lib/types/draft-types.js +++ b/lib/types/draft-types.js @@ -23,9 +23,15 @@ +type: 'remove_all', }; +export type RemoveDraftsOperation = { + +type: 'remove', + +payload: { +ids: $ReadOnlyArray }, +}; + export type DraftStoreOperation = | UpdateDraftOperation | MoveDraftOperation - | RemoveAllDraftsOperation; + | RemoveAllDraftsOperation + | RemoveDraftsOperation; export type ClientDBDraftStoreOperation = DraftStoreOperation; diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h @@ -30,6 +30,7 @@ virtual bool moveDraft(std::string oldKey, std::string newKey) const = 0; virtual std::vector getAllDrafts() const = 0; virtual void removeAllDrafts() const = 0; + virtual void removeDrafts(const std::vector &ids) const = 0; virtual void removeAllMessages() const = 0; virtual std::vector>> getAllMessages() const = 0; diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h @@ -35,6 +35,7 @@ bool moveDraft(std::string oldKey, std::string newKey) const override; std::vector getAllDrafts() const override; void removeAllDrafts() const override; + void removeDrafts(const std::vector &ids) const override; void removeAllMessages() const override; std::vector>> getAllMessages() const override; diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -1111,6 +1111,12 @@ SQLiteQueryExecutor::getStorage().remove_all(); } +void SQLiteQueryExecutor::removeDrafts( + const std::vector &ids) const { + SQLiteQueryExecutor::getStorage().remove_all( + where(in(&Draft::key, ids))); +} + void SQLiteQueryExecutor::removeAllMessages() const { SQLiteQueryExecutor::getStorage().remove_all(); } diff --git a/native/cpp/CommonCpp/NativeModules/DraftStoreOperations.h b/native/cpp/CommonCpp/NativeModules/DraftStoreOperations.h --- a/native/cpp/CommonCpp/NativeModules/DraftStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/DraftStoreOperations.h @@ -50,4 +50,23 @@ } }; +class RemoveDraftsOperation : public DraftStoreOperationBase { +public: + RemoveDraftsOperation(jsi::Runtime &rt, const jsi::Object &payload) + : idsToRemove{} { + auto payload_ids = payload.getProperty(rt, "ids").asObject(rt).asArray(rt); + for (size_t idx = 0; idx < payload_ids.size(rt); idx++) { + this->idsToRemove.push_back( + payload_ids.getValueAtIndex(rt, idx).asString(rt).utf8(rt)); + } + } + + virtual void execute() override { + DatabaseManager::getQueryExecutor().removeDrafts(this->idsToRemove); + } + +private: + std::vector idsToRemove; +}; + } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.h --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.h @@ -13,6 +13,7 @@ static OperationType UPDATE_DRAFT_OPERATION; static OperationType MOVE_DRAFT_OPERATION; static OperationType REMOVE_ALL_DRAFTS_OPERATION; + static OperationType REMOVE_DRAFTS_OPERATION; public: DraftStore(std::shared_ptr jsInvoker); diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.cpp --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.cpp @@ -8,6 +8,7 @@ OperationType DraftStore::UPDATE_DRAFT_OPERATION = "update"; OperationType DraftStore::MOVE_DRAFT_OPERATION = "move"; OperationType DraftStore::REMOVE_ALL_DRAFTS_OPERATION = "remove_all"; +OperationType DraftStore::REMOVE_DRAFTS_OPERATION = "remove"; DraftStore::DraftStore(std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { @@ -55,6 +56,9 @@ } else if (op_type == MOVE_DRAFT_OPERATION) { draftStoreOps.push_back( std::make_unique(rt, payload_obj)); + } else if (op_type == REMOVE_DRAFTS_OPERATION) { + draftStoreOps.push_back( + std::make_unique(rt, payload_obj)); } else { throw std::runtime_error("unsupported operation: " + op_type); } diff --git a/web/cpp/SQLiteQueryExecutorBindings.cpp b/web/cpp/SQLiteQueryExecutorBindings.cpp --- a/web/cpp/SQLiteQueryExecutorBindings.cpp +++ b/web/cpp/SQLiteQueryExecutorBindings.cpp @@ -67,6 +67,7 @@ .function("moveDraft", &SQLiteQueryExecutor::moveDraft) .function("getAllDrafts", &SQLiteQueryExecutor::getAllDrafts) .function("removeAllDrafts", &SQLiteQueryExecutor::removeAllDrafts) + .function("removeDrafts", &SQLiteQueryExecutor::removeDrafts) .function("setMetadata", &SQLiteQueryExecutor::setMetadata) .function("clearMetadata", &SQLiteQueryExecutor::clearMetadata) .function("getMetadata", &SQLiteQueryExecutor::getMetadata) diff --git a/web/database/_generated/comm_query_executor.wasm b/web/database/_generated/comm_query_executor.wasm index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@ d.key === newKey); expect(newKeyDraft?.text).toBe(draftText); }); + + it('should remove drafts with specified keys', () => { + queryExecutor.removeDrafts(['thread_a']); + const drafts = queryExecutor.getAllDrafts(); + expect(drafts).toEqual([{ key: 'thread_b', text: 'draft b' }]); + }); }); diff --git a/web/database/types/sqlite-query-executor.js b/web/database/types/sqlite-query-executor.js --- a/web/database/types/sqlite-query-executor.js +++ b/web/database/types/sqlite-query-executor.js @@ -14,6 +14,7 @@ moveDraft(oldKey: string, newKey: string): boolean; getAllDrafts(): ClientDBDraftInfo[]; removeAllDrafts(): void; + removeDrafts(ids: $ReadOnlyArray): void; setMetadata(entryName: string, data: string): void; clearMetadata(entryName: string): void; diff --git a/web/database/worker/process-operations.js b/web/database/worker/process-operations.js --- a/web/database/worker/process-operations.js +++ b/web/database/worker/process-operations.js @@ -37,6 +37,9 @@ try { if (operation.type === 'remove_all') { sqliteQueryExecutor.removeAllDrafts(); + } else if (operation.type === 'remove') { + const { ids } = operation.payload; + sqliteQueryExecutor.removeDrafts(ids); } else if (operation.type === 'update') { const { key, text } = operation.payload; sqliteQueryExecutor.updateDraft(key, text);