mail/src/js/util/update/update-v4.js

107 lines
3.8 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
/**
* Update handler for transition database version 3 -> 4
*
* In database version 4, we need to add a "provider" flag to the
* indexeddb. only gmail was allowed as a mail service provider before,
* so let's add this...
*/
function update(options, callback) {
var VERSION_DB_TYPE = 'dbVersion',
EMAIL_ADDR_DB_KEY = 'emailaddress',
USERNAME_DB_KEY = 'username',
PROVIDER_DB_KEY = 'provider',
IMAP_DB_KEY = 'imap',
SMTP_DB_KEY = 'smtp',
REALNAME_DB_KEY = 'realname',
POST_UPDATE_DB_VERSION = 4;
var imap = {
host: 'imap.gmail.com',
port: 993,
secure: true
},
smtp = {
host: 'smtp.gmail.com',
port: 465,
secure: true
};
2014-10-02 16:05:44 -04:00
// load the email address (if existing)
loadFromDB(EMAIL_ADDR_DB_KEY, function(err, emailAddress) {
if (err) {
return callback(err);
}
// load the provider (if existing)
loadFromDB(PROVIDER_DB_KEY, function(err, provider) {
if (err) {
return callback(err);
}
2014-10-02 16:05:44 -04:00
// if there is an email address without a provider, we need to add the missing provider entry
// for any other situation, we're good.
2014-10-02 16:05:44 -04:00
if (!(emailAddress && !provider)) {
// update the database version to POST_UPDATE_DB_VERSION
return options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE, callback);
}
2014-10-02 16:05:44 -04:00
// add the missing provider key
options.appConfigStorage.storeList(['gmail'], PROVIDER_DB_KEY, function(err) {
if (err) {
return callback(err);
}
2014-10-02 16:05:44 -04:00
// add the missing user name key
options.appConfigStorage.storeList([emailAddress], USERNAME_DB_KEY, function(err) {
if (err) {
return callback(err);
}
2014-10-02 16:05:44 -04:00
// add the missing imap host info key
options.appConfigStorage.storeList([imap], IMAP_DB_KEY, function(err) {
if (err) {
return callback(err);
}
2014-10-02 16:05:44 -04:00
// add the missing empty real name
options.appConfigStorage.storeList([''], REALNAME_DB_KEY, function(err) {
if (err) {
return callback(err);
}
2014-10-02 16:05:44 -04:00
// add the missing smtp host info key
options.appConfigStorage.storeList([smtp], SMTP_DB_KEY, function(err) {
if (err) {
return callback(err);
}
2014-10-02 16:05:44 -04:00
// reload the credentials
options.auth.initialized = false;
options.auth._loadCredentials(function(err) {
if (err) {
return callback(err);
}
2014-10-02 16:05:44 -04:00
// update the database version to POST_UPDATE_DB_VERSION
options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE, callback);
});
});
});
});
});
});
});
2014-10-02 16:05:44 -04:00
});
2014-10-02 16:05:44 -04:00
function loadFromDB(key, callback) {
options.appConfigStorage.listItems(key, 0, null, function(err, cachedItems) {
callback(err, (!err && cachedItems && cachedItems[0]));
});
}
2014-10-02 16:05:44 -04:00
}
2014-10-08 06:34:34 -04:00
module.exports = update;