diff --git a/lib/email-client/base-client.js b/lib/email-client/base-client.js index bc297b58..6e10b55c 100644 --- a/lib/email-client/base-client.js +++ b/lib/email-client/base-client.js @@ -165,7 +165,7 @@ class BaseClient { return `${REDIS_PREFIX}iaz:logged`; } - currentState() { + async currentState() { return 'connected'; } diff --git a/lib/email-client/gmail-client.js b/lib/email-client/gmail-client.js index fccb84fa..3f4f53ad 100644 --- a/lib/email-client/gmail-client.js +++ b/lib/email-client/gmail-client.js @@ -324,6 +324,10 @@ class GmailClient extends BaseClient { return null; } + async currentState() { + return (await this.redis.hget(this.getAccountKey(), 'state')) || 'disconnected'; + } + async delete() { clearTimeout(this.renewWatchTimer); this.closed = true; diff --git a/lib/email-client/imap-client.js b/lib/email-client/imap-client.js index c26b6551..63f6b42b 100644 --- a/lib/email-client/imap-client.js +++ b/lib/email-client/imap-client.js @@ -1288,7 +1288,7 @@ class IMAPClient extends BaseClient { return this.imapClient && this.imapClient.usable && !this.isClosing && !this.isClosed; } - currentState() { + async currentState() { if (this.state === 'connected' && !this.isConnected()) { this.state = 'disconnected'; } diff --git a/lib/email-client/outlook-client.js b/lib/email-client/outlook-client.js index 09b3ca15..62362aa1 100644 --- a/lib/email-client/outlook-client.js +++ b/lib/email-client/outlook-client.js @@ -225,6 +225,10 @@ class OutlookClient extends BaseClient { return null; } + async currentState() { + return (await this.redis.hget(this.getAccountKey(), 'state')) || 'disconnected'; + } + async delete() { clearTimeout(this.renewWatchTimer); this.closed = true; diff --git a/workers/imap.js b/workers/imap.js index 9aa3375b..ffb3d134 100644 --- a/workers/imap.js +++ b/workers/imap.js @@ -749,24 +749,20 @@ class ConnectionHandler { case 'countConnections': { let results = Object.assign({}, DEFAULT_STATES); - let count = status => { - if (!results[status]) { - results[status] = 0; - } - results[status] += 1; - }; - - this.accounts.forEach(accountObject => { + for (let accountObject of this.accounts) { let state; if (!accountObject || !accountObject.connection) { state = 'unassigned'; } else { - state = accountObject.connection.currentState(); + state = await accountObject.connection.currentState(); } - return count(state); - }); + if (!results[state]) { + results[state] = 0; + } + results[state] += 1; + } return results; }