Return a new token for existing account instead of duplicating accounts

This commit is contained in:
Ben Gotow 2016-07-11 16:01:18 -07:00
parent 5085730902
commit 3c63268eec
4 changed files with 49 additions and 24 deletions

View file

@ -43,24 +43,33 @@ const buildAccountWith = ({name, email, provider, settings, credentials}) => {
return DatabaseConnector.forShared().then((db) => { return DatabaseConnector.forShared().then((db) => {
const {AccountToken, Account} = db; const {AccountToken, Account} = db;
const account = Account.build({ return Account.find({
name: name, where: {
provider: provider, emailAddress: email,
emailAddress: email, connectionSettings: JSON.stringify(settings),
connectionSettings: settings, },
syncPolicy: SyncPolicy.defaultPolicy(), }).then((existing) => {
lastSyncCompletions: [], const account = existing || Account.build({
}) name: name,
account.setCredentials(credentials); provider: provider,
emailAddress: email,
connectionSettings: settings,
syncPolicy: SyncPolicy.defaultPolicy(),
lastSyncCompletions: [],
})
return account.save().then((saved) => // always update with the latest credentials
AccountToken.create({accountId: saved.id}).then((token) => account.setCredentials(credentials);
DatabaseConnector.prepareAccountDatabase(saved.id).thenReturn({
account: saved, return account.save().then((saved) =>
token: token, AccountToken.create({accountId: saved.id}).then((token) =>
}) DatabaseConnector.prepareAccountDatabase(saved.id).thenReturn({
) account: saved,
); token: token,
})
)
);
});
}); });
} }
@ -76,7 +85,6 @@ module.exports = (server) => {
validate: { validate: {
query: { query: {
client_id: Joi.string().required(), client_id: Joi.string().required(),
n1_id: Joi.string(),
}, },
payload: { payload: {
email: Joi.string().email().required(), email: Joi.string().email().required(),
@ -98,7 +106,11 @@ module.exports = (server) => {
const {settings, email, provider, name} = request.payload; const {settings, email, provider, name} = request.payload;
if (provider === 'imap') { if (provider === 'imap') {
connectionChecks.push(IMAPConnection.connect({db: dbStub, settings})) connectionChecks.push(IMAPConnection.connect({
logger: request.logger,
settings: settings,
db: dbStub,
}));
} }
Promise.all(connectionChecks).then(() => { Promise.all(connectionChecks).then(() => {
@ -187,9 +199,12 @@ module.exports = (server) => {
client_id: GMAIL_CLIENT_ID, client_id: GMAIL_CLIENT_ID,
client_secret: GMAIL_CLIENT_SECRET, client_secret: GMAIL_CLIENT_SECRET,
} }
Promise.all([ Promise.all([
IMAPConnection.connect({db: {}, settings: Object.assign({}, settings, credentials)}), IMAPConnection.connect({
logger: request.logger,
settings: Object.assign({}, settings, credentials),
db: {},
}),
]) ])
.then(() => .then(() =>
buildAccountWith({ buildAccountWith({

View file

@ -156,8 +156,6 @@ class IMAPBox {
} }
return this._imap.closeBoxAsync(expunge) return this._imap.closeBoxAsync(expunge)
} }
} }
@ -178,6 +176,11 @@ class IMAPConnection extends EventEmitter {
constructor({db, settings, logger = console} = {}) { constructor({db, settings, logger = console} = {}) {
super(); super();
if (!(settings instanceof Object)) {
throw new Error("IMAPConnection: Must be instantiated with `settings`")
}
this._logger = logger; this._logger = logger;
this._db = db; this._db = db;
this._queue = []; this._queue = [];

View file

@ -1,4 +1,6 @@
/* eslint react/react-in-jsx-scope: 0*/ /* eslint react/react-in-jsx-scope: 0*/
/* eslint no-console: 0*/
const React = window.React; const React = window.React;
const ReactDOM = window.ReactDOM; const ReactDOM = window.ReactDOM;
const { const {

View file

@ -108,7 +108,12 @@ class SyncWorker {
return Promise.reject(new Error("ensureConnection: There are no IMAP connection credentials for this account.")) return Promise.reject(new Error("ensureConnection: There are no IMAP connection credentials for this account."))
} }
const conn = new IMAPConnection({db: this._db, settings: Object.assign({}, settings, credentials), logger: this._logger}); const conn = new IMAPConnection({
db: this._db,
settings: Object.assign({}, settings, credentials),
logger: this._logger,
});
conn.on('mail', () => { conn.on('mail', () => {
this._onConnectionIdleUpdate(); this._onConnectionIdleUpdate();
}) })