diff --git a/lib/api-client/base-client.js b/lib/api-client/base-client.js index 1b4e65d8..9465567f 100644 --- a/lib/api-client/base-client.js +++ b/lib/api-client/base-client.js @@ -17,6 +17,8 @@ class BaseClient { this.accountObject = this.options.accountObject; this.accountLogger = this.options.accountLogger; this.redis = this.options.redis; + + this.call = this.options.call; this.logger = this.options.logger || logger; this.secret = this.options.secret; diff --git a/lib/api-client/gmail-client.js b/lib/api-client/gmail-client.js index 953e75c0..7858f0bf 100644 --- a/lib/api-client/gmail-client.js +++ b/lib/api-client/gmail-client.js @@ -178,6 +178,25 @@ class GmailClient extends BaseClient { super(account, options); } + async request(...args) { + let result, accessToken; + try { + accessToken = await this.getToken(); + } catch (err) { + console.log(err); + throw err; + } + + try { + result = await await this.oAuth2Client.request(accessToken, ...args); + } catch (err) { + console.log(err); + throw err; + } + + return result; + } + // PUBLIC METHODS async listMailboxes(options) { @@ -186,7 +205,7 @@ class GmailClient extends BaseClient { console.log(1); const accessToken = await this.getToken(); console.log(2, accessToken); - let labelsResult = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/labels`); + let labelsResult = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/labels`); console.log(3, labelsResult); let labels = labelsResult.labels.filter(label => !SKIP_LABELS.includes(label.id)); @@ -194,7 +213,7 @@ class GmailClient extends BaseClient { if (options && options.counters) { resultLabels = []; for (let label of labels) { - let labelResult = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/labels/${label.id}`); + let labelResult = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/labels/${label.id}`); resultLabels.push(labelResult); } } else { @@ -291,7 +310,7 @@ class GmailClient extends BaseClient { .join('/') .replace(/^INBOX(\/|$)/gi, 'INBOX'); - let labelsResult = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/labels`); + let labelsResult = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/labels`); let label = labelsResult.labels.find(entry => entry.name === path || entry.id === path); if (!label) { return false; @@ -305,7 +324,7 @@ class GmailClient extends BaseClient { if (query.search.emailId) { // Return only a single matching email - let messageEntry = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${query.search.emailId}`, 'get', { + let messageEntry = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${query.search.emailId}`, 'get', { format: 'full' }); if (messageEntry) { @@ -394,7 +413,7 @@ class GmailClient extends BaseClient { console.log('REQUEST QUERY', requestQuery); - let listingResult = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages`, 'get', requestQuery); + let listingResult = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages`, 'get', requestQuery); let messageCount = listingResult.resultSizeEstimate; let pages = Math.ceil(messageCount / pageSize); @@ -445,12 +464,10 @@ class GmailClient extends BaseClient { async getRawMessage(messageId) { await this.prepare(); - const accessToken = await this.getToken(); - const requestQuery = { format: 'raw' }; - const result = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery); + const result = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery); return result?.raw ? Buffer.from(result?.raw, 'base64url') : null; } @@ -458,11 +475,9 @@ class GmailClient extends BaseClient { async deleteMessage(messageId /*, force*/) { await this.prepare(); - const accessToken = await this.getToken(); - // Move to trash const url = `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}/trash`; - const result = await this.oAuth2Client.request(accessToken, url, 'post', Buffer.alloc(0)); + const result = await this.request(url, 'post', Buffer.alloc(0)); return { deleted: result && result.labelIds?.includes('TRASH'), @@ -478,8 +493,6 @@ class GmailClient extends BaseClient { await this.prepare(); - const accessToken = await this.getToken(); - const requestQuery = { format: 'minimal' }; @@ -502,7 +515,7 @@ class GmailClient extends BaseClient { label = path; } - const messageData = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery); + const messageData = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery); console.log({ label, p: path.toUpperCase(), s: SYSTEM_NAMES[path.toUpperCase()] }); @@ -554,12 +567,10 @@ class GmailClient extends BaseClient { options = options || {}; await this.prepare(); - const accessToken = await this.getToken(); - const requestQuery = { format: 'full' }; - const messageData = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery); + const messageData = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery); let result = this.formatMessage(messageData, { extended: true, textType: options.textType }); @@ -575,8 +586,6 @@ class GmailClient extends BaseClient { options = options || {}; await this.prepare(); - const accessToken = await this.getToken(); - const [messageId, textParts] = msgpack.decode(Buffer.from(textId, 'base64url')); const bodyParts = new Map(); @@ -592,7 +601,7 @@ class GmailClient extends BaseClient { const requestQuery = { format: 'full' }; - const messageData = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery); + const messageData = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery); const response = {}; diff --git a/lib/imap-connection.js b/lib/imap-connection.js index 11db0a4a..e3346380 100644 --- a/lib/imap-connection.js +++ b/lib/imap-connection.js @@ -101,8 +101,6 @@ class IMAPConnection extends BaseClient { this.isClosing = false; this.isClosed = false; - this.call = options.call; - this.logger = this.getLogger(); this.imapConfig = { @@ -122,7 +120,6 @@ class IMAPConnection extends BaseClient { this.mailboxes = new Map(); this.untaggedExistsTimer = false; - this.redis = options.redis; this.notifyQueue = options.notifyQueue; this.submitQueue = options.submitQueue; diff --git a/lib/routes-ui.js b/lib/routes-ui.js index 1eb62831..cbdca897 100644 --- a/lib/routes-ui.js +++ b/lib/routes-ui.js @@ -428,7 +428,8 @@ function formatAccountData(account) { case 'init': account.stateLabel = { type: 'info', - name: 'Initializing' + name: 'Initializing', + spinner: true }; break; diff --git a/static/js/app.js b/static/js/app.js index fd093488..7aa253a6 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -244,7 +244,8 @@ document.addEventListener('DOMContentLoaded', () => { case 'init': stateLabel = { type: 'info', - name: 'Initializing' + name: 'Initializing', + spinner: true }; break; case 'connecting':