Skip to content

Commit

Permalink
started with message moving
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Feb 2, 2024
1 parent 89f0f01 commit 7ddc836
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions lib/api-client/gmail-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,26 @@ const SYSTEM_NAMES = {
/*
✅ listMessages
❌ paging - not implemented
✅ getText
✅ getMessage
case 'updateMessage':
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':
✅ getAttachment
case 'submitMessage':
case 'queueMessage':
case 'uploadMessage':
subconnections
⭕️ subconnections - not supported
*/

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7ddc836

Please sign in to comment.