From eaf1ff770c0e431de96e80e72a636e39eb1d292a Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Wed, 8 May 2024 13:58:38 +0300 Subject: [PATCH] Functioning list messages and list mailboxes API endpoints for Gmail API --- lib/account.js | 48 ++++++++++++++++--------------- lib/api-client/gmail-client.js | 1 - translations/messages.pot | 52 +++++++++++++++++----------------- 3 files changed, 51 insertions(+), 50 deletions(-) diff --git a/lib/account.js b/lib/account.js index 3ad5dd4c..3f0109f8 100644 --- a/lib/account.js +++ b/lib/account.js @@ -827,7 +827,20 @@ class Account { let accountData = await this.loadAccountData(this.account, false); let mailboxListing; - if (accountData.state === 'connected' || query.counters) { + if (await this.isApiClient(accountData)) { + mailboxListing = await this.listMailboxes(query); + + if (mailboxListing && mailboxListing.error) { + let error = Boom.boomify(new Error(mailboxListing.error), { statusCode: mailboxListing.statusCode || 500 }); + if (mailboxListing.code) { + error.output.payload.code = mailboxListing.code; + } + throw error; + } + + // just pass through, do nothing + return mailboxListing; + } else if (accountData.state === 'connected' || query.counters) { // run LIST mailboxListing = await this.listMailboxes(query); if (mailboxListing && mailboxListing.error) { @@ -944,13 +957,14 @@ class Account { unseen: true }; } - - return await this.call({ + let x = await this.call({ cmd: 'listMailboxes', account: this.account, options, timeout: this.timeout }); + + return x; } async moveMessage(message, target) { @@ -1402,16 +1416,6 @@ class Account { await this.loadAccountData(this.account, true); - let path = normalizePath(query.path); - let encodedMailboxData = await this.redis.hgetBuffer(this.getMailboxListKey(), path); - if (!encodedMailboxData) { - let message = 'Mailbox record was not found'; - let error = Boom.boomify(new Error(message), { statusCode: 404 }); - error.output.payload.path = query.path; - throw error; - } - - // mailbox seems to exist, so call parent to resolve open connection return await this.call( Object.assign( { @@ -1809,16 +1813,6 @@ class Account { await this.loadAccountData(this.account, true); - let path = normalizePath(query.path); - let encodedMailboxData = await this.redis.hgetBuffer(this.getMailboxListKey(), path); - if (!encodedMailboxData) { - let message = 'Mailbox record was not found'; - let error = Boom.boomify(new Error(message), { statusCode: 404 }); - error.output.payload.path = query.path; - throw error; - } - - // mailbox seems to exist, so call parent to resolve open connection return await this.call( Object.assign( { @@ -2203,6 +2197,14 @@ class Account { cached }; } + + async isApiClient(accountData) { + if (accountData.oauth2?.provider) { + let app = await oauth2Apps.get(accountData.oauth2.provider); + return app?.baseScopes === 'api'; + } + return false; + } } module.exports = { Account }; diff --git a/lib/api-client/gmail-client.js b/lib/api-client/gmail-client.js index eab3c76f..95dba695 100644 --- a/lib/api-client/gmail-client.js +++ b/lib/api-client/gmail-client.js @@ -255,7 +255,6 @@ class GmailClient extends BaseClient { return a.path.toLowerCase().localeCompare(b.path.toLowerCase()); }); - console.log(555, mailboxes); return mailboxes; } diff --git a/translations/messages.pot b/translations/messages.pot index 04b45f02..7e7953d6 100644 --- a/translations/messages.pot +++ b/translations/messages.pot @@ -1,7 +1,7 @@ msgid "" msgstr "" "Content-Type: text/plain; charset=ascii\n" -"POT-Creation-Date: 2024-05-07 12:38+0000\n" +"POT-Creation-Date: 2024-05-08 10:50+0000\n" #: views/config/license.hbs:48 msgid "%d day" @@ -9,10 +9,6 @@ msgid_plural "%d days" msgstr[0] "" msgstr[1] "" -#: views/redirect.hbs:1 -msgid "Click here to continue…" -msgstr "" - #: views/unsubscribe.hbs:1 #: views/unsubscribe.hbs:79 msgid "Unsubscribe" @@ -57,27 +53,8 @@ msgstr "" msgid "Enter your email address" msgstr "" -#: views/accounts/register/imap.hbs:11 -msgid "Your name" -msgstr "" - -#: views/accounts/register/imap.hbs:17 -msgid "Enter your full name" -msgstr "" - -#: views/accounts/register/imap.hbs:31 -#: views/accounts/register/imap-server.hbs:35 -#: views/accounts/register/imap-server.hbs:114 -msgid "Password" -msgstr "" - -#: views/accounts/register/imap.hbs:37 -msgid "Enter your account password" -msgstr "" - -#: views/accounts/register/imap.hbs:51 -#: views/accounts/register/imap-server.hbs:192 -msgid "Continue" +#: views/redirect.hbs:1 +msgid "Click here to continue…" msgstr "" #: views/accounts/register/index.hbs:2 @@ -97,6 +74,12 @@ msgstr "" msgid "Username" msgstr "" +#: views/accounts/register/imap-server.hbs:35 +#: views/accounts/register/imap-server.hbs:114 +#: views/accounts/register/imap.hbs:31 +msgid "Password" +msgstr "" + #: views/accounts/register/imap-server.hbs:41 #: views/accounts/register/imap-server.hbs:46 #: views/accounts/register/imap-server.hbs:120 @@ -157,6 +140,11 @@ msgstr "" msgid "Test settings" msgstr "" +#: views/accounts/register/imap-server.hbs:192 +#: views/accounts/register/imap.hbs:51 +msgid "Continue" +msgstr "" + #: views/accounts/register/imap-server.hbs:200 msgid "Continue without testing" msgstr "" @@ -189,6 +177,18 @@ msgstr "" msgid "HTTP error!" msgstr "" +#: views/accounts/register/imap.hbs:11 +msgid "Your name" +msgstr "" + +#: views/accounts/register/imap.hbs:17 +msgid "Enter your full name" +msgstr "" + +#: views/accounts/register/imap.hbs:37 +msgid "Enter your account password" +msgstr "" + #: lib/routes-ui.js:581 msgid "Invalid API key for OpenAI" msgstr ""