Skip to content

Commit

Permalink
account: do not load accounts and configurations when autoload not en…
Browse files Browse the repository at this point in the history
…abled

Change-Id: I89e11e723ab0cc53c8e025bb9aa4a0b8322ec82c
  • Loading branch information
katekm committed Feb 23, 2024
1 parent ff9e9bd commit 62d39d7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 36 deletions.
98 changes: 62 additions & 36 deletions src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ bool Manager::isIOSExtension = {false};

bool Manager::syncOnRegister = {true};

bool Manager::autoLoad = {true};

static void
copy_over(const std::filesystem::path& srcPath, const std::filesystem::path& destPath)
{
Expand Down Expand Up @@ -818,11 +820,9 @@ Manager::init(const std::filesystem::path& config_file, libjami::InitFlag flags)
// So only create the SipLink once
pimpl_->sipLink_ = std::make_unique<SIPVoIPLink>();

check_rename(fileutils::get_cache_dir(PACKAGE_OLD),
fileutils::get_cache_dir());
check_rename(fileutils::get_cache_dir(PACKAGE_OLD), fileutils::get_cache_dir());
check_rename(fileutils::get_data_dir(PACKAGE_OLD), fileutils::get_data_dir());
check_rename(fileutils::get_config_dir(PACKAGE_OLD),
fileutils::get_config_dir());
check_rename(fileutils::get_config_dir(PACKAGE_OLD), fileutils::get_config_dir());

pimpl_->ice_tf_ = std::make_shared<dhtnet::IceTransportFactory>(Logger::dhtLogger());

Expand All @@ -838,28 +838,33 @@ Manager::init(const std::filesystem::path& config_file, libjami::InitFlag flags)
// manager can restart without being recreated (Unit tests)
pimpl_->finished_ = false;

try {
no_errors = pimpl_->parseConfiguration();
} catch (const YAML::Exception& e) {
JAMI_ERR("%s", e.what());
no_errors = false;
}

// always back up last error-free configuration
if (no_errors) {
make_backup(pimpl_->path_);
if (libjami::LIBJAMI_FLAG_NO_AUTOLOAD & flags) {
autoLoad = false;
JAMI_DBG("LIBJAMI_FLAG_NO_AUTOLOAD is set, accounts will neither be loaded nor backed up");
} else {
// restore previous configuration
JAMI_WARNING("Restoring last working configuration");

try {
// remove accounts from broken configuration
removeAccounts();
restore_backup(pimpl_->path_);
pimpl_->parseConfiguration();
no_errors = pimpl_->parseConfiguration();
} catch (const YAML::Exception& e) {
JAMI_ERROR("{}", e.what());
JAMI_WARNING("Restoring backup failed");
JAMI_ERR("%s", e.what());
no_errors = false;
}

// always back up last error-free configuration
if (no_errors) {
make_backup(pimpl_->path_);
} else {
// restore previous configuration
JAMI_WARNING("Restoring last working configuration");

try {
// remove accounts from broken configuration
removeAccounts();
restore_backup(pimpl_->path_);
pimpl_->parseConfiguration();
} catch (const YAML::Exception& e) {
JAMI_ERROR("{}", e.what());
JAMI_WARNING("Restoring backup failed");
}
}
}

Expand Down Expand Up @@ -3053,21 +3058,42 @@ Manager::setAccountActive(const std::string& accountID, bool active, bool shutdo
void
Manager::loadAccountAndConversation(const std::string& accountID, const std::string& convID)
{
if (const auto a = getAccount(accountID)) {
a->enableAutoLoadConversations(false);
if (a->getRegistrationState() == RegistrationState::UNLOADED) {
a->loadConfig();
}
a->setActive(true);
if (a->isUsable())
a->doRegister();
if (auto jamiAcc = std::dynamic_pointer_cast<JamiAccount>(a)) {
// load requests before loading conversation
if (auto convModule = jamiAcc->convModule()) {
convModule->reloadRequests();
auto account = getAccount(accountID);
if (!account && !autoLoad) {
/*
With the LIBJAMI_FLAG_NO_AUTOLOAD flag active, accounts are not
automatically created during manager initialization, nor are
their configurations set or backed up. This is because account
creation triggers the initialization of the certStore. There why
account creation now occurs here in response to a received notification.
*/
auto accountBaseDir = fileutils::get_data_dir();
auto configFile = accountBaseDir / accountID / "config.yml";
try {
if (account = accountFactory.createAccount(JamiAccount::ACCOUNT_TYPE, accountID)) {
account->enableAutoLoadConversations(false);
auto configNode = YAML::LoadFile(configFile.string());
auto config = account->buildConfig();
config->unserialize(configNode);
account->setConfig(std::move(config));
}
jamiAcc->loadConversation(convID);
} catch (const std::runtime_error& e) {
JAMI_WARN("Account loading failed: %s", e.what());
return;
}
}
if (!account) {
JAMI_WARN("Could not load account %s", accountID.c_str());
return;
}
if (auto jamiAcc = std::dynamic_pointer_cast<JamiAccount>(account)) {
jamiAcc->setActive(true);
if (jamiAcc->isUsable())
jamiAcc->doRegister();
if (auto convModule = jamiAcc->convModule()) {
convModule->reloadRequests();
}
jamiAcc->loadConversation(convID);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class LIBJAMI_TESTABLE Manager

static bool syncOnRegister;

static bool autoLoad;

/**
* Initialisation of thread (sound) and map.
* Init a new VoIPLink, audio codec and audio driver.
Expand Down

0 comments on commit 62d39d7

Please sign in to comment.