diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h index 6ac50be4e..3c6215d9f 100644 --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h @@ -1,123 +1,129 @@ #pragma once #include "../CryptoTools/Persist.h" #include "entities/CommunityInfo.h" #include "entities/Draft.h" #include "entities/KeyserverInfo.h" #include "entities/Message.h" #include "entities/MessageStoreThread.h" #include "entities/MessageToDevice.h" #include "entities/OlmPersistAccount.h" #include "entities/OlmPersistSession.h" #include "entities/PersistItem.h" #include "entities/Report.h" #include "entities/Thread.h" #include "entities/UserInfo.h" #include namespace comm { /** * if any initialization/cleaning up steps are required for specific * database managers they should appear in constructors/destructors * following the RAII pattern */ class DatabaseQueryExecutor { public: virtual std::string getDraft(std::string key) const = 0; virtual std::unique_ptr getThread(std::string threadID) const = 0; virtual void updateDraft(std::string key, std::string text) const = 0; 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; virtual void removeMessages(const std::vector &ids) const = 0; virtual void removeMessagesForThreads(const std::vector &threadIDs) const = 0; virtual void replaceMessage(const Message &message) const = 0; virtual void rekeyMessage(std::string from, std::string to) const = 0; virtual void removeAllMedia() const = 0; virtual void replaceMessageStoreThreads( const std::vector &threads) const = 0; virtual void removeMessageStoreThreads(const std::vector &ids) const = 0; virtual void removeAllMessageStoreThreads() const = 0; virtual std::vector getAllMessageStoreThreads() const = 0; virtual void removeMediaForMessages(const std::vector &msg_ids) const = 0; virtual void removeMediaForMessage(std::string msg_id) const = 0; virtual void removeMediaForThreads(const std::vector &thread_ids) const = 0; virtual void replaceMedia(const Media &media) const = 0; virtual void rekeyMediaContainers(std::string from, std::string to) const = 0; virtual std::vector getAllThreads() const = 0; virtual void removeThreads(std::vector ids) const = 0; virtual void replaceThread(const Thread &thread) const = 0; virtual void removeAllThreads() const = 0; virtual void replaceReport(const Report &report) const = 0; virtual void removeReports(const std::vector &ids) const = 0; virtual void removeAllReports() const = 0; virtual std::vector getAllReports() const = 0; virtual void setPersistStorageItem(std::string key, std::string item) const = 0; virtual void removePersistStorageItem(std::string key) const = 0; virtual std::string getPersistStorageItem(std::string key) const = 0; virtual void replaceUser(const UserInfo &user_info) const = 0; virtual void removeUsers(const std::vector &ids) const = 0; virtual void removeAllUsers() const = 0; virtual std::vector getAllUsers() const = 0; virtual void replaceKeyserver(const KeyserverInfo &keyserver_info) const = 0; virtual void removeKeyservers(const std::vector &ids) const = 0; virtual void removeAllKeyservers() const = 0; virtual std::vector getAllKeyservers() const = 0; virtual void replaceCommunity(const CommunityInfo &community_info) const = 0; virtual void removeCommunities(const std::vector &ids) const = 0; virtual void removeAllCommunities() const = 0; virtual std::vector getAllCommunities() const = 0; virtual void beginTransaction() const = 0; virtual void commitTransaction() const = 0; virtual void rollbackTransaction() const = 0; + virtual int getContentAccountID() const = 0; + virtual int getNotifsAccountID() const = 0; virtual std::vector getOlmPersistSessionsData() const = 0; - virtual std::optional getOlmPersistAccountData() const = 0; + virtual std::optional + getOlmPersistAccountData(int accountID) const = 0; virtual void storeOlmPersistSession(const OlmPersistSession &session) const = 0; - virtual void storeOlmPersistAccount(const std::string &accountData) const = 0; - virtual void storeOlmPersistData(crypto::Persist persist) const = 0; + virtual void storeOlmPersistAccount( + int accountID, + const std::string &accountData) const = 0; + virtual void + storeOlmPersistData(int accountID, crypto::Persist persist) const = 0; virtual void setNotifyToken(std::string token) const = 0; virtual void clearNotifyToken() const = 0; virtual void setCurrentUserID(std::string userID) const = 0; virtual std::string getCurrentUserID() const = 0; virtual void setMetadata(std::string entry_name, std::string data) const = 0; virtual void clearMetadata(std::string entry_name) const = 0; virtual std::string getMetadata(std::string entry_name) const = 0; virtual void restoreFromMainCompaction( std::string mainCompactionPath, std::string mainCompactionEncryptionKey) const = 0; virtual void restoreFromBackupLog(const std::vector &backupLog) const = 0; virtual void addMessagesToDevice( const std::vector &messages) const = 0; virtual std::vector getAllMessagesToDevice(const std::string &deviceID) const = 0; virtual void removeMessagesToDeviceOlderThan( const ClientMessageToDevice &lastConfirmedMessage) const = 0; virtual void removeAllMessagesForDevice(const std::string &deviceID) const = 0; #ifdef EMSCRIPTEN virtual std::vector getAllThreadsWeb() const = 0; virtual void replaceThreadWeb(const WebThread &thread) const = 0; virtual std::vector getAllMessagesWeb() const = 0; virtual void replaceMessageWeb(const WebMessage &message) const = 0; - virtual NullableString getOlmPersistAccountDataWeb() const = 0; + virtual NullableString getOlmPersistAccountDataWeb(int accountID) const = 0; #else virtual void createMainCompaction(std::string backupID) const = 0; virtual void captureBackupLogs() const = 0; #endif }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp index bd90bcf8f..baefe7fa8 100644 --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -1,2093 +1,2113 @@ #include "SQLiteQueryExecutor.h" #include "Logger.h" #include "entities/CommunityInfo.h" #include "entities/EntityQueryHelpers.h" #include "entities/KeyserverInfo.h" #include "entities/Metadata.h" #include "entities/UserInfo.h" #include #include #include #ifndef EMSCRIPTEN #include "CommSecureStore.h" #include "PlatformSpecificTools.h" #include "StaffUtils.h" #endif -#define ACCOUNT_ID 1 +const int CONTENT_ACCOUNT_ID = 1; +const int NOTIFS_ACCOUNT_ID = 2; namespace comm { std::string SQLiteQueryExecutor::sqliteFilePath; std::string SQLiteQueryExecutor::encryptionKey; std::once_flag SQLiteQueryExecutor::initialized; int SQLiteQueryExecutor::sqlcipherEncryptionKeySize = 64; // Should match constant defined in `native_rust_library/src/constants.rs` std::string SQLiteQueryExecutor::secureStoreEncryptionKeyID = "comm.encryptionKey"; int SQLiteQueryExecutor::backupLogsEncryptionKeySize = 32; std::string SQLiteQueryExecutor::secureStoreBackupLogsEncryptionKeyID = "comm.backupLogsEncryptionKey"; std::string SQLiteQueryExecutor::backupLogsEncryptionKey; #ifndef EMSCRIPTEN NativeSQLiteConnectionManager SQLiteQueryExecutor::connectionManager; #else SQLiteConnectionManager SQLiteQueryExecutor::connectionManager; #endif bool create_table(sqlite3 *db, std::string query, std::string tableName) { char *error; sqlite3_exec(db, query.c_str(), nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error creating '" << tableName << "' table: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool create_drafts_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS drafts (threadID TEXT UNIQUE PRIMARY KEY, " "text TEXT);"; return create_table(db, query, "drafts"); } bool rename_threadID_to_key(sqlite3 *db) { sqlite3_stmt *key_column_stmt; sqlite3_prepare_v2( db, "SELECT name AS col_name FROM pragma_table_xinfo ('drafts') WHERE " "col_name='key';", -1, &key_column_stmt, nullptr); sqlite3_step(key_column_stmt); auto num_bytes = sqlite3_column_bytes(key_column_stmt, 0); sqlite3_finalize(key_column_stmt); if (num_bytes) { return true; } char *error; sqlite3_exec( db, "ALTER TABLE drafts RENAME COLUMN `threadID` TO `key`;", nullptr, nullptr, &error); if (error) { std::ostringstream stringStream; stringStream << "Error occurred renaming threadID column in drafts table " << "to key: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } return true; } bool create_persist_account_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS olm_persist_account(" "id INTEGER UNIQUE PRIMARY KEY NOT NULL, " "account_data TEXT NOT NULL);"; return create_table(db, query, "olm_persist_account"); } bool create_persist_sessions_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS olm_persist_sessions(" "target_user_id TEXT UNIQUE PRIMARY KEY NOT NULL, " "session_data TEXT NOT NULL);"; return create_table(db, query, "olm_persist_sessions"); } bool drop_messages_table(sqlite3 *db) { char *error; sqlite3_exec(db, "DROP TABLE IF EXISTS messages;", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error dropping 'messages' table: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool recreate_messages_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS messages ( " "id TEXT UNIQUE PRIMARY KEY NOT NULL, " "local_id TEXT, " "thread TEXT NOT NULL, " "user TEXT NOT NULL, " "type INTEGER NOT NULL, " "future_type INTEGER, " "content TEXT, " "time INTEGER NOT NULL);"; return create_table(db, query, "messages"); } bool create_messages_idx_thread_time(sqlite3 *db) { char *error; sqlite3_exec( db, "CREATE INDEX IF NOT EXISTS messages_idx_thread_time " "ON messages (thread, time);", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error creating (thread, time) index on messages table: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool create_media_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS media ( " "id TEXT UNIQUE PRIMARY KEY NOT NULL, " "container TEXT NOT NULL, " "thread TEXT NOT NULL, " "uri TEXT NOT NULL, " "type TEXT NOT NULL, " "extras TEXT NOT NULL);"; return create_table(db, query, "media"); } bool create_media_idx_container(sqlite3 *db) { char *error; sqlite3_exec( db, "CREATE INDEX IF NOT EXISTS media_idx_container " "ON media (container);", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error creating (container) index on media table: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool create_threads_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS threads ( " "id TEXT UNIQUE PRIMARY KEY NOT NULL, " "type INTEGER NOT NULL, " "name TEXT, " "description TEXT, " "color TEXT NOT NULL, " "creation_time BIGINT NOT NULL, " "parent_thread_id TEXT, " "containing_thread_id TEXT, " "community TEXT, " "members TEXT NOT NULL, " "roles TEXT NOT NULL, " "current_user TEXT NOT NULL, " "source_message_id TEXT, " "replies_count INTEGER NOT NULL);"; return create_table(db, query, "threads"); } bool update_threadID_for_pending_threads_in_drafts(sqlite3 *db) { char *error; sqlite3_exec( db, "UPDATE drafts SET key = " "REPLACE(REPLACE(REPLACE(REPLACE(key, 'type4/', '')," "'type5/', ''),'type6/', ''),'type7/', '')" "WHERE key LIKE 'pending/%'", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error update pending threadIDs on drafts table: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool enable_write_ahead_logging_mode(sqlite3 *db) { char *error; sqlite3_exec(db, "PRAGMA journal_mode=wal;", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error enabling write-ahead logging mode: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool create_metadata_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS metadata ( " "name TEXT UNIQUE PRIMARY KEY NOT NULL, " "data TEXT);"; return create_table(db, query, "metadata"); } bool add_not_null_constraint_to_drafts(sqlite3 *db) { char *error; sqlite3_exec( db, "CREATE TABLE IF NOT EXISTS temporary_drafts (" "key TEXT UNIQUE PRIMARY KEY NOT NULL, " "text TEXT NOT NULL);" "INSERT INTO temporary_drafts SELECT * FROM drafts " "WHERE key IS NOT NULL AND text IS NOT NULL;" "DROP TABLE drafts;" "ALTER TABLE temporary_drafts RENAME TO drafts;", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error adding NOT NULL constraint to drafts table: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool add_not_null_constraint_to_metadata(sqlite3 *db) { char *error; sqlite3_exec( db, "CREATE TABLE IF NOT EXISTS temporary_metadata (" "name TEXT UNIQUE PRIMARY KEY NOT NULL, " "data TEXT NOT NULL);" "INSERT INTO temporary_metadata SELECT * FROM metadata " "WHERE data IS NOT NULL;" "DROP TABLE metadata;" "ALTER TABLE temporary_metadata RENAME TO metadata;", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error adding NOT NULL constraint to metadata table: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool add_avatar_column_to_threads_table(sqlite3 *db) { char *error; sqlite3_exec( db, "ALTER TABLE threads ADD COLUMN avatar TEXT;", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error adding avatar column to threads table: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool add_pinned_count_column_to_threads(sqlite3 *db) { sqlite3_stmt *pinned_column_stmt; sqlite3_prepare_v2( db, "SELECT name AS col_name FROM pragma_table_xinfo ('threads') WHERE " "col_name='pinned_count';", -1, &pinned_column_stmt, nullptr); sqlite3_step(pinned_column_stmt); auto num_bytes = sqlite3_column_bytes(pinned_column_stmt, 0); sqlite3_finalize(pinned_column_stmt); if (num_bytes) { return true; } char *error; sqlite3_exec( db, "ALTER TABLE threads ADD COLUMN pinned_count INTEGER NOT NULL DEFAULT 0;", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error adding pinned_count column to threads table: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } bool create_message_store_threads_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS message_store_threads (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " start_reached INTEGER NOT NULL," " last_navigated_to BIGINT NOT NULL," " last_pruned BIGINT NOT NULL" ");"; return create_table(db, query, "message_store_threads"); } bool create_reports_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS reports (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " report TEXT NOT NULL" ");"; return create_table(db, query, "reports"); } bool create_persist_storage_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS persist_storage (" " key TEXT UNIQUE PRIMARY KEY NOT NULL," " item TEXT NOT NULL" ");"; return create_table(db, query, "persist_storage"); } bool recreate_message_store_threads_table(sqlite3 *db) { char *errMsg = 0; // 1. Create table without `last_navigated_to` or `last_pruned`. std::string create_new_table_query = "CREATE TABLE IF NOT EXISTS temp_message_store_threads (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " start_reached INTEGER NOT NULL" ");"; if (sqlite3_exec(db, create_new_table_query.c_str(), NULL, NULL, &errMsg) != SQLITE_OK) { Logger::log( "Error creating temp_message_store_threads: " + std::string{errMsg}); sqlite3_free(errMsg); return false; } // 2. Dump data from existing `message_store_threads` table into temp table. std::string copy_data_query = "INSERT INTO temp_message_store_threads (id, start_reached)" "SELECT id, start_reached FROM message_store_threads;"; if (sqlite3_exec(db, copy_data_query.c_str(), NULL, NULL, &errMsg) != SQLITE_OK) { Logger::log( "Error dumping data from existing message_store_threads to " "temp_message_store_threads: " + std::string{errMsg}); sqlite3_free(errMsg); return false; } // 3. Drop the existing `message_store_threads` table. std::string drop_old_table_query = "DROP TABLE message_store_threads;"; if (sqlite3_exec(db, drop_old_table_query.c_str(), NULL, NULL, &errMsg) != SQLITE_OK) { Logger::log( "Error dropping message_store_threads table: " + std::string{errMsg}); sqlite3_free(errMsg); return false; } // 4. Rename the temp table back to `message_store_threads`. std::string rename_table_query = "ALTER TABLE temp_message_store_threads RENAME TO message_store_threads;"; if (sqlite3_exec(db, rename_table_query.c_str(), NULL, NULL, &errMsg) != SQLITE_OK) { Logger::log( "Error renaming temp_message_store_threads to message_store_threads: " + std::string{errMsg}); sqlite3_free(errMsg); return false; } return true; } bool create_users_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS users (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " user_info TEXT NOT NULL" ");"; return create_table(db, query, "users"); } bool create_keyservers_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS keyservers (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " keyserver_info TEXT NOT NULL" ");"; return create_table(db, query, "keyservers"); } bool enable_rollback_journal_mode(sqlite3 *db) { char *error; sqlite3_exec(db, "PRAGMA journal_mode=DELETE;", nullptr, nullptr, &error); if (!error) { return true; } std::stringstream error_message; error_message << "Error disabling write-ahead logging mode: " << error; Logger::log(error_message.str()); sqlite3_free(error); return false; } bool create_communities_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS communities (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " community_info TEXT NOT NULL" ");"; return create_table(db, query, "communities"); } bool create_messages_to_device_table(sqlite3 *db) { std::string query = "CREATE TABLE IF NOT EXISTS messages_to_device (" " message_id TEXT NOT NULL," " device_id TEXT NOT NULL," " user_id TEXT NOT NULL," " timestamp BIGINT NOT NULL," " plaintext TEXT NOT NULL," " ciphertext TEXT NOT NULL," " PRIMARY KEY (message_id, device_id)" ");" "CREATE INDEX IF NOT EXISTS messages_to_device_idx_id_timestamp" " ON messages_to_device (device_id, timestamp);"; return create_table(db, query, "messages_to_device"); } bool create_schema(sqlite3 *db) { char *error; sqlite3_exec( db, "CREATE TABLE IF NOT EXISTS drafts (" " key TEXT UNIQUE PRIMARY KEY NOT NULL," " text TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS messages (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " local_id TEXT," " thread TEXT NOT NULL," " user TEXT NOT NULL," " type INTEGER NOT NULL," " future_type INTEGER," " content TEXT," " time INTEGER NOT NULL" ");" "CREATE TABLE IF NOT EXISTS olm_persist_account (" " id INTEGER UNIQUE PRIMARY KEY NOT NULL," " account_data TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS olm_persist_sessions (" " target_user_id TEXT UNIQUE PRIMARY KEY NOT NULL," " session_data TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS media (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " container TEXT NOT NULL," " thread TEXT NOT NULL," " uri TEXT NOT NULL," " type TEXT NOT NULL," " extras TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS threads (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " type INTEGER NOT NULL," " name TEXT," " description TEXT," " color TEXT NOT NULL," " creation_time BIGINT NOT NULL," " parent_thread_id TEXT," " containing_thread_id TEXT," " community TEXT," " members TEXT NOT NULL," " roles TEXT NOT NULL," " current_user TEXT NOT NULL," " source_message_id TEXT," " replies_count INTEGER NOT NULL," " avatar TEXT," " pinned_count INTEGER NOT NULL DEFAULT 0" ");" "CREATE TABLE IF NOT EXISTS metadata (" " name TEXT UNIQUE PRIMARY KEY NOT NULL," " data TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS message_store_threads (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " start_reached INTEGER NOT NULL" ");" "CREATE TABLE IF NOT EXISTS reports (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " report TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS persist_storage (" " key TEXT UNIQUE PRIMARY KEY NOT NULL," " item TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS users (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " user_info TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS keyservers (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " keyserver_info TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS communities (" " id TEXT UNIQUE PRIMARY KEY NOT NULL," " community_info TEXT NOT NULL" ");" "CREATE TABLE IF NOT EXISTS messages_to_device (" " message_id TEXT NOT NULL," " device_id TEXT NOT NULL," " user_id TEXT NOT NULL," " timestamp BIGINT NOT NULL," " plaintext TEXT NOT NULL," " ciphertext TEXT NOT NULL," " PRIMARY KEY (message_id, device_id)" ");" "CREATE INDEX IF NOT EXISTS media_idx_container" " ON media (container);" "CREATE INDEX IF NOT EXISTS messages_idx_thread_time" " ON messages (thread, time);" "CREATE INDEX IF NOT EXISTS messages_to_device_idx_id_timestamp" " ON messages_to_device (device_id, timestamp);", nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream stringStream; stringStream << "Error creating tables: " << error; Logger::log(stringStream.str()); sqlite3_free(error); return false; } void set_encryption_key( sqlite3 *db, const std::string &encryptionKey = SQLiteQueryExecutor::encryptionKey) { std::string set_encryption_key_query = "PRAGMA key = \"x'" + encryptionKey + "'\";"; char *error_set_key; sqlite3_exec( db, set_encryption_key_query.c_str(), nullptr, nullptr, &error_set_key); if (error_set_key) { std::ostringstream error_message; error_message << "Failed to set encryption key: " << error_set_key; throw std::system_error( ECANCELED, std::generic_category(), error_message.str()); } } int get_database_version(sqlite3 *db) { sqlite3_stmt *user_version_stmt; sqlite3_prepare_v2( db, "PRAGMA user_version;", -1, &user_version_stmt, nullptr); sqlite3_step(user_version_stmt); int current_user_version = sqlite3_column_int(user_version_stmt, 0); sqlite3_finalize(user_version_stmt); return current_user_version; } bool set_database_version(sqlite3 *db, int db_version) { std::stringstream update_version; update_version << "PRAGMA user_version=" << db_version << ";"; auto update_version_str = update_version.str(); char *error; sqlite3_exec(db, update_version_str.c_str(), nullptr, nullptr, &error); if (!error) { return true; } std::ostringstream errorStream; errorStream << "Error setting database version to " << db_version << ": " << error; Logger::log(errorStream.str()); sqlite3_free(error); return false; } // We don't want to run `PRAGMA key = ...;` // on main web database. The context is here: // https://linear.app/comm/issue/ENG-6398/issues-with-sqlcipher-on-web void default_on_db_open_callback(sqlite3 *db) { #ifndef EMSCRIPTEN set_encryption_key(db); #endif } // This is a temporary solution. In future we want to keep // a separate table for blob hashes. Tracked on Linear: // https://linear.app/comm/issue/ENG-6261/introduce-blob-hash-table std::string blob_hash_from_blob_service_uri(const std::string &media_uri) { static const std::string blob_service_prefix = "comm-blob-service://"; return media_uri.substr(blob_service_prefix.size()); } bool file_exists(const std::string &file_path) { std::ifstream file(file_path.c_str()); return file.good(); } void attempt_delete_file( const std::string &file_path, const char *error_message) { if (std::remove(file_path.c_str())) { throw std::system_error(errno, std::generic_category(), error_message); } } void attempt_rename_file( const std::string &old_path, const std::string &new_path, const char *error_message) { if (std::rename(old_path.c_str(), new_path.c_str())) { throw std::system_error(errno, std::generic_category(), error_message); } } bool is_database_queryable( sqlite3 *db, bool use_encryption_key, const std::string &path = SQLiteQueryExecutor::sqliteFilePath, const std::string &encryptionKey = SQLiteQueryExecutor::encryptionKey) { char *err_msg; sqlite3_open(path.c_str(), &db); // According to SQLCipher documentation running some SELECT is the only way to // check for key validity if (use_encryption_key) { set_encryption_key(db, encryptionKey); } sqlite3_exec( db, "SELECT COUNT(*) FROM sqlite_master;", nullptr, nullptr, &err_msg); sqlite3_close(db); return !err_msg; } void validate_encryption() { std::string temp_encrypted_db_path = SQLiteQueryExecutor::sqliteFilePath + "_temp_encrypted"; bool temp_encrypted_exists = file_exists(temp_encrypted_db_path); bool default_location_exists = file_exists(SQLiteQueryExecutor::sqliteFilePath); if (temp_encrypted_exists && default_location_exists) { Logger::log( "Previous encryption attempt failed. Repeating encryption process from " "the beginning."); attempt_delete_file( temp_encrypted_db_path, "Failed to delete corrupted encrypted database."); } else if (temp_encrypted_exists && !default_location_exists) { Logger::log( "Moving temporary encrypted database to default location failed in " "previous encryption attempt. Repeating rename step."); attempt_rename_file( temp_encrypted_db_path, SQLiteQueryExecutor::sqliteFilePath, "Failed to move encrypted database to default location."); return; } else if (!default_location_exists) { Logger::log( "Database not present yet. It will be created encrypted under default " "path."); return; } sqlite3 *db; if (is_database_queryable(db, true)) { Logger::log( "Database exists under default path and it is correctly encrypted."); return; } if (!is_database_queryable(db, false)) { Logger::log( "Database exists but it is encrypted with key that was lost. " "Attempting database deletion. New encrypted one will be created."); attempt_delete_file( SQLiteQueryExecutor::sqliteFilePath.c_str(), "Failed to delete database encrypted with lost key."); return; } else { Logger::log( "Database exists but it is not encrypted. Attempting encryption " "process."); } sqlite3_open(SQLiteQueryExecutor::sqliteFilePath.c_str(), &db); std::string createEncryptedCopySQL = "ATTACH DATABASE '" + temp_encrypted_db_path + "' AS encrypted_comm " "KEY \"x'" + SQLiteQueryExecutor::encryptionKey + "'\";" "SELECT sqlcipher_export('encrypted_comm');" "DETACH DATABASE encrypted_comm;"; char *encryption_error; sqlite3_exec( db, createEncryptedCopySQL.c_str(), nullptr, nullptr, &encryption_error); if (encryption_error) { throw std::system_error( ECANCELED, std::generic_category(), "Failed to create encrypted copy of the original database."); } sqlite3_close(db); attempt_delete_file( SQLiteQueryExecutor::sqliteFilePath, "Failed to delete unencrypted database."); attempt_rename_file( temp_encrypted_db_path, SQLiteQueryExecutor::sqliteFilePath, "Failed to move encrypted database to default location."); Logger::log("Encryption completed successfully."); } typedef bool ShouldBeInTransaction; typedef std::function MigrateFunction; typedef std::pair SQLiteMigration; std::vector> migrations{ {{1, {create_drafts_table, true}}, {2, {rename_threadID_to_key, true}}, {4, {create_persist_account_table, true}}, {5, {create_persist_sessions_table, true}}, {15, {create_media_table, true}}, {16, {drop_messages_table, true}}, {17, {recreate_messages_table, true}}, {18, {create_messages_idx_thread_time, true}}, {19, {create_media_idx_container, true}}, {20, {create_threads_table, true}}, {21, {update_threadID_for_pending_threads_in_drafts, true}}, {22, {enable_write_ahead_logging_mode, false}}, {23, {create_metadata_table, true}}, {24, {add_not_null_constraint_to_drafts, true}}, {25, {add_not_null_constraint_to_metadata, true}}, {26, {add_avatar_column_to_threads_table, true}}, {27, {add_pinned_count_column_to_threads, true}}, {28, {create_message_store_threads_table, true}}, {29, {create_reports_table, true}}, {30, {create_persist_storage_table, true}}, {31, {recreate_message_store_threads_table, true}}, {32, {create_users_table, true}}, {33, {create_keyservers_table, true}}, {34, {enable_rollback_journal_mode, false}}, {35, {create_communities_table, true}}, {36, {create_messages_to_device_table, true}}}}; enum class MigrationResult { SUCCESS, FAILURE, NOT_APPLIED }; MigrationResult applyMigrationWithTransaction( sqlite3 *db, const MigrateFunction &migrate, int index) { sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, nullptr); auto db_version = get_database_version(db); if (index <= db_version) { sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr); return MigrationResult::NOT_APPLIED; } auto rc = migrate(db); if (!rc) { sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr); return MigrationResult::FAILURE; } auto database_version_set = set_database_version(db, index); if (!database_version_set) { sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr); return MigrationResult::FAILURE; } sqlite3_exec(db, "END TRANSACTION;", nullptr, nullptr, nullptr); return MigrationResult::SUCCESS; } MigrationResult applyMigrationWithoutTransaction( sqlite3 *db, const MigrateFunction &migrate, int index) { auto db_version = get_database_version(db); if (index <= db_version) { return MigrationResult::NOT_APPLIED; } auto rc = migrate(db); if (!rc) { return MigrationResult::FAILURE; } sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, nullptr); auto inner_db_version = get_database_version(db); if (index <= inner_db_version) { sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr); return MigrationResult::NOT_APPLIED; } auto database_version_set = set_database_version(db, index); if (!database_version_set) { sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr); return MigrationResult::FAILURE; } sqlite3_exec(db, "END TRANSACTION;", nullptr, nullptr, nullptr); return MigrationResult::SUCCESS; } bool set_up_database(sqlite3 *db) { sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, nullptr); auto db_version = get_database_version(db); auto latest_version = migrations.back().first; if (db_version == latest_version) { sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr); return true; } if (db_version != 0 || !create_schema(db) || !set_database_version(db, latest_version)) { sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr); return false; } sqlite3_exec(db, "END TRANSACTION;", nullptr, nullptr, nullptr); return true; } void SQLiteQueryExecutor::migrate() { // We don't want to run `PRAGMA key = ...;` // on main web database. The context is here: // https://linear.app/comm/issue/ENG-6398/issues-with-sqlcipher-on-web #ifndef EMSCRIPTEN validate_encryption(); #endif sqlite3 *db; sqlite3_open(SQLiteQueryExecutor::sqliteFilePath.c_str(), &db); default_on_db_open_callback(db); std::stringstream db_path; db_path << "db path: " << SQLiteQueryExecutor::sqliteFilePath.c_str() << std::endl; Logger::log(db_path.str()); auto db_version = get_database_version(db); std::stringstream version_msg; version_msg << "db version: " << db_version << std::endl; Logger::log(version_msg.str()); if (db_version == 0) { auto db_created = set_up_database(db); if (!db_created) { sqlite3_close(db); Logger::log("Database structure creation error."); throw std::runtime_error("Database structure creation error"); } Logger::log("Database structure created."); sqlite3_close(db); return; } for (const auto &[idx, migration] : migrations) { const auto &[applyMigration, shouldBeInTransaction] = migration; MigrationResult migrationResult; if (shouldBeInTransaction) { migrationResult = applyMigrationWithTransaction(db, applyMigration, idx); } else { migrationResult = applyMigrationWithoutTransaction(db, applyMigration, idx); } if (migrationResult == MigrationResult::NOT_APPLIED) { continue; } std::stringstream migration_msg; if (migrationResult == MigrationResult::FAILURE) { migration_msg << "migration " << idx << " failed." << std::endl; Logger::log(migration_msg.str()); sqlite3_close(db); throw std::runtime_error(migration_msg.str()); } if (migrationResult == MigrationResult::SUCCESS) { migration_msg << "migration " << idx << " succeeded." << std::endl; Logger::log(migration_msg.str()); } } sqlite3_close(db); } SQLiteQueryExecutor::SQLiteQueryExecutor() { SQLiteQueryExecutor::migrate(); #ifndef EMSCRIPTEN std::string currentBackupID = this->getMetadata("backupID"); if (!StaffUtils::isStaffRelease() || !currentBackupID.size()) { return; } SQLiteQueryExecutor::connectionManager.setLogsMonitoring(true); #endif } SQLiteQueryExecutor::SQLiteQueryExecutor(std::string sqliteFilePath) { SQLiteQueryExecutor::sqliteFilePath = sqliteFilePath; SQLiteQueryExecutor::migrate(); } sqlite3 *SQLiteQueryExecutor::getConnection() { if (SQLiteQueryExecutor::connectionManager.getConnection()) { return SQLiteQueryExecutor::connectionManager.getConnection(); } SQLiteQueryExecutor::connectionManager.initializeConnection( SQLiteQueryExecutor::sqliteFilePath, default_on_db_open_callback); return SQLiteQueryExecutor::connectionManager.getConnection(); } void SQLiteQueryExecutor::closeConnection() { SQLiteQueryExecutor::connectionManager.closeConnection(); } SQLiteQueryExecutor::~SQLiteQueryExecutor() { SQLiteQueryExecutor::closeConnection(); } std::string SQLiteQueryExecutor::getDraft(std::string key) const { static std::string getDraftByPrimaryKeySQL = "SELECT * " "FROM drafts " "WHERE key = ?;"; std::unique_ptr draft = getEntityByPrimaryKey( SQLiteQueryExecutor::getConnection(), getDraftByPrimaryKeySQL, key); return (draft == nullptr) ? "" : draft->text; } std::unique_ptr SQLiteQueryExecutor::getThread(std::string threadID) const { static std::string getThreadByPrimaryKeySQL = "SELECT * " "FROM threads " "WHERE id = ?;"; return getEntityByPrimaryKey( SQLiteQueryExecutor::getConnection(), getThreadByPrimaryKeySQL, threadID); } void SQLiteQueryExecutor::updateDraft(std::string key, std::string text) const { static std::string replaceDraftSQL = "REPLACE INTO drafts (key, text) " "VALUES (?, ?);"; Draft draft = {key, text}; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceDraftSQL, draft); } bool SQLiteQueryExecutor::moveDraft(std::string oldKey, std::string newKey) const { std::string draftText = this->getDraft(oldKey); if (!draftText.size()) { return false; } static std::string rekeyDraftSQL = "UPDATE OR REPLACE drafts " "SET key = ? " "WHERE key = ?;"; rekeyAllEntities( SQLiteQueryExecutor::getConnection(), rekeyDraftSQL, oldKey, newKey); return true; } std::vector SQLiteQueryExecutor::getAllDrafts() const { static std::string getAllDraftsSQL = "SELECT * " "FROM drafts;"; return getAllEntities( SQLiteQueryExecutor::getConnection(), getAllDraftsSQL); } void SQLiteQueryExecutor::removeAllDrafts() const { static std::string removeAllDraftsSQL = "DELETE FROM drafts;"; removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllDraftsSQL); } void SQLiteQueryExecutor::removeDrafts( const std::vector &ids) const { if (!ids.size()) { return; } std::stringstream removeDraftsByKeysSQLStream; removeDraftsByKeysSQLStream << "DELETE FROM drafts " "WHERE key IN " << getSQLStatementArray(ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeDraftsByKeysSQLStream.str(), ids); } void SQLiteQueryExecutor::removeAllMessages() const { static std::string removeAllMessagesSQL = "DELETE FROM messages;"; removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllMessagesSQL); } std::vector>> SQLiteQueryExecutor::getAllMessages() const { static std::string getAllMessagesSQL = "SELECT * " "FROM messages " "LEFT JOIN media " " ON messages.id = media.container " "ORDER BY messages.id;"; SQLiteStatementWrapper preparedSQL( SQLiteQueryExecutor::getConnection(), getAllMessagesSQL, "Failed to retrieve all messages."); std::string prevMsgIdx{}; std::vector>> allMessages; for (int stepResult = sqlite3_step(preparedSQL); stepResult == SQLITE_ROW; stepResult = sqlite3_step(preparedSQL)) { Message message = Message::fromSQLResult(preparedSQL, 0); if (message.id == prevMsgIdx) { allMessages.back().second.push_back(Media::fromSQLResult(preparedSQL, 8)); } else { prevMsgIdx = message.id; std::vector mediaForMsg; if (sqlite3_column_type(preparedSQL, 8) != SQLITE_NULL) { mediaForMsg.push_back(Media::fromSQLResult(preparedSQL, 8)); } allMessages.push_back(std::make_pair(std::move(message), mediaForMsg)); } } return allMessages; } void SQLiteQueryExecutor::removeMessages( const std::vector &ids) const { if (!ids.size()) { return; } std::stringstream removeMessagesByKeysSQLStream; removeMessagesByKeysSQLStream << "DELETE FROM messages " "WHERE id IN " << getSQLStatementArray(ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeMessagesByKeysSQLStream.str(), ids); } void SQLiteQueryExecutor::removeMessagesForThreads( const std::vector &threadIDs) const { if (!threadIDs.size()) { return; } std::stringstream removeMessagesByKeysSQLStream; removeMessagesByKeysSQLStream << "DELETE FROM messages " "WHERE thread IN " << getSQLStatementArray(threadIDs.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeMessagesByKeysSQLStream.str(), threadIDs); } void SQLiteQueryExecutor::replaceMessage(const Message &message) const { static std::string replaceMessageSQL = "REPLACE INTO messages " "(id, local_id, thread, user, type, future_type, content, time) " "VALUES (?, ?, ?, ?, ?, ?, ?, ?);"; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceMessageSQL, message); } void SQLiteQueryExecutor::rekeyMessage(std::string from, std::string to) const { static std::string rekeyMessageSQL = "UPDATE OR REPLACE messages " "SET id = ? " "WHERE id = ?"; rekeyAllEntities( SQLiteQueryExecutor::getConnection(), rekeyMessageSQL, from, to); } void SQLiteQueryExecutor::removeAllMedia() const { static std::string removeAllMediaSQL = "DELETE FROM media;"; removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllMediaSQL); } void SQLiteQueryExecutor::removeMediaForMessages( const std::vector &msg_ids) const { if (!msg_ids.size()) { return; } std::stringstream removeMediaByKeysSQLStream; removeMediaByKeysSQLStream << "DELETE FROM media " "WHERE container IN " << getSQLStatementArray(msg_ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeMediaByKeysSQLStream.str(), msg_ids); } void SQLiteQueryExecutor::removeMediaForMessage(std::string msg_id) const { static std::string removeMediaByKeySQL = "DELETE FROM media " "WHERE container IN (?);"; std::vector keys = {msg_id}; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeMediaByKeySQL, keys); } void SQLiteQueryExecutor::removeMediaForThreads( const std::vector &thread_ids) const { if (!thread_ids.size()) { return; } std::stringstream removeMediaByKeysSQLStream; removeMediaByKeysSQLStream << "DELETE FROM media " "WHERE thread IN " << getSQLStatementArray(thread_ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeMediaByKeysSQLStream.str(), thread_ids); } void SQLiteQueryExecutor::replaceMedia(const Media &media) const { static std::string replaceMediaSQL = "REPLACE INTO media " "(id, container, thread, uri, type, extras) " "VALUES (?, ?, ?, ?, ?, ?)"; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceMediaSQL, media); } void SQLiteQueryExecutor::rekeyMediaContainers(std::string from, std::string to) const { static std::string rekeyMediaContainersSQL = "UPDATE media SET container = ? WHERE container = ?;"; rekeyAllEntities( SQLiteQueryExecutor::getConnection(), rekeyMediaContainersSQL, from, to); } void SQLiteQueryExecutor::replaceMessageStoreThreads( const std::vector &threads) const { static std::string replaceMessageStoreThreadSQL = "REPLACE INTO message_store_threads " "(id, start_reached) " "VALUES (?, ?);"; for (auto &thread : threads) { replaceEntity( SQLiteQueryExecutor::getConnection(), replaceMessageStoreThreadSQL, thread); } } void SQLiteQueryExecutor::removeAllMessageStoreThreads() const { static std::string removeAllMessageStoreThreadsSQL = "DELETE FROM message_store_threads;"; removeAllEntities( SQLiteQueryExecutor::getConnection(), removeAllMessageStoreThreadsSQL); } void SQLiteQueryExecutor::removeMessageStoreThreads( const std::vector &ids) const { if (!ids.size()) { return; } std::stringstream removeMessageStoreThreadsByKeysSQLStream; removeMessageStoreThreadsByKeysSQLStream << "DELETE FROM message_store_threads " "WHERE id IN " << getSQLStatementArray(ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeMessageStoreThreadsByKeysSQLStream.str(), ids); } std::vector SQLiteQueryExecutor::getAllMessageStoreThreads() const { static std::string getAllMessageStoreThreadsSQL = "SELECT * " "FROM message_store_threads;"; return getAllEntities( SQLiteQueryExecutor::getConnection(), getAllMessageStoreThreadsSQL); } std::vector SQLiteQueryExecutor::getAllThreads() const { static std::string getAllThreadsSQL = "SELECT * " "FROM threads;"; return getAllEntities( SQLiteQueryExecutor::getConnection(), getAllThreadsSQL); }; void SQLiteQueryExecutor::removeThreads(std::vector ids) const { if (!ids.size()) { return; } std::stringstream removeThreadsByKeysSQLStream; removeThreadsByKeysSQLStream << "DELETE FROM threads " "WHERE id IN " << getSQLStatementArray(ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeThreadsByKeysSQLStream.str(), ids); }; void SQLiteQueryExecutor::replaceThread(const Thread &thread) const { static std::string replaceThreadSQL = "REPLACE INTO threads (" " id, type, name, description, color, creation_time, parent_thread_id," " containing_thread_id, community, members, roles, current_user," " source_message_id, replies_count, avatar, pinned_count) " "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceThreadSQL, thread); }; void SQLiteQueryExecutor::removeAllThreads() const { static std::string removeAllThreadsSQL = "DELETE FROM threads;"; removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllThreadsSQL); }; void SQLiteQueryExecutor::replaceReport(const Report &report) const { static std::string replaceReportSQL = "REPLACE INTO reports (id, report) " "VALUES (?, ?);"; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceReportSQL, report); } void SQLiteQueryExecutor::removeAllReports() const { static std::string removeAllReportsSQL = "DELETE FROM reports;"; removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllReportsSQL); } void SQLiteQueryExecutor::removeReports( const std::vector &ids) const { if (!ids.size()) { return; } std::stringstream removeReportsByKeysSQLStream; removeReportsByKeysSQLStream << "DELETE FROM reports " "WHERE id IN " << getSQLStatementArray(ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeReportsByKeysSQLStream.str(), ids); } std::vector SQLiteQueryExecutor::getAllReports() const { static std::string getAllReportsSQL = "SELECT * " "FROM reports;"; return getAllEntities( SQLiteQueryExecutor::getConnection(), getAllReportsSQL); } void SQLiteQueryExecutor::setPersistStorageItem( std::string key, std::string item) const { static std::string replacePersistStorageItemSQL = "REPLACE INTO persist_storage (key, item) " "VALUES (?, ?);"; PersistItem entry{ key, item, }; replaceEntity( SQLiteQueryExecutor::getConnection(), replacePersistStorageItemSQL, entry); } void SQLiteQueryExecutor::removePersistStorageItem(std::string key) const { static std::string removePersistStorageItemByKeySQL = "DELETE FROM persist_storage " "WHERE key IN (?);"; std::vector keys = {key}; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removePersistStorageItemByKeySQL, keys); } std::string SQLiteQueryExecutor::getPersistStorageItem(std::string key) const { static std::string getPersistStorageItemByPrimaryKeySQL = "SELECT * " "FROM persist_storage " "WHERE key = ?;"; std::unique_ptr entry = getEntityByPrimaryKey( SQLiteQueryExecutor::getConnection(), getPersistStorageItemByPrimaryKeySQL, key); return (entry == nullptr) ? "" : entry->item; } void SQLiteQueryExecutor::replaceUser(const UserInfo &user_info) const { static std::string replaceUserSQL = "REPLACE INTO users (id, user_info) " "VALUES (?, ?);"; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceUserSQL, user_info); } void SQLiteQueryExecutor::removeAllUsers() const { static std::string removeAllUsersSQL = "DELETE FROM users;"; removeAllEntities(SQLiteQueryExecutor::getConnection(), removeAllUsersSQL); } void SQLiteQueryExecutor::removeUsers( const std::vector &ids) const { if (!ids.size()) { return; } std::stringstream removeUsersByKeysSQLStream; removeUsersByKeysSQLStream << "DELETE FROM users " "WHERE id IN " << getSQLStatementArray(ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeUsersByKeysSQLStream.str(), ids); } void SQLiteQueryExecutor::replaceKeyserver( const KeyserverInfo &keyserver_info) const { static std::string replaceKeyserverSQL = "REPLACE INTO keyservers (id, keyserver_info) " "VALUES (?, ?);"; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceKeyserverSQL, keyserver_info); } void SQLiteQueryExecutor::removeAllKeyservers() const { static std::string removeAllKeyserversSQL = "DELETE FROM keyservers;"; removeAllEntities( SQLiteQueryExecutor::getConnection(), removeAllKeyserversSQL); } void SQLiteQueryExecutor::removeKeyservers( const std::vector &ids) const { if (!ids.size()) { return; } std::stringstream removeKeyserversByKeysSQLStream; removeKeyserversByKeysSQLStream << "DELETE FROM keyservers " "WHERE id IN " << getSQLStatementArray(ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeKeyserversByKeysSQLStream.str(), ids); } std::vector SQLiteQueryExecutor::getAllKeyservers() const { static std::string getAllKeyserversSQL = "SELECT * " "FROM keyservers;"; return getAllEntities( SQLiteQueryExecutor::getConnection(), getAllKeyserversSQL); } std::vector SQLiteQueryExecutor::getAllUsers() const { static std::string getAllUsersSQL = "SELECT * " "FROM users;"; return getAllEntities( SQLiteQueryExecutor::getConnection(), getAllUsersSQL); } void SQLiteQueryExecutor::replaceCommunity( const CommunityInfo &community_info) const { static std::string replaceCommunitySQL = "REPLACE INTO communities (id, community_info) " "VALUES (?, ?);"; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceCommunitySQL, community_info); } void SQLiteQueryExecutor::removeAllCommunities() const { static std::string removeAllCommunitiesSQL = "DELETE FROM communities;"; removeAllEntities( SQLiteQueryExecutor::getConnection(), removeAllCommunitiesSQL); } void SQLiteQueryExecutor::removeCommunities( const std::vector &ids) const { if (!ids.size()) { return; } std::stringstream removeCommunitiesByKeysSQLStream; removeCommunitiesByKeysSQLStream << "DELETE FROM communities " "WHERE id IN " << getSQLStatementArray(ids.size()) << ";"; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeCommunitiesByKeysSQLStream.str(), ids); } std::vector SQLiteQueryExecutor::getAllCommunities() const { static std::string getAllCommunitiesSQL = "SELECT * " "FROM communities;"; return getAllEntities( SQLiteQueryExecutor::getConnection(), getAllCommunitiesSQL); } void SQLiteQueryExecutor::beginTransaction() const { executeQuery(SQLiteQueryExecutor::getConnection(), "BEGIN TRANSACTION;"); } void SQLiteQueryExecutor::commitTransaction() const { executeQuery(SQLiteQueryExecutor::getConnection(), "COMMIT;"); } void SQLiteQueryExecutor::rollbackTransaction() const { executeQuery(SQLiteQueryExecutor::getConnection(), "ROLLBACK;"); } +int SQLiteQueryExecutor::getContentAccountID() const { + return CONTENT_ACCOUNT_ID; +} + +int SQLiteQueryExecutor::getNotifsAccountID() const { + return NOTIFS_ACCOUNT_ID; +} + std::vector SQLiteQueryExecutor::getOlmPersistSessionsData() const { static std::string getAllOlmPersistSessionsSQL = "SELECT * " "FROM olm_persist_sessions;"; return getAllEntities( SQLiteQueryExecutor::getConnection(), getAllOlmPersistSessionsSQL); } std::optional -SQLiteQueryExecutor::getOlmPersistAccountData() const { - static std::string getAllOlmPersistAccountSQL = +SQLiteQueryExecutor::getOlmPersistAccountData(int accountID) const { + static std::string getOlmPersistAccountSQL = "SELECT * " - "FROM olm_persist_account;"; - std::vector result = getAllEntities( - SQLiteQueryExecutor::getConnection(), getAllOlmPersistAccountSQL); - if (result.size() > 1) { - throw std::system_error( - ECANCELED, - std::generic_category(), - "Multiple records found for the olm_persist_account table"); + "FROM olm_persist_account " + "WHERE id = ?;"; + std::unique_ptr result = + getEntityByIntegerPrimaryKey( + SQLiteQueryExecutor::getConnection(), + getOlmPersistAccountSQL, + accountID); + if (result == nullptr) { + return std::nullopt; } - return (result.size() == 0) - ? std::nullopt - : std::optional(result[0].account_data); + return result->account_data; } void SQLiteQueryExecutor::storeOlmPersistAccount( + int accountID, const std::string &accountData) const { static std::string replaceOlmPersistAccountSQL = "REPLACE INTO olm_persist_account (id, account_data) " "VALUES (?, ?);"; - OlmPersistAccount persistAccount = {ACCOUNT_ID, accountData}; + OlmPersistAccount persistAccount = {accountID, accountData}; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceOlmPersistAccountSQL, persistAccount); } void SQLiteQueryExecutor::storeOlmPersistSession( const OlmPersistSession &session) const { static std::string replaceOlmPersistSessionSQL = "REPLACE INTO olm_persist_sessions (target_user_id, session_data) " "VALUES (?, ?);"; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceOlmPersistSessionSQL, session); } -void SQLiteQueryExecutor::storeOlmPersistData(crypto::Persist persist) const { +void SQLiteQueryExecutor::storeOlmPersistData( + int accountID, + crypto::Persist persist) const { + + if (accountID != CONTENT_ACCOUNT_ID && persist.sessions.size() > 0) { + throw std::runtime_error( + "Attempt to store notifications sessions in SQLite. Notifications " + "sessions must be stored in storage shared with NSE."); + } + std::string accountData = std::string(persist.account.begin(), persist.account.end()); - this->storeOlmPersistAccount(accountData); + this->storeOlmPersistAccount(accountID, accountData); for (auto it = persist.sessions.begin(); it != persist.sessions.end(); it++) { OlmPersistSession persistSession = { it->first, std::string(it->second.begin(), it->second.end())}; this->storeOlmPersistSession(persistSession); } } void SQLiteQueryExecutor::setNotifyToken(std::string token) const { this->setMetadata("notify_token", token); } void SQLiteQueryExecutor::clearNotifyToken() const { this->clearMetadata("notify_token"); } void SQLiteQueryExecutor::setCurrentUserID(std::string userID) const { this->setMetadata("current_user_id", userID); } std::string SQLiteQueryExecutor::getCurrentUserID() const { return this->getMetadata("current_user_id"); } void SQLiteQueryExecutor::setMetadata(std::string entry_name, std::string data) const { std::string replaceMetadataSQL = "REPLACE INTO metadata (name, data) " "VALUES (?, ?);"; Metadata entry{ entry_name, data, }; replaceEntity( SQLiteQueryExecutor::getConnection(), replaceMetadataSQL, entry); } void SQLiteQueryExecutor::clearMetadata(std::string entry_name) const { static std::string removeMetadataByKeySQL = "DELETE FROM metadata " "WHERE name IN (?);"; std::vector keys = {entry_name}; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeMetadataByKeySQL, keys); } std::string SQLiteQueryExecutor::getMetadata(std::string entry_name) const { std::string getMetadataByPrimaryKeySQL = "SELECT * " "FROM metadata " "WHERE name = ?;"; std::unique_ptr entry = getEntityByPrimaryKey( SQLiteQueryExecutor::getConnection(), getMetadataByPrimaryKeySQL, entry_name); return (entry == nullptr) ? "" : entry->data; } void SQLiteQueryExecutor::addMessagesToDevice( const std::vector &messages) const { static std::string addMessageToDevice = "REPLACE INTO messages_to_device (" " message_id, device_id, user_id, timestamp, plaintext, ciphertext) " "VALUES (?, ?, ?, ?, ?, ?);"; for (const ClientMessageToDevice &clientMessage : messages) { MessageToDevice message = clientMessage.toMessageToDevice(); replaceEntity( SQLiteQueryExecutor::getConnection(), addMessageToDevice, message); } } std::vector SQLiteQueryExecutor::getAllMessagesToDevice(const std::string &deviceID) const { std::string query = "SELECT * FROM messages_to_device " "WHERE device_id = ? " "ORDER BY timestamp;"; SQLiteStatementWrapper preparedSQL( SQLiteQueryExecutor::getConnection(), query, "Failed to get all messages to device"); sqlite3_bind_text(preparedSQL, 1, deviceID.c_str(), -1, SQLITE_TRANSIENT); std::vector messages; for (int stepResult = sqlite3_step(preparedSQL); stepResult == SQLITE_ROW; stepResult = sqlite3_step(preparedSQL)) { messages.emplace_back( ClientMessageToDevice(MessageToDevice::fromSQLResult(preparedSQL, 0))); } return messages; } void SQLiteQueryExecutor::removeMessagesToDeviceOlderThan( const ClientMessageToDevice &lastConfirmedMessageClient) const { static std::string query = "DELETE FROM messages_to_device " "WHERE timestamp <= ? AND device_id IN (?);"; MessageToDevice lastConfirmedMessage = lastConfirmedMessageClient.toMessageToDevice(); comm::SQLiteStatementWrapper preparedSQL( SQLiteQueryExecutor::getConnection(), query, "Failed to remove messages to device"); sqlite3_bind_int64(preparedSQL, 1, lastConfirmedMessage.timestamp); sqlite3_bind_text( preparedSQL, 2, lastConfirmedMessage.device_id.c_str(), -1, SQLITE_TRANSIENT); int result = sqlite3_step(preparedSQL); if (result != SQLITE_DONE) { throw std::runtime_error( "Failed to execute removeMessagesToDeviceOlderThan statement: " + std::string(sqlite3_errmsg(SQLiteQueryExecutor::getConnection()))); } } void SQLiteQueryExecutor::removeAllMessagesForDevice( const std::string &deviceID) const { static std::string removeMessagesSQL = "DELETE FROM messages_to_device " "WHERE device_id IN (?);"; std::vector keys = {deviceID}; removeEntitiesByKeys( SQLiteQueryExecutor::getConnection(), removeMessagesSQL, keys); } #ifdef EMSCRIPTEN std::vector SQLiteQueryExecutor::getAllThreadsWeb() const { auto threads = this->getAllThreads(); std::vector webThreads; webThreads.reserve(threads.size()); for (const auto &thread : threads) { webThreads.emplace_back(thread); } return webThreads; }; void SQLiteQueryExecutor::replaceThreadWeb(const WebThread &thread) const { this->replaceThread(thread.toThread()); }; std::vector SQLiteQueryExecutor::getAllMessagesWeb() const { auto allMessages = this->getAllMessages(); std::vector allMessageWithMedias; for (auto &messageWitMedia : allMessages) { allMessageWithMedias.push_back( {std::move(messageWitMedia.first), messageWitMedia.second}); } return allMessageWithMedias; } void SQLiteQueryExecutor::replaceMessageWeb(const WebMessage &message) const { this->replaceMessage(message.toMessage()); }; -NullableString SQLiteQueryExecutor::getOlmPersistAccountDataWeb() const { - std::optional accountData = this->getOlmPersistAccountData(); +NullableString +SQLiteQueryExecutor::getOlmPersistAccountDataWeb(int accountID) const { + std::optional accountData = + this->getOlmPersistAccountData(accountID); if (!accountData.has_value()) { return NullableString(); } return std::make_unique(accountData.value()); } #else void SQLiteQueryExecutor::clearSensitiveData() { SQLiteQueryExecutor::closeConnection(); if (file_exists(SQLiteQueryExecutor::sqliteFilePath) && std::remove(SQLiteQueryExecutor::sqliteFilePath.c_str())) { std::ostringstream errorStream; errorStream << "Failed to delete database file. Details: " << strerror(errno); throw std::system_error(errno, std::generic_category(), errorStream.str()); } SQLiteQueryExecutor::generateFreshEncryptionKey(); SQLiteQueryExecutor::migrate(); } void SQLiteQueryExecutor::initialize(std::string &databasePath) { std::call_once(SQLiteQueryExecutor::initialized, [&databasePath]() { SQLiteQueryExecutor::sqliteFilePath = databasePath; folly::Optional maybeEncryptionKey = CommSecureStore::get(SQLiteQueryExecutor::secureStoreEncryptionKeyID); folly::Optional maybeBackupLogsEncryptionKey = CommSecureStore::get( SQLiteQueryExecutor::secureStoreBackupLogsEncryptionKeyID); if (file_exists(databasePath) && maybeEncryptionKey && maybeBackupLogsEncryptionKey) { SQLiteQueryExecutor::encryptionKey = maybeEncryptionKey.value(); SQLiteQueryExecutor::backupLogsEncryptionKey = maybeBackupLogsEncryptionKey.value(); return; } else if (file_exists(databasePath) && maybeEncryptionKey) { SQLiteQueryExecutor::encryptionKey = maybeEncryptionKey.value(); SQLiteQueryExecutor::generateFreshBackupLogsEncryptionKey(); return; } SQLiteQueryExecutor::generateFreshEncryptionKey(); }); } void SQLiteQueryExecutor::createMainCompaction(std::string backupID) const { std::string finalBackupPath = PlatformSpecificTools::getBackupFilePath(backupID, false); std::string finalAttachmentsPath = PlatformSpecificTools::getBackupFilePath(backupID, true); std::string tempBackupPath = finalBackupPath + "_tmp"; std::string tempAttachmentsPath = finalAttachmentsPath + "_tmp"; if (file_exists(tempBackupPath)) { Logger::log( "Attempting to delete temporary backup file from previous backup " "attempt."); attempt_delete_file( tempBackupPath, "Failed to delete temporary backup file from previous backup attempt."); } if (file_exists(tempAttachmentsPath)) { Logger::log( "Attempting to delete temporary attachments file from previous backup " "attempt."); attempt_delete_file( tempAttachmentsPath, "Failed to delete temporary attachments file from previous backup " "attempt."); } sqlite3 *backupDB; sqlite3_open(tempBackupPath.c_str(), &backupDB); set_encryption_key(backupDB); sqlite3_backup *backupObj = sqlite3_backup_init( backupDB, "main", SQLiteQueryExecutor::getConnection(), "main"); if (!backupObj) { std::stringstream error_message; error_message << "Failed to init backup for main compaction. Details: " << sqlite3_errmsg(backupDB) << std::endl; sqlite3_close(backupDB); throw std::runtime_error(error_message.str()); } int backupResult = sqlite3_backup_step(backupObj, -1); sqlite3_backup_finish(backupObj); if (backupResult == SQLITE_BUSY || backupResult == SQLITE_LOCKED) { sqlite3_close(backupDB); throw std::runtime_error( "Programmer error. Database in transaction during backup attempt."); } else if (backupResult != SQLITE_DONE) { sqlite3_close(backupDB); std::stringstream error_message; error_message << "Failed to create database backup. Details: " << sqlite3_errstr(backupResult); throw std::runtime_error(error_message.str()); } std::string removeDeviceSpecificDataSQL = "DELETE FROM olm_persist_account;" "DELETE FROM olm_persist_sessions;" "DELETE FROM metadata;" "DELETE FROM messages_to_device;"; executeQuery(backupDB, removeDeviceSpecificDataSQL); executeQuery(backupDB, "VACUUM;"); sqlite3_close(backupDB); attempt_rename_file( tempBackupPath, finalBackupPath, "Failed to rename complete temporary backup file to final backup file."); std::ofstream tempAttachmentsFile(tempAttachmentsPath); if (!tempAttachmentsFile.is_open()) { throw std::runtime_error( "Unable to create attachments file for backup id: " + backupID); } std::string getAllBlobServiceMediaSQL = "SELECT * FROM media WHERE uri LIKE 'comm-blob-service://%';"; std::vector blobServiceMedia = getAllEntities( SQLiteQueryExecutor::getConnection(), getAllBlobServiceMediaSQL); for (const auto &media : blobServiceMedia) { std::string blobServiceURI = media.uri; std::string blobHash = blob_hash_from_blob_service_uri(blobServiceURI); tempAttachmentsFile << blobHash << "\n"; } tempAttachmentsFile.close(); attempt_rename_file( tempAttachmentsPath, finalAttachmentsPath, "Failed to rename complete temporary attachments file to final " "attachments file."); this->setMetadata("backupID", backupID); this->clearMetadata("logID"); if (StaffUtils::isStaffRelease()) { SQLiteQueryExecutor::connectionManager.setLogsMonitoring(true); } } void SQLiteQueryExecutor::generateFreshEncryptionKey() { std::string encryptionKey = comm::crypto::Tools::generateRandomHexString( SQLiteQueryExecutor::sqlcipherEncryptionKeySize); CommSecureStore::set( SQLiteQueryExecutor::secureStoreEncryptionKeyID, encryptionKey); SQLiteQueryExecutor::encryptionKey = encryptionKey; SQLiteQueryExecutor::generateFreshBackupLogsEncryptionKey(); } void SQLiteQueryExecutor::generateFreshBackupLogsEncryptionKey() { std::string backupLogsEncryptionKey = comm::crypto::Tools::generateRandomHexString( SQLiteQueryExecutor::backupLogsEncryptionKeySize); CommSecureStore::set( SQLiteQueryExecutor::secureStoreBackupLogsEncryptionKeyID, backupLogsEncryptionKey); SQLiteQueryExecutor::backupLogsEncryptionKey = backupLogsEncryptionKey; } void SQLiteQueryExecutor::captureBackupLogs() const { std::string backupID = this->getMetadata("backupID"); if (!backupID.size()) { return; } std::string logID = this->getMetadata("logID"); if (!logID.size()) { logID = "1"; } bool newLogCreated = SQLiteQueryExecutor::connectionManager.captureLogs( backupID, logID, SQLiteQueryExecutor::backupLogsEncryptionKey); if (!newLogCreated) { return; } this->setMetadata("logID", std::to_string(std::stoi(logID) + 1)); } #endif void SQLiteQueryExecutor::restoreFromMainCompaction( std::string mainCompactionPath, std::string mainCompactionEncryptionKey) const { if (!file_exists(mainCompactionPath)) { throw std::runtime_error("Restore attempt but backup file does not exist."); } sqlite3 *backupDB; if (!is_database_queryable( backupDB, true, mainCompactionPath, mainCompactionEncryptionKey)) { throw std::runtime_error("Backup file or encryption key corrupted."); } // We don't want to run `PRAGMA key = ...;` // on main web database. The context is here: // https://linear.app/comm/issue/ENG-6398/issues-with-sqlcipher-on-web #ifdef EMSCRIPTEN std::string plaintextBackupPath = mainCompactionPath + "_plaintext"; if (file_exists(plaintextBackupPath)) { attempt_delete_file( plaintextBackupPath, "Failed to delete plaintext backup file from previous backup attempt."); } std::string plaintextMigrationDBQuery = "PRAGMA key = \"x'" + mainCompactionEncryptionKey + "'\";" "ATTACH DATABASE '" + plaintextBackupPath + "' AS plaintext KEY '';" "SELECT sqlcipher_export('plaintext');" "DETACH DATABASE plaintext;"; sqlite3_open(mainCompactionPath.c_str(), &backupDB); char *plaintextMigrationErr; sqlite3_exec( backupDB, plaintextMigrationDBQuery.c_str(), nullptr, nullptr, &plaintextMigrationErr); sqlite3_close(backupDB); if (plaintextMigrationErr) { std::stringstream error_message; error_message << "Failed to migrate backup SQLCipher file to plaintext " "SQLite file. Details" << plaintextMigrationErr << std::endl; std::string error_message_str = error_message.str(); sqlite3_free(plaintextMigrationErr); throw std::runtime_error(error_message_str); } sqlite3_open(plaintextBackupPath.c_str(), &backupDB); #else sqlite3_open(mainCompactionPath.c_str(), &backupDB); set_encryption_key(backupDB, mainCompactionEncryptionKey); #endif sqlite3_backup *backupObj = sqlite3_backup_init( SQLiteQueryExecutor::getConnection(), "main", backupDB, "main"); if (!backupObj) { std::stringstream error_message; error_message << "Failed to init backup for main compaction. Details: " << sqlite3_errmsg(SQLiteQueryExecutor::getConnection()) << std::endl; sqlite3_close(backupDB); throw std::runtime_error(error_message.str()); } int backupResult = sqlite3_backup_step(backupObj, -1); sqlite3_backup_finish(backupObj); sqlite3_close(backupDB); if (backupResult == SQLITE_BUSY || backupResult == SQLITE_LOCKED) { throw std::runtime_error( "Programmer error. Database in transaction during restore attempt."); } else if (backupResult != SQLITE_DONE) { std::stringstream error_message; error_message << "Failed to restore database from backup. Details: " << sqlite3_errstr(backupResult); throw std::runtime_error(error_message.str()); } #ifdef EMSCRIPTEN attempt_delete_file( plaintextBackupPath, "Failed to delete plaintext compaction file after successful restore."); #endif attempt_delete_file( mainCompactionPath, "Failed to delete main compaction file after successful restore."); } void SQLiteQueryExecutor::restoreFromBackupLog( const std::vector &backupLog) const { SQLiteQueryExecutor::connectionManager.restoreFromBackupLog(backupLog); } } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h index d04d865e8..5ffa414bc 100644 --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h @@ -1,137 +1,142 @@ #pragma once #include "../CryptoTools/Persist.h" #include "DatabaseQueryExecutor.h" #include "NativeSQLiteConnectionManager.h" #include "entities/CommunityInfo.h" #include "entities/Draft.h" #include "entities/KeyserverInfo.h" #include "entities/UserInfo.h" #include #include namespace comm { class SQLiteQueryExecutor : public DatabaseQueryExecutor { static void migrate(); static sqlite3 *getConnection(); static void closeConnection(); static std::once_flag initialized; static int sqlcipherEncryptionKeySize; static std::string secureStoreEncryptionKeyID; static int backupLogsEncryptionKeySize; static std::string secureStoreBackupLogsEncryptionKeyID; static std::string backupLogsEncryptionKey; #ifndef EMSCRIPTEN static NativeSQLiteConnectionManager connectionManager; static void generateFreshEncryptionKey(); static void generateFreshBackupLogsEncryptionKey(); #else static SQLiteConnectionManager connectionManager; #endif public: static std::string sqliteFilePath; static std::string encryptionKey; SQLiteQueryExecutor(); ~SQLiteQueryExecutor(); SQLiteQueryExecutor(std::string sqliteFilePath); std::unique_ptr getThread(std::string threadID) const override; std::string getDraft(std::string key) const override; void updateDraft(std::string key, std::string text) const override; 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; void removeMessages(const std::vector &ids) const override; void removeMessagesForThreads( const std::vector &threadIDs) const override; void replaceMessage(const Message &message) const override; void rekeyMessage(std::string from, std::string to) const override; void replaceMessageStoreThreads( const std::vector &threads) const override; void removeMessageStoreThreads(const std::vector &ids) const override; void removeAllMessageStoreThreads() const override; std::vector getAllMessageStoreThreads() const override; void removeAllMedia() const override; void removeMediaForMessages( const std::vector &msg_ids) const override; void removeMediaForMessage(std::string msg_id) const override; void removeMediaForThreads( const std::vector &thread_ids) const override; void replaceMedia(const Media &media) const override; void rekeyMediaContainers(std::string from, std::string to) const override; std::vector getAllThreads() const override; void removeThreads(std::vector ids) const override; void replaceThread(const Thread &thread) const override; void removeAllThreads() const override; void replaceReport(const Report &report) const override; void removeReports(const std::vector &ids) const override; void removeAllReports() const override; std::vector getAllReports() const override; void setPersistStorageItem(std::string key, std::string item) const override; void removePersistStorageItem(std::string key) const override; std::string getPersistStorageItem(std::string key) const override; void replaceUser(const UserInfo &user_info) const override; void removeUsers(const std::vector &ids) const override; void removeAllUsers() const override; std::vector getAllUsers() const override; void replaceKeyserver(const KeyserverInfo &keyserver_info) const override; void removeKeyservers(const std::vector &ids) const override; void removeAllKeyservers() const override; std::vector getAllKeyservers() const override; void replaceCommunity(const CommunityInfo &community_info) const override; void removeCommunities(const std::vector &ids) const override; void removeAllCommunities() const override; std::vector getAllCommunities() const override; void beginTransaction() const override; void commitTransaction() const override; void rollbackTransaction() const override; + int getContentAccountID() const override; + int getNotifsAccountID() const override; std::vector getOlmPersistSessionsData() const override; - std::optional getOlmPersistAccountData() const override; + std::optional + getOlmPersistAccountData(int accountID) const override; void storeOlmPersistSession(const OlmPersistSession &session) const override; - void storeOlmPersistAccount(const std::string &accountData) const override; - void storeOlmPersistData(crypto::Persist persist) const override; + void storeOlmPersistAccount(int accountID, const std::string &accountData) + const override; + void + storeOlmPersistData(int accountID, crypto::Persist persist) const override; void setNotifyToken(std::string token) const override; void clearNotifyToken() const override; void setCurrentUserID(std::string userID) const override; std::string getCurrentUserID() const override; void setMetadata(std::string entry_name, std::string data) const override; void clearMetadata(std::string entry_name) const override; std::string getMetadata(std::string entry_name) const override; void restoreFromMainCompaction( std::string mainCompactionPath, std::string mainCompactionEncryptionKey) const override; void restoreFromBackupLog( const std::vector &backupLog) const override; void addMessagesToDevice( const std::vector &messages) const override; std::vector getAllMessagesToDevice(const std::string &deviceID) const override; void removeMessagesToDeviceOlderThan( const ClientMessageToDevice &lastConfirmedMessage) const override; void removeAllMessagesForDevice(const std::string &deviceID) const override; #ifdef EMSCRIPTEN std::vector getAllThreadsWeb() const override; void replaceThreadWeb(const WebThread &thread) const override; std::vector getAllMessagesWeb() const override; void replaceMessageWeb(const WebMessage &message) const override; - NullableString getOlmPersistAccountDataWeb() const override; + NullableString getOlmPersistAccountDataWeb(int accountID) const override; #else static void clearSensitiveData(); static void initialize(std::string &databasePath); void createMainCompaction(std::string backupID) const override; void captureBackupLogs() const override; #endif }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/entities/EntityQueryHelpers.h b/native/cpp/CommonCpp/DatabaseManagers/entities/EntityQueryHelpers.h index 7228fc22c..94016202a 100644 --- a/native/cpp/CommonCpp/DatabaseManagers/entities/EntityQueryHelpers.h +++ b/native/cpp/CommonCpp/DatabaseManagers/entities/EntityQueryHelpers.h @@ -1,140 +1,163 @@ #pragma once #include "SQLiteDataConverters.h" #include "SQLiteStatementWrapper.h" #include #include #include namespace comm { template std::vector getAllEntities(sqlite3 *db, std::string getAllEntitiesSQL) { SQLiteStatementWrapper preparedSQL( db, getAllEntitiesSQL, "Failed to retrieve entities."); std::vector allEntities; for (int stepResult = sqlite3_step(preparedSQL); stepResult == SQLITE_ROW; stepResult = sqlite3_step(preparedSQL)) { allEntities.emplace_back(T::fromSQLResult(preparedSQL, 0)); } return allEntities; } +template +std::unique_ptr +getEntityByPrimaryKeyCommon(SQLiteStatementWrapper &preparedSQL) { + int stepResult = sqlite3_step(preparedSQL); + if (stepResult == SQLITE_DONE) { + return nullptr; + } + + if (stepResult != SQLITE_ROW) { + std::stringstream error_message; + error_message << "Failed to fetch row by primary key. Details: " + << sqlite3_errstr(stepResult) << std::endl; + throw std::runtime_error(error_message.str()); + } + + T entity = T::fromSQLResult(preparedSQL, 0); + return std::make_unique(std::move(entity)); +} + template std::unique_ptr getEntityByPrimaryKey( sqlite3 *db, std::string getEntityByPrimaryKeySQL, std::string primaryKey) { SQLiteStatementWrapper preparedSQL( db, getEntityByPrimaryKeySQL, "Failed to fetch row by primary key."); int bindResult = bindStringToSQL(primaryKey, preparedSQL, 1); if (bindResult != SQLITE_OK) { std::stringstream error_message; error_message << "Failed to bind primary key to SQL statement. Details: " << sqlite3_errstr(bindResult) << std::endl; throw std::runtime_error(error_message.str()); } - int stepResult = sqlite3_step(preparedSQL); - if (stepResult == SQLITE_DONE) { - return nullptr; - } + return getEntityByPrimaryKeyCommon(preparedSQL); +} - if (stepResult != SQLITE_ROW) { +template +std::unique_ptr getEntityByIntegerPrimaryKey( + sqlite3 *db, + std::string getEntityByPrimaryKeySQL, + int primaryKey) { + SQLiteStatementWrapper preparedSQL( + db, getEntityByPrimaryKeySQL, "Failed to fetch row by primary key."); + int bindResult = bindIntToSQL(primaryKey, preparedSQL, 1); + if (bindResult != SQLITE_OK) { std::stringstream error_message; - error_message << "Failed to fetch row by primary key. Details: " - << sqlite3_errstr(stepResult) << std::endl; + error_message << "Failed to bind primary key to SQL statement. Details: " + << sqlite3_errstr(bindResult) << std::endl; throw std::runtime_error(error_message.str()); } - - T entity = T::fromSQLResult(preparedSQL, 0); - return std::make_unique(std::move(entity)); + return getEntityByPrimaryKeyCommon(preparedSQL); } template void replaceEntity(sqlite3 *db, std::string replaceEntitySQL, const T &entity) { SQLiteStatementWrapper preparedSQL( db, replaceEntitySQL, "Failed to replace entity."); // "REPLACE INTO ..." query is assumed since // it was used by orm previously int bindResult = entity.bindToSQL(preparedSQL, 1); if (bindResult != SQLITE_OK) { std::stringstream error_message; error_message << "Failed to bind entity to SQL statement. Details: " << sqlite3_errstr(bindResult) << std::endl; throw std::runtime_error(error_message.str()); } sqlite3_step(preparedSQL); } void removeAllEntities(sqlite3 *db, std::string removeAllEntitiesSQL) { SQLiteStatementWrapper preparedSQL( db, removeAllEntitiesSQL, "Failed to remove all entities."); sqlite3_step(preparedSQL); } void removeEntitiesByKeys( sqlite3 *db, std::string removeEntitiesByKeysSQL, const std::vector &keys) { SQLiteStatementWrapper preparedSQL( db, removeEntitiesByKeysSQL, "Failed to remove entities by keys."); for (int i = 0; i < keys.size(); i++) { int bindResult = bindStringToSQL(keys[i], preparedSQL, i + 1); if (bindResult != SQLITE_OK) { std::stringstream error_message; error_message << "Failed to bind key to SQL statement. Details: " << sqlite3_errstr(bindResult) << std::endl; sqlite3_finalize(preparedSQL); throw std::runtime_error(error_message.str()); } } sqlite3_step(preparedSQL); } void rekeyAllEntities( sqlite3 *db, std::string rekeyAllEntitiesSQL, std::string from, std::string to) { SQLiteStatementWrapper preparedSQL( db, rekeyAllEntitiesSQL, "Failed to rekey all entities."); bindStringToSQL(to, preparedSQL, 1); int bindResult = bindStringToSQL(from, preparedSQL, 2); if (bindResult != SQLITE_OK) { std::stringstream error_message; error_message << "Failed to bind key to SQL statement. Details: " << sqlite3_errstr(bindResult) << std::endl; throw std::runtime_error(error_message.str()); } sqlite3_step(preparedSQL); } std::string getSQLStatementArray(int length) { std::stringstream array; array << "("; for (int i = 0; i < length - 1; i++) { array << "?, "; } array << "?)"; return array.str(); } void executeQuery(sqlite3 *db, std::string querySQL) { char *err; sqlite3_exec(db, querySQL.c_str(), nullptr, nullptr, &err); if (err) { std::stringstream error_message; error_message << "Failed to execute query. Details: " << err << std::endl; sqlite3_free(err); throw std::runtime_error(error_message.str()); } } } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp index f2d833f0d..95096ca5d 100644 --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp @@ -1,1633 +1,1638 @@ #include "CommCoreModule.h" #include "../Notifications/BackgroundDataStorage/NotificationsCryptoModule.h" #include "BaseDataStore.h" #include "CommServicesAuthMetadataEmitter.h" #include "DatabaseManager.h" #include "InternalModules/GlobalDBSingleton.h" #include "InternalModules/RustPromiseManager.h" #include "NativeModuleUtils.h" #include "TerminateApp.h" #include #include #include #include #include "JSIRust.h" #include "lib.rs.h" #include namespace comm { using namespace facebook::react; jsi::Value CommCoreModule::getDraft(jsi::Runtime &rt, jsi::String key) { std::string keyStr = key.utf8(rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::string draftStr; try { draftStr = DatabaseManager::getQueryExecutor().getDraft(keyStr); } catch (std::system_error &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } jsi::String draft = jsi::String::createFromUtf8(innerRt, draftStr); promise->resolve(std::move(draft)); }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::updateDraft( jsi::Runtime &rt, jsi::String key, jsi::String text) { std::string keyStr = key.utf8(rt); std::string textStr = text.utf8(rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=]() { std::string error; try { DatabaseManager::getQueryExecutor().updateDraft(keyStr, textStr); } catch (std::system_error &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=]() { if (error.size()) { promise->reject(error); } else { promise->resolve(true); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::moveDraft( jsi::Runtime &rt, jsi::String oldKey, jsi::String newKey) { std::string oldKeyStr = oldKey.utf8(rt); std::string newKeyStr = newKey.utf8(rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=]() { std::string error; bool result = false; try { result = DatabaseManager::getQueryExecutor().moveDraft( oldKeyStr, newKeyStr); } catch (std::system_error &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=]() { if (error.size()) { promise->reject(error); } else { promise->resolve(result); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::getClientDBStore(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::vector draftsVector; std::vector threadsVector; std::vector>> messagesVector; std::vector messageStoreThreadsVector; std::vector reportStoreVector; std::vector userStoreVector; std::vector keyserverStoreVector; std::vector communityStoreVector; try { draftsVector = DatabaseManager::getQueryExecutor().getAllDrafts(); messagesVector = DatabaseManager::getQueryExecutor().getAllMessages(); threadsVector = DatabaseManager::getQueryExecutor().getAllThreads(); messageStoreThreadsVector = DatabaseManager::getQueryExecutor().getAllMessageStoreThreads(); reportStoreVector = DatabaseManager::getQueryExecutor().getAllReports(); userStoreVector = DatabaseManager::getQueryExecutor().getAllUsers(); keyserverStoreVector = DatabaseManager::getQueryExecutor().getAllKeyservers(); communityStoreVector = DatabaseManager::getQueryExecutor().getAllCommunities(); } catch (std::system_error &e) { error = e.what(); } auto draftsVectorPtr = std::make_shared>(std::move(draftsVector)); auto messagesVectorPtr = std::make_shared< std::vector>>>( std::move(messagesVector)); auto threadsVectorPtr = std::make_shared>(std::move(threadsVector)); auto messageStoreThreadsVectorPtr = std::make_shared>( std::move(messageStoreThreadsVector)); auto reportStoreVectorPtr = std::make_shared>( std::move(reportStoreVector)); auto userStoreVectorPtr = std::make_shared>( std::move(userStoreVector)); auto keyserveStoreVectorPtr = std::make_shared>( std::move(keyserverStoreVector)); auto communityStoreVectorPtr = std::make_shared>( std::move(communityStoreVector)); this->jsInvoker_->invokeAsync([&innerRt, draftsVectorPtr, messagesVectorPtr, threadsVectorPtr, messageStoreThreadsVectorPtr, reportStoreVectorPtr, userStoreVectorPtr, keyserveStoreVectorPtr, communityStoreVectorPtr, error, promise, draftStore = this->draftStore, threadStore = this->threadStore, messageStore = this->messageStore, reportStore = this->reportStore, userStore = this->userStore, keyserverStore = this->keyserverStore, communityStore = this->communityStore]() { if (error.size()) { promise->reject(error); return; } jsi::Array jsiDrafts = draftStore.parseDBDataStore(innerRt, draftsVectorPtr); jsi::Array jsiMessages = messageStore.parseDBDataStore(innerRt, messagesVectorPtr); jsi::Array jsiThreads = threadStore.parseDBDataStore(innerRt, threadsVectorPtr); jsi::Array jsiMessageStoreThreads = messageStore.parseDBMessageStoreThreads( innerRt, messageStoreThreadsVectorPtr); jsi::Array jsiReportStore = reportStore.parseDBDataStore(innerRt, reportStoreVectorPtr); jsi::Array jsiUserStore = userStore.parseDBDataStore(innerRt, userStoreVectorPtr); jsi::Array jsiKeyserverStore = keyserverStore.parseDBDataStore( innerRt, keyserveStoreVectorPtr); jsi::Array jsiCommunityStore = communityStore.parseDBDataStore( innerRt, communityStoreVectorPtr); auto jsiClientDBStore = jsi::Object(innerRt); jsiClientDBStore.setProperty(innerRt, "messages", jsiMessages); jsiClientDBStore.setProperty(innerRt, "threads", jsiThreads); jsiClientDBStore.setProperty(innerRt, "drafts", jsiDrafts); jsiClientDBStore.setProperty( innerRt, "messageStoreThreads", jsiMessageStoreThreads); jsiClientDBStore.setProperty(innerRt, "reports", jsiReportStore); jsiClientDBStore.setProperty(innerRt, "users", jsiUserStore); jsiClientDBStore.setProperty( innerRt, "keyservers", jsiKeyserverStore); jsiClientDBStore.setProperty( innerRt, "communities", jsiCommunityStore); promise->resolve(std::move(jsiClientDBStore)); }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::removeAllDrafts(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=]() { std::string error; try { DatabaseManager::getQueryExecutor().removeAllDrafts(); } catch (std::system_error &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=]() { if (error.size()) { promise->reject(error); return; } promise->resolve(jsi::Value::undefined()); }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Array CommCoreModule::getAllMessagesSync(jsi::Runtime &rt) { auto messagesVector = NativeModuleUtils::runSyncOrThrowJSError< std::vector>>>(rt, []() { return DatabaseManager::getQueryExecutor().getAllMessages(); }); auto messagesVectorPtr = std::make_shared>>>( std::move(messagesVector)); jsi::Array jsiMessages = this->messageStore.parseDBDataStore(rt, messagesVectorPtr); return jsiMessages; } jsi::Value CommCoreModule::processDraftStoreOperations( jsi::Runtime &rt, jsi::Array operations) { return this->draftStore.processStoreOperations(rt, std::move(operations)); } jsi::Value CommCoreModule::processMessageStoreOperations( jsi::Runtime &rt, jsi::Array operations) { return this->messageStore.processStoreOperations(rt, std::move(operations)); } void CommCoreModule::processMessageStoreOperationsSync( jsi::Runtime &rt, jsi::Array operations) { return this->messageStore.processStoreOperationsSync( rt, std::move(operations)); } jsi::Array CommCoreModule::getAllThreadsSync(jsi::Runtime &rt) { auto threadsVector = NativeModuleUtils::runSyncOrThrowJSError>(rt, []() { return DatabaseManager::getQueryExecutor().getAllThreads(); }); auto threadsVectorPtr = std::make_shared>(std::move(threadsVector)); jsi::Array jsiThreads = this->threadStore.parseDBDataStore(rt, threadsVectorPtr); return jsiThreads; } jsi::Value CommCoreModule::processThreadStoreOperations( jsi::Runtime &rt, jsi::Array operations) { return this->threadStore.processStoreOperations(rt, std::move(operations)); } void CommCoreModule::processThreadStoreOperationsSync( jsi::Runtime &rt, jsi::Array operations) { this->threadStore.processStoreOperationsSync(rt, std::move(operations)); } jsi::Value CommCoreModule::processReportStoreOperations( jsi::Runtime &rt, jsi::Array operations) { return this->reportStore.processStoreOperations(rt, std::move(operations)); } void CommCoreModule::processReportStoreOperationsSync( jsi::Runtime &rt, jsi::Array operations) { this->reportStore.processStoreOperationsSync(rt, std::move(operations)); } jsi::Value CommCoreModule::processUserStoreOperations( jsi::Runtime &rt, jsi::Array operations) { return this->userStore.processStoreOperations(rt, std::move(operations)); } jsi::Value CommCoreModule::processKeyserverStoreOperations( jsi::Runtime &rt, jsi::Array operations) { return this->keyserverStore.processStoreOperations(rt, std::move(operations)); } jsi::Value CommCoreModule::processCommunityStoreOperations( jsi::Runtime &rt, jsi::Array operations) { return this->communityStore.processStoreOperations(rt, std::move(operations)); } void CommCoreModule::terminate(jsi::Runtime &rt) { TerminateApp::terminate(); } void CommCoreModule::persistCryptoModule() { folly::Optional storedSecretKey = CommSecureStore::get(this->secureStoreAccountDataKey); if (!storedSecretKey.hasValue()) { storedSecretKey = crypto::Tools::generateRandomString(64); CommSecureStore::set( this->secureStoreAccountDataKey, storedSecretKey.value()); } crypto::Persist newPersist = this->cryptoModule->storeAsB64(storedSecretKey.value()); std::promise persistencePromise; std::future persistenceFuture = persistencePromise.get_future(); GlobalDBSingleton::instance.scheduleOrRunCancellable( [=, &persistencePromise]() { try { - DatabaseManager::getQueryExecutor().storeOlmPersistData(newPersist); + DatabaseManager::getQueryExecutor().storeOlmPersistData( + DatabaseManager::getQueryExecutor().getContentAccountID(), + newPersist); persistencePromise.set_value(); } catch (std::system_error &e) { persistencePromise.set_exception(std::make_exception_ptr(e)); } }); persistenceFuture.get(); } jsi::Value CommCoreModule::initializeCryptoAccount(jsi::Runtime &rt) { folly::Optional storedSecretKey = CommSecureStore::get(this->secureStoreAccountDataKey); if (!storedSecretKey.hasValue()) { storedSecretKey = crypto::Tools::generateRandomString(64); CommSecureStore::set( this->secureStoreAccountDataKey, storedSecretKey.value()); } return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=]() { crypto::Persist persist; std::string error; try { std::optional accountData = - DatabaseManager::getQueryExecutor().getOlmPersistAccountData(); + DatabaseManager::getQueryExecutor().getOlmPersistAccountData( + DatabaseManager::getQueryExecutor().getContentAccountID()); if (accountData.has_value()) { persist.account = crypto::OlmBuffer(accountData->begin(), accountData->end()); // handle sessions data std::vector sessionsData = DatabaseManager::getQueryExecutor() .getOlmPersistSessionsData(); for (OlmPersistSession &sessionsDataItem : sessionsData) { crypto::OlmBuffer sessionDataBuffer( sessionsDataItem.session_data.begin(), sessionsDataItem.session_data.end()); persist.sessions.insert(std::make_pair( sessionsDataItem.target_user_id, sessionDataBuffer)); } } } catch (std::system_error &e) { error = e.what(); } this->cryptoThread->scheduleTask([=]() { std::string error; this->cryptoModule.reset(new crypto::CryptoModule( this->publicCryptoAccountID, storedSecretKey.value(), persist)); if (persist.isEmpty()) { crypto::Persist newPersist = this->cryptoModule->storeAsB64(storedSecretKey.value()); GlobalDBSingleton::instance.scheduleOrRunCancellable( [=]() { std::string error; try { DatabaseManager::getQueryExecutor().storeOlmPersistData( + DatabaseManager::getQueryExecutor() + .getContentAccountID(), newPersist); } catch (std::system_error &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=]() { if (error.size()) { promise->reject(error); return; } }); }, promise, this->jsInvoker_); } else { this->cryptoModule->restoreFromB64( storedSecretKey.value(), persist); this->jsInvoker_->invokeAsync([=]() { if (error.size()) { promise->reject(error); return; } }); } try { NotificationsCryptoModule::initializeNotificationsCryptoAccount( "Comm"); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=]() { if (error.size()) { promise->reject(error); return; } promise->resolve(jsi::Value::undefined()); }); }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::getUserPublicKey(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::string primaryKeysResult; std::string notificationsKeysResult; if (this->cryptoModule == nullptr) { error = "user has not been initialized"; } else { primaryKeysResult = this->cryptoModule->getIdentityKeys(); } try { if (!error.size()) { notificationsKeysResult = NotificationsCryptoModule::getNotificationsIdentityKeys( "Comm"); } } catch (const std::exception &e) { error = e.what(); } std::string notificationsCurve25519Cpp, notificationsEd25519Cpp, blobPayloadCpp, signatureCpp, primaryCurve25519Cpp, primaryEd25519Cpp; if (!error.size()) { folly::dynamic parsedPrimary; try { parsedPrimary = folly::parseJson(primaryKeysResult); } catch (const folly::json::parse_error &e) { error = "parsing identity keys failed with: " + std::string(e.what()); } if (!error.size()) { primaryCurve25519Cpp = parsedPrimary["curve25519"].asString(); primaryEd25519Cpp = parsedPrimary["ed25519"].asString(); folly::dynamic parsedNotifications; try { parsedNotifications = folly::parseJson(notificationsKeysResult); } catch (const folly::json::parse_error &e) { error = "parsing notifications keys failed with: " + std::string(e.what()); } if (!error.size()) { notificationsCurve25519Cpp = parsedNotifications["curve25519"].asString(); notificationsEd25519Cpp = parsedNotifications["ed25519"].asString(); folly::dynamic blobPayloadJSON = folly::dynamic::object( "primaryIdentityPublicKeys", folly::dynamic::object("ed25519", primaryEd25519Cpp)( "curve25519", primaryCurve25519Cpp))( "notificationIdentityPublicKeys", folly::dynamic::object("ed25519", notificationsEd25519Cpp)( "curve25519", notificationsCurve25519Cpp)); blobPayloadCpp = folly::toJson(blobPayloadJSON); signatureCpp = this->cryptoModule->signMessage(blobPayloadCpp); } } } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } auto primaryCurve25519{ jsi::String::createFromUtf8(innerRt, primaryCurve25519Cpp)}; auto primaryEd25519{ jsi::String::createFromUtf8(innerRt, primaryEd25519Cpp)}; auto jsiPrimaryIdentityPublicKeys = jsi::Object(innerRt); jsiPrimaryIdentityPublicKeys.setProperty( innerRt, "ed25519", primaryEd25519); jsiPrimaryIdentityPublicKeys.setProperty( innerRt, "curve25519", primaryCurve25519); auto notificationsCurve25519{jsi::String::createFromUtf8( innerRt, notificationsCurve25519Cpp)}; auto notificationsEd25519{ jsi::String::createFromUtf8(innerRt, notificationsEd25519Cpp)}; auto jsiNotificationIdentityPublicKeys = jsi::Object(innerRt); jsiNotificationIdentityPublicKeys.setProperty( innerRt, "ed25519", notificationsEd25519); jsiNotificationIdentityPublicKeys.setProperty( innerRt, "curve25519", notificationsCurve25519); auto blobPayload{ jsi::String::createFromUtf8(innerRt, blobPayloadCpp)}; auto signature{jsi::String::createFromUtf8(innerRt, signatureCpp)}; auto jsiClientPublicKeys = jsi::Object(innerRt); jsiClientPublicKeys.setProperty( innerRt, "primaryIdentityPublicKeys", jsiPrimaryIdentityPublicKeys); jsiClientPublicKeys.setProperty( innerRt, "notificationIdentityPublicKeys", jsiNotificationIdentityPublicKeys); jsiClientPublicKeys.setProperty( innerRt, "blobPayload", blobPayload); jsiClientPublicKeys.setProperty(innerRt, "signature", signature); promise->resolve(std::move(jsiClientPublicKeys)); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Object parseOLMOneTimeKeys(jsi::Runtime &rt, std::string oneTimeKeysBlob) { folly::dynamic parsedOneTimeKeys = folly::parseJson(oneTimeKeysBlob); auto jsiOneTimeKeysInner = jsi::Object(rt); for (auto &kvPair : parsedOneTimeKeys["curve25519"].items()) { jsiOneTimeKeysInner.setProperty( rt, kvPair.first.asString().c_str(), jsi::String::createFromUtf8(rt, kvPair.second.asString())); } auto jsiOneTimeKeys = jsi::Object(rt); jsiOneTimeKeys.setProperty(rt, "curve25519", jsiOneTimeKeysInner); return jsiOneTimeKeys; } jsi::Object parseOneTimeKeysResult( jsi::Runtime &rt, std::string contentOneTimeKeysBlob, std::string notifOneTimeKeysBlob) { auto contentOneTimeKeys = parseOLMOneTimeKeys(rt, contentOneTimeKeysBlob); auto notifOneTimeKeys = parseOLMOneTimeKeys(rt, notifOneTimeKeysBlob); auto jsiOneTimeKeysResult = jsi::Object(rt); jsiOneTimeKeysResult.setProperty( rt, "contentOneTimeKeys", contentOneTimeKeys); jsiOneTimeKeysResult.setProperty( rt, "notificationsOneTimeKeys", notifOneTimeKeys); return jsiOneTimeKeysResult; } jsi::Value CommCoreModule::getOneTimeKeys(jsi::Runtime &rt, double oneTimeKeysAmount) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::string contentResult; std::string notifResult; if (this->cryptoModule == nullptr) { this->jsInvoker_->invokeAsync([=, &innerRt]() { promise->reject("user has not been initialized"); }); return; } try { contentResult = this->cryptoModule->getOneTimeKeysForPublishing( oneTimeKeysAmount); this->persistCryptoModule(); notifResult = NotificationsCryptoModule:: getNotificationsOneTimeKeysForPublishing( oneTimeKeysAmount, "Comm"); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } promise->resolve( parseOneTimeKeysResult(innerRt, contentResult, notifResult)); }); }; this->cryptoThread->scheduleTask(job); }); } std::pair getNotificationsPrekeyAndSignature() { // TODO: Implement notifs prekey rotation. // Notifications prekey is not rotated at this moment. It // is fetched with signature to match identity service API. std::string notificationsPrekey = NotificationsCryptoModule::getNotificationsPrekey("Comm"); std::string notificationsPrekeySignature = NotificationsCryptoModule::getNotificationsPrekeySignature("Comm"); return std::make_pair(notificationsPrekey, notificationsPrekeySignature); } jsi::Value CommCoreModule::validateAndUploadPrekeys( jsi::Runtime &rt, jsi::String authUserID, jsi::String authDeviceID, jsi::String authAccessToken) { auto authUserIDRust = jsiStringToRustString(authUserID, rt); auto authDeviceIDRust = jsiStringToRustString(authDeviceID, rt); auto authAccessTokenRust = jsiStringToRustString(authAccessToken, rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::optional maybePrekeyToUpload; if (this->cryptoModule == nullptr) { this->jsInvoker_->invokeAsync([=, &innerRt]() { promise->reject("user has not been initialized"); }); return; } try { maybePrekeyToUpload = this->cryptoModule->validatePrekey(); this->persistCryptoModule(); if (!maybePrekeyToUpload.has_value()) { maybePrekeyToUpload = this->cryptoModule->getUnpublishedPrekey(); } } catch (const std::exception &e) { error = e.what(); } if (error.size()) { this->jsInvoker_->invokeAsync( [=, &innerRt]() { promise->reject(error); }); return; } else if (!maybePrekeyToUpload.has_value()) { this->jsInvoker_->invokeAsync( [=]() { promise->resolve(jsi::Value::undefined()); }); return; } std::string prekeyToUpload = maybePrekeyToUpload.value(); std::string prekeyUploadError; try { std::string prekeySignature = this->cryptoModule->getPrekeySignature(); std::string notificationsPrekey, notificationsPrekeySignature; std::tie(notificationsPrekey, notificationsPrekeySignature) = getNotificationsPrekeyAndSignature(); try { std::promise prekeyPromise; std::future prekeyFuture = prekeyPromise.get_future(); RustPromiseManager::CPPPromiseInfo promiseInfo = { std::move(prekeyPromise)}; auto currentID = RustPromiseManager::instance.addPromise( std::move(promiseInfo)); ::identityRefreshUserPrekeys( authUserIDRust, authDeviceIDRust, authAccessTokenRust, rust::string(prekeyToUpload), rust::string(prekeySignature), rust::string(notificationsPrekey), rust::string(notificationsPrekeySignature), currentID); prekeyFuture.get(); } catch (const std::exception &e) { prekeyUploadError = e.what(); } if (!prekeyUploadError.size()) { this->cryptoModule->markPrekeyAsPublished(); this->persistCryptoModule(); } } catch (std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=]() { if (error.size()) { promise->reject(error); return; } if (prekeyUploadError.size()) { promise->reject(prekeyUploadError); return; } promise->resolve(jsi::Value::undefined()); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::validateAndGetPrekeys(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::string contentPrekeySignature, notifPrekey, notifPrekeySignature; std::optional contentPrekey; if (this->cryptoModule == nullptr) { this->jsInvoker_->invokeAsync([=, &innerRt]() { promise->reject("user has not been initialized"); }); return; } try { contentPrekey = this->cryptoModule->validatePrekey(); if (!contentPrekey) { contentPrekey = this->cryptoModule->getUnpublishedPrekey(); } if (!contentPrekey) { contentPrekey = this->cryptoModule->getPrekey(); } this->persistCryptoModule(); contentPrekeySignature = this->cryptoModule->getPrekeySignature(); std::tie(notifPrekey, notifPrekeySignature) = getNotificationsPrekeyAndSignature(); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } auto contentPrekeyJSI = jsi::String::createFromUtf8(innerRt, contentPrekey.value()); auto contentPrekeySignatureJSI = jsi::String::createFromUtf8(innerRt, contentPrekeySignature); auto notifPrekeyJSI = jsi::String::createFromUtf8(innerRt, notifPrekey); auto notifPrekeySignatureJSI = jsi::String::createFromUtf8(innerRt, notifPrekeySignature); auto signedPrekeysJSI = jsi::Object(innerRt); signedPrekeysJSI.setProperty( innerRt, "contentPrekey", contentPrekeyJSI); signedPrekeysJSI.setProperty( innerRt, "contentPrekeySignature", contentPrekeySignatureJSI); signedPrekeysJSI.setProperty( innerRt, "notifPrekey", notifPrekeyJSI); signedPrekeysJSI.setProperty( innerRt, "notifPrekeySignature", notifPrekeySignatureJSI); promise->resolve(std::move(signedPrekeysJSI)); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::initializeNotificationsSession( jsi::Runtime &rt, jsi::String identityKeys, jsi::String prekey, jsi::String prekeySignature, jsi::String oneTimeKey, jsi::String keyserverID) { auto identityKeysCpp{identityKeys.utf8(rt)}; auto prekeyCpp{prekey.utf8(rt)}; auto prekeySignatureCpp{prekeySignature.utf8(rt)}; auto oneTimeKeyCpp{oneTimeKey.utf8(rt)}; return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; crypto::EncryptedData result; try { result = NotificationsCryptoModule::initializeNotificationsSession( identityKeysCpp, prekeyCpp, prekeySignatureCpp, oneTimeKeyCpp, "Comm"); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } promise->resolve(jsi::String::createFromUtf8( innerRt, std::string{result.message.begin(), result.message.end()})); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::isNotificationsSessionInitialized(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; bool result; try { result = NotificationsCryptoModule::isNotificationsSessionInitialized( "Comm"); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } promise->resolve(result); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::updateKeyserverDataInNotifStorage( jsi::Runtime &rt, jsi::Array keyserversData) { std::vector> keyserversDataCpp; for (auto idx = 0; idx < keyserversData.size(rt); idx++) { auto data = keyserversData.getValueAtIndex(rt, idx).asObject(rt); std::string keyserverID = data.getProperty(rt, "id").asString(rt).utf8(rt); std::string keyserverUnreadCountKey = "KEYSERVER." + keyserverID + ".UNREAD_COUNT"; int unreadCount = data.getProperty(rt, "unreadCount").asNumber(); keyserversDataCpp.push_back({keyserverUnreadCountKey, unreadCount}); } return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { std::string error; try { for (const auto &keyserverData : keyserversDataCpp) { CommMMKV::setInt(keyserverData.first, keyserverData.second); } } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } promise->resolve(jsi::Value::undefined()); }); }); } jsi::Value CommCoreModule::removeKeyserverDataFromNotifStorage( jsi::Runtime &rt, jsi::Array keyserverIDsToDelete) { std::vector keyserverIDsToDeleteCpp{}; for (auto idx = 0; idx < keyserverIDsToDelete.size(rt); idx++) { std::string keyserverID = keyserverIDsToDelete.getValueAtIndex(rt, idx).asString(rt).utf8(rt); std::string keyserverUnreadCountKey = "KEYSERVER." + keyserverID + ".UNREAD_COUNT"; keyserverIDsToDeleteCpp.push_back(keyserverUnreadCountKey); } return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { std::string error; try { CommMMKV::removeKeys(keyserverIDsToDeleteCpp); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } promise->resolve(jsi::Value::undefined()); }); }); } jsi::Value CommCoreModule::getKeyserverDataFromNotifStorage( jsi::Runtime &rt, jsi::Array keyserverIDs) { std::vector keyserverIDsCpp{}; for (auto idx = 0; idx < keyserverIDs.size(rt); idx++) { std::string keyserverID = keyserverIDs.getValueAtIndex(rt, idx).asString(rt).utf8(rt); keyserverIDsCpp.push_back(keyserverID); } return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { std::string error; std::vector> keyserversDataVector{}; try { for (const auto &keyserverID : keyserverIDsCpp) { std::string keyserverUnreadCountKey = "KEYSERVER." + keyserverID + ".UNREAD_COUNT"; std::optional unreadCount = CommMMKV::getInt(keyserverUnreadCountKey, -1); if (!unreadCount.has_value()) { continue; } keyserversDataVector.push_back({keyserverID, unreadCount.value()}); } } catch (const std::exception &e) { error = e.what(); } auto keyserversDataVectorPtr = std::make_shared>>( std::move(keyserversDataVector)); this->jsInvoker_->invokeAsync( [&innerRt, keyserversDataVectorPtr, error, promise]() { if (error.size()) { promise->reject(error); return; } size_t numKeyserversData = keyserversDataVectorPtr->size(); jsi::Array jsiKeyserversData = jsi::Array(innerRt, numKeyserversData); size_t writeIdx = 0; for (const auto &keyserverData : *keyserversDataVectorPtr) { jsi::Object jsiKeyserverData = jsi::Object(innerRt); jsiKeyserverData.setProperty( innerRt, "id", keyserverData.first); jsiKeyserverData.setProperty( innerRt, "unreadCount", keyserverData.second); jsiKeyserversData.setValueAtIndex( innerRt, writeIdx++, jsiKeyserverData); } promise->resolve(std::move(jsiKeyserversData)); }); }); } jsi::Value CommCoreModule::initializeContentOutboundSession( jsi::Runtime &rt, jsi::String identityKeys, jsi::String prekey, jsi::String prekeySignature, jsi::String oneTimeKey, jsi::String deviceID) { auto identityKeysCpp{identityKeys.utf8(rt)}; auto prekeyCpp{prekey.utf8(rt)}; auto prekeySignatureCpp{prekeySignature.utf8(rt)}; auto oneTimeKeyCpp{oneTimeKey.utf8(rt)}; auto deviceIDCpp{deviceID.utf8(rt)}; return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; crypto::EncryptedData initialEncryptedMessage; try { this->cryptoModule->initializeOutboundForSendingSession( deviceIDCpp, std::vector( identityKeysCpp.begin(), identityKeysCpp.end()), std::vector(prekeyCpp.begin(), prekeyCpp.end()), std::vector( prekeySignatureCpp.begin(), prekeySignatureCpp.end()), std::vector( oneTimeKeyCpp.begin(), oneTimeKeyCpp.end())); const std::string initMessage = "{\"type\": \"init\"}"; initialEncryptedMessage = cryptoModule->encrypt(deviceIDCpp, initMessage); this->persistCryptoModule(); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } promise->resolve(jsi::String::createFromUtf8( innerRt, std::string{ initialEncryptedMessage.message.begin(), initialEncryptedMessage.message.end()})); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::initializeContentInboundSession( jsi::Runtime &rt, jsi::String identityKeys, jsi::String encryptedMessage, jsi::String deviceID) { auto identityKeysCpp{identityKeys.utf8(rt)}; auto encryptedMessageCpp{encryptedMessage.utf8(rt)}; auto deviceIDCpp{deviceID.utf8(rt)}; return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::string decryptedMessage; try { this->cryptoModule->initializeInboundForReceivingSession( deviceIDCpp, std::vector( encryptedMessageCpp.begin(), encryptedMessageCpp.end()), std::vector( identityKeysCpp.begin(), identityKeysCpp.end())); crypto::EncryptedData encryptedData{std::vector( encryptedMessageCpp.begin(), encryptedMessageCpp.end())}; decryptedMessage = cryptoModule->decrypt(deviceIDCpp, encryptedData); this->persistCryptoModule(); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } promise->resolve( jsi::String::createFromUtf8(innerRt, decryptedMessage)); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::encrypt( jsi::Runtime &rt, jsi::String message, jsi::String deviceID) { auto messageCpp{message.utf8(rt)}; auto deviceIDCpp{deviceID.utf8(rt)}; return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; crypto::EncryptedData encryptedMessage; try { encryptedMessage = cryptoModule->encrypt(deviceIDCpp, messageCpp); this->persistCryptoModule(); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } promise->resolve(jsi::String::createFromUtf8( innerRt, std::string{ encryptedMessage.message.begin(), encryptedMessage.message.end()})); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::decrypt( jsi::Runtime &rt, jsi::String message, jsi::String deviceID) { auto messageCpp{message.utf8(rt)}; auto deviceIDCpp{deviceID.utf8(rt)}; return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::string decryptedMessage; try { crypto::EncryptedData encryptedData{ std::vector(messageCpp.begin(), messageCpp.end()), ENCRYPTED_MESSAGE_TYPE}; decryptedMessage = cryptoModule->decrypt(deviceIDCpp, encryptedData); this->persistCryptoModule(); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } promise->resolve( jsi::String::createFromUtf8(innerRt, decryptedMessage)); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::signMessage(jsi::Runtime &rt, jsi::String message) { std::string messageStr = message.utf8(rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::string signature; try { signature = this->cryptoModule->signMessage(messageStr); } catch (const std::exception &e) { error = "signing message failed with: " + std::string(e.what()); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } auto jsiSignature{jsi::String::createFromUtf8(innerRt, signature)}; promise->resolve(std::move(jsiSignature)); }); }; this->cryptoThread->scheduleTask(job); }); } CommCoreModule::CommCoreModule( std::shared_ptr jsInvoker) : facebook::react::CommCoreModuleSchemaCxxSpecJSI(jsInvoker), cryptoThread(std::make_unique("crypto")), draftStore(jsInvoker), threadStore(jsInvoker), messageStore(jsInvoker), reportStore(jsInvoker), userStore(jsInvoker), keyserverStore(jsInvoker), communityStore(jsInvoker) { GlobalDBSingleton::instance.enableMultithreading(); } double CommCoreModule::getCodeVersion(jsi::Runtime &rt) { return this->codeVersion; } jsi::Value CommCoreModule::setNotifyToken(jsi::Runtime &rt, jsi::String token) { auto notifyToken{token.utf8(rt)}; return createPromiseAsJSIValue( rt, [this, notifyToken](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [this, notifyToken, promise]() { std::string error; try { DatabaseManager::getQueryExecutor().setNotifyToken(notifyToken); } catch (std::system_error &e) { error = e.what(); } this->jsInvoker_->invokeAsync([error, promise]() { if (error.size()) { promise->reject(error); } else { promise->resolve(jsi::Value::undefined()); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::clearNotifyToken(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [this, promise]() { std::string error; try { DatabaseManager::getQueryExecutor().clearNotifyToken(); } catch (std::system_error &e) { error = e.what(); } this->jsInvoker_->invokeAsync([error, promise]() { if (error.size()) { promise->reject(error); } else { promise->resolve(jsi::Value::undefined()); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); }; jsi::Value CommCoreModule::setCurrentUserID(jsi::Runtime &rt, jsi::String userID) { auto currentUserID{userID.utf8(rt)}; return createPromiseAsJSIValue( rt, [this, currentUserID](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [this, promise, currentUserID]() { std::string error; try { DatabaseManager::getQueryExecutor().setCurrentUserID(currentUserID); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([error, promise]() { if (error.size()) { promise->reject(error); } else { promise->resolve(jsi::Value::undefined()); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::getCurrentUserID(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [this, &innerRt, promise]() { std::string error; std::string result; try { result = DatabaseManager::getQueryExecutor().getCurrentUserID(); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([&innerRt, error, result, promise]() { if (error.size()) { promise->reject(error); } else { promise->resolve(jsi::String::createFromUtf8(innerRt, result)); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::clearSensitiveData(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { GlobalDBSingleton::instance.setTasksCancelled(true); taskType job = [this, promise]() { std::string error; try { DatabaseManager::clearSensitiveData(); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([error, promise]() { if (error.size()) { promise->reject(error); } else { promise->resolve(jsi::Value::undefined()); } }); GlobalDBSingleton::instance.scheduleOrRun( []() { GlobalDBSingleton::instance.setTasksCancelled(false); }); }; GlobalDBSingleton::instance.scheduleOrRun(job); }); } bool CommCoreModule::checkIfDatabaseNeedsDeletion(jsi::Runtime &rt) { return DatabaseManager::checkIfDatabaseNeedsDeletion(); } void CommCoreModule::reportDBOperationsFailure(jsi::Runtime &rt) { DatabaseManager::reportDBOperationsFailure(); } jsi::Value CommCoreModule::computeBackupKey( jsi::Runtime &rt, jsi::String password, jsi::String backupID) { std::string passwordStr = password.utf8(rt); std::string backupIDStr = backupID.utf8(rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::array<::std::uint8_t, 32> backupKey; try { backupKey = compute_backup_key(passwordStr, backupIDStr); } catch (const std::exception &e) { error = std::string{"Failed to compute backup key: "} + e.what(); } this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } auto size = backupKey.size(); auto arrayBuffer = innerRt.global() .getPropertyAsFunction(innerRt, "ArrayBuffer") .callAsConstructor(innerRt, {static_cast(size)}) .asObject(innerRt) .getArrayBuffer(innerRt); auto bufferPtr = arrayBuffer.data(innerRt); memcpy(bufferPtr, backupKey.data(), size); promise->resolve(std::move(arrayBuffer)); }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::generateRandomString(jsi::Runtime &rt, double size) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; std::string randomString; try { randomString = crypto::Tools::generateRandomString(static_cast(size)); } catch (const std::exception &e) { error = "Failed to generate random string for size " + std::to_string(size) + ": " + e.what(); } this->jsInvoker_->invokeAsync( [&innerRt, error, randomString, promise]() { if (error.size()) { promise->reject(error); } else { jsi::String jsiRandomString = jsi::String::createFromUtf8(innerRt, randomString); promise->resolve(std::move(jsiRandomString)); } }); }; this->cryptoThread->scheduleTask(job); }); } jsi::Value CommCoreModule::setCommServicesAuthMetadata( jsi::Runtime &rt, jsi::String userID, jsi::String deviceID, jsi::String accessToken) { auto userIDStr{userID.utf8(rt)}; auto deviceIDStr{deviceID.utf8(rt)}; auto accessTokenStr{accessToken.utf8(rt)}; return createPromiseAsJSIValue( rt, [this, userIDStr, deviceIDStr, accessTokenStr]( jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [this, promise, userIDStr, deviceIDStr, accessTokenStr]() { std::string error; try { CommSecureStore::set(CommSecureStore::userID, userIDStr); CommSecureStore::set(CommSecureStore::deviceID, deviceIDStr); CommSecureStore::set( CommSecureStore::commServicesAccessToken, accessTokenStr); CommServicesAuthMetadataEmitter::sendAuthMetadataToJS( accessTokenStr, userIDStr); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([error, promise]() { if (error.size()) { promise->reject(error); } else { promise->resolve(jsi::Value::undefined()); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::getCommServicesAuthMetadata(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [this, &innerRt, promise]() { std::string error; std::string userID; std::string deviceID; std::string accessToken; try { folly::Optional userIDOpt = CommSecureStore::get(CommSecureStore::userID); if (userIDOpt.hasValue()) { userID = userIDOpt.value(); } folly::Optional deviceIDOpt = CommSecureStore::get(CommSecureStore::deviceID); if (deviceIDOpt.hasValue()) { deviceID = deviceIDOpt.value(); } folly::Optional accessTokenOpt = CommSecureStore::get(CommSecureStore::commServicesAccessToken); if (accessTokenOpt.hasValue()) { accessToken = accessTokenOpt.value(); } } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync( [&innerRt, error, userID, deviceID, accessToken, promise]() { if (error.size()) { promise->reject(error); } else { auto authMetadata = jsi::Object(innerRt); if (!userID.empty()) { authMetadata.setProperty( innerRt, "userID", jsi::String::createFromUtf8(innerRt, userID)); } if (!deviceID.empty()) { authMetadata.setProperty( innerRt, "deviceID", jsi::String::createFromUtf8(innerRt, deviceID)); } if (!accessToken.empty()) { authMetadata.setProperty( innerRt, "accessToken", jsi::String::createFromUtf8(innerRt, accessToken)); } promise->resolve(std::move(authMetadata)); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::setCommServicesAccessToken( jsi::Runtime &rt, jsi::String accessToken) { auto accessTokenStr{accessToken.utf8(rt)}; return createPromiseAsJSIValue( rt, [this, accessTokenStr]( jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [this, promise, accessTokenStr]() { std::string error; try { CommSecureStore::set( CommSecureStore::commServicesAccessToken, accessTokenStr); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([error, promise]() { if (error.size()) { promise->reject(error); } else { promise->resolve(jsi::Value::undefined()); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } jsi::Value CommCoreModule::clearCommServicesAccessToken(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [this, promise]() { std::string error; try { CommSecureStore::set(CommSecureStore::commServicesAccessToken, ""); } catch (const std::exception &e) { error = e.what(); } this->jsInvoker_->invokeAsync([error, promise]() { if (error.size()) { promise->reject(error); } else { promise->resolve(jsi::Value::undefined()); } }); }; GlobalDBSingleton::instance.scheduleOrRunCancellable( job, promise, this->jsInvoker_); }); } void CommCoreModule::startBackupHandler(jsi::Runtime &rt) { try { ::startBackupHandler(); } catch (const std::exception &e) { throw jsi::JSError(rt, e.what()); } } void CommCoreModule::stopBackupHandler(jsi::Runtime &rt) { try { ::stopBackupHandler(); } catch (const std::exception &e) { throw jsi::JSError(rt, e.what()); } } jsi::Value CommCoreModule::createNewBackup(jsi::Runtime &rt, jsi::String backupSecret) { std::string backupSecretStr = backupSecret.utf8(rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { this->cryptoThread->scheduleTask([=, &innerRt]() { std::string error; std::string backupID; try { backupID = crypto::Tools::generateRandomString(32); } catch (const std::exception &e) { error = "Failed to generate backupID"; } std::string pickleKey; std::string pickledAccount; if (!error.size()) { try { pickleKey = crypto::Tools::generateRandomString(64); crypto::Persist persist = this->cryptoModule->storeAsB64(pickleKey); pickledAccount = std::string(persist.account.begin(), persist.account.end()); } catch (const std::exception &e) { error = "Failed to pickle crypto account"; } } if (!error.size()) { auto currentID = RustPromiseManager::instance.addPromise( {promise, this->jsInvoker_, innerRt}); ::createBackup( rust::string(backupID), rust::string(backupSecretStr), rust::string(pickleKey), rust::string(pickledAccount), currentID); } else { this->jsInvoker_->invokeAsync( [=, &innerRt]() { promise->reject(error); }); } }); }); } jsi::Value CommCoreModule::restoreBackup(jsi::Runtime &rt, jsi::String backupSecret) { std::string backupSecretStr = backupSecret.utf8(rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { auto currentID = RustPromiseManager::instance.addPromise( {promise, this->jsInvoker_, innerRt}); ::restoreBackup(rust::string(backupSecretStr), currentID); }); } } // namespace comm diff --git a/web/cpp/SQLiteQueryExecutorBindings.cpp b/web/cpp/SQLiteQueryExecutorBindings.cpp index 9a795c18c..aa3ea22f8 100644 --- a/web/cpp/SQLiteQueryExecutorBindings.cpp +++ b/web/cpp/SQLiteQueryExecutorBindings.cpp @@ -1,246 +1,249 @@ #include "SQLiteQueryExecutor.cpp" #include "entities/MessageToDevice.h" #include "entities/Nullable.h" #include #include namespace comm { using namespace emscripten; std::string getExceptionMessage(int exceptionPtr) { if (exceptionPtr == 0) { return std::string("Exception pointer value was null"); } std::exception *e = reinterpret_cast(exceptionPtr); if (e) { return std::string(e->what()); } return std::string("Pointer to exception was invalid"); } EMSCRIPTEN_BINDINGS(SQLiteQueryExecutor) { function("getExceptionMessage", &getExceptionMessage); value_object("NullableString") .field("value", &NullableString::value) .field("isNull", &NullableString::isNull); value_object("NullableInt") .field("value", &NullableInt::value) .field("isNull", &NullableInt::isNull); value_object("Draft") .field("key", &Draft::key) .field("text", &Draft::text); value_object("Report") .field("id", &Report::id) .field("report", &Report::report); value_object("PersistItem") .field("key", &PersistItem::key) .field("item", &PersistItem::item); value_object("UserInfo") .field("id", &UserInfo::id) .field("userInfo", &UserInfo::user_info); value_object("KeyserverInfo") .field("id", &KeyserverInfo::id) .field("keyserverInfo", &KeyserverInfo::keyserver_info); value_object("MessageStoreThreads") .field("id", &MessageStoreThread::id) .field("startReached", &MessageStoreThread::start_reached); value_object("CommunityInfo") .field("id", &CommunityInfo::id) .field("communityInfo", &CommunityInfo::community_info); value_object("WebThread") .field("id", &WebThread::id) .field("type", &WebThread::type) .field("name", &WebThread::name) .field("description", &WebThread::description) .field("color", &WebThread::color) .field("creationTime", &WebThread::creation_time) .field("parentThreadID", &WebThread::parent_thread_id) .field("containingThreadID", &WebThread::containing_thread_id) .field("community", &WebThread::community) .field("members", &WebThread::members) .field("roles", &WebThread::roles) .field("currentUser", &WebThread::current_user) .field("sourceMessageID", &WebThread::source_message_id) .field("repliesCount", &WebThread::replies_count) .field("avatar", &WebThread::avatar) .field("pinnedCount", &WebThread::pinned_count); value_object("WebMessage") .field("id", &WebMessage::id) .field("localID", &WebMessage::local_id) .field("thread", &WebMessage::thread) .field("user", &WebMessage::user) .field("type", &WebMessage::type) .field("futureType", &WebMessage::future_type) .field("content", &WebMessage::content) .field("time", &WebMessage::time); value_object("Media") .field("id", &Media::id) .field("container", &Media::container) .field("thread", &Media::thread) .field("uri", &Media::uri) .field("type", &Media::type) .field("extras", &Media::extras); value_object("MessageWithMedias") .field("message", &MessageWithMedias::message) .field("medias", &MessageWithMedias::medias); value_object("OlmPersistSession") .field("targetUserID", &OlmPersistSession::target_user_id) .field("sessionData", &OlmPersistSession::session_data); value_object("ClientMessageToDevice") .field("messageID", &ClientMessageToDevice::message_id) .field("deviceID", &ClientMessageToDevice::device_id) .field("userID", &ClientMessageToDevice::user_id) .field("timestamp", &ClientMessageToDevice::timestamp) .field("plaintext", &ClientMessageToDevice::plaintext) .field("ciphertext", &ClientMessageToDevice::ciphertext); class_("SQLiteQueryExecutor") .constructor() .function("updateDraft", &SQLiteQueryExecutor::updateDraft) .function("moveDraft", &SQLiteQueryExecutor::moveDraft) .function("getAllDrafts", &SQLiteQueryExecutor::getAllDrafts) .function("removeAllDrafts", &SQLiteQueryExecutor::removeAllDrafts) .function("removeDrafts", &SQLiteQueryExecutor::removeDrafts) .function("getAllMessagesWeb", &SQLiteQueryExecutor::getAllMessagesWeb) .function("removeAllMessages", &SQLiteQueryExecutor::removeAllMessages) .function("removeMessages", &SQLiteQueryExecutor::removeMessages) .function( "removeMessagesForThreads", &SQLiteQueryExecutor::removeMessagesForThreads) .function("replaceMessageWeb", &SQLiteQueryExecutor::replaceMessageWeb) .function("rekeyMessage", &SQLiteQueryExecutor::rekeyMessage) .function("removeAllMedia", &SQLiteQueryExecutor::removeAllMedia) .function( "removeMediaForThreads", &SQLiteQueryExecutor::removeMediaForThreads) .function( "removeMediaForMessage", &SQLiteQueryExecutor::removeMediaForMessage) .function( "removeMediaForMessages", &SQLiteQueryExecutor::removeMediaForMessages) .function("replaceMedia", &SQLiteQueryExecutor::replaceMedia) .function( "rekeyMediaContainers", &SQLiteQueryExecutor::rekeyMediaContainers) .function( "replaceMessageStoreThreads", &SQLiteQueryExecutor::replaceMessageStoreThreads) .function( "removeMessageStoreThreads", &SQLiteQueryExecutor::removeMessageStoreThreads) .function( "getAllMessageStoreThreads", &SQLiteQueryExecutor::getAllMessageStoreThreads) .function( "removeAllMessageStoreThreads", &SQLiteQueryExecutor::removeAllMessageStoreThreads) .function("setMetadata", &SQLiteQueryExecutor::setMetadata) .function("clearMetadata", &SQLiteQueryExecutor::clearMetadata) .function("getMetadata", &SQLiteQueryExecutor::getMetadata) .function("replaceReport", &SQLiteQueryExecutor::replaceReport) .function("removeReports", &SQLiteQueryExecutor::removeReports) .function("removeAllReports", &SQLiteQueryExecutor::removeAllReports) .function("getAllReports", &SQLiteQueryExecutor::getAllReports) .function( "setPersistStorageItem", &SQLiteQueryExecutor::setPersistStorageItem) .function( "removePersistStorageItem", &SQLiteQueryExecutor::removePersistStorageItem) .function( "getPersistStorageItem", &SQLiteQueryExecutor::getPersistStorageItem) .function("replaceUser", &SQLiteQueryExecutor::replaceUser) .function("removeUsers", &SQLiteQueryExecutor::removeUsers) .function("removeAllUsers", &SQLiteQueryExecutor::removeAllUsers) .function("getAllUsers", &SQLiteQueryExecutor::getAllUsers) .function("replaceThreadWeb", &SQLiteQueryExecutor::replaceThreadWeb) .function("getAllThreadsWeb", &SQLiteQueryExecutor::getAllThreadsWeb) .function("removeAllThreads", &SQLiteQueryExecutor::removeAllThreads) .function("removeThreads", &SQLiteQueryExecutor::removeThreads) .function("replaceKeyserver", &SQLiteQueryExecutor::replaceKeyserver) .function("removeKeyservers", &SQLiteQueryExecutor::removeKeyservers) .function( "removeAllKeyservers", &SQLiteQueryExecutor::removeAllKeyservers) .function("getAllKeyservers", &SQLiteQueryExecutor::getAllKeyservers) .function("replaceCommunity", &SQLiteQueryExecutor::replaceCommunity) .function("removeCommunities", &SQLiteQueryExecutor::removeCommunities) .function( "removeAllCommunities", &SQLiteQueryExecutor::removeAllCommunities) .function("getAllCommunities", &SQLiteQueryExecutor::getAllCommunities) .function("beginTransaction", &SQLiteQueryExecutor::beginTransaction) .function("commitTransaction", &SQLiteQueryExecutor::commitTransaction) + .function( + "getContentAccountID", &SQLiteQueryExecutor::getContentAccountID) + .function("getNotifsAccountID", &SQLiteQueryExecutor::getNotifsAccountID) .function( "getOlmPersistSessionsData", &SQLiteQueryExecutor::getOlmPersistSessionsData) .function( "getOlmPersistAccountDataWeb", &SQLiteQueryExecutor::getOlmPersistAccountDataWeb) .function( "storeOlmPersistSession", &SQLiteQueryExecutor::storeOlmPersistSession) .function( "storeOlmPersistAccount", &SQLiteQueryExecutor::storeOlmPersistAccount) .function( "rollbackTransaction", &SQLiteQueryExecutor::rollbackTransaction) .function( "restoreFromMainCompaction", &SQLiteQueryExecutor::restoreFromMainCompaction) .function( "restoreFromBackupLog", &SQLiteQueryExecutor::restoreFromBackupLog) .function( "addMessagesToDevice", &SQLiteQueryExecutor::addMessagesToDevice) .function( "removeMessagesToDeviceOlderThan", &SQLiteQueryExecutor::removeMessagesToDeviceOlderThan) .function( "removeAllMessagesForDevice", &SQLiteQueryExecutor::removeAllMessagesForDevice) .function( "getAllMessagesToDevice", &SQLiteQueryExecutor::getAllMessagesToDevice); } } // namespace comm namespace emscripten { namespace internal { template struct BindingType> { using ValBinding = BindingType; using WireType = ValBinding::WireType; static WireType toWireType(const std::vector &vec) { std::vector valVec(vec.begin(), vec.end()); return BindingType::toWireType(val::array(valVec)); } static std::vector fromWireType(WireType value) { return vecFromJSArray(ValBinding::fromWireType(value)); } }; template struct TypeID< T, typename std::enable_if_t::type, std::vector< typename Canonicalized::type::value_type, typename Canonicalized::type::allocator_type>>::value>> { static constexpr TYPEID get() { return TypeID::get(); } }; } // namespace internal } // namespace emscripten diff --git a/web/database/_generated/comm-query-executor.js b/web/database/_generated/comm-query-executor.js index ea3e5f8a1..1ba140886 100644 --- a/web/database/_generated/comm-query-executor.js +++ b/web/database/_generated/comm-query-executor.js @@ -1,158 +1,158 @@ // @generated var Module = (() => { var _scriptDir = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined; if (typeof __filename !== 'undefined') _scriptDir = _scriptDir || __filename; return ( function(Module) { Module = Module || {}; var e;e||(e=typeof Module !== 'undefined' ? Module : {});var aa,ba;e.ready=new Promise(function(a,b){aa=a;ba=b});var ca=Object.assign({},e),da="./this.program",ea="object"==typeof window,fa="function"==typeof importScripts,ha="object"==typeof process&&"object"==typeof process.versions&&"string"==typeof process.versions.node,m="",ia,ja,ka,fs,la,ma; if(ha)m=fa?require("path").dirname(m)+"/":__dirname+"/",ma=()=>{la||(fs=require("fs"),la=require("path"))},ia=function(a,b){ma();a=la.normalize(a);return fs.readFileSync(a,b?void 0:"utf8")},ka=a=>{a=ia(a,!0);a.buffer||(a=new Uint8Array(a));return a},ja=(a,b,c)=>{ma();a=la.normalize(a);fs.readFile(a,function(d,f){d?c(d):b(f.buffer)})},1{var b=new XMLHttpRequest;b.open("GET",a,!1);b.send(null);return b.responseText},fa&&(ka=a=>{var b=new XMLHttpRequest;b.open("GET",a,!1);b.responseType="arraybuffer";b.send(null);return new Uint8Array(b.response)}),ja=(a,b,c)=>{var d=new XMLHttpRequest;d.open("GET",a,!0);d.responseType="arraybuffer"; d.onload=()=>{200==d.status||0==d.status&&d.response?b(d.response):c()};d.onerror=c;d.send(null)};var na=e.print||console.log.bind(console),r=e.printErr||console.warn.bind(console);Object.assign(e,ca);ca=null;e.thisProgram&&(da=e.thisProgram);var oa;e.wasmBinary&&(oa=e.wasmBinary);var noExitRuntime=e.noExitRuntime||!0;"object"!=typeof WebAssembly&&v("no native wasm support detected");var pa,qa=!1,ra="undefined"!=typeof TextDecoder?new TextDecoder("utf8"):void 0; function sa(a,b,c){var d=b+c;for(c=b;a[c]&&!(c>=d);)++c;if(16f?d+=String.fromCharCode(f):(f-=65536,d+=String.fromCharCode(55296|f>>10,56320|f&1023))}}else d+=String.fromCharCode(f)}return d}function y(a,b){return a?sa(z,a,b):""} function A(a,b,c,d){if(!(0=k){var h=a.charCodeAt(++g);k=65536+((k&1023)<<10)|h&1023}if(127>=k){if(c>=d)break;b[c++]=k}else{if(2047>=k){if(c+1>=d)break;b[c++]=192|k>>6}else{if(65535>=k){if(c+2>=d)break;b[c++]=224|k>>12}else{if(c+3>=d)break;b[c++]=240|k>>18;b[c++]=128|k>>12&63}b[c++]=128|k>>6&63}b[c++]=128|k&63}}b[c]=0;return c-f} function ta(a){for(var b=0,c=0;c=d?b++:2047>=d?b+=2:55296<=d&&57343>=d?(b+=4,++c):b+=3}return b}var ua,B,z,D,va,E,F,wa,xa;function ya(){var a=pa.buffer;ua=a;e.HEAP8=B=new Int8Array(a);e.HEAP16=D=new Int16Array(a);e.HEAP32=E=new Int32Array(a);e.HEAPU8=z=new Uint8Array(a);e.HEAPU16=va=new Uint16Array(a);e.HEAPU32=F=new Uint32Array(a);e.HEAPF32=wa=new Float32Array(a);e.HEAPF64=xa=new Float64Array(a)}var za,Aa=[],Ba=[],Ca=[]; function Da(){var a=e.preRun.shift();Aa.unshift(a)}var Ea=0,Fa=null,Ha=null;function Ia(){Ea++;e.monitorRunDependencies&&e.monitorRunDependencies(Ea)}function Ja(){Ea--;e.monitorRunDependencies&&e.monitorRunDependencies(Ea);if(0==Ea&&(null!==Fa&&(clearInterval(Fa),Fa=null),Ha)){var a=Ha;Ha=null;a()}}function v(a){if(e.onAbort)e.onAbort(a);a="Aborted("+a+")";r(a);qa=!0;a=new WebAssembly.RuntimeError(a+". Build with -sASSERTIONS for more info.");ba(a);throw a;}var Ka;Ka="comm-query-executor.wasm"; if(!Ka.startsWith("data:application/octet-stream;base64,")){var La=Ka;Ka=e.locateFile?e.locateFile(La,m):m+La} function Ma(a){var b=Ka;try{a:{try{if(b==Ka&&oa){var c=new Uint8Array(oa);break a}if(ka){c=ka(b);break a}throw"sync fetching of the wasm failed: you can preload it to Module['wasmBinary'] manually, or emcc.py will do that for you when generating HTML (but not JS)";}catch(g){v(g)}c=void 0}var d=new WebAssembly.Module(c);var f=new WebAssembly.Instance(d,a)}catch(g){throw a=g.toString(),r("failed to compile wasm module: "+a),(a.includes("imported Memory")||a.includes("memory import"))&&r("Memory size incompatibility issues may be due to changing INITIAL_MEMORY at runtime to something too large. Use ALLOW_MEMORY_GROWTH to allow any size memory (and also make sure not to set INITIAL_MEMORY at runtime to something smaller than it was at compile time)."), g;}return[f,d]}var G,J;function Na(a){for(;0>2]=b};this.Xc=function(b){F[this.Sa+8>>2]=b};this.Yc=function(){E[this.Sa>>2]=0};this.ac=function(){B[this.Sa+12>>0]=0};this.Zc=function(){B[this.Sa+13>>0]=0};this.Db=function(b,c){this.cb();this.$c(b);this.Xc(c);this.Yc();this.ac();this.Zc()};this.cb=function(){F[this.Sa+16>>2]=0}} var Pa=0,Qa=(a,b)=>{for(var c=0,d=a.length-1;0<=d;d--){var f=a[d];"."===f?a.splice(d,1):".."===f?(a.splice(d,1),c++):c&&(a.splice(d,1),c--)}if(b)for(;c;c--)a.unshift("..");return a},L=a=>{var b="/"===a.charAt(0),c="/"===a.substr(-1);(a=Qa(a.split("/").filter(d=>!!d),!b).join("/"))||b||(a=".");a&&c&&(a+="/");return(b?"/":"")+a},Ra=a=>{var b=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/.exec(a).slice(1);a=b[0];b=b[1];if(!a&&!b)return".";b&&(b=b.substr(0,b.length-1));return a+b},M= a=>{if("/"===a)return"/";a=L(a);a=a.replace(/\/$/,"");var b=a.lastIndexOf("/");return-1===b?a:a.substr(b+1)},Sa=(a,b)=>L(a+"/"+b);function Ta(){if("object"==typeof crypto&&"function"==typeof crypto.getRandomValues){var a=new Uint8Array(1);return()=>{crypto.getRandomValues(a);return a[0]}}if(ha)try{var b=require("crypto");return()=>b.randomBytes(1)[0]}catch(c){}return()=>v("randomDevice")} function Ua(){for(var a="",b=!1,c=arguments.length-1;-1<=c&&!b;c--){b=0<=c?arguments[c]:N.cwd();if("string"!=typeof b)throw new TypeError("Arguments to path.resolve must be strings");if(!b)return"";a=b+"/"+a;b="/"===b.charAt(0)}a=Qa(a.split("/").filter(d=>!!d),!b).join("/");return(b?"/":"")+a||"."} var Va=(a,b)=>{function c(k){for(var h=0;hn?[]:k.slice(h,n-h+1)}a=Ua(a).substr(1);b=Ua(b).substr(1);a=c(a.split("/"));b=c(b.split("/"));for(var d=Math.min(a.length,b.length),f=d,g=0;g=b||(b=Math.max(b,c*(1048576>c?2:1.125)>>>0),0!=c&&(b=Math.max(b,256)),c=a.Pa,a.Pa=new Uint8Array(b),0=a.node.Va)return 0;a=Math.min(a.node.Va-f,d);if(8b)throw new N.Ma(28);return b},Ib:function(a,b,c){O.Kc(a.node,b+c);a.node.Va=Math.max(a.node.Va,b+c)},yb:function(a,b,c,d,f){if(!N.isFile(a.node.mode))throw new N.Ma(43);a=a.node.Pa;if(f&2||a.buffer!==ua){if(0{f||v('Loading data file "'+a+'" failed (no arrayBuffer).');b(new Uint8Array(f));d&&Ja(d)},()=>{if(c)c();else throw'Loading data file "'+a+'" failed.';});d&&Ia(d)} var N={root:null,Mb:[],Ic:{},streams:[],Hd:1,lb:null,Gc:"/",jc:!1,Rc:!0,Ma:null,fc:{},sd:null,Yb:0,Ua:(a,b={})=>{a=Ua(N.cwd(),a);if(!a)return{path:"",node:null};b=Object.assign({dc:!0,tc:0},b);if(8!!k),!1);for(var c=N.root,d="/",f=0;f{for(var b;;){if(N.Ub(a))return a=a.Wa.Sc,b?"/"!==a[a.length-1]?a+"/"+b:a+b:a;b=b?a.name+"/"+b:a.name;a=a.parent}},ic:(a,b)=>{for(var c=0,d=0;d>>0)%N.lb.length},Pc:a=>{var b=N.ic(a.parent.id,a.name);a.zb=N.lb[b];N.lb[b]=a},Qc:a=>{var b=N.ic(a.parent.id,a.name);if(N.lb[b]===a)N.lb[b]=a.zb;else for(b=N.lb[b];b;){if(b.zb===a){b.zb=a.zb;break}b=b.zb}},kb:(a,b)=>{var c=N.Ed(a);if(c)throw new N.Ma(c, a);for(c=N.lb[N.ic(a.id,b)];c;c=c.zb){var d=c.name;if(c.parent.id===a.id&&d===b)return c}return N.lookup(a,b)},createNode:(a,b,c,d)=>{a=new N.Vc(a,b,c,d);N.Pc(a);return a},cc:a=>{N.Qc(a)},Ub:a=>a===a.parent,wb:a=>!!a.Lb,isFile:a=>32768===(a&61440),Ya:a=>16384===(a&61440),vb:a=>40960===(a&61440),Jb:a=>8192===(a&61440),Bd:a=>24576===(a&61440),isFIFO:a=>4096===(a&61440),isSocket:a=>49152===(a&49152),td:{r:0,"r+":2,w:577,"w+":578,a:1089,"a+":1090},Gd:a=>{var b=N.td[a];if("undefined"==typeof b)throw Error("Unknown file open mode: "+ a);return b},Mc:a=>{var b=["r","w","rw"][a&3];a&512&&(b+="w");return b},sb:(a,b)=>{if(N.Rc)return 0;if(!b.includes("r")||a.mode&292){if(b.includes("w")&&!(a.mode&146)||b.includes("x")&&!(a.mode&73))return 2}else return 2;return 0},Ed:a=>{var b=N.sb(a,"x");return b?b:a.Oa.lookup?0:2},pc:(a,b)=>{try{return N.kb(a,b),20}catch(c){}return N.sb(a,"wx")},Wb:(a,b,c)=>{try{var d=N.kb(a,b)}catch(f){return f.Ra}if(a=N.sb(a,"wx"))return a;if(c){if(!N.Ya(d.mode))return 54;if(N.Ub(d)||N.qb(d)===N.cwd())return 10}else if(N.Ya(d.mode))return 31; return 0},Fd:(a,b)=>a?N.vb(a.mode)?32:N.Ya(a.mode)&&("r"!==N.Mc(b)||b&512)?31:N.sb(a,N.Mc(b)):44,Wc:4096,Id:(a=0,b=N.Wc)=>{for(;a<=b;a++)if(!N.streams[a])return a;throw new N.Ma(33);},rb:a=>N.streams[a],Fc:(a,b,c)=>{N.Pb||(N.Pb=function(){this.cb={}},N.Pb.prototype={},Object.defineProperties(N.Pb.prototype,{object:{get:function(){return this.node},set:function(d){this.node=d}},flags:{get:function(){return this.cb.flags},set:function(d){this.cb.flags=d}},position:{get:function(){return this.cb.position}, set:function(d){this.cb.position=d}}}));a=Object.assign(new N.Pb,a);b=N.Id(b,c);a.fd=b;return N.streams[b]=a},hd:a=>{N.streams[a]=null},gd:{open:a=>{a.Qa=N.wd(a.node.rdev).Qa;a.Qa.open&&a.Qa.open(a)},bb:()=>{throw new N.Ma(70);}},oc:a=>a>>8,fe:a=>a&255,xb:(a,b)=>a<<8|b,vc:(a,b)=>{N.Ic[a]={Qa:b}},wd:a=>N.Ic[a],Nc:a=>{var b=[];for(a=[a];a.length;){var c=a.pop();b.push(c);a.push.apply(a,c.Mb)}return b},Uc:(a,b)=>{function c(k){N.Yb--;return b(k)}function d(k){if(k){if(!d.qd)return d.qd=!0,c(k)}else++g>= f.length&&c(null)}"function"==typeof a&&(b=a,a=!1);N.Yb++;1{if(!k.type.Uc)return d(null);k.type.Uc(k,a,d)})},Wa:(a,b,c)=>{var d="/"===c,f=!c;if(d&&N.root)throw new N.Ma(10);if(!d&&!f){var g=N.Ua(c,{dc:!1});c=g.path;g=g.node;if(N.wb(g))throw new N.Ma(10);if(!N.Ya(g.mode))throw new N.Ma(54);}b={type:a,je:b,Sc:c,Mb:[]};a=a.Wa(b);a.Wa=b;b.root=a;d?N.root=a:g&&(g.Lb= b,g.Wa&&g.Wa.Mb.push(b));return a},ne:a=>{a=N.Ua(a,{dc:!1});if(!N.wb(a.node))throw new N.Ma(28);a=a.node;var b=a.Lb,c=N.Nc(b);Object.keys(N.lb).forEach(d=>{for(d=N.lb[d];d;){var f=d.zb;c.includes(d.Wa)&&N.cc(d);d=f}});a.Lb=null;a.Wa.Mb.splice(a.Wa.Mb.indexOf(b),1)},lookup:(a,b)=>a.Oa.lookup(a,b),ob:(a,b,c)=>{var d=N.Ua(a,{parent:!0}).node;a=M(a);if(!a||"."===a||".."===a)throw new N.Ma(28);var f=N.pc(d,a);if(f)throw new N.Ma(f);if(!d.Oa.ob)throw new N.Ma(63);return d.Oa.ob(d,a,b,c)},create:(a,b)=> N.ob(a,(void 0!==b?b:438)&4095|32768,0),mkdir:(a,b)=>N.ob(a,(void 0!==b?b:511)&1023|16384,0),ge:(a,b)=>{a=a.split("/");for(var c="",d=0;d{"undefined"==typeof c&&(c=b,b=438);return N.ob(a,b|8192,c)},symlink:(a,b)=>{if(!Ua(a))throw new N.Ma(44);var c=N.Ua(b,{parent:!0}).node;if(!c)throw new N.Ma(44);b=M(b);var d=N.pc(c,b);if(d)throw new N.Ma(d);if(!c.Oa.symlink)throw new N.Ma(63);return c.Oa.symlink(c,b, a)},rename:(a,b)=>{var c=Ra(a),d=Ra(b),f=M(a),g=M(b);var k=N.Ua(a,{parent:!0});var h=k.node;k=N.Ua(b,{parent:!0});k=k.node;if(!h||!k)throw new N.Ma(44);if(h.Wa!==k.Wa)throw new N.Ma(75);var n=N.kb(h,f);a=Va(a,d);if("."!==a.charAt(0))throw new N.Ma(28);a=Va(b,c);if("."!==a.charAt(0))throw new N.Ma(55);try{var q=N.kb(k,g)}catch(p){}if(n!==q){b=N.Ya(n.mode);if(f=N.Wb(h,f,b))throw new N.Ma(f);if(f=q?N.Wb(k,g,b):N.pc(k,g))throw new N.Ma(f);if(!h.Oa.rename)throw new N.Ma(63);if(N.wb(n)||q&&N.wb(q))throw new N.Ma(10); if(k!==h&&(f=N.sb(h,"w")))throw new N.Ma(f);N.Qc(n);try{h.Oa.rename(n,k,g)}catch(p){throw p;}finally{N.Pc(n)}}},rmdir:a=>{var b=N.Ua(a,{parent:!0}).node;a=M(a);var c=N.kb(b,a),d=N.Wb(b,a,!0);if(d)throw new N.Ma(d);if(!b.Oa.rmdir)throw new N.Ma(63);if(N.wb(c))throw new N.Ma(10);b.Oa.rmdir(b,a);N.cc(c)},readdir:a=>{a=N.Ua(a,{eb:!0}).node;if(!a.Oa.readdir)throw new N.Ma(54);return a.Oa.readdir(a)},unlink:a=>{var b=N.Ua(a,{parent:!0}).node;if(!b)throw new N.Ma(44);a=M(a);var c=N.kb(b,a),d=N.Wb(b,a,!1); if(d)throw new N.Ma(d);if(!b.Oa.unlink)throw new N.Ma(63);if(N.wb(c))throw new N.Ma(10);b.Oa.unlink(b,a);N.cc(c)},readlink:a=>{a=N.Ua(a).node;if(!a)throw new N.Ma(44);if(!a.Oa.readlink)throw new N.Ma(28);return Ua(N.qb(a.parent),a.Oa.readlink(a))},stat:(a,b)=>{a=N.Ua(a,{eb:!b}).node;if(!a)throw new N.Ma(44);if(!a.Oa.jb)throw new N.Ma(63);return a.Oa.jb(a)},lstat:a=>N.stat(a,!0),chmod:(a,b,c)=>{a="string"==typeof a?N.Ua(a,{eb:!c}).node:a;if(!a.Oa.Za)throw new N.Ma(63);a.Oa.Za(a,{mode:b&4095|a.mode& -4096,timestamp:Date.now()})},lchmod:(a,b)=>{N.chmod(a,b,!0)},fchmod:(a,b)=>{a=N.rb(a);if(!a)throw new N.Ma(8);N.chmod(a.node,b)},chown:(a,b,c,d)=>{a="string"==typeof a?N.Ua(a,{eb:!d}).node:a;if(!a.Oa.Za)throw new N.Ma(63);a.Oa.Za(a,{timestamp:Date.now()})},lchown:(a,b,c)=>{N.chown(a,b,c,!0)},fchown:(a,b,c)=>{a=N.rb(a);if(!a)throw new N.Ma(8);N.chown(a.node,b,c)},truncate:(a,b)=>{if(0>b)throw new N.Ma(28);a="string"==typeof a?N.Ua(a,{eb:!0}).node:a;if(!a.Oa.Za)throw new N.Ma(63);if(N.Ya(a.mode))throw new N.Ma(31); if(!N.isFile(a.mode))throw new N.Ma(28);var c=N.sb(a,"w");if(c)throw new N.Ma(c);a.Oa.Za(a,{size:b,timestamp:Date.now()})},ud:(a,b)=>{a=N.rb(a);if(!a)throw new N.Ma(8);if(0===(a.flags&2097155))throw new N.Ma(28);N.truncate(a.node,b)},ae:(a,b,c)=>{a=N.Ua(a,{eb:!0}).node;a.Oa.Za(a,{timestamp:Math.max(b,c)})},open:(a,b,c)=>{if(""===a)throw new N.Ma(44);b="string"==typeof b?N.Gd(b):b;c=b&64?("undefined"==typeof c?438:c)&4095|32768:0;if("object"==typeof a)var d=a;else{a=L(a);try{d=N.Ua(a,{eb:!(b&131072)}).node}catch(g){}}var f= !1;if(b&64)if(d){if(b&128)throw new N.Ma(20);}else d=N.ob(a,c,0),f=!0;if(!d)throw new N.Ma(44);N.Jb(d.mode)&&(b&=-513);if(b&65536&&!N.Ya(d.mode))throw new N.Ma(54);if(!f&&(c=N.Fd(d,b)))throw new N.Ma(c);b&512&&!f&&N.truncate(d,0);b&=-131713;d=N.Fc({node:d,path:N.qb(d),flags:b,seekable:!0,position:0,Qa:d.Qa,$d:[],error:!1});d.Qa.open&&d.Qa.open(d);!e.logReadFiles||b&1||(N.sc||(N.sc={}),a in N.sc||(N.sc[a]=1));return d},close:a=>{if(N.Kb(a))throw new N.Ma(8);a.ub&&(a.ub=null);try{a.Qa.close&&a.Qa.close(a)}catch(b){throw b; }finally{N.hd(a.fd)}a.fd=null},Kb:a=>null===a.fd,bb:(a,b,c)=>{if(N.Kb(a))throw new N.Ma(8);if(!a.seekable||!a.Qa.bb)throw new N.Ma(70);if(0!=c&&1!=c&&2!=c)throw new N.Ma(28);a.position=a.Qa.bb(a,b,c);a.$d=[];return a.position},read:(a,b,c,d,f)=>{if(0>d||0>f)throw new N.Ma(28);if(N.Kb(a))throw new N.Ma(8);if(1===(a.flags&2097155))throw new N.Ma(8);if(N.Ya(a.node.mode))throw new N.Ma(31);if(!a.Qa.read)throw new N.Ma(28);var g="undefined"!=typeof f;if(!g)f=a.position;else if(!a.seekable)throw new N.Ma(70); b=a.Qa.read(a,b,c,d,f);g||(a.position+=b);return b},write:(a,b,c,d,f,g)=>{if(0>d||0>f)throw new N.Ma(28);if(N.Kb(a))throw new N.Ma(8);if(0===(a.flags&2097155))throw new N.Ma(8);if(N.Ya(a.node.mode))throw new N.Ma(31);if(!a.Qa.write)throw new N.Ma(28);a.seekable&&a.flags&1024&&N.bb(a,0,2);var k="undefined"!=typeof f;if(!k)f=a.position;else if(!a.seekable)throw new N.Ma(70);b=a.Qa.write(a,b,c,d,f,g);k||(a.position+=b);return b},Ib:(a,b,c)=>{if(N.Kb(a))throw new N.Ma(8);if(0>b||0>=c)throw new N.Ma(28); if(0===(a.flags&2097155))throw new N.Ma(8);if(!N.isFile(a.node.mode)&&!N.Ya(a.node.mode))throw new N.Ma(43);if(!a.Qa.Ib)throw new N.Ma(138);a.Qa.Ib(a,b,c)},yb:(a,b,c,d,f)=>{if(0!==(d&2)&&0===(f&2)&&2!==(a.flags&2097155))throw new N.Ma(2);if(1===(a.flags&2097155))throw new N.Ma(2);if(!a.Qa.yb)throw new N.Ma(43);return a.Qa.yb(a,b,c,d,f)},Fb:(a,b,c,d,f)=>a&&a.Qa.Fb?a.Qa.Fb(a,b,c,d,f):0,he:()=>0,kc:(a,b,c)=>{if(!a.Qa.kc)throw new N.Ma(59);return a.Qa.kc(a,b,c)},readFile:(a,b={})=>{b.flags=b.flags||0; b.encoding=b.encoding||"binary";if("utf8"!==b.encoding&&"binary"!==b.encoding)throw Error('Invalid encoding type "'+b.encoding+'"');var c,d=N.open(a,b.flags);a=N.stat(a).size;var f=new Uint8Array(a);N.read(d,f,0,a,0);"utf8"===b.encoding?c=sa(f,0):"binary"===b.encoding&&(c=f);N.close(d);return c},writeFile:(a,b,c={})=>{c.flags=c.flags||577;a=N.open(a,c.flags,c.mode);if("string"==typeof b){var d=new Uint8Array(ta(b)+1);b=A(b,d,0,d.length);N.write(a,d,0,b,void 0,c.ed)}else if(ArrayBuffer.isView(b))N.write(a, b,0,b.byteLength,void 0,c.ed);else throw Error("Unsupported data type");N.close(a)},cwd:()=>N.Gc,chdir:a=>{a=N.Ua(a,{eb:!0});if(null===a.node)throw new N.Ma(44);if(!N.Ya(a.node.mode))throw new N.Ma(54);var b=N.sb(a.node,"x");if(b)throw new N.Ma(b);N.Gc=a.path},ld:()=>{N.mkdir("/tmp");N.mkdir("/home");N.mkdir("/home/web_user")},kd:()=>{N.mkdir("/dev");N.vc(N.xb(1,3),{read:()=>0,write:(b,c,d,f)=>f});N.Xb("/dev/null",N.xb(1,3));Ya(N.xb(5,0),$a);Ya(N.xb(6,0),ab);N.Xb("/dev/tty",N.xb(5,0));N.Xb("/dev/tty1", N.xb(6,0));var a=Ta();N.hb("/dev","random",a);N.hb("/dev","urandom",a);N.mkdir("/dev/shm");N.mkdir("/dev/shm/tmp")},nd:()=>{N.mkdir("/proc");var a=N.mkdir("/proc/self");N.mkdir("/proc/self/fd");N.Wa({Wa:()=>{var b=N.createNode(a,"fd",16895,73);b.Oa={lookup:(c,d)=>{var f=N.rb(+d);if(!f)throw new N.Ma(8);c={parent:null,Wa:{Sc:"fake"},Oa:{readlink:()=>f.path}};return c.parent=c}};return b}},{},"/proc/self/fd")},od:()=>{e.stdin?N.hb("/dev","stdin",e.stdin):N.symlink("/dev/tty","/dev/stdin");e.stdout? N.hb("/dev","stdout",null,e.stdout):N.symlink("/dev/tty","/dev/stdout");e.stderr?N.hb("/dev","stderr",null,e.stderr):N.symlink("/dev/tty1","/dev/stderr");N.open("/dev/stdin",0);N.open("/dev/stdout",1);N.open("/dev/stderr",1)},Jc:()=>{N.Ma||(N.Ma=function(a,b){this.node=b;this.Qd=function(c){this.Ra=c};this.Qd(a);this.message="FS error"},N.Ma.prototype=Error(),N.Ma.prototype.constructor=N.Ma,[44].forEach(a=>{N.fc[a]=new N.Ma(a);N.fc[a].stack=""}))},Vd:()=>{N.Jc();N.lb=Array(4096); N.Wa(O,{},"/");N.ld();N.kd();N.nd();N.sd={MEMFS:O}},Db:(a,b,c)=>{N.Db.jc=!0;N.Jc();e.stdin=a||e.stdin;e.stdout=b||e.stdout;e.stderr=c||e.stderr;N.od()},ke:()=>{N.Db.jc=!1;for(var a=0;a{var c=0;a&&(c|=365);b&&(c|=146);return c},be:(a,b)=>{a=N.bc(a,b);return a.exists?a.object:null},bc:(a,b)=>{try{var c=N.Ua(a,{eb:!b});a=c.path}catch(f){}var d={Ub:!1,exists:!1,error:0,name:null,path:null,object:null,Jd:!1,Ld:null,Kd:null};try{c=N.Ua(a, {parent:!0}),d.Jd=!0,d.Ld=c.path,d.Kd=c.node,d.name=M(a),c=N.Ua(a,{eb:!b}),d.exists=!0,d.path=c.path,d.object=c.node,d.name=c.node.name,d.Ub="/"===c.path}catch(f){d.error=f.Ra}return d},Dc:(a,b)=>{a="string"==typeof a?a:N.qb(a);for(b=b.split("/").reverse();b.length;){var c=b.pop();if(c){var d=L(a+"/"+c);try{N.mkdir(d)}catch(f){}a=d}}return d},md:(a,b,c,d,f)=>{a="string"==typeof a?a:N.qb(a);b=L(a+"/"+b);return N.create(b,N.hc(d,f))},Rb:(a,b,c,d,f,g)=>{var k=b;a&&(a="string"==typeof a?a:N.qb(a),k=b? L(a+"/"+b):a);a=N.hc(d,f);k=N.create(k,a);if(c){if("string"==typeof c){b=Array(c.length);d=0;for(f=c.length;d{a=Sa("string"==typeof a?a:N.qb(a),b);b=N.hc(!!c,!!d);N.hb.oc||(N.hb.oc=64);var f=N.xb(N.hb.oc++,0);N.vc(f,{open:g=>{g.seekable=!1},close:()=>{d&&d.buffer&&d.buffer.length&&d(10)},read:(g,k,h,n)=>{for(var q=0,p=0;p{for(var q=0;q{if(a.lc||a.Cd||a.link||a.Pa)return!0;if("undefined"!=typeof XMLHttpRequest)throw Error("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread."); if(ia)try{a.Pa=Wa(ia(a.url),!0),a.Va=a.Pa.length}catch(b){throw new N.Ma(29);}else throw Error("Cannot load without read() or XMLHttpRequest.");},Cc:(a,b,c,d,f)=>{function g(){this.nc=!1;this.cb=[]}function k(p,t,x,l,u){p=p.node.Pa;if(u>=p.length)return 0;l=Math.min(p.length-u,l);if(p.slice)for(var w=0;wthis.length-1||0>p)){var t=p%this.chunkSize;return this.Sb(p/this.chunkSize|0)[t]}};g.prototype.ac= function(p){this.Sb=p};g.prototype.Ac=function(){var p=new XMLHttpRequest;p.open("HEAD",c,!1);p.send(null);if(!(200<=p.status&&300>p.status||304===p.status))throw Error("Couldn't load "+c+". Status: "+p.status);var t=Number(p.getResponseHeader("Content-length")),x,l=(x=p.getResponseHeader("Accept-Ranges"))&&"bytes"===x;p=(x=p.getResponseHeader("Content-Encoding"))&&"gzip"===x;var u=1048576;l||(u=t);var w=this;w.ac(C=>{var K=C*u,H=(C+1)*u-1;H=Math.min(H,t-1);if("undefined"==typeof w.cb[C]){var Ga= w.cb;if(K>H)throw Error("invalid range ("+K+", "+H+") or no bytes requested!");if(H>t-1)throw Error("only "+t+" bytes available! programmer error!");var I=new XMLHttpRequest;I.open("GET",c,!1);t!==u&&I.setRequestHeader("Range","bytes="+K+"-"+H);I.responseType="arraybuffer";I.overrideMimeType&&I.overrideMimeType("text/plain; charset=x-user-defined");I.send(null);if(!(200<=I.status&&300>I.status||304===I.status))throw Error("Couldn't load "+c+". Status: "+I.status);K=void 0!==I.response?new Uint8Array(I.response|| []):Wa(I.responseText||"",!0);Ga[C]=K}if("undefined"==typeof w.cb[C])throw Error("doXHR failed!");return w.cb[C]});if(p||!t)u=t=1,u=t=this.Sb(0).length,na("LazyFiles on gzip forces download of the whole file when length is accessed");this.bd=t;this.ad=u;this.nc=!0};if("undefined"!=typeof XMLHttpRequest){if(!fa)throw"Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc";var h=new g;Object.defineProperties(h,{length:{get:function(){this.nc|| this.Ac();return this.bd}},chunkSize:{get:function(){this.nc||this.Ac();return this.ad}}});h={lc:!1,Pa:h}}else h={lc:!1,url:c};var n=N.md(a,b,h,d,f);h.Pa?n.Pa=h.Pa:h.url&&(n.Pa=null,n.url=h.url);Object.defineProperties(n,{Va:{get:function(){return this.Pa.length}}});var q={};Object.keys(n.Qa).forEach(p=>{var t=n.Qa[p];q[p]=function(){N.ec(n);return t.apply(null,arguments)}});q.read=(p,t,x,l,u)=>{N.ec(n);return k(p,t,x,l,u)};q.yb=(p,t,x)=>{N.ec(n);var l=bb(t);if(!l)throw new N.Ma(48);k(p,B,l,t,x); return{Sa:l,zc:!0}};n.Qa=q;return n},Ec:(a,b,c,d,f,g,k,h,n,q)=>{function p(l){function u(w){q&&q();h||N.Rb(a,b,w,d,f,n);g&&g();Ja(x)}eb.de(l,t,u,()=>{k&&k();Ja(x)})||u(l)}var t=b?Ua(L(a+"/"+b)):a,x="cp "+t;Ia(x);"string"==typeof c?db(c,l=>p(l),k):p(c)},indexedDB:()=>window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,xc:()=>"EM_FS_"+window.location.pathname,yc:20,Hb:"FILE_DATA",le:(a,b,c)=>{b=b||(()=>{});c=c||(()=>{});var d=N.indexedDB();try{var f=d.open(N.xc(),N.yc)}catch(g){return c(g)}f.onupgradeneeded= ()=>{na("creating db");f.result.createObjectStore(N.Hb)};f.onsuccess=()=>{var g=f.result.transaction([N.Hb],"readwrite"),k=g.objectStore(N.Hb),h=0,n=0,q=a.length;a.forEach(p=>{p=k.put(N.bc(p).object.Pa,p);p.onsuccess=()=>{h++;h+n==q&&(0==n?b():c())};p.onerror=()=>{n++;h+n==q&&(0==n?b():c())}});g.onerror=c};f.onerror=c},ee:(a,b,c)=>{b=b||(()=>{});c=c||(()=>{});var d=N.indexedDB();try{var f=d.open(N.xc(),N.yc)}catch(g){return c(g)}f.onupgradeneeded=c;f.onsuccess=()=>{var g=f.result;try{var k=g.transaction([N.Hb], "readonly")}catch(t){c(t);return}var h=k.objectStore(N.Hb),n=0,q=0,p=a.length;a.forEach(t=>{var x=h.get(t);x.onsuccess=()=>{N.bc(t).exists&&N.unlink(t);N.Rb(Ra(t),M(t),x.result,!0,!0,!0);n++;n+q==p&&(0==q?b():c())};x.onerror=()=>{q++;n+q==p&&(0==q?b():c())}});k.onerror=c};f.onerror=c}};function P(a,b,c){if("/"===b.charAt(0))return b;if(-100===a)a=N.cwd();else{a=N.rb(a);if(!a)throw new N.Ma(8);a=a.path}if(0==b.length){if(!c)throw new N.Ma(44);return a}return L(a+"/"+b)} function fb(a,b,c){try{var d=a(b)}catch(f){if(f&&f.node&&L(b)!==L(N.qb(f.node)))return-54;throw f;}E[c>>2]=d.dev;E[c+4>>2]=0;E[c+8>>2]=d.ino;E[c+12>>2]=d.mode;E[c+16>>2]=d.nlink;E[c+20>>2]=d.uid;E[c+24>>2]=d.gid;E[c+28>>2]=d.rdev;E[c+32>>2]=0;J=[d.size>>>0,(G=d.size,1<=+Math.abs(G)?0>>0:~~+Math.ceil((G-+(~~G>>>0))/4294967296)>>>0:0)];E[c+40>>2]=J[0];E[c+44>>2]=J[1];E[c+48>>2]=4096;E[c+52>>2]=d.blocks;J=[Math.floor(d.atime.getTime()/1E3)>>>0,(G= Math.floor(d.atime.getTime()/1E3),1<=+Math.abs(G)?0>>0:~~+Math.ceil((G-+(~~G>>>0))/4294967296)>>>0:0)];E[c+56>>2]=J[0];E[c+60>>2]=J[1];E[c+64>>2]=0;J=[Math.floor(d.mtime.getTime()/1E3)>>>0,(G=Math.floor(d.mtime.getTime()/1E3),1<=+Math.abs(G)?0>>0:~~+Math.ceil((G-+(~~G>>>0))/4294967296)>>>0:0)];E[c+72>>2]=J[0];E[c+76>>2]=J[1];E[c+80>>2]=0;J=[Math.floor(d.ctime.getTime()/1E3)>>>0,(G=Math.floor(d.ctime.getTime()/ 1E3),1<=+Math.abs(G)?0>>0:~~+Math.ceil((G-+(~~G>>>0))/4294967296)>>>0:0)];E[c+88>>2]=J[0];E[c+92>>2]=J[1];E[c+96>>2]=0;J=[d.ino>>>0,(G=d.ino,1<=+Math.abs(G)?0>>0:~~+Math.ceil((G-+(~~G>>>0))/4294967296)>>>0:0)];E[c+104>>2]=J[0];E[c+108>>2]=J[1];return 0}var gb=void 0;function hb(){gb+=4;return E[gb-4>>2]}function Q(a){a=N.rb(a);if(!a)throw new N.Ma(8);return a} function ib(a){return F[a>>2]+4294967296*E[a+4>>2]}var jb={};function kb(a){for(;a.length;){var b=a.pop();a.pop()(b)}}function lb(a){return this.fromWireType(E[a>>2])}var mb={},nb={},ob={};function pb(a){if(void 0===a)return"_unknown";a=a.replace(/[^a-zA-Z0-9_]/g,"$");var b=a.charCodeAt(0);return 48<=b&&57>=b?"_"+a:a}function qb(a,b){a=pb(a);return(new Function("body","return function "+a+'() {\n "use strict"; return body.apply(this, arguments);\n};\n'))(b)} function rb(a){var b=Error,c=qb(a,function(d){this.name=a;this.message=d;d=Error(d).stack;void 0!==d&&(this.stack=this.toString()+"\n"+d.replace(/^Error(:[^\n]*)?\n/,""))});c.prototype=Object.create(b.prototype);c.prototype.constructor=c;c.prototype.toString=function(){return void 0===this.message?this.name:this.name+": "+this.message};return c}var sb=void 0;function tb(a){throw new sb(a);} function ub(a,b,c){function d(h){h=c(h);h.length!==a.length&&tb("Mismatched type converter count");for(var n=0;n{nb.hasOwnProperty(h)?f[n]=nb[h]:(g.push(h),mb.hasOwnProperty(h)||(mb[h]=[]),mb[h].push(()=>{f[n]=nb[h];++k;k===g.length&&d(f)}))});0===g.length&&d(f)} function vb(a){switch(a){case 1:return 0;case 2:return 1;case 4:return 2;case 8:return 3;default:throw new TypeError("Unknown type size: "+a);}}var wb=void 0;function S(a){for(var b="";z[a];)b+=wb[z[a++]];return b}var xb=void 0;function T(a){throw new xb(a);} function R(a,b,c={}){if(!("argPackAdvance"in b))throw new TypeError("registerType registeredInstance requires argPackAdvance");var d=b.name;a||T('type "'+d+'" must have a positive integer typeid pointer');if(nb.hasOwnProperty(a)){if(c.Ad)return;T("Cannot register type '"+d+"' twice")}nb[a]=b;delete ob[a];mb.hasOwnProperty(a)&&(b=mb[a],delete mb[a],b.forEach(f=>f()))}function yb(a){T(a.Na.Xa.Ta.name+" instance already deleted")}var zb=!1;function Ab(){} function Bb(a){--a.count.value;0===a.count.value&&(a.ab?a.fb.pb(a.ab):a.Xa.Ta.pb(a.Sa))}function Cb(a,b,c){if(b===c)return a;if(void 0===c.gb)return null;a=Cb(a,b,c.gb);return null===a?null:c.pd(a)}var Db={},Eb=[];function Fb(){for(;Eb.length;){var a=Eb.pop();a.Na.Cb=!1;a["delete"]()}}var Gb=void 0,Hb={};function Ib(a,b){for(void 0===b&&T("ptr should not be undefined");a.gb;)b=a.Ob(b),a=a.gb;return Hb[b]} function Jb(a,b){b.Xa&&b.Sa||tb("makeClassHandle requires ptr and ptrType");!!b.fb!==!!b.ab&&tb("Both smartPtrType and smartPtr must be specified");b.count={value:1};return Kb(Object.create(a,{Na:{value:b}}))}function Kb(a){if("undefined"===typeof FinalizationRegistry)return Kb=b=>b,a;zb=new FinalizationRegistry(b=>{Bb(b.Na)});Kb=b=>{var c=b.Na;c.ab&&zb.register(b,{Na:c},b);return b};Ab=b=>{zb.unregister(b)};return Kb(a)}function U(){} function Lb(a,b,c){if(void 0===a[b].$a){var d=a[b];a[b]=function(){a[b].$a.hasOwnProperty(arguments.length)||T("Function '"+c+"' called with an invalid number of arguments ("+arguments.length+") - expects one of ("+a[b].$a+")!");return a[b].$a[arguments.length].apply(this,arguments)};a[b].$a=[];a[b].$a[d.Qb]=d}} function Mb(a,b,c){e.hasOwnProperty(a)?((void 0===c||void 0!==e[a].$a&&void 0!==e[a].$a[c])&&T("Cannot register public name '"+a+"' twice"),Lb(e,a,a),e.hasOwnProperty(c)&&T("Cannot register multiple overloads of a function with the same number of arguments ("+c+")!"),e[a].$a[c]=b):(e[a]=b,void 0!==c&&(e[a].ie=c))}function Nb(a,b,c,d,f,g,k,h){this.name=a;this.constructor=b;this.Eb=c;this.pb=d;this.gb=f;this.vd=g;this.Ob=k;this.pd=h;this.Nd=[]} function Ob(a,b,c){for(;b!==c;)b.Ob||T("Expected null or instance of "+c.name+", got an instance of "+b.name),a=b.Ob(a),b=b.gb;return a}function Pb(a,b){if(null===b)return this.mc&&T("null is not a valid "+this.name),0;b.Na||T('Cannot pass "'+Qb(b)+'" as a '+this.name);b.Na.Sa||T("Cannot pass deleted object as a pointer of type "+this.name);return Ob(b.Na.Sa,b.Na.Xa.Ta,this.Ta)} function Rb(a,b){if(null===b){this.mc&&T("null is not a valid "+this.name);if(this.Vb){var c=this.rc();null!==a&&a.push(this.pb,c);return c}return 0}b.Na||T('Cannot pass "'+Qb(b)+'" as a '+this.name);b.Na.Sa||T("Cannot pass deleted object as a pointer of type "+this.name);!this.Tb&&b.Na.Xa.Tb&&T("Cannot convert argument of type "+(b.Na.fb?b.Na.fb.name:b.Na.Xa.name)+" to parameter type "+this.name);c=Ob(b.Na.Sa,b.Na.Xa.Ta,this.Ta);if(this.Vb)switch(void 0===b.Na.ab&&T("Passing raw pointer to smart pointer is illegal"), this.Ud){case 0:b.Na.fb===this?c=b.Na.ab:T("Cannot convert argument of type "+(b.Na.fb?b.Na.fb.name:b.Na.Xa.name)+" to parameter type "+this.name);break;case 1:c=b.Na.ab;break;case 2:if(b.Na.fb===this)c=b.Na.ab;else{var d=b.clone();c=this.Od(c,Sb(function(){d["delete"]()}));null!==a&&a.push(this.pb,c)}break;default:T("Unsupporting sharing policy")}return c} function Tb(a,b){if(null===b)return this.mc&&T("null is not a valid "+this.name),0;b.Na||T('Cannot pass "'+Qb(b)+'" as a '+this.name);b.Na.Sa||T("Cannot pass deleted object as a pointer of type "+this.name);b.Na.Xa.Tb&&T("Cannot convert argument of type "+b.Na.Xa.name+" to parameter type "+this.name);return Ob(b.Na.Sa,b.Na.Xa.Ta,this.Ta)} function V(a,b,c,d){this.name=a;this.Ta=b;this.mc=c;this.Tb=d;this.Vb=!1;this.pb=this.Od=this.rc=this.Tc=this.Ud=this.Md=void 0;void 0!==b.gb?this.toWireType=Rb:(this.toWireType=d?Pb:Tb,this.ib=null)}function Ub(a,b,c){e.hasOwnProperty(a)||tb("Replacing nonexistant public symbol");void 0!==e[a].$a&&void 0!==c?e[a].$a[c]=b:(e[a]=b,e[a].Qb=c)} function Vb(a,b){var c=[];return function(){c.length=0;Object.assign(c,arguments);if(a.includes("j")){var d=e["dynCall_"+a];d=c&&c.length?d.apply(null,[b].concat(c)):d.call(null,b)}else d=za.get(b).apply(null,c);return d}}function W(a,b){a=S(a);var c=a.includes("j")?Vb(a,b):za.get(b);"function"!=typeof c&&T("unknown function pointer with signature "+a+": "+b);return c}var Wb=void 0;function Xb(a){a=Yb(a);var b=S(a);X(a);return b} function Zb(a,b){function c(g){f[g]||nb[g]||(ob[g]?ob[g].forEach(c):(d.push(g),f[g]=!0))}var d=[],f={};b.forEach(c);throw new Wb(a+": "+d.map(Xb).join([", "]));}function $b(a,b){for(var c=[],d=0;d>2]);return c} function ac(a){var b=Function;if(!(b instanceof Function))throw new TypeError("new_ called with constructor type "+typeof b+" which is not a function");var c=qb(b.name||"unknownFunctionName",function(){});c.prototype=b.prototype;c=new c;a=b.apply(c,a);return a instanceof Object?a:c} function bc(a,b,c,d,f){var g=b.length;2>g&&T("argTypes array size mismatch! Must at least get return value and 'this' types!");var k=null!==b[1]&&null!==c,h=!1;for(c=1;c{a||T("Cannot use deleted val. handle = "+a);return Y[a].value},Sb=a=>{switch(a){case void 0:return 1;case null:return 2;case !0:return 3;case !1:return 4;default:var b=cc.length?cc.pop():Y.length;Y[b]={uc:1,value:a};return b}};function Qb(a){if(null===a)return"null";var b=typeof a;return"object"===b||"array"===b||"function"===b?a.toString():""+a} function fc(a,b){switch(b){case 2:return function(c){return this.fromWireType(wa[c>>2])};case 3:return function(c){return this.fromWireType(xa[c>>3])};default:throw new TypeError("Unknown float type: "+a);}} function gc(a,b,c){switch(b){case 0:return c?function(d){return B[d]}:function(d){return z[d]};case 1:return c?function(d){return D[d>>1]}:function(d){return va[d>>1]};case 2:return c?function(d){return E[d>>2]}:function(d){return F[d>>2]};default:throw new TypeError("Unknown integer type: "+a);}}var hc="undefined"!=typeof TextDecoder?new TextDecoder("utf-16le"):void 0; function ic(a,b){var c=a>>1;for(var d=c+b/2;!(c>=d)&&va[c];)++c;c<<=1;if(32=b/2);++d){var f=D[a+2*d>>1];if(0==f)break;c+=String.fromCharCode(f)}return c}function jc(a,b,c){void 0===c&&(c=2147483647);if(2>c)return 0;c-=2;var d=b;c=c<2*a.length?c/2:a.length;for(var f=0;f>1]=a.charCodeAt(f),b+=2;D[b>>1]=0;return b-d}function kc(a){return 2*a.length} function lc(a,b){for(var c=0,d="";!(c>=b/4);){var f=E[a+4*c>>2];if(0==f)break;++c;65536<=f?(f-=65536,d+=String.fromCharCode(55296|f>>10,56320|f&1023)):d+=String.fromCharCode(f)}return d}function mc(a,b,c){void 0===c&&(c=2147483647);if(4>c)return 0;var d=b;c=d+c-4;for(var f=0;f=g){var k=a.charCodeAt(++f);g=65536+((g&1023)<<10)|k&1023}E[b>>2]=g;b+=4;if(b+4>c)break}E[b>>2]=0;return b-d} function nc(a){for(var b=0,c=0;c=d&&++c;b+=4}return b}function oc(a,b){var c=nb[a];void 0===c&&T(b+" has unknown type "+Xb(a));return c}var pc={};function qc(a){var b=pc[a];return void 0===b?S(a):b}var rc=[];function sc(a){var b=rc.length;rc.push(a);return b}function tc(a,b){for(var c=Array(a),d=0;d>2],"parameter "+d);return c}var uc=[];function vc(a){var b=ta(a)+1,c=wc(b);c&&A(a,B,c,b);return c} function xc(a,b,c){function d(n){return(n=n.toTimeString().match(/\(([A-Za-z ]+)\)$/))?n[1]:"GMT"}var f=(new Date).getFullYear(),g=new Date(f,0,1),k=new Date(f,6,1);f=g.getTimezoneOffset();var h=k.getTimezoneOffset();E[a>>2]=60*Math.max(f,h);E[b>>2]=Number(f!=h);a=d(g);b=d(k);a=vc(a);b=vc(b);h>2]=a,F[c+4>>2]=b):(F[c>>2]=b,F[c+4>>2]=a)}function yc(a,b,c){yc.dd||(yc.dd=!0,xc(a,b,c))}var zc;zc=ha?()=>{var a=process.hrtime();return 1E3*a[0]+a[1]/1E6}:()=>performance.now();var Ac={}; function Bc(){if(!Cc){var a={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:("object"==typeof navigator&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:da||"./this.program"},b;for(b in Ac)void 0===Ac[b]?delete a[b]:a[b]=Ac[b];var c=[];for(b in a)c.push(b+"="+a[b]);Cc=c}return Cc}var Cc;function Dc(a){return 0===a%4&&(0!==a%100||0===a%400)}var Ec=[31,29,31,30,31,30,31,31,30,31,30,31],Fc=[31,28,31,30,31,30,31,31,30,31,30,31]; function Gc(a,b,c,d){function f(l,u,w){for(l="number"==typeof l?l.toString():l||"";l.lengthK?-1:0C-l.getDate())u-=C-l.getDate()+1,l.setDate(1),11>w?l.setMonth(w+1):(l.setMonth(0),l.setFullYear(l.getFullYear()+1));else{l.setDate(l.getDate()+u);break}}w=new Date(l.getFullYear()+1,0,4);u=h(new Date(l.getFullYear(), 0,4));w=h(w);return 0>=k(u,l)?0>=k(w,l)?l.getFullYear()+1:l.getFullYear():l.getFullYear()-1}var q=E[d+40>>2];d={Yd:E[d>>2],Xd:E[d+4>>2],Zb:E[d+8>>2],wc:E[d+12>>2],$b:E[d+16>>2],Bb:E[d+20>>2],nb:E[d+24>>2],Ab:E[d+28>>2],me:E[d+32>>2],Wd:E[d+36>>2],Zd:q?y(q):""};c=y(c);q={"%c":"%a %b %d %H:%M:%S %Y","%D":"%m/%d/%y","%F":"%Y-%m-%d","%h":"%b","%r":"%I:%M:%S %p","%R":"%H:%M","%T":"%H:%M:%S","%x":"%m/%d/%y","%X":"%H:%M:%S","%Ec":"%c","%EC":"%C","%Ex":"%m/%d/%y","%EX":"%H:%M:%S","%Ey":"%y","%EY":"%Y","%Od":"%d", "%Oe":"%e","%OH":"%H","%OI":"%I","%Om":"%m","%OM":"%M","%OS":"%S","%Ou":"%u","%OU":"%U","%OV":"%V","%Ow":"%w","%OW":"%W","%Oy":"%y"};for(var p in q)c=c.replace(new RegExp(p,"g"),q[p]);var t="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),x="January February March April May June July August September October November December".split(" ");q={"%a":function(l){return t[l.nb].substring(0,3)},"%A":function(l){return t[l.nb]},"%b":function(l){return x[l.$b].substring(0,3)},"%B":function(l){return x[l.$b]}, "%C":function(l){return g((l.Bb+1900)/100|0,2)},"%d":function(l){return g(l.wc,2)},"%e":function(l){return f(l.wc,2," ")},"%g":function(l){return n(l).toString().substring(2)},"%G":function(l){return n(l)},"%H":function(l){return g(l.Zb,2)},"%I":function(l){l=l.Zb;0==l?l=12:12l.Zb?"AM":"PM"},"%S":function(l){return g(l.Yd,2)},"%t":function(){return"\t"},"%u":function(l){return l.nb||7},"%U":function(l){return g(Math.floor((l.Ab+7-l.nb)/7),2)},"%V":function(l){var u=Math.floor((l.Ab+7-(l.nb+6)%7)/7);2>=(l.nb+371-l.Ab-2)%7&&u++;if(u)53==u&&(w=(l.nb+371-l.Ab)%7,4==w||3==w&&Dc(l.Bb)||(u=1));else{u=52;var w=(l.nb+7-l.Ab-1)%7;(4==w||5==w&&Dc(l.Bb%400-1))&&u++}return g(u,2)},"%w":function(l){return l.nb},"%W":function(l){return g(Math.floor((l.Ab+7-(l.nb+6)%7)/7),2)}, "%y":function(l){return(l.Bb+1900).toString().substring(2)},"%Y":function(l){return l.Bb+1900},"%z":function(l){l=l.Wd;var u=0<=l;l=Math.abs(l)/60;return(u?"+":"-")+String("0000"+(l/60*100+l%60)).slice(-4)},"%Z":function(l){return l.Zd},"%%":function(){return"%"}};c=c.replace(/%%/g,"\x00\x00");for(p in q)c.includes(p)&&(c=c.replace(new RegExp(p,"g"),q[p](d)));c=c.replace(/\0\0/g,"%");p=Wa(c,!1);if(p.length>b)return 0;B.set(p,a);return p.length-1} function Hc(a,b,c,d){a||(a=this);this.parent=a;this.Wa=a.Wa;this.Lb=null;this.id=N.Hd++;this.name=b;this.mode=c;this.Oa={};this.Qa={};this.rdev=d}Object.defineProperties(Hc.prototype,{read:{get:function(){return 365===(this.mode&365)},set:function(a){a?this.mode|=365:this.mode&=-366}},write:{get:function(){return 146===(this.mode&146)},set:function(a){a?this.mode|=146:this.mode&=-147}},Cd:{get:function(){return N.Ya(this.mode)}},lc:{get:function(){return N.Jb(this.mode)}}});N.Vc=Hc;N.Vd();var eb; e.FS_createPath=N.Dc;e.FS_createDataFile=N.Rb;e.FS_createPreloadedFile=N.Ec;e.FS_unlink=N.unlink;e.FS_createLazyFile=N.Cc;e.FS_createDevice=N.hb;sb=e.InternalError=rb("InternalError");for(var Ic=Array(256),Jc=0;256>Jc;++Jc)Ic[Jc]=String.fromCharCode(Jc);wb=Ic;xb=e.BindingError=rb("BindingError"); U.prototype.isAliasOf=function(a){if(!(this instanceof U&&a instanceof U))return!1;var b=this.Na.Xa.Ta,c=this.Na.Sa,d=a.Na.Xa.Ta;for(a=a.Na.Sa;b.gb;)c=b.Ob(c),b=b.gb;for(;d.gb;)a=d.Ob(a),d=d.gb;return b===d&&c===a}; U.prototype.clone=function(){this.Na.Sa||yb(this);if(this.Na.Nb)return this.Na.count.value+=1,this;var a=Kb,b=Object,c=b.create,d=Object.getPrototypeOf(this),f=this.Na;a=a(c.call(b,d,{Na:{value:{count:f.count,Cb:f.Cb,Nb:f.Nb,Sa:f.Sa,Xa:f.Xa,ab:f.ab,fb:f.fb}}}));a.Na.count.value+=1;a.Na.Cb=!1;return a};U.prototype["delete"]=function(){this.Na.Sa||yb(this);this.Na.Cb&&!this.Na.Nb&&T("Object already scheduled for deletion");Ab(this);Bb(this.Na);this.Na.Nb||(this.Na.ab=void 0,this.Na.Sa=void 0)}; U.prototype.isDeleted=function(){return!this.Na.Sa};U.prototype.deleteLater=function(){this.Na.Sa||yb(this);this.Na.Cb&&!this.Na.Nb&&T("Object already scheduled for deletion");Eb.push(this);1===Eb.length&&Gb&&Gb(Fb);this.Na.Cb=!0;return this};e.getInheritedInstanceCount=function(){return Object.keys(Hb).length};e.getLiveInheritedInstances=function(){var a=[],b;for(b in Hb)Hb.hasOwnProperty(b)&&a.push(Hb[b]);return a};e.flushPendingDeletes=Fb;e.setDelayFunction=function(a){Gb=a;Eb.length&&Gb&&Gb(Fb)}; V.prototype.xd=function(a){this.Tc&&(a=this.Tc(a));return a};V.prototype.Hc=function(a){this.pb&&this.pb(a)};V.prototype.argPackAdvance=8;V.prototype.readValueFromPointer=lb;V.prototype.deleteObject=function(a){if(null!==a)a["delete"]()}; V.prototype.fromWireType=function(a){function b(){return this.Vb?Jb(this.Ta.Eb,{Xa:this.Md,Sa:c,fb:this,ab:a}):Jb(this.Ta.Eb,{Xa:this,Sa:a})}var c=this.xd(a);if(!c)return this.Hc(a),null;var d=Ib(this.Ta,c);if(void 0!==d){if(0===d.Na.count.value)return d.Na.Sa=c,d.Na.ab=a,d.clone();d=d.clone();this.Hc(a);return d}d=this.Ta.vd(c);d=Db[d];if(!d)return b.call(this);d=this.Tb?d.jd:d.pointerType;var f=Cb(c,this.Ta,d.Ta);return null===f?b.call(this):this.Vb?Jb(d.Ta.Eb,{Xa:d,Sa:f,fb:this,ab:a}):Jb(d.Ta.Eb, {Xa:d,Sa:f})};Wb=e.UnboundTypeError=rb("UnboundTypeError");e.count_emval_handles=function(){for(var a=0,b=5;bf?-28:N.Fc(d,f).fd;case 1:case 2:return 0;case 3:return d.flags;case 4:return f=hb(),d.flags|=f,0;case 5:return f=hb(),D[f+0>>1]=2,0;case 6:case 7:return 0;case 16:case 8:return-28;case 9:return E[Kc()>>2]=28,-1;default:return-28}}catch(g){if("undefined"== typeof N||!(g instanceof N.Ma))throw g;return-g.Ra}},X:function(a,b){try{var c=Q(a);return fb(N.stat,c.path,b)}catch(d){if("undefined"==typeof N||!(d instanceof N.Ma))throw d;return-d.Ra}},E:function(a,b,c){try{b=c+2097152>>>0<4194305-!!b?(b>>>0)+4294967296*c:NaN;if(isNaN(b))return-61;N.ud(a,b);return 0}catch(d){if("undefined"==typeof N||!(d instanceof N.Ma))throw d;return-d.Ra}},S:function(a,b){try{if(0===b)return-28;var c=N.cwd(),d=ta(c)+1;if(b>>0,(G=h,1<=+Math.abs(G)?0>>0:~~+Math.ceil((G-+(~~G>>>0))/4294967296)>>> 0:0)];E[b+a>>2]=J[0];E[b+a+4>>2]=J[1];J=[280*(g+1)>>>0,(G=280*(g+1),1<=+Math.abs(G)?0>>0:~~+Math.ceil((G-+(~~G>>>0))/4294967296)>>>0:0)];E[b+a+8>>2]=J[0];E[b+a+12>>2]=J[1];D[b+a+16>>1]=280;B[b+a+18>>0]=n;A(k,z,b+a+19,256);a+=280;g+=1}N.bb(d,280*g,0);return a}catch(p){if("undefined"==typeof N||!(p instanceof N.Ma))throw p;return-p.Ra}},Y:function(a,b,c){gb=c;try{var d=Q(a);switch(b){case 21509:case 21505:return d.tty?0:-59;case 21510:case 21511:case 21512:case 21506:case 21507:case 21508:return d.tty? 0:-59;case 21519:if(!d.tty)return-59;var f=hb();return E[f>>2]=0;case 21520:return d.tty?-28:-59;case 21531:return f=hb(),N.kc(d,b,f);case 21523:return d.tty?0:-59;case 21524:return d.tty?0:-59;default:v("bad ioctl syscall "+b)}}catch(g){if("undefined"==typeof N||!(g instanceof N.Ma))throw g;return-g.Ra}},V:function(a,b){try{return a=y(a),fb(N.lstat,a,b)}catch(c){if("undefined"==typeof N||!(c instanceof N.Ma))throw c;return-c.Ra}},P:function(a,b,c){try{return b=y(b),b=P(a,b),b=L(b),"/"===b[b.length- 1]&&(b=b.substr(0,b.length-1)),N.mkdir(b,c,0),0}catch(d){if("undefined"==typeof N||!(d instanceof N.Ma))throw d;return-d.Ra}},U:function(a,b,c,d){try{b=y(b);var f=d&256;b=P(a,b,d&4096);return fb(f?N.lstat:N.stat,b,c)}catch(g){if("undefined"==typeof N||!(g instanceof N.Ma))throw g;return-g.Ra}},v:function(a,b,c,d){gb=d;try{b=y(b);b=P(a,b);var f=d?hb():0;return N.open(b,c,f).fd}catch(g){if("undefined"==typeof N||!(g instanceof N.Ma))throw g;return-g.Ra}},L:function(a,b,c,d){try{b=y(b);b=P(a,b);if(0>= d)return-28;var f=N.readlink(b),g=Math.min(d,ta(f)),k=B[c+g];A(f,z,c,d+1);B[c+g]=k;return g}catch(h){if("undefined"==typeof N||!(h instanceof N.Ma))throw h;return-h.Ra}},K:function(a,b,c,d){try{return b=y(b),d=y(d),b=P(a,b),d=P(c,d),N.rename(b,d),0}catch(f){if("undefined"==typeof N||!(f instanceof N.Ma))throw f;return-f.Ra}},r:function(a){try{return a=y(a),N.rmdir(a),0}catch(b){if("undefined"==typeof N||!(b instanceof N.Ma))throw b;return-b.Ra}},W:function(a,b){try{return a=y(a),fb(N.stat,a,b)}catch(c){if("undefined"== typeof N||!(c instanceof N.Ma))throw c;return-c.Ra}},s:function(a,b,c){try{return b=y(b),b=P(a,b),0===c?N.unlink(b):512===c?N.rmdir(b):v("Invalid flags passed to unlinkat"),0}catch(d){if("undefined"==typeof N||!(d instanceof N.Ma))throw d;return-d.Ra}},I:function(a,b,c){try{b=y(b);b=P(a,b,!0);if(c){var d=ib(c),f=E[c+8>>2];g=1E3*d+f/1E6;c+=16;d=ib(c);f=E[c+8>>2];k=1E3*d+f/1E6}else var g=Date.now(),k=g;N.ae(b,g,k);return 0}catch(h){if("undefined"==typeof N||!(h instanceof N.Ma))throw h;return-h.Ra}}, f:function(a){var b=jb[a];delete jb[a];var c=b.rc,d=b.pb,f=b.Lc,g=f.map(k=>k.zd).concat(f.map(k=>k.Sd));ub([a],g,k=>{var h={};f.forEach((n,q)=>{var p=k[q],t=n.Sb,x=n.yd,l=k[q+f.length],u=n.Rd,w=n.Td;h[n.rd]={read:C=>p.fromWireType(t(x,C)),write:(C,K)=>{var H=[];u(w,C,l.toWireType(H,K));kb(H)}}});return[{name:b.name,fromWireType:function(n){var q={},p;for(p in h)q[p]=h[p].read(n);d(n);return q},toWireType:function(n,q){for(var p in h)if(!(p in q))throw new TypeError('Missing field: "'+p+'"');var t= c();for(p in h)h[p].write(t,q[p]);null!==n&&n.push(d,t);return t},argPackAdvance:8,readValueFromPointer:lb,ib:d}]})},F:function(){},fa:function(a,b,c,d,f){var g=vb(c);b=S(b);R(a,{name:b,fromWireType:function(k){return!!k},toWireType:function(k,h){return h?d:f},argPackAdvance:8,readValueFromPointer:function(k){if(1===c)var h=B;else if(2===c)h=D;else if(4===c)h=E;else throw new TypeError("Unknown boolean type size: "+b);return this.fromWireType(h[k>>g])},ib:null})},ka:function(a,b,c,d,f,g,k,h,n,q,p, t,x){p=S(p);g=W(f,g);h&&(h=W(k,h));q&&(q=W(n,q));x=W(t,x);var l=pb(p);Mb(l,function(){Zb("Cannot construct "+p+" due to unbound types",[d])});ub([a,b,c],d?[d]:[],function(u){u=u[0];if(d){var w=u.Ta;var C=w.Eb}else C=U.prototype;u=qb(l,function(){if(Object.getPrototypeOf(this)!==K)throw new xb("Use 'new' to construct "+p);if(void 0===H.tb)throw new xb(p+" has no accessible constructor");var I=H.tb[arguments.length];if(void 0===I)throw new xb("Tried to invoke ctor of "+p+" with invalid number of parameters ("+ arguments.length+") - expected ("+Object.keys(H.tb).toString()+") parameters instead!");return I.apply(this,arguments)});var K=Object.create(C,{constructor:{value:u}});u.prototype=K;var H=new Nb(p,u,K,x,w,g,h,q);w=new V(p,H,!0,!1);C=new V(p+"*",H,!1,!1);var Ga=new V(p+" const*",H,!1,!0);Db[a]={pointerType:C,jd:Ga};Ub(l,u);return[w,C,Ga]})},ja:function(a,b,c,d,f,g){0{Zb("Cannot construct "+h.name+" due to unbound types",k)};ub([],k,function(q){q.splice(1,0,null);h.Ta.tb[b-1]=bc(n,q,null,f,g);return[]});return[]})},a:function(a,b,c,d,f,g,k,h){var n=$b(c,d);b=S(b);g=W(f,g);ub([],[a],function(q){function p(){Zb("Cannot call "+ +1])throw new xb("Cannot register multiple constructors with identical number of parameters ("+(b-1)+") for class '"+h.name+"'! Overload resolution is currently only performed using the parameter count, not actual type info!");h.Ta.tb[b-1]=()=>{Zb("Cannot construct "+h.name+" due to unbound types",k)};ub([],k,function(q){q.splice(1,0,null);h.Ta.tb[b-1]=bc(n,q,null,f,g);return[]});return[]})},b:function(a,b,c,d,f,g,k,h){var n=$b(c,d);b=S(b);g=W(f,g);ub([],[a],function(q){function p(){Zb("Cannot call "+ t+" due to unbound types",n)}q=q[0];var t=q.name+"."+b;b.startsWith("@@")&&(b=Symbol[b.substring(2)]);h&&q.Ta.Nd.push(b);var x=q.Ta.Eb,l=x[b];void 0===l||void 0===l.$a&&l.className!==q.name&&l.Qb===c-2?(p.Qb=c-2,p.className=q.name,x[b]=p):(Lb(x,b,t),x[b].$a[c-2]=p);ub([],n,function(u){u=bc(t,u,q,g,k);void 0===x[b].$a?(u.Qb=c-2,x[b]=u):x[b].$a[c-2]=u;return[]});return[]})},ea:function(a,b){b=S(b);R(a,{name:b,fromWireType:function(c){var d=ec(c);dc(c);return d},toWireType:function(c,d){return Sb(d)}, argPackAdvance:8,readValueFromPointer:lb,ib:null})},x:function(a,b,c){c=vb(c);b=S(b);R(a,{name:b,fromWireType:function(d){return d},toWireType:function(d,f){return f},argPackAdvance:8,readValueFromPointer:fc(b,c),ib:null})},ha:function(a,b,c,d,f,g){var k=$b(b,c);a=S(a);f=W(d,f);Mb(a,function(){Zb("Cannot call "+a+" due to unbound types",k)},b-1);ub([],k,function(h){h=[h[0],null].concat(h.slice(1));Ub(a,bc(a,h,null,f,g),b-1);return[]})},i:function(a,b,c,d,f){b=S(b);-1===f&&(f=4294967295);f=vb(c);var g= h=>h;if(0===d){var k=32-8*c;g=h=>h<>>k}c=b.includes("unsigned")?function(h,n){return n>>>0}:function(h,n){return n};R(a,{name:b,fromWireType:g,toWireType:c,argPackAdvance:8,readValueFromPointer:gc(b,f,0!==d),ib:null})},e:function(a,b,c){function d(g){g>>=2;var k=F;return new f(ua,k[g+1],k[g])}var f=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array][b];c=S(c);R(a,{name:c,fromWireType:d,argPackAdvance:8,readValueFromPointer:d},{Ad:!0})},y:function(a,b){b= S(b);var c="std::string"===b;R(a,{name:b,fromWireType:function(d){var f=F[d>>2],g=d+4;if(c)for(var k=g,h=0;h<=f;++h){var n=g+h;if(h==f||0==z[n]){k=y(k,n-k);if(void 0===q)var q=k;else q+=String.fromCharCode(0),q+=k;k=n+1}}else{q=Array(f);for(h=0;h>2]=k;if(c&&g)A(f,z,n,k+1);else if(g)for(g=0;gva;var h=1}else 4===b&&(d=lc,f=mc,g=nc,k=()=>F,h=2);R(a,{name:c,fromWireType:function(n){for(var q= F[n>>2],p=k(),t,x=n+4,l=0;l<=q;++l){var u=n+4+l*b;if(l==q||0==p[u>>h])x=d(x,u-x),void 0===t?t=x:(t+=String.fromCharCode(0),t+=x),x=u+b}X(n);return t},toWireType:function(n,q){"string"!=typeof q&&T("Cannot pass non-string to C++ string type "+c);var p=g(q),t=wc(4+p+b);F[t>>2]=p>>h;f(q,t+4,p+b);null!==n&&n.push(X,t);return t},argPackAdvance:8,readValueFromPointer:lb,ib:function(n){X(n)}})},g:function(a,b,c,d,f,g){jb[a]={name:S(b),rc:W(c,d),pb:W(f,g),Lc:[]}},d:function(a,b,c,d,f,g,k,h,n,q){jb[a].Lc.push({rd:S(b), zd:c,Sb:W(d,f),yd:g,Sd:k,Rd:W(h,n),Td:q})},ga:function(a,b){b=S(b);R(a,{Dd:!0,name:b,argPackAdvance:0,fromWireType:function(){},toWireType:function(){}})},o:function(){return Date.now()},aa:function(){return!0},k:function(a,b,c){a=ec(a);b=oc(b,"emval::as");var d=[],f=Sb(d);F[c>>2]=f;return b.toWireType(d,a)},A:function(a,b,c,d){a=rc[a];b=ec(b);c=qc(c);a(b,c,null,d)},na:dc,oa:function(a,b){var c=tc(a,b),d=c[0];b=d.name+"_$"+c.slice(1).map(function(p){return p.name}).join("_")+"$";var f=uc[b];if(void 0!== f)return f;f=["retType"];for(var g=[d],k="",h=0;h>2]=a.getSeconds();E[b+4>>2]=a.getMinutes();E[b+8>>2]=a.getHours();E[b+12>>2]=a.getDate();E[b+16>>2]=a.getMonth();E[b+20>>2]=a.getFullYear()- 1900;E[b+24>>2]=a.getDay();var c=new Date(a.getFullYear(),0,1);E[b+28>>2]=(a.getTime()-c.getTime())/864E5|0;E[b+36>>2]=-(60*a.getTimezoneOffset());var d=(new Date(a.getFullYear(),6,1)).getTimezoneOffset();c=c.getTimezoneOffset();E[b+32>>2]=(d!=c&&a.getTimezoneOffset()==Math.min(c,d))|0},N:function(a,b,c,d,f,g){try{var k=N.rb(d);if(!k)return-8;var h=N.yb(k,a,f,b,c),n=h.Sa;E[g>>2]=h.zc;return n}catch(q){if("undefined"==typeof N||!(q instanceof N.Ma))throw q;return-q.Ra}},O:function(a,b,c,d,f,g){try{var k= N.rb(f);if(k&&c&2){var h=z.slice(a,a+b);N.Fb(k,h,g,b,d)}}catch(n){if("undefined"==typeof N||!(n instanceof N.Ma))throw n;return-n.Ra}},ca:yc,m:function(){v("")},J:function(){return 2147483648},w:zc,n:function(a){var b=z.length;a>>>=0;if(2147483648=c;c*=2){var d=b*(1+.2/c);d=Math.min(d,a+100663296);var f=Math;d=Math.max(a,d);f=f.min.call(f,2147483648,d+(65536-d%65536)%65536);a:{try{pa.grow(f-ua.byteLength+65535>>>16);ya();var g=1;break a}catch(k){}g=void 0}if(g)return!0}return!1}, Q:function(a,b){var c=0;Bc().forEach(function(d,f){var g=b+c;f=F[a+4*f>>2]=g;for(g=0;g>0]=d.charCodeAt(g);B[f>>0]=0;c+=d.length+1});return 0},R:function(a,b){var c=Bc();F[a>>2]=c.length;var d=0;c.forEach(function(f){d+=f.length+1});F[b>>2]=d;return 0},l:function(a){try{var b=Q(a);N.close(b);return 0}catch(c){if("undefined"==typeof N||!(c instanceof N.Ma))throw c;return c.Ra}},H:function(a,b){try{var c=Q(a);B[b>>0]=c.tty?2:N.Ya(c.mode)?3:N.vb(c.mode)?7:4;return 0}catch(d){if("undefined"== typeof N||!(d instanceof N.Ma))throw d;return d.Ra}},u:function(a,b,c,d){try{a:{var f=Q(a);a=b;for(var g=b=0;g>2],h=F[a+4>>2];a+=8;var n=N.read(f,B,k,h,void 0);if(0>n){var q=-1;break a}b+=n;if(n>2]=q;return 0}catch(p){if("undefined"==typeof N||!(p instanceof N.Ma))throw p;return p.Ra}},D:function(a,b,c,d,f){try{b=c+2097152>>>0<4194305-!!b?(b>>>0)+4294967296*c:NaN;if(isNaN(b))return 61;var g=Q(a);N.bb(g,b,d);J=[g.position>>>0,(G=g.position,1<=+Math.abs(G)?0>>0:~~+Math.ceil((G-+(~~G>>>0))/4294967296)>>>0:0)];E[f>>2]=J[0];E[f+4>>2]=J[1];g.ub&&0===b&&0===d&&(g.ub=null);return 0}catch(k){if("undefined"==typeof N||!(k instanceof N.Ma))throw k;return k.Ra}},T:function(a){try{var b=Q(a);return b.Qa&&b.Qa.fsync?-b.Qa.fsync(b):0}catch(c){if("undefined"==typeof N||!(c instanceof N.Ma))throw c;return c.Ra}},t:function(a,b,c,d){try{a:{var f=Q(a);a=b;for(var g=b=0;g>2],h=F[a+4>>2];a+=8;var n=N.write(f,B,k,h,void 0);if(0> n){var q=-1;break a}b+=n}q=b}F[d>>2]=q;return 0}catch(p){if("undefined"==typeof N||!(p instanceof N.Ma))throw p;return p.Ra}},q:function(){},ia:Gc,G:function(a,b,c,d){return Gc(a,b,c,d)}},Z=function(){function a(c){e.asm=c.exports;pa=e.asm.pa;ya();za=e.asm.ua;Ba.unshift(e.asm.qa);Ja("wasm-instantiate")}var b={a:Lc};Ia("wasm-instantiate");if(e.instantiateWasm)try{return e.instantiateWasm(b,a)}catch(c){return r("Module.instantiateWasm callback failed with error: "+c),!1}b=Ma(b);a(b[0]);return e.asm}(); e.___wasm_call_ctors=Z.qa;var Kc=e.___errno_location=Z.ra,wc=e._malloc=Z.sa,X=e._free=Z.ta,Yb=e.___getTypeName=Z.va;e.___embind_register_native_and_builtin_types=Z.wa;var cb=e._emscripten_builtin_memalign=Z.xa;e.___cxa_is_pointer_type=Z.ya;e.dynCall_iiiij=Z.za;e.dynCall_iij=Z.Aa;e.dynCall_iijii=Z.Ba;e.dynCall_iiji=Z.Ca;e.dynCall_iiiiiij=Z.Da;e.dynCall_vjii=Z.Ea;e.dynCall_vji=Z.Fa;e.dynCall_ji=Z.Ga;e.dynCall_jiji=Z.Ha;e.dynCall_viijii=Z.Ia;e.dynCall_iiiiij=Z.Ja;e.dynCall_iiiiijj=Z.Ka; e.dynCall_iiiiiijj=Z.La;e.addRunDependency=Ia;e.removeRunDependency=Ja;e.FS_createPath=N.Dc;e.FS_createDataFile=N.Rb;e.FS_createPreloadedFile=N.Ec;e.FS_createLazyFile=N.Cc;e.FS_createDevice=N.hb;e.FS_unlink=N.unlink;e.FS=N;var Mc;Ha=function Nc(){Mc||Oc();Mc||(Ha=Nc)}; function Oc(){function a(){if(!Mc&&(Mc=!0,e.calledRun=!0,!qa)){e.noFSInit||N.Db.jc||N.Db();N.Rc=!1;Na(Ba);aa(e);if(e.onRuntimeInitialized)e.onRuntimeInitialized();if(e.postRun)for("function"==typeof e.postRun&&(e.postRun=[e.postRun]);e.postRun.length;){var b=e.postRun.shift();Ca.unshift(b)}Na(Ca)}}if(!(0 { let queryExecutor; let dbModule; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { if (!dbModule) { throw new Error('Database module is missing'); } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); if (!queryExecutor) { throw new Error('SQLiteQueryExecutor is missing'); } - queryExecutor.storeOlmPersistAccount('accountData'); + queryExecutor.storeOlmPersistAccount( + queryExecutor.getContentAccountID(), + 'contentAccountData', + ); + queryExecutor.storeOlmPersistAccount( + queryExecutor.getNotifsAccountID(), + 'notifsAccountData', + ); queryExecutor.storeOlmPersistSession({ targetUserID: '1', sessionData: '1', }); queryExecutor.storeOlmPersistSession({ targetUserID: '2', sessionData: '2', }); queryExecutor.storeOlmPersistSession({ targetUserID: '3', sessionData: '3', }); }); afterEach(() => { clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return olm account data', () => { - const olmAccount = queryExecutor.getOlmPersistAccountDataWeb(); - expect(olmAccount.value).toBe('accountData'); + const contentOlmAccount = queryExecutor.getOlmPersistAccountDataWeb( + queryExecutor.getContentAccountID(), + ); + expect(contentOlmAccount.value).toBe('contentAccountData'); + + const notifsOlmAccount = queryExecutor.getOlmPersistAccountDataWeb( + queryExecutor.getNotifsAccountID(), + ); + expect(notifsOlmAccount.value).toBe('notifsAccountData'); }); it('should return all olm sessions', () => { const olmSessions = queryExecutor.getOlmPersistSessionsData(); expect(olmSessions.length).toBe(3); }); }); diff --git a/web/database/types/sqlite-query-executor.js b/web/database/types/sqlite-query-executor.js index d4d4550e5..332e83061 100644 --- a/web/database/types/sqlite-query-executor.js +++ b/web/database/types/sqlite-query-executor.js @@ -1,147 +1,149 @@ // @flow import type { ClientDBCommunityInfo } from 'lib/ops/community-store-ops.js'; import type { ClientDBKeyserverInfo } from 'lib/ops/keyserver-store-ops.js'; import type { ClientDBReport } from 'lib/ops/report-store-ops.js'; import type { ClientDBUserInfo } from 'lib/ops/user-store-ops.js'; import type { ClientDBDraftInfo } from 'lib/types/draft-types.js'; import { type WebClientDBThreadInfo, type NullableString, type NullableInt, } from './entities.js'; type WebMessage = { +id: string, +localID: NullableString, +thread: string, +user: string, +type: number, +futureType: NullableInt, +content: NullableString, +time: string, }; type Media = { +id: string, +container: string, +thread: string, +uri: string, +type: string, +extras: string, }; type OlmPersistSession = { +targetUserID: string, +sessionData: string, }; export type ClientMessageToDevice = { +messageID: string, +deviceID: string, +userID: string, +timestamp: string, +plaintext: string, +ciphertext: string, }; declare export class SQLiteQueryExecutor { constructor(sqliteFilePath: string): void; updateDraft(key: string, text: string): void; moveDraft(oldKey: string, newKey: string): boolean; getAllDrafts(): ClientDBDraftInfo[]; removeAllDrafts(): void; removeDrafts(ids: $ReadOnlyArray): void; getAllMessagesWeb(): $ReadOnlyArray<{ +message: WebMessage, +medias: $ReadOnlyArray, }>; removeAllMessages(): void; removeMessages(ids: $ReadOnlyArray): void; removeMessagesForThreads(threadIDs: $ReadOnlyArray): void; replaceMessageWeb(message: WebMessage): void; rekeyMessage(from: string, to: string): void; removeAllMedia(): void; removeMediaForThreads(threadIDs: $ReadOnlyArray): void; removeMediaForMessages(msgIDs: $ReadOnlyArray): void; removeMediaForMessage(msgID: string): void; replaceMedia(media: Media): void; rekeyMediaContainers(from: string, to: string): void; replaceMessageStoreThreads( threads: $ReadOnlyArray<{ +id: string, +startReached: number }>, ): void; removeMessageStoreThreads($ReadOnlyArray): void; getAllMessageStoreThreads(): $ReadOnlyArray<{ +id: string, +startReached: number, }>; removeAllMessageStoreThreads(): void; setMetadata(entryName: string, data: string): void; clearMetadata(entryName: string): void; getMetadata(entryName: string): string; replaceReport(report: ClientDBReport): void; removeReports(ids: $ReadOnlyArray): void; removeAllReports(): void; getAllReports(): ClientDBReport[]; setPersistStorageItem(key: string, item: string): void; removePersistStorageItem(key: string): void; getPersistStorageItem(key: string): string; replaceUser(userInfo: ClientDBUserInfo): void; removeUsers(ids: $ReadOnlyArray): void; removeAllUsers(): void; getAllUsers(): ClientDBUserInfo[]; replaceThreadWeb(thread: WebClientDBThreadInfo): void; removeThreads(ids: $ReadOnlyArray): void; removeAllThreads(): void; getAllThreadsWeb(): WebClientDBThreadInfo[]; replaceKeyserver(keyserverInfo: ClientDBKeyserverInfo): void; removeKeyservers(ids: $ReadOnlyArray): void; removeAllKeyservers(): void; getAllKeyservers(): ClientDBKeyserverInfo[]; replaceCommunity(communityInfo: ClientDBCommunityInfo): void; removeCommunities(ids: $ReadOnlyArray): void; removeAllCommunities(): void; getAllCommunities(): ClientDBCommunityInfo[]; beginTransaction(): void; commitTransaction(): void; rollbackTransaction(): void; - getOlmPersistAccountDataWeb(): NullableString; + getContentAccountID(): number; + getNotifsAccountID(): number; + getOlmPersistAccountDataWeb(accountID: number): NullableString; getOlmPersistSessionsData(): $ReadOnlyArray; - storeOlmPersistAccount(accountData: string): void; + storeOlmPersistAccount(accountID: number, accountData: string): void; storeOlmPersistSession(session: OlmPersistSession): void; restoreFromMainCompaction( mainCompactionPath: string, mainCompactionEncryptionKey: string, ): void; restoreFromBackupLog(backupLog: Uint8Array): void; addMessagesToDevice(messages: $ReadOnlyArray): void; removeMessagesToDeviceOlderThan( lastConfirmedMessage: ClientMessageToDevice, ): void; removeAllMessagesForDevice(deviceID: string): void; getAllMessagesToDevice( deviceID: string, ): $ReadOnlyArray; // method is provided to manually signal that a C++ object // is no longer needed and can be deleted delete(): void; } export type SQLiteQueryExecutorType = typeof SQLiteQueryExecutor;