diff --git a/native/cpp/CommonCpp/NativeModules/AuxUserStoreOperations.h b/native/cpp/CommonCpp/NativeModules/AuxUserStoreOperations.h index 894d3fb83..23333ed01 100644 --- a/native/cpp/CommonCpp/NativeModules/AuxUserStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/AuxUserStoreOperations.h @@ -1,48 +1,44 @@ #pragma once #include "../DatabaseManagers/entities/AuxUserInfo.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class AuxUserStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~AuxUserStoreOperationBase(){}; -}; -class RemoveAuxUserInfosOperation : public AuxUserStoreOperationBase { +class RemoveAuxUserInfosOperation : public DBOperationBase { public: RemoveAuxUserInfosOperation(std::vector ids) : ids{ids} { } virtual void execute() override { DatabaseManager::getQueryExecutor().removeAuxUserInfos(this->ids); } private: std::vector ids; }; -class ReplaceAuxUserInfoOperation : public AuxUserStoreOperationBase { +class ReplaceAuxUserInfoOperation : public DBOperationBase { public: ReplaceAuxUserInfoOperation(AuxUserInfo &&auxUserInfo) : auxUserInfo{std::move(auxUserInfo)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceAuxUserInfo(this->auxUserInfo); } private: AuxUserInfo auxUserInfo; }; -class RemoveAllAuxUserInfosOperation : public AuxUserStoreOperationBase { +class RemoveAllAuxUserInfosOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllAuxUserInfos(); } }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/CommunityStoreOperations.h b/native/cpp/CommonCpp/NativeModules/CommunityStoreOperations.h index 9241abc3f..63e3f9c79 100644 --- a/native/cpp/CommonCpp/NativeModules/CommunityStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/CommunityStoreOperations.h @@ -1,48 +1,44 @@ #pragma once #include "../DatabaseManagers/entities/CommunityInfo.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class CommunityStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~CommunityStoreOperationBase(){}; -}; -class RemoveCommunitiesOperation : public CommunityStoreOperationBase { +class RemoveCommunitiesOperation : public DBOperationBase { public: RemoveCommunitiesOperation(std::vector ids) : ids{ids} { } virtual void execute() override { DatabaseManager::getQueryExecutor().removeCommunities(this->ids); } private: std::vector ids; }; -class ReplaceCommunityOperation : public CommunityStoreOperationBase { +class ReplaceCommunityOperation : public DBOperationBase { public: ReplaceCommunityOperation(CommunityInfo &&community) : community{std::move(community)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceCommunity(this->community); } private: CommunityInfo community; }; -class RemoveAllCommunitiesOperation : public CommunityStoreOperationBase { +class RemoveAllCommunitiesOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllCommunities(); } }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/DBOperationBase.h b/native/cpp/CommonCpp/NativeModules/DBOperationBase.h new file mode 100644 index 000000000..9c421f70e --- /dev/null +++ b/native/cpp/CommonCpp/NativeModules/DBOperationBase.h @@ -0,0 +1,11 @@ +#pragma once + +namespace comm { + +class DBOperationBase { +public: + virtual void execute() = 0; + virtual ~DBOperationBase(){}; +}; + +} // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/DraftStoreOperations.h b/native/cpp/CommonCpp/NativeModules/DraftStoreOperations.h index dd1271060..e69a24b5f 100644 --- a/native/cpp/CommonCpp/NativeModules/DraftStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/DraftStoreOperations.h @@ -1,72 +1,67 @@ #pragma once +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { namespace jsi = facebook::jsi; -class DraftStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~DraftStoreOperationBase(){}; -}; - -class UpdateDraftOperation : public DraftStoreOperationBase { +class UpdateDraftOperation : public DBOperationBase { public: UpdateDraftOperation(jsi::Runtime &rt, const jsi::Object &payload) : key{payload.getProperty(rt, "key").asString(rt).utf8(rt)}, text{payload.getProperty(rt, "text").asString(rt).utf8(rt)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().updateDraft(this->key, this->text); } private: std::string key; std::string text; }; -class MoveDraftOperation : public DraftStoreOperationBase { +class MoveDraftOperation : public DBOperationBase { public: MoveDraftOperation(jsi::Runtime &rt, const jsi::Object &payload) : oldKey{payload.getProperty(rt, "oldKey").asString(rt).utf8(rt)}, newKey{payload.getProperty(rt, "newKey").asString(rt).utf8(rt)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().moveDraft(this->oldKey, this->newKey); } private: std::string oldKey; std::string newKey; }; -class RemoveAllDraftsOperation : public DraftStoreOperationBase { +class RemoveAllDraftsOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllDrafts(); } }; -class RemoveDraftsOperation : public DraftStoreOperationBase { +class RemoveDraftsOperation : public DBOperationBase { 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/IntegrityStoreOperations.h b/native/cpp/CommonCpp/NativeModules/IntegrityStoreOperations.h index e2233c86d..c9f4496bd 100644 --- a/native/cpp/CommonCpp/NativeModules/IntegrityStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/IntegrityStoreOperations.h @@ -1,54 +1,47 @@ #pragma once #include "../DatabaseManagers/entities/IntegrityThreadHash.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class IntegrityStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~IntegrityStoreOperationBase(){}; -}; -class RemoveIntegrityThreadHashesOperation - : public IntegrityStoreOperationBase { +class RemoveIntegrityThreadHashesOperation : public DBOperationBase { public: RemoveIntegrityThreadHashesOperation(std::vector ids) : ids{ids} { } virtual void execute() override { DatabaseManager::getQueryExecutor().removeIntegrityThreadHashes(this->ids); } private: std::vector ids; }; -class ReplaceIntegrityThreadHashesOperation - : public IntegrityStoreOperationBase { +class ReplaceIntegrityThreadHashesOperation : public DBOperationBase { public: ReplaceIntegrityThreadHashesOperation( std::vector &&threadHashes) : threadHashes{std::move(threadHashes)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceIntegrityThreadHashes( this->threadHashes); } private: std::vector threadHashes; }; -class RemoveAllIntegrityThreadHashesOperation - : public IntegrityStoreOperationBase { +class RemoveAllIntegrityThreadHashesOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllIntegrityThreadHashes(); } }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/KeyserverStoreOperations.h b/native/cpp/CommonCpp/NativeModules/KeyserverStoreOperations.h index 268ac780a..f33a196b3 100644 --- a/native/cpp/CommonCpp/NativeModules/KeyserverStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/KeyserverStoreOperations.h @@ -1,48 +1,44 @@ #pragma once #include "../DatabaseManagers/entities/KeyserverInfo.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class KeyserverStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~KeyserverStoreOperationBase(){}; -}; -class RemoveKeyserversOperation : public KeyserverStoreOperationBase { +class RemoveKeyserversOperation : public DBOperationBase { public: RemoveKeyserversOperation(std::vector ids) : ids{ids} { } virtual void execute() override { DatabaseManager::getQueryExecutor().removeKeyservers(this->ids); } private: std::vector ids; }; -class ReplaceKeyserverOperation : public KeyserverStoreOperationBase { +class ReplaceKeyserverOperation : public DBOperationBase { public: ReplaceKeyserverOperation(KeyserverInfo &&keyserver) : keyserver{std::move(keyserver)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceKeyserver(this->keyserver); } private: KeyserverInfo keyserver; }; -class RemoveAllKeyserversOperation : public KeyserverStoreOperationBase { +class RemoveAllKeyserversOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllKeyservers(); } }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/MessageStoreOperations.h b/native/cpp/CommonCpp/NativeModules/MessageStoreOperations.h index 65b22db99..6ee056e8d 100644 --- a/native/cpp/CommonCpp/NativeModules/MessageStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/MessageStoreOperations.h @@ -1,217 +1,213 @@ #pragma once #include "../DatabaseManagers/entities/Media.h" #include "../DatabaseManagers/entities/Message.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class MessageStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~MessageStoreOperationBase(){}; -}; -class RemoveMessagesOperation : public MessageStoreOperationBase { +class RemoveMessagesOperation : public DBOperationBase { public: RemoveMessagesOperation(jsi::Runtime &rt, const jsi::Object &payload) : msg_ids_to_remove{} { auto payload_ids = payload.getProperty(rt, "ids").asObject(rt).asArray(rt); for (size_t idx = 0; idx < payload_ids.size(rt); idx++) { this->msg_ids_to_remove.push_back( payload_ids.getValueAtIndex(rt, idx).asString(rt).utf8(rt)); } } virtual void execute() override { DatabaseManager::getQueryExecutor().removeMessages(this->msg_ids_to_remove); DatabaseManager::getQueryExecutor().removeMediaForMessages( this->msg_ids_to_remove); } private: std::vector msg_ids_to_remove; }; -class RemoveMessagesForThreadsOperation : public MessageStoreOperationBase { +class RemoveMessagesForThreadsOperation : public DBOperationBase { public: RemoveMessagesForThreadsOperation( jsi::Runtime &rt, const jsi::Object &payload) : thread_ids{} { auto payload_ids = payload.getProperty(rt, "threadIDs").asObject(rt).asArray(rt); for (size_t idx = 0; idx < payload_ids.size(rt); idx++) { this->thread_ids.push_back( payload_ids.getValueAtIndex(rt, idx).asString(rt).utf8(rt)); } } virtual void execute() override { DatabaseManager::getQueryExecutor().removeMessagesForThreads( this->thread_ids); DatabaseManager::getQueryExecutor().removeMediaForThreads(this->thread_ids); } private: std::vector thread_ids; }; -class ReplaceMessageOperation : public MessageStoreOperationBase { +class ReplaceMessageOperation : public DBOperationBase { public: ReplaceMessageOperation(jsi::Runtime &rt, const jsi::Object &payload) : media_vector{} { auto msg_id = payload.getProperty(rt, "id").asString(rt).utf8(rt); auto maybe_local_id = payload.getProperty(rt, "local_id"); auto local_id = maybe_local_id.isString() ? std::make_unique(maybe_local_id.asString(rt).utf8(rt)) : nullptr; auto thread = payload.getProperty(rt, "thread").asString(rt).utf8(rt); auto user = payload.getProperty(rt, "user").asString(rt).utf8(rt); auto type = std::stoi(payload.getProperty(rt, "type").asString(rt).utf8(rt)); auto maybe_future_type = payload.getProperty(rt, "future_type"); auto future_type = maybe_future_type.isString() ? std::make_unique( std::stoi(maybe_future_type.asString(rt).utf8(rt))) : nullptr; auto maybe_content = payload.getProperty(rt, "content"); auto content = maybe_content.isString() ? std::make_unique(maybe_content.asString(rt).utf8(rt)) : nullptr; auto time = std::stoll(payload.getProperty(rt, "time").asString(rt).utf8(rt)); this->msg = std::make_unique(Message{ msg_id, std::move(local_id), thread, user, type, std::move(future_type), std::move(content), time}); if (payload.getProperty(rt, "media_infos").isObject()) { auto media_infos = payload.getProperty(rt, "media_infos").asObject(rt).asArray(rt); for (size_t media_info_idx = 0; media_info_idx < media_infos.size(rt); media_info_idx++) { auto media_info = media_infos.getValueAtIndex(rt, media_info_idx).asObject(rt); auto media_id = media_info.getProperty(rt, "id").asString(rt).utf8(rt); auto media_uri = media_info.getProperty(rt, "uri").asString(rt).utf8(rt); auto media_type = media_info.getProperty(rt, "type").asString(rt).utf8(rt); auto media_extras = media_info.getProperty(rt, "extras").asString(rt).utf8(rt); this->media_vector.push_back(std::make_unique(Media{ media_id, msg_id, thread, media_uri, media_type, media_extras})); } } } virtual void execute() override { DatabaseManager::getQueryExecutor().removeMediaForMessage(msg->id); for (auto &&media : this->media_vector) { DatabaseManager::getQueryExecutor().replaceMedia(std::move(*media)); } DatabaseManager::getQueryExecutor().replaceMessage(std::move(*this->msg)); } private: std::unique_ptr msg; std::vector> media_vector; }; -class RekeyMessageOperation : public MessageStoreOperationBase { +class RekeyMessageOperation : public DBOperationBase { public: RekeyMessageOperation(jsi::Runtime &rt, const jsi::Object &payload) { this->from = payload.getProperty(rt, "from").asString(rt).utf8(rt); this->to = payload.getProperty(rt, "to").asString(rt).utf8(rt); } virtual void execute() override { DatabaseManager::getQueryExecutor().rekeyMessage(this->from, this->to); DatabaseManager::getQueryExecutor().rekeyMediaContainers( this->from, this->to); } private: std::string from; std::string to; }; -class RemoveAllMessagesOperation : public MessageStoreOperationBase { +class RemoveAllMessagesOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllMessages(); DatabaseManager::getQueryExecutor().removeAllMedia(); } }; -class ReplaceMessageThreadsOperation : public MessageStoreOperationBase { +class ReplaceMessageThreadsOperation : public DBOperationBase { public: ReplaceMessageThreadsOperation(jsi::Runtime &rt, const jsi::Object &payload) : msg_threads{} { auto threads = payload.getProperty(rt, "threads").asObject(rt).asArray(rt); for (size_t idx = 0; idx < threads.size(rt); idx++) { auto thread = threads.getValueAtIndex(rt, idx).asObject(rt); auto thread_id = thread.getProperty(rt, "id").asString(rt).utf8(rt); auto start_reached = std::stoi( thread.getProperty(rt, "start_reached").asString(rt).utf8(rt)); MessageStoreThread msg_thread = MessageStoreThread{thread_id, start_reached}; this->msg_threads.push_back(msg_thread); } } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceMessageStoreThreads( this->msg_threads); } private: std::vector msg_threads; }; -class RemoveAllMessageStoreThreadsOperation : public MessageStoreOperationBase { +class RemoveAllMessageStoreThreadsOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllMessageStoreThreads(); } }; -class RemoveMessageStoreThreadsOperation : public MessageStoreOperationBase { +class RemoveMessageStoreThreadsOperation : public DBOperationBase { public: RemoveMessageStoreThreadsOperation( jsi::Runtime &rt, const jsi::Object &payload) : thread_ids{} { auto payload_ids = payload.getProperty(rt, "ids").asObject(rt).asArray(rt); for (size_t idx = 0; idx < payload_ids.size(rt); idx++) { this->thread_ids.push_back( payload_ids.getValueAtIndex(rt, idx).asString(rt).utf8(rt)); } } virtual void execute() override { DatabaseManager::getQueryExecutor().removeMessageStoreThreads( this->thread_ids); } private: std::vector thread_ids; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/AuxUserStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/AuxUserStore.cpp index 4607d3ff6..b39338309 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/AuxUserStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/AuxUserStore.cpp @@ -1,76 +1,77 @@ #include "AuxUserStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType AuxUserStore::REMOVE_OPERATION = "remove_aux_user_infos"; OperationType AuxUserStore::REMOVE_ALL_OPERATION = "remove_all_aux_user_infos"; OperationType AuxUserStore::REPLACE_OPERATION = "replace_aux_user_info"; AuxUserStore::AuxUserStore( std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array AuxUserStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> auxUserInfosVectorPtr) const { size_t numAuxUserInfos = auxUserInfosVectorPtr->size(); jsi::Array jsiAuxUserInfos = jsi::Array(rt, numAuxUserInfos); size_t writeIdx = 0; for (const AuxUserInfo &auxUserInfo : *auxUserInfosVectorPtr) { jsi::Object jsiAuxUserInfo = jsi::Object(rt); jsiAuxUserInfo.setProperty(rt, "id", auxUserInfo.id); jsiAuxUserInfo.setProperty(rt, "auxUserInfo", auxUserInfo.aux_user_info); jsiAuxUserInfos.setValueAtIndex(rt, writeIdx++, jsiAuxUserInfo); } return jsiAuxUserInfos; } -std::vector> -AuxUserStore::createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const { - std::vector> auxUserStoreOps; +std::vector> AuxUserStore::createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const { + std::vector> auxUserStoreOps; for (size_t idx = 0; idx < operations.size(rt); idx++) { jsi::Object op = operations.getValueAtIndex(rt, idx).asObject(rt); std::string opType = op.getProperty(rt, "type").asString(rt).utf8(rt); if (opType == REMOVE_OPERATION) { std::vector auxUserIDsToRemove; jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); jsi::Array auxUserIDs = payloadObj.getProperty(rt, "ids").asObject(rt).asArray(rt); for (int auxUserInfoIdx = 0; auxUserInfoIdx < auxUserIDs.size(rt); auxUserInfoIdx++) { auxUserIDsToRemove.push_back( auxUserIDs.getValueAtIndex(rt, auxUserInfoIdx) .asString(rt) .utf8(rt)); } auxUserStoreOps.push_back(std::make_unique( std::move(auxUserIDsToRemove))); } else if (opType == REMOVE_ALL_OPERATION) { auxUserStoreOps.push_back( std::make_unique()); } else if (opType == REPLACE_OPERATION) { jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); std::string id = payloadObj.getProperty(rt, "id").asString(rt).utf8(rt); std::string aux_user_info = payloadObj.getProperty(rt, "auxUserInfo").asString(rt).utf8(rt); AuxUserInfo auxUserInfo{id, aux_user_info}; auxUserStoreOps.push_back(std::make_unique( std::move(auxUserInfo))); } else { throw std::runtime_error("unsupported operation: " + opType); } }; return auxUserStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/AuxUserStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/AuxUserStore.h index 1a1881c95..c8d530fd2 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/AuxUserStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/AuxUserStore.h @@ -1,30 +1,30 @@ #pragma once #include "../../../DatabaseManagers/entities/AuxUserInfo.h" +#include "../../DBOperationBase.h" #include "AuxUserStoreOperations.h" #include "BaseDataStore.h" #include namespace comm { -class AuxUserStore - : public BaseDataStore { +class AuxUserStore : public BaseDataStore { private: static OperationType REMOVE_OPERATION; static OperationType REMOVE_ALL_OPERATION; static OperationType REPLACE_OPERATION; public: AuxUserStore(std::shared_ptr jsInvoker); - std::vector> createOperations( + std::vector> createOperations( jsi::Runtime &rt, const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/CommunityStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/CommunityStore.cpp index 30cb14148..c2dadd834 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/CommunityStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/CommunityStore.cpp @@ -1,75 +1,76 @@ #include "CommunityStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType CommunityStore::REMOVE_OPERATION = "remove_communities"; OperationType CommunityStore::REMOVE_ALL_OPERATION = "remove_all_communities"; OperationType CommunityStore::REPLACE_OPERATION = "replace_community"; CommunityStore::CommunityStore( std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array CommunityStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> communitiesVectorPtr) const { size_t numCommunities = communitiesVectorPtr->size(); jsi::Array jsiCommunities = jsi::Array(rt, numCommunities); size_t writeIdx = 0; for (const CommunityInfo &community : *communitiesVectorPtr) { jsi::Object jsiCommunity = jsi::Object(rt); jsiCommunity.setProperty(rt, "id", community.id); jsiCommunity.setProperty(rt, "communityInfo", community.community_info); jsiCommunities.setValueAtIndex(rt, writeIdx++, jsiCommunity); } return jsiCommunities; } -std::vector> -CommunityStore::createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const { - std::vector> communityStoreOps; +std::vector> CommunityStore::createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const { + std::vector> communityStoreOps; for (size_t idx = 0; idx < operations.size(rt); idx++) { jsi::Object op = operations.getValueAtIndex(rt, idx).asObject(rt); std::string opType = op.getProperty(rt, "type").asString(rt).utf8(rt); if (opType == REMOVE_OPERATION) { std::vector communityIDsToRemove; jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); jsi::Array communityIDs = payloadObj.getProperty(rt, "ids").asObject(rt).asArray(rt); for (int communityIdx = 0; communityIdx < communityIDs.size(rt); communityIdx++) { communityIDsToRemove.push_back( communityIDs.getValueAtIndex(rt, communityIdx) .asString(rt) .utf8(rt)); } communityStoreOps.push_back(std::make_unique( std::move(communityIDsToRemove))); } else if (opType == REMOVE_ALL_OPERATION) { communityStoreOps.push_back( std::make_unique()); } else if (opType == REPLACE_OPERATION) { jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); std::string id = payloadObj.getProperty(rt, "id").asString(rt).utf8(rt); std::string community_info = payloadObj.getProperty(rt, "communityInfo").asString(rt).utf8(rt); CommunityInfo community{id, community_info}; communityStoreOps.push_back( std::make_unique(std::move(community))); } else { throw std::runtime_error("unsupported operation: " + opType); } }; return communityStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/CommunityStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/CommunityStore.h index 2e504d220..7711bca59 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/CommunityStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/CommunityStore.h @@ -1,30 +1,30 @@ #pragma once #include "../../../DatabaseManagers/entities/CommunityInfo.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "CommunityStoreOperations.h" #include namespace comm { -class CommunityStore - : public BaseDataStore { +class CommunityStore : public BaseDataStore { private: static OperationType REMOVE_OPERATION; static OperationType REMOVE_ALL_OPERATION; static OperationType REPLACE_OPERATION; public: CommunityStore(std::shared_ptr jsInvoker); - std::vector> createOperations( + std::vector> createOperations( jsi::Runtime &rt, const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.cpp index 7773d812a..f18a56aee 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.cpp @@ -1,69 +1,70 @@ #include "DraftStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { 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) { } jsi::Array DraftStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> draftsVectorPtr) const { size_t numDrafts = count_if( draftsVectorPtr->begin(), draftsVectorPtr->end(), [](Draft draft) { return !draft.text.empty(); }); jsi::Array jsiDrafts = jsi::Array(rt, numDrafts); size_t writeIndex = 0; for (Draft draft : *draftsVectorPtr) { if (draft.text.empty()) { continue; } auto jsiDraft = jsi::Object(rt); jsiDraft.setProperty(rt, "key", draft.key); jsiDraft.setProperty(rt, "text", draft.text); jsiDrafts.setValueAtIndex(rt, writeIndex++, jsiDraft); } return jsiDrafts; } -std::vector> -DraftStore::createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const { - std::vector> draftStoreOps; +std::vector> DraftStore::createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const { + std::vector> draftStoreOps; for (auto idx = 0; idx < operations.size(rt); idx++) { auto op = operations.getValueAtIndex(rt, idx).asObject(rt); auto op_type = op.getProperty(rt, "type").asString(rt).utf8(rt); if (op_type == REMOVE_ALL_DRAFTS_OPERATION) { draftStoreOps.push_back(std::make_unique()); continue; } auto payload_obj = op.getProperty(rt, "payload").asObject(rt); if (op_type == UPDATE_DRAFT_OPERATION) { draftStoreOps.push_back( std::make_unique(rt, payload_obj)); } 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); } } return draftStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.h index ebffaddf8..5805cfeb7 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/DraftStore.h @@ -1,30 +1,31 @@ #pragma once #include "../../../DatabaseManagers/entities/Draft.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "DraftStoreOperations.h" #include namespace comm { -class DraftStore : public BaseDataStore { +class DraftStore : public BaseDataStore { private: 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); - std::vector> createOperations( + std::vector> createOperations( jsi::Runtime &rt, const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/IntegrityStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/IntegrityStore.cpp index 1a9676eb9..ade6c1d33 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/IntegrityStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/IntegrityStore.cpp @@ -1,103 +1,104 @@ #include "IntegrityStore.h" #include "../../../DatabaseManagers/entities/IntegrityThreadHash.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType IntegrityStore::REMOVE_OPERATION = "remove_integrity_thread_hashes"; OperationType IntegrityStore::REMOVE_ALL_OPERATION = "remove_all_integrity_thread_hashes"; OperationType IntegrityStore::REPLACE_OPERATION = "replace_integrity_thread_hashes"; IntegrityStore::IntegrityStore( std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array IntegrityStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> integrityThreadHashesVectorPtr) const { size_t numIntegrityThreadHashes = integrityThreadHashesVectorPtr->size(); jsi::Array jsiIntegrityThreadHashes = jsi::Array(rt, numIntegrityThreadHashes); size_t writeIdx = 0; for (const IntegrityThreadHash &integrityThreadHash : *integrityThreadHashesVectorPtr) { jsi::Object jsiIntegrityThreadHash = jsi::Object(rt); jsiIntegrityThreadHash.setProperty(rt, "id", integrityThreadHash.id); jsiIntegrityThreadHash.setProperty( rt, "threadHash", integrityThreadHash.thread_hash); jsiIntegrityThreadHashes.setValueAtIndex( rt, writeIdx++, jsiIntegrityThreadHash); } return jsiIntegrityThreadHashes; } -std::vector> -IntegrityStore::createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const { - std::vector> integrityStoreOps; +std::vector> IntegrityStore::createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const { + std::vector> integrityStoreOps; for (size_t idx = 0; idx < operations.size(rt); idx++) { jsi::Object op = operations.getValueAtIndex(rt, idx).asObject(rt); std::string opType = op.getProperty(rt, "type").asString(rt).utf8(rt); if (opType == REMOVE_OPERATION) { std::vector integrityThreadHashIDsToRemove; jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); jsi::Array integrityThreadHashIDs = payloadObj.getProperty(rt, "ids").asObject(rt).asArray(rt); for (int integrityThreadHashIDIdx = 0; integrityThreadHashIDIdx < integrityThreadHashIDs.size(rt); integrityThreadHashIDIdx++) { integrityThreadHashIDsToRemove.push_back( integrityThreadHashIDs.getValueAtIndex(rt, integrityThreadHashIDIdx) .asString(rt) .utf8(rt)); } integrityStoreOps.push_back( std::make_unique( std::move(integrityThreadHashIDsToRemove))); } else if (opType == REMOVE_ALL_OPERATION) { integrityStoreOps.push_back( std::make_unique()); } else if (opType == REPLACE_OPERATION) { std::vector integrityThreadHashesToReplace; jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); jsi::Array integrityThreadHashes = payloadObj.getProperty(rt, "threadHashes").asObject(rt).asArray(rt); for (int integrityThreadHashIdx = 0; integrityThreadHashIdx < integrityThreadHashes.size(rt); integrityThreadHashIdx++) { jsi::Object integrityThreadHashObj = integrityThreadHashes.getValueAtIndex(rt, integrityThreadHashIdx) .asObject(rt); std::string id = integrityThreadHashObj.getProperty(rt, "id").asString(rt).utf8(rt); std::string threadHash = integrityThreadHashObj.getProperty(rt, "threadHash") .asString(rt) .utf8(rt); IntegrityThreadHash integrityThreadHash{id, threadHash}; integrityThreadHashesToReplace.push_back(integrityThreadHash); } integrityStoreOps.push_back( std::make_unique( std::move(integrityThreadHashesToReplace))); } else { throw std::runtime_error("unsupported operation: " + opType); } }; return integrityStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/IntegrityStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/IntegrityStore.h index 8bd928bef..d4406b0a0 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/IntegrityStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/IntegrityStore.h @@ -1,31 +1,32 @@ #pragma once #include "../../../DatabaseManagers/entities/IntegrityThreadHash.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "IntegrityStoreOperations.h" #include namespace comm { class IntegrityStore - : public BaseDataStore { + : public BaseDataStore { private: static OperationType REMOVE_OPERATION; static OperationType REMOVE_ALL_OPERATION; static OperationType REPLACE_OPERATION; public: IntegrityStore(std::shared_ptr jsInvoker); - std::vector> createOperations( + std::vector> createOperations( jsi::Runtime &rt, const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/KeyserverStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/KeyserverStore.cpp index c99d8830c..c7153757d 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/KeyserverStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/KeyserverStore.cpp @@ -1,81 +1,82 @@ #include "KeyserverStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType KeyserverStore::REMOVE_OPERATION = "remove_keyservers"; OperationType KeyserverStore::REMOVE_ALL_OPERATION = "remove_all_keyservers"; OperationType KeyserverStore::REPLACE_OPERATION = "replace_keyserver"; KeyserverStore::KeyserverStore( std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array KeyserverStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> keyserversVectorPtr) const { size_t numKeyservers = keyserversVectorPtr->size(); jsi::Array jsiKeyservers = jsi::Array(rt, numKeyservers); size_t writeIdx = 0; for (const KeyserverInfo &keyserver : *keyserversVectorPtr) { jsi::Object jsiKeyserver = jsi::Object(rt); jsiKeyserver.setProperty(rt, "id", keyserver.id); jsiKeyserver.setProperty(rt, "keyserverInfo", keyserver.keyserver_info); jsiKeyserver.setProperty( rt, "syncedKeyserverInfo", keyserver.synced_keyserver_info); jsiKeyservers.setValueAtIndex(rt, writeIdx++, jsiKeyserver); } return jsiKeyservers; } -std::vector> -KeyserverStore::createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const { - std::vector> keyserverStoreOps; +std::vector> KeyserverStore::createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const { + std::vector> keyserverStoreOps; for (size_t idx = 0; idx < operations.size(rt); idx++) { jsi::Object op = operations.getValueAtIndex(rt, idx).asObject(rt); std::string opType = op.getProperty(rt, "type").asString(rt).utf8(rt); if (opType == REMOVE_OPERATION) { std::vector keyserverIDsToRemove; jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); jsi::Array keyserverIDs = payloadObj.getProperty(rt, "ids").asObject(rt).asArray(rt); for (int keyserverIdx = 0; keyserverIdx < keyserverIDs.size(rt); keyserverIdx++) { keyserverIDsToRemove.push_back( keyserverIDs.getValueAtIndex(rt, keyserverIdx) .asString(rt) .utf8(rt)); } keyserverStoreOps.push_back(std::make_unique( std::move(keyserverIDsToRemove))); } else if (opType == REMOVE_ALL_OPERATION) { keyserverStoreOps.push_back( std::make_unique()); } else if (opType == REPLACE_OPERATION) { jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); std::string id = payloadObj.getProperty(rt, "id").asString(rt).utf8(rt); std::string keyserver_info = payloadObj.getProperty(rt, "keyserverInfo").asString(rt).utf8(rt); std::string synced_keyserver_info = payloadObj.getProperty(rt, "syncedKeyserverInfo") .asString(rt) .utf8(rt); KeyserverInfo keyserver{id, keyserver_info, synced_keyserver_info}; keyserverStoreOps.push_back( std::make_unique(std::move(keyserver))); } else { throw std::runtime_error("unsupported operation: " + opType); } }; return keyserverStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/KeyserverStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/KeyserverStore.h index 75d33a5bd..cea977ce8 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/KeyserverStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/KeyserverStore.h @@ -1,30 +1,30 @@ #pragma once #include "../../../DatabaseManagers/entities/KeyserverInfo.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "KeyserverStoreOperations.h" #include namespace comm { -class KeyserverStore - : public BaseDataStore { +class KeyserverStore : public BaseDataStore { private: static OperationType REMOVE_OPERATION; static OperationType REMOVE_ALL_OPERATION; static OperationType REPLACE_OPERATION; public: KeyserverStore(std::shared_ptr jsInvoker); - std::vector> createOperations( + std::vector> createOperations( jsi::Runtime &rt, const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/MessageStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/MessageStore.cpp index f5674bb52..d3ce6e5f7 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/MessageStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/MessageStore.cpp @@ -1,146 +1,147 @@ #include "MessageStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType MessageStore::REKEY_OPERATION = "rekey"; OperationType MessageStore::REMOVE_OPERATION = "remove"; OperationType MessageStore::REPLACE_OPERATION = "replace"; OperationType MessageStore::REMOVE_MSGS_FOR_THREADS_OPERATION = "remove_messages_for_threads"; OperationType MessageStore::REMOVE_ALL_OPERATION = "remove_all"; OperationType MessageStore::REPLACE_MESSAGE_THREADS_OPERATION = "replace_threads"; OperationType MessageStore::REMOVE_MESSAGE_THREADS_OPERATION = "remove_threads"; OperationType MessageStore::REMOVE_ALL_MESSAGE_THREADS_OPERATION = "remove_all_threads"; MessageStore::MessageStore( std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array MessageStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> messagesVectorPtr) const { size_t numMessages = messagesVectorPtr->size(); jsi::Array jsiMessages = jsi::Array(rt, numMessages); size_t writeIndex = 0; for (const auto &[message, media] : *messagesVectorPtr) { auto jsiMessage = jsi::Object(rt); jsiMessage.setProperty(rt, "id", message.id); if (message.local_id) { auto local_id = message.local_id.get(); jsiMessage.setProperty(rt, "local_id", *local_id); } jsiMessage.setProperty(rt, "thread", message.thread); jsiMessage.setProperty(rt, "user", message.user); jsiMessage.setProperty(rt, "type", std::to_string(message.type)); if (message.future_type) { auto future_type = message.future_type.get(); jsiMessage.setProperty(rt, "future_type", std::to_string(*future_type)); } if (message.content) { auto content = message.content.get(); jsiMessage.setProperty(rt, "content", *content); } jsiMessage.setProperty(rt, "time", std::to_string(message.time)); size_t media_idx = 0; jsi::Array jsiMediaArray = jsi::Array(rt, media.size()); for (const auto &media_info : media) { auto jsiMedia = jsi::Object(rt); jsiMedia.setProperty(rt, "id", media_info.id); jsiMedia.setProperty(rt, "uri", media_info.uri); jsiMedia.setProperty(rt, "type", media_info.type); jsiMedia.setProperty(rt, "extras", media_info.extras); jsiMediaArray.setValueAtIndex(rt, media_idx++, jsiMedia); } jsiMessage.setProperty(rt, "media_infos", jsiMediaArray); jsiMessages.setValueAtIndex(rt, writeIndex++, jsiMessage); } return jsiMessages; } -std::vector> -MessageStore::createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const { +std::vector> MessageStore::createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const { - std::vector> messageStoreOps; + std::vector> messageStoreOps; for (auto idx = 0; idx < operations.size(rt); idx++) { auto op = operations.getValueAtIndex(rt, idx).asObject(rt); auto op_type = op.getProperty(rt, "type").asString(rt).utf8(rt); if (op_type == REMOVE_ALL_OPERATION) { messageStoreOps.push_back(std::make_unique()); continue; } if (op_type == REMOVE_ALL_MESSAGE_THREADS_OPERATION) { messageStoreOps.push_back( std::make_unique()); continue; } auto payload_obj = op.getProperty(rt, "payload").asObject(rt); if (op_type == REMOVE_OPERATION) { messageStoreOps.push_back( std::make_unique(rt, payload_obj)); } else if (op_type == REMOVE_MSGS_FOR_THREADS_OPERATION) { messageStoreOps.push_back( std::make_unique(rt, payload_obj)); } else if (op_type == REPLACE_OPERATION) { messageStoreOps.push_back( std::make_unique(rt, payload_obj)); } else if (op_type == REKEY_OPERATION) { messageStoreOps.push_back( std::make_unique(rt, payload_obj)); } else if (op_type == REPLACE_MESSAGE_THREADS_OPERATION) { messageStoreOps.push_back( std::make_unique(rt, payload_obj)); } else if (op_type == REMOVE_MESSAGE_THREADS_OPERATION) { messageStoreOps.push_back( std::make_unique( rt, payload_obj)); } else { throw std::runtime_error("unsupported operation: " + op_type); } } return messageStoreOps; } jsi::Array MessageStore::parseDBMessageStoreThreads( jsi::Runtime &rt, std::shared_ptr> threadsVectorPtr) const { size_t numThreads = threadsVectorPtr->size(); jsi::Array jsiThreads = jsi::Array(rt, numThreads); size_t writeIdx = 0; for (const MessageStoreThread &thread : *threadsVectorPtr) { jsi::Object jsiThread = jsi::Object(rt); jsiThread.setProperty(rt, "id", thread.id); jsiThread.setProperty( rt, "start_reached", std::to_string(thread.start_reached)); jsiThreads.setValueAtIndex(rt, writeIdx++, jsiThread); } return jsiThreads; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/MessageStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/MessageStore.h index 65daafcdf..d2be4ce09 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/MessageStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/MessageStore.h @@ -1,43 +1,43 @@ #pragma once #include "../../../DatabaseManagers/entities/Media.h" #include "../../../DatabaseManagers/entities/Message.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "MessageStoreOperations.h" #include namespace comm { using MessageEntity = std::pair>; -class MessageStore - : public BaseDataStore { +class MessageStore : public BaseDataStore { private: static OperationType REKEY_OPERATION; static OperationType REMOVE_OPERATION; static OperationType REPLACE_OPERATION; static OperationType REMOVE_MSGS_FOR_THREADS_OPERATION; static OperationType REMOVE_ALL_OPERATION; static OperationType REPLACE_MESSAGE_THREADS_OPERATION; static OperationType REMOVE_MESSAGE_THREADS_OPERATION; static OperationType REMOVE_ALL_MESSAGE_THREADS_OPERATION; public: MessageStore(std::shared_ptr jsInvoker); - std::vector> createOperations( + std::vector> createOperations( jsi::Runtime &rt, const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; jsi::Array parseDBMessageStoreThreads( jsi::Runtime &rt, std::shared_ptr> threadsVectorPtr) const; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ReportStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ReportStore.cpp index 8e45431df..afb07338a 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ReportStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ReportStore.cpp @@ -1,60 +1,61 @@ #include "ReportStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType ReportStore::REPLACE_REPORT_OPERATION = "replace_report"; OperationType ReportStore::REMOVE_REPORTS_OPERATION = "remove_reports"; OperationType ReportStore::REMOVE_ALL_REPORTS_OPERATION = "remove_all_reports"; ReportStore::ReportStore( std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array ReportStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> reportStoreVectorPtr) const { size_t numReports = reportStoreVectorPtr->size(); jsi::Array jsiReports = jsi::Array(rt, numReports); size_t writeIdx = 0; for (const Report &report : *reportStoreVectorPtr) { jsi::Object jsiReport = jsi::Object(rt); jsiReport.setProperty(rt, "id", report.id); jsiReport.setProperty(rt, "report", report.report); jsiReports.setValueAtIndex(rt, writeIdx++, jsiReport); } return jsiReports; } -std::vector> -ReportStore::createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const { - std::vector> reportStoreOps; +std::vector> ReportStore::createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const { + std::vector> reportStoreOps; for (auto idx = 0; idx < operations.size(rt); idx++) { auto op = operations.getValueAtIndex(rt, idx).asObject(rt); auto op_type = op.getProperty(rt, "type").asString(rt).utf8(rt); if (op_type == REMOVE_ALL_REPORTS_OPERATION) { reportStoreOps.push_back(std::make_unique()); continue; } auto payload_obj = op.getProperty(rt, "payload").asObject(rt); if (op_type == REPLACE_REPORT_OPERATION) { reportStoreOps.push_back( std::make_unique(rt, payload_obj)); } else if (op_type == REMOVE_REPORTS_OPERATION) { reportStoreOps.push_back( std::make_unique(rt, payload_obj)); } else { throw std::runtime_error{"unsupported operation: " + op_type}; } } return reportStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ReportStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ReportStore.h index 1512fa13d..f66eb9abc 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ReportStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ReportStore.h @@ -1,29 +1,30 @@ #pragma once #include "../../../DatabaseManagers/entities/Report.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "ReportStoreOperations.h" #include namespace comm { -class ReportStore : public BaseDataStore { +class ReportStore : public BaseDataStore { private: static OperationType REPLACE_REPORT_OPERATION; static OperationType REMOVE_REPORTS_OPERATION; static OperationType REMOVE_ALL_REPORTS_OPERATION; public: ReportStore(std::shared_ptr jsInvoker); - std::vector> createOperations( + std::vector> createOperations( jsi::Runtime &rt, const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/SyncedMetadataStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/SyncedMetadataStore.cpp index 7b23db9b8..3be438216 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/SyncedMetadataStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/SyncedMetadataStore.cpp @@ -1,83 +1,83 @@ #include "SyncedMetadataStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType SyncedMetadataStore::REMOVE_OPERATION = "remove_synced_metadata"; OperationType SyncedMetadataStore::REMOVE_ALL_OPERATION = "remove_all_synced_metadata"; OperationType SyncedMetadataStore::REPLACE_OPERATION = "replace_synced_metadata_entry"; SyncedMetadataStore::SyncedMetadataStore( std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array SyncedMetadataStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> syncedMetadataVectorPtr) const { size_t numSyncedMetadata = syncedMetadataVectorPtr->size(); jsi::Array jsiSyncedMetadata = jsi::Array(rt, numSyncedMetadata); size_t writeIdx = 0; for (const SyncedMetadataEntry &syncedMetadataEntry : *syncedMetadataVectorPtr) { jsi::Object jsiSyncedMetadataEntry = jsi::Object(rt); jsiSyncedMetadataEntry.setProperty(rt, "name", syncedMetadataEntry.name); jsiSyncedMetadataEntry.setProperty(rt, "data", syncedMetadataEntry.data); jsiSyncedMetadata.setValueAtIndex(rt, writeIdx++, jsiSyncedMetadataEntry); } return jsiSyncedMetadata; } -std::vector> +std::vector> SyncedMetadataStore::createOperations( jsi::Runtime &rt, const jsi::Array &operations) const { - std::vector> - syncedMetadataStoreOps; + std::vector> syncedMetadataStoreOps; for (size_t idx = 0; idx < operations.size(rt); idx++) { jsi::Object op = operations.getValueAtIndex(rt, idx).asObject(rt); std::string opType = op.getProperty(rt, "type").asString(rt).utf8(rt); if (opType == REMOVE_OPERATION) { std::vector syncedMetadataNamesToRemove; jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); jsi::Array syncedMetadataNames = payloadObj.getProperty(rt, "names").asObject(rt).asArray(rt); for (int nameIdx = 0; nameIdx < syncedMetadataNames.size(rt); nameIdx++) { syncedMetadataNamesToRemove.push_back( syncedMetadataNames.getValueAtIndex(rt, nameIdx) .asString(rt) .utf8(rt)); } syncedMetadataStoreOps.push_back( std::make_unique( std::move(syncedMetadataNamesToRemove))); } else if (opType == REMOVE_ALL_OPERATION) { syncedMetadataStoreOps.push_back( std::make_unique()); } else if (opType == REPLACE_OPERATION) { jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); std::string name = payloadObj.getProperty(rt, "name").asString(rt).utf8(rt); std::string data = payloadObj.getProperty(rt, "data").asString(rt).utf8(rt); SyncedMetadataEntry syncedMetadataEntry{name, data}; syncedMetadataStoreOps.push_back( std::make_unique( std::move(syncedMetadataEntry))); } else { throw std::runtime_error("unsupported operation: " + opType); } }; return syncedMetadataStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/SyncedMetadataStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/SyncedMetadataStore.h index 481e0e8df..c82d90bc4 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/SyncedMetadataStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/SyncedMetadataStore.h @@ -1,32 +1,32 @@ #pragma once #include "../../../DatabaseManagers/entities/SyncedMetadataEntry.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "SyncedMetadataStoreOperations.h" #include namespace comm { -class SyncedMetadataStore : public BaseDataStore< - SyncedMetadataStoreOperationBase, - SyncedMetadataEntry> { +class SyncedMetadataStore + : public BaseDataStore { private: static OperationType REMOVE_OPERATION; static OperationType REMOVE_ALL_OPERATION; static OperationType REPLACE_OPERATION; public: SyncedMetadataStore(std::shared_ptr jsInvoker); - std::vector> - createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const override; + std::vector> createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadActivityStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadActivityStore.cpp index fded4d7d2..7aa4798e4 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadActivityStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadActivityStore.cpp @@ -1,88 +1,88 @@ #include "ThreadActivityStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType ThreadActivityStore::REPLACE_OPERATION = "replace_thread_activity_entry"; OperationType ThreadActivityStore::REMOVE_OPERATION = "remove_thread_activity_entries"; OperationType ThreadActivityStore::REMOVE_ALL_OPERATION = "remove_all_thread_activity_entries"; ThreadActivityStore::ThreadActivityStore( std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array ThreadActivityStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> threadActivityVectorPtr) const { size_t numThreadActivityEntries = threadActivityVectorPtr->size(); jsi::Array jsiThreadActivitiyEntries = jsi::Array(rt, numThreadActivityEntries); size_t writeIdx = 0; for (const ThreadActivityEntry &threadActivityEntry : *threadActivityVectorPtr) { jsi::Object jsiThreadActivityEntry = jsi::Object(rt); jsiThreadActivityEntry.setProperty(rt, "id", threadActivityEntry.id); jsiThreadActivityEntry.setProperty( rt, "threadActivityStoreEntry", threadActivityEntry.thread_activity_store_entry); jsiThreadActivitiyEntries.setValueAtIndex( rt, writeIdx++, jsiThreadActivityEntry); } return jsiThreadActivitiyEntries; } -std::vector> +std::vector> ThreadActivityStore::createOperations( jsi::Runtime &rt, const jsi::Array &operations) const { - std::vector> - threadActivityStoreOps; + std::vector> threadActivityStoreOps; for (size_t idx = 0; idx < operations.size(rt); idx++) { jsi::Object op = operations.getValueAtIndex(rt, idx).asObject(rt); std::string opType = op.getProperty(rt, "type").asString(rt).utf8(rt); if (opType == REMOVE_OPERATION) { std::vector threadIDsToRemove; jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); jsi::Array threadIDs = payloadObj.getProperty(rt, "ids").asObject(rt).asArray(rt); for (int threadIdx = 0; threadIdx < threadIDs.size(rt); threadIdx++) { threadIDsToRemove.push_back( threadIDs.getValueAtIndex(rt, threadIdx).asString(rt).utf8(rt)); } threadActivityStoreOps.push_back( std::make_unique( std::move(threadIDsToRemove))); } else if (opType == REMOVE_ALL_OPERATION) { threadActivityStoreOps.push_back( std::make_unique()); } else if (opType == REPLACE_OPERATION) { jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); std::string id = payloadObj.getProperty(rt, "id").asString(rt).utf8(rt); std::string thread_activity_store_entry = payloadObj.getProperty(rt, "threadActivityStoreEntry") .asString(rt) .utf8(rt); ThreadActivityEntry threadActivityEntry{id, thread_activity_store_entry}; threadActivityStoreOps.push_back( std::make_unique( std::move(threadActivityEntry))); } else { throw std::runtime_error("unsupported operation: " + opType); } }; return threadActivityStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadActivityStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadActivityStore.h index 4886d5b30..9c28c64d6 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadActivityStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadActivityStore.h @@ -1,32 +1,32 @@ #pragma once #include "../../../DatabaseManagers/entities/ThreadActivityEntry.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "ThreadActivityStoreOperations.h" #include namespace comm { -class ThreadActivityStore : public BaseDataStore< - ThreadActivityStoreOperationBase, - ThreadActivityEntry> { +class ThreadActivityStore + : public BaseDataStore { private: static OperationType REMOVE_OPERATION; static OperationType REMOVE_ALL_OPERATION; static OperationType REPLACE_OPERATION; public: ThreadActivityStore(std::shared_ptr jsInvoker); - std::vector> - createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const override; + std::vector> createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadStore.cpp index d68c3d3cd..d369d3c7d 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadStore.cpp @@ -1,198 +1,199 @@ #include "ThreadStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType ThreadStore::REMOVE_OPERATION = "remove"; OperationType ThreadStore::REMOVE_ALL_OPERATION = "remove_all"; OperationType ThreadStore::REPLACE_OPERATION = "replace"; ThreadStore::ThreadStore( std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array ThreadStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> threadsVectorPtr) const { size_t numThreads = threadsVectorPtr->size(); jsi::Array jsiThreads = jsi::Array(rt, numThreads); size_t writeIdx = 0; for (const Thread &thread : *threadsVectorPtr) { jsi::Object jsiThread = jsi::Object(rt); jsiThread.setProperty(rt, "id", thread.id); jsiThread.setProperty(rt, "type", thread.type); jsiThread.setProperty( rt, "name", thread.name ? jsi::String::createFromUtf8(rt, *thread.name) : jsi::Value::null()); jsiThread.setProperty( rt, "description", thread.description ? jsi::String::createFromUtf8(rt, *thread.description) : jsi::Value::null()); jsiThread.setProperty(rt, "color", thread.color); jsiThread.setProperty( rt, "creationTime", std::to_string(thread.creation_time)); jsiThread.setProperty( rt, "parentThreadID", thread.parent_thread_id ? jsi::String::createFromUtf8(rt, *thread.parent_thread_id) : jsi::Value::null()); jsiThread.setProperty( rt, "containingThreadID", thread.containing_thread_id ? jsi::String::createFromUtf8(rt, *thread.containing_thread_id) : jsi::Value::null()); jsiThread.setProperty( rt, "community", thread.community ? jsi::String::createFromUtf8(rt, *thread.community) : jsi::Value::null()); jsiThread.setProperty(rt, "members", thread.members); jsiThread.setProperty(rt, "roles", thread.roles); jsiThread.setProperty(rt, "currentUser", thread.current_user); jsiThread.setProperty( rt, "sourceMessageID", thread.source_message_id ? jsi::String::createFromUtf8(rt, *thread.source_message_id) : jsi::Value::null()); jsiThread.setProperty(rt, "repliesCount", thread.replies_count); jsiThread.setProperty(rt, "pinnedCount", thread.pinned_count); if (thread.avatar) { auto avatar = jsi::String::createFromUtf8(rt, *thread.avatar); jsiThread.setProperty(rt, "avatar", avatar); } jsiThreads.setValueAtIndex(rt, writeIdx++, jsiThread); } return jsiThreads; } -std::vector> -ThreadStore::createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const { - std::vector> threadStoreOps; +std::vector> ThreadStore::createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const { + std::vector> threadStoreOps; for (size_t idx = 0; idx < operations.size(rt); idx++) { jsi::Object op = operations.getValueAtIndex(rt, idx).asObject(rt); std::string opType = op.getProperty(rt, "type").asString(rt).utf8(rt); if (opType == REMOVE_OPERATION) { std::vector threadIDsToRemove; jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); jsi::Array threadIDs = payloadObj.getProperty(rt, "ids").asObject(rt).asArray(rt); for (int threadIdx = 0; threadIdx < threadIDs.size(rt); threadIdx++) { threadIDsToRemove.push_back( threadIDs.getValueAtIndex(rt, threadIdx).asString(rt).utf8(rt)); } threadStoreOps.push_back(std::make_unique( std::move(threadIDsToRemove))); } else if (opType == REMOVE_ALL_OPERATION) { threadStoreOps.push_back(std::make_unique()); } else if (opType == REPLACE_OPERATION) { jsi::Object threadObj = op.getProperty(rt, "payload").asObject(rt); std::string threadID = threadObj.getProperty(rt, "id").asString(rt).utf8(rt); int type = std::lround(threadObj.getProperty(rt, "type").asNumber()); jsi::Value maybeName = threadObj.getProperty(rt, "name"); std::unique_ptr name = maybeName.isString() ? std::make_unique(maybeName.asString(rt).utf8(rt)) : nullptr; jsi::Value maybeDescription = threadObj.getProperty(rt, "description"); std::unique_ptr description = maybeDescription.isString() ? std::make_unique( maybeDescription.asString(rt).utf8(rt)) : nullptr; std::string color = threadObj.getProperty(rt, "color").asString(rt).utf8(rt); int64_t creationTime = std::stoll( threadObj.getProperty(rt, "creationTime").asString(rt).utf8(rt)); jsi::Value maybeParentThreadID = threadObj.getProperty(rt, "parentThreadID"); std::unique_ptr parentThreadID = maybeParentThreadID.isString() ? std::make_unique( maybeParentThreadID.asString(rt).utf8(rt)) : nullptr; jsi::Value maybeContainingThreadID = threadObj.getProperty(rt, "containingThreadID"); std::unique_ptr containingThreadID = maybeContainingThreadID.isString() ? std::make_unique( maybeContainingThreadID.asString(rt).utf8(rt)) : nullptr; jsi::Value maybeCommunity = threadObj.getProperty(rt, "community"); std::unique_ptr community = maybeCommunity.isString() ? std::make_unique(maybeCommunity.asString(rt).utf8(rt)) : nullptr; std::string members = threadObj.getProperty(rt, "members").asString(rt).utf8(rt); std::string roles = threadObj.getProperty(rt, "roles").asString(rt).utf8(rt); std::string currentUser = threadObj.getProperty(rt, "currentUser").asString(rt).utf8(rt); jsi::Value maybeSourceMessageID = threadObj.getProperty(rt, "sourceMessageID"); std::unique_ptr sourceMessageID = maybeSourceMessageID.isString() ? std::make_unique( maybeSourceMessageID.asString(rt).utf8(rt)) : nullptr; int repliesCount = std::lround(threadObj.getProperty(rt, "repliesCount").asNumber()); jsi::Value maybeAvatar = threadObj.getProperty(rt, "avatar"); std::unique_ptr avatar = maybeAvatar.isString() ? std::make_unique(maybeAvatar.asString(rt).utf8(rt)) : nullptr; jsi::Value maybePinnedCount = threadObj.getProperty(rt, "pinnedCount"); int pinnedCount = maybePinnedCount.isNumber() ? std::lround(maybePinnedCount.asNumber()) : 0; Thread thread{ threadID, type, std::move(name), std::move(description), color, creationTime, std::move(parentThreadID), std::move(containingThreadID), std::move(community), members, roles, currentUser, std::move(sourceMessageID), repliesCount, std::move(avatar), pinnedCount}; threadStoreOps.push_back( std::make_unique(std::move(thread))); } else { throw std::runtime_error("unsupported operation: " + opType); } }; return threadStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadStore.h index 13c0cf8a8..c4e6d2ca5 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/ThreadStore.h @@ -1,29 +1,30 @@ #pragma once #include "../../../DatabaseManagers/entities/Thread.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "ThreadStoreOperations.h" #include namespace comm { -class ThreadStore : public BaseDataStore { +class ThreadStore : public BaseDataStore { private: static OperationType REMOVE_OPERATION; static OperationType REMOVE_ALL_OPERATION; static OperationType REPLACE_OPERATION; public: ThreadStore(std::shared_ptr jsInvoker); - std::vector> createOperations( + std::vector> createOperations( jsi::Runtime &rt, const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/UserStore.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/UserStore.cpp index 723dfe90a..bb46063bf 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/UserStore.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/UserStore.cpp @@ -1,70 +1,71 @@ #include "UserStore.h" +#include "../../DBOperationBase.h" #include #include namespace comm { OperationType UserStore::REMOVE_OPERATION = "remove_users"; OperationType UserStore::REMOVE_ALL_OPERATION = "remove_all_users"; OperationType UserStore::REPLACE_OPERATION = "replace_user"; UserStore::UserStore(std::shared_ptr jsInvoker) : BaseDataStore(jsInvoker) { } jsi::Array UserStore::parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> usersVectorPtr) const { size_t numUsers = usersVectorPtr->size(); jsi::Array jsiUsers = jsi::Array(rt, numUsers); size_t writeIdx = 0; for (const UserInfo &user : *usersVectorPtr) { jsi::Object jsiUser = jsi::Object(rt); jsiUser.setProperty(rt, "id", user.id); jsiUser.setProperty(rt, "userInfo", user.user_info); jsiUsers.setValueAtIndex(rt, writeIdx++, jsiUser); } return jsiUsers; } -std::vector> -UserStore::createOperations(jsi::Runtime &rt, const jsi::Array &operations) - const { - std::vector> userStoreOps; +std::vector> UserStore::createOperations( + jsi::Runtime &rt, + const jsi::Array &operations) const { + std::vector> userStoreOps; for (size_t idx = 0; idx < operations.size(rt); idx++) { jsi::Object op = operations.getValueAtIndex(rt, idx).asObject(rt); std::string opType = op.getProperty(rt, "type").asString(rt).utf8(rt); if (opType == REMOVE_OPERATION) { std::vector userIDsToRemove; jsi::Object payloadObj = op.getProperty(rt, "payload").asObject(rt); jsi::Array userIDs = payloadObj.getProperty(rt, "ids").asObject(rt).asArray(rt); for (int userIdx = 0; userIdx < userIDs.size(rt); userIdx++) { userIDsToRemove.push_back( userIDs.getValueAtIndex(rt, userIdx).asString(rt).utf8(rt)); } userStoreOps.push_back( std::make_unique(std::move(userIDsToRemove))); } else if (opType == REMOVE_ALL_OPERATION) { userStoreOps.push_back(std::make_unique()); } else if (opType == REPLACE_OPERATION) { jsi::Object userObj = op.getProperty(rt, "payload").asObject(rt); std::string id = userObj.getProperty(rt, "id").asString(rt).utf8(rt); std::string user_info = userObj.getProperty(rt, "userInfo").asString(rt).utf8(rt); UserInfo user{id, user_info}; userStoreOps.push_back( std::make_unique(std::move(user))); } else { throw std::runtime_error("unsupported operation: " + opType); } }; return userStoreOps; } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/UserStore.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/UserStore.h index 662dc286a..3c3ab3fb9 100644 --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/UserStore.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/DataStores/UserStore.h @@ -1,29 +1,30 @@ #pragma once #include "../../../DatabaseManagers/entities/UserInfo.h" +#include "../../DBOperationBase.h" #include "BaseDataStore.h" #include "UserStoreOperations.h" #include namespace comm { -class UserStore : public BaseDataStore { +class UserStore : public BaseDataStore { private: static OperationType REMOVE_OPERATION; static OperationType REMOVE_ALL_OPERATION; static OperationType REPLACE_OPERATION; public: UserStore(std::shared_ptr jsInvoker); - std::vector> createOperations( + std::vector> createOperations( jsi::Runtime &rt, const jsi::Array &operations) const override; jsi::Array parseDBDataStore( jsi::Runtime &rt, std::shared_ptr> dataVectorPtr) const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/ReportStoreOperations.h b/native/cpp/CommonCpp/NativeModules/ReportStoreOperations.h index 6ca061f74..05acb7f31 100644 --- a/native/cpp/CommonCpp/NativeModules/ReportStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/ReportStoreOperations.h @@ -1,57 +1,53 @@ #pragma once #include "../DatabaseManagers/entities/Media.h" #include "../DatabaseManagers/entities/Report.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class ReportStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~ReportStoreOperationBase(){}; -}; -class RemoveReportsOperation : public ReportStoreOperationBase { +class RemoveReportsOperation : public DBOperationBase { public: RemoveReportsOperation(jsi::Runtime &rt, const jsi::Object &payload) : ids_to_remove{} { auto payload_ids = payload.getProperty(rt, "ids").asObject(rt).asArray(rt); for (size_t idx = 0; idx < payload_ids.size(rt); idx++) { this->ids_to_remove.push_back( payload_ids.getValueAtIndex(rt, idx).asString(rt).utf8(rt)); } } virtual void execute() override { DatabaseManager::getQueryExecutor().removeReports(this->ids_to_remove); } private: std::vector ids_to_remove; }; -class ReplaceReportOperation : public ReportStoreOperationBase { +class ReplaceReportOperation : public DBOperationBase { public: ReplaceReportOperation(jsi::Runtime &rt, const jsi::Object &payload) { auto report_id = payload.getProperty(rt, "id").asString(rt).utf8(rt); auto report_data = payload.getProperty(rt, "report").asString(rt).utf8(rt); this->report = std::make_unique(Report{report_id, report_data}); } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceReport(std::move(*this->report)); } private: std::unique_ptr report; }; -class RemoveAllReportsOperation : public ReportStoreOperationBase { +class RemoveAllReportsOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllReports(); } }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/SyncedMetadataStoreOperations.h b/native/cpp/CommonCpp/NativeModules/SyncedMetadataStoreOperations.h index e984d7fd6..6897e3298 100644 --- a/native/cpp/CommonCpp/NativeModules/SyncedMetadataStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/SyncedMetadataStoreOperations.h @@ -1,51 +1,45 @@ #pragma once #include "../DatabaseManagers/entities/SyncedMetadataEntry.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class SyncedMetadataStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~SyncedMetadataStoreOperationBase(){}; -}; - -class RemoveSyncedMetadataOperation : public SyncedMetadataStoreOperationBase { +class RemoveSyncedMetadataOperation : public DBOperationBase { public: RemoveSyncedMetadataOperation(std::vector names) : names{names} { } virtual void execute() override { DatabaseManager::getQueryExecutor().removeSyncedMetadata(this->names); } private: std::vector names; }; -class ReplaceSyncedMetadataOperation : public SyncedMetadataStoreOperationBase { +class ReplaceSyncedMetadataOperation : public DBOperationBase { public: ReplaceSyncedMetadataOperation(SyncedMetadataEntry &&syncedMetadataEntry) : syncedMetadataEntry{std::move(syncedMetadataEntry)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceSyncedMetadataEntry( this->syncedMetadataEntry); } private: SyncedMetadataEntry syncedMetadataEntry; }; -class RemoveAllSyncedMetadataOperation - : public SyncedMetadataStoreOperationBase { +class RemoveAllSyncedMetadataOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllSyncedMetadata(); } }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/ThreadActivityStoreOperations.h b/native/cpp/CommonCpp/NativeModules/ThreadActivityStoreOperations.h index fa2ac64a2..05d659941 100644 --- a/native/cpp/CommonCpp/NativeModules/ThreadActivityStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/ThreadActivityStoreOperations.h @@ -1,53 +1,46 @@ #pragma once #include "../DatabaseManagers/entities/ThreadActivityEntry.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class ThreadActivityStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~ThreadActivityStoreOperationBase(){}; -}; -class RemoveThreadActivityEntriesOperation - : public ThreadActivityStoreOperationBase { +class RemoveThreadActivityEntriesOperation : public DBOperationBase { public: RemoveThreadActivityEntriesOperation(std::vector ids) : ids{ids} { } virtual void execute() override { DatabaseManager::getQueryExecutor().removeThreadActivityEntries(this->ids); } private: std::vector ids; }; -class ReplaceThreadActivityEntryOperation - : public ThreadActivityStoreOperationBase { +class ReplaceThreadActivityEntryOperation : public DBOperationBase { public: ReplaceThreadActivityEntryOperation(ThreadActivityEntry &&threadActivityEntry) : threadActivityEntry{std::move(threadActivityEntry)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceThreadActivityEntry( this->threadActivityEntry); } private: ThreadActivityEntry threadActivityEntry; }; -class RemoveAllThreadActivityEntriesOperation - : public ThreadActivityStoreOperationBase { +class RemoveAllThreadActivityEntriesOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllThreadActivityEntries(); } }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/ThreadStoreOperations.h b/native/cpp/CommonCpp/NativeModules/ThreadStoreOperations.h index 3af66ec24..80d6b126a 100644 --- a/native/cpp/CommonCpp/NativeModules/ThreadStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/ThreadStoreOperations.h @@ -1,48 +1,44 @@ #pragma once #include "../DatabaseManagers/entities/Media.h" #include "../DatabaseManagers/entities/Thread.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class ThreadStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~ThreadStoreOperationBase(){}; -}; -class RemoveThreadsOperation : public ThreadStoreOperationBase { +class RemoveThreadsOperation : public DBOperationBase { public: RemoveThreadsOperation(std::vector ids) : ids{ids} { } virtual void execute() override { DatabaseManager::getQueryExecutor().removeThreads(this->ids); } private: std::vector ids; }; -class ReplaceThreadOperation : public ThreadStoreOperationBase { +class ReplaceThreadOperation : public DBOperationBase { public: ReplaceThreadOperation(Thread &&thread) : thread{std::move(thread)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceThread(this->thread); } private: Thread thread; }; -class RemoveAllThreadsOperation : public ThreadStoreOperationBase { +class RemoveAllThreadsOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllThreads(); } }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/UserStoreOperations.h b/native/cpp/CommonCpp/NativeModules/UserStoreOperations.h index 086021702..1dd6c81b4 100644 --- a/native/cpp/CommonCpp/NativeModules/UserStoreOperations.h +++ b/native/cpp/CommonCpp/NativeModules/UserStoreOperations.h @@ -1,47 +1,43 @@ #pragma once #include "../DatabaseManagers/entities/UserInfo.h" +#include "DBOperationBase.h" #include "DatabaseManager.h" #include namespace comm { -class UserStoreOperationBase { -public: - virtual void execute() = 0; - virtual ~UserStoreOperationBase(){}; -}; -class RemoveUsersOperation : public UserStoreOperationBase { +class RemoveUsersOperation : public DBOperationBase { public: RemoveUsersOperation(std::vector ids) : ids{ids} { } virtual void execute() override { DatabaseManager::getQueryExecutor().removeUsers(this->ids); } private: std::vector ids; }; -class ReplaceUserOperation : public UserStoreOperationBase { +class ReplaceUserOperation : public DBOperationBase { public: ReplaceUserOperation(UserInfo &&user) : user{std::move(user)} { } virtual void execute() override { DatabaseManager::getQueryExecutor().replaceUser(this->user); } private: UserInfo user; }; -class RemoveAllUsersOperation : public UserStoreOperationBase { +class RemoveAllUsersOperation : public DBOperationBase { public: virtual void execute() override { DatabaseManager::getQueryExecutor().removeAllUsers(); } }; } // namespace comm