From eccacbc451b15bc7af309d4b7c1632161ba44b79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Mon, 4 Nov 2019 10:57:51 -0500
Subject: [PATCH] jamiaccount: remove account if import from backup is failing

Change-Id: I18bbe04999e7fe92ec71672644bb2dc3fd725ec2
---
 src/jamidht/jamiaccount.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 2af18fa46a..0229f92e63 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -968,6 +968,7 @@ JamiAccount::loadAccount(const std::string& archive_password, const std::string&
                     acreds->scheme = "local";
                     acreds->uri = std::move(archivePath);
                     acreds->updateIdentity = id;
+                    migrating = true;
                 }
                 creds = std::move(acreds);
             } else {
@@ -977,6 +978,7 @@ JamiAccount::loadAccount(const std::string& archive_password, const std::string&
             }
             creds->password = archive_password;
             archiveHasPassword_ = !archive_password.empty();
+            bool isManaged = !managerUri_.empty();
 
             accountManager_->initAuthentication(
                 std::move(fReq),
@@ -1019,13 +1021,15 @@ JamiAccount::loadAccount(const std::string& archive_password, const std::string&
                 setRegistrationState(RegistrationState::UNREGISTERED);
                 saveConfig();
                 doRegister();
-            }, [w = weak(), id = getAccountID()](AccountManager::AuthError error, const std::string& message) {
+            }, [w = weak(), id = getAccountID(), isManaged, migrating](AccountManager::AuthError error, const std::string& message) {
                 JAMI_WARN("[Account %s] Auth error: %d %s", id.c_str(), (int)error, message.c_str());
-                if (error == AccountManager::AuthError::INVALID_ARGUMENTS) {
+                if ((isManaged || migrating) && error == AccountManager::AuthError::INVALID_ARGUMENTS) {
+                    // In cast of a migration or manager connexion failure stop the migration and block the account
                     Migration::setState(id, Migration::State::INVALID);
                     if (auto acc = w.lock())
                         acc->setRegistrationState(RegistrationState::ERROR_NEED_MIGRATION);
                 } else {
+                    // In case of a DHT or backup import failure, just remove the account
                     if (auto acc = w.lock())
                         acc->setRegistrationState(RegistrationState::ERROR_GENERIC);
                     runOnMainThread([id = std::move(id)] {
-- 
GitLab