Bug 1658781 - Fix the existing account name condition in the account manager. r=mkmelin

This commit is contained in:
Alessandro Castellani 2020-12-16 11:57:14 -08:00
Родитель 2dcfb56805
Коммит 531a61c4f1
2 изменённых файлов: 56 добавлений и 2 удалений

Просмотреть файл

@ -94,8 +94,14 @@ function createAccountInBackend(config) {
// STARTTLS
inServer.socketType = Ci.nsMsgSocketType.alwaysSTARTTLS;
}
// inServer.prettyName = config.displayName;
inServer.prettyName = config.identity.emailAddress;
// If we already have an account with an identical name, generate a unique
// name for the new account to avoid duplicates.
inServer.prettyName = checkAccountNameAlreadyExists(
config.identity.emailAddress
)
? generateUniqueAccountName(config)
: config.identity.emailAddress;
inServer.doBiff = true;
inServer.biffMinutes = config.incoming.checkInterval;
@ -388,6 +394,46 @@ function checkOutgoingServerAlreadyExists(config) {
return null;
}
/**
* Check whether the user's setup already has an account with the same email
* address. This might happen if the user uses the same email for different
* protocols (eg. IMAP and POP3).
*
* @param {string} name - The name or email address of the new account.
* @returns {boolean} True if an account with the same name is found.
*/
function checkAccountNameAlreadyExists(name) {
return MailServices.accounts.accounts.some(
a => a.incomingServer.prettyName == name
);
}
/**
* Generate a unique account name by appending the incoming protocol type, and
* a counter if necessary.
*
* @param {AccountConfig} config - The config data of the account being created.
* @returns {string} - The unique account name.
*/
function generateUniqueAccountName(config) {
// Generate a potential unique name. e.g. "foo@bar.com (POP3)".
let name = `${
config.identity.emailAddress
} (${config.incoming.type.toUpperCase()})`;
// If this name already exists, append a counter until we find a unique name.
if (checkAccountNameAlreadyExists(name)) {
let counter = 2;
while (checkAccountNameAlreadyExists(`${name}_${counter}`)) {
counter++;
}
// e.g. "foo@bar.com (POP3)_1".
name = `${name}_${counter}`;
}
return name;
}
/**
* Check if there already is a "Local Folders". If not, create it.
* Copied from AccountWizard.js with minor updates.

Просмотреть файл

@ -767,6 +767,14 @@ function checkAccountNameIsValid() {
alertText = prefBundle.getString("accountNameEmpty");
} else if (accountNameExists(accountName, currentAccount.key)) {
alertText = prefBundle.getString("accountNameExists");
// Change the account name to prevent UI freeze.
let counter = 2;
while (
accountNameExists(`${accountName}_${counter}`, currentAccount.key)
) {
counter++;
}
serverNameElem.value = `${accountName}_${counter}`;
}
if (alertText) {