From 7ddc836b4289cc4ebe35b75f1d080908a8c23125 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Fri, 2 Feb 2024 13:55:29 +0200 Subject: [PATCH] started with message moving --- lib/api-client/gmail-client.js | 61 ++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/api-client/gmail-client.js b/lib/api-client/gmail-client.js index 0625256e..473304d1 100644 --- a/lib/api-client/gmail-client.js +++ b/lib/api-client/gmail-client.js @@ -41,6 +41,7 @@ const SYSTEM_NAMES = { /* ✅ listMessages + ❌ paging - not implemented ✅ getText ✅ getMessage case 'updateMessage': @@ -48,10 +49,10 @@ case 'updateMessages': ✅ listMailboxes case 'moveMessage': case 'moveMessages': -✅ deleteMessage (no force option) +✅ deleteMessage - no force option case 'deleteMessages': ✅ getRawMessage -case 'getQuota': +⭕️ getQuota - not supported case 'createMailbox': case 'renameMailbox': case 'deleteMailbox': @@ -59,7 +60,7 @@ case 'deleteMailbox': case 'submitMessage': case 'queueMessage': case 'uploadMessage': -✅ subconnections +⭕️ subconnections - not supported */ @@ -101,6 +102,11 @@ class GmailClient { await this.getClient(); } + // Treat Quota request as unsupported by mail server + async getQuota() { + return false; + } + async listMailboxes(options) { await this.prepare(); @@ -513,6 +519,51 @@ class GmailClient { }; } + async moveMessage(messageId, target) { + target = target || {}; + // target.path + + await this.prepare(); + + const accessToken = await this.getToken(); + + const requestQuery = { + format: 'minimal' + }; + + let path = (target.path || '').toString().trim(); + + let label; + + if (/^inbox$/i.test(path)) { + label = 'INBOX'; + } + + for (let key of Object.keys(SYSTEM_NAMES)) { + if (path === SYSTEM_NAMES[key]) { + label = key; + } + } + + if (!label) { + label = path; + } + + const messageData = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery); + + console.log({ label, p: path.toUpperCase(), s: SYSTEM_NAMES[path.toUpperCase()] }); + + if (!messageData) { + return false; + } + + if (messageData.labelIds.includes(label)) { + return true; + } + + console.log(messageData); + } + async getAttachmentContent(attachmentId) { let sepPos = attachmentId.indexOf('.'); if (sepPos < 0) { @@ -688,6 +739,10 @@ let main = async () => { const textContent = await gmailClient.getText(msg.text.id, { textType: '*' }); console.log('TEXT CONTENT', textContent); + console.log('MOVE MESSAGE'); + let moveRes = await gmailClient.moveMessage(msg.id, { path: 'Inbox' }); + console.log('MOVE RES', moveRes); + let raw = await gmailClient.getRawMessage(msg.id); await fs.promises.writeFile(`/Users/andris/Desktop/${msg.id}.eml`, raw); for (let a of msg.attachments) {