Skip to content

Commit

Permalink
Delete message support for gmail api
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Jan 23, 2024
1 parent 1ed38d8 commit 1eb80a6
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions lib/api-client/gmail-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ case 'updateMessages':
✅ listMailboxes
case 'moveMessage':
case 'moveMessages':
case 'deleteMessage':
deleteMessage (no force option)
case 'deleteMessages':
case 'getRawMessage':
getRawMessage
case 'getQuota':
case 'createMailbox':
case 'renameMailbox':
Expand All @@ -59,7 +59,7 @@ case 'deleteMailbox':
case 'submitMessage':
case 'queueMessage':
case 'uploadMessage':
case 'subconnections':
subconnections
*/

Expand Down Expand Up @@ -488,19 +488,31 @@ class GmailClient {

const accessToken = await this.getToken();

const requestQuery = {};
const result = await this.oAuth2Client.request(
accessToken,
`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}?format=raw`,
'get',
requestQuery
);

console.log(result);
const requestQuery = {
format: 'raw'
};
const result = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);

return result?.raw ? Buffer.from(result?.raw, 'base64url') : null;
}

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));

return {
deleted: result && result.labelIds?.includes('TRASH'),
moved: {
message: result.id
}
};
}

async getAttachmentContent(attachmentId) {
let sepPos = attachmentId.indexOf('.');
if (sepPos < 0) {
Expand Down Expand Up @@ -568,13 +580,10 @@ class GmailClient {

const accessToken = await this.getToken();

const requestQuery = {};
const messageData = await this.oAuth2Client.request(
accessToken,
`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}?format=full`,
'get',
requestQuery
);
const requestQuery = {
format: 'full'
};
const messageData = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);

let result = this.formatMessage(messageData, { extended: true, textType: options.textType });

Expand Down Expand Up @@ -604,13 +613,10 @@ class GmailClient {
bodyParts.set(p, 'html');
});

const requestQuery = {};
const messageData = await this.oAuth2Client.request(
accessToken,
`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}?format=full`,
'get',
requestQuery
);
const requestQuery = {
format: 'full'
};
const messageData = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);

const response = {};

Expand Down Expand Up @@ -645,6 +651,11 @@ class GmailClient {

return response;
}

// stub. no support or need for subconnections
async subconnections() {
return [];
}
}

module.exports = { GmailClient };
Expand All @@ -659,7 +670,18 @@ let main = async () => {

let messages = await gmailClient.listMessages({ path: 'INBOX' });
console.log(JSON.stringify(messages, false, 2));

let deleted = false;

for (let msg of messages) {
if (/testkiri/i.test(msg.subject) && !deleted) {
deleted = true;

console.log('DELETING', msg.id);
let y = await gmailClient.deleteMessage(msg.id, true);
console.log('DELETE RESULT', y);
}

if (msg.attachments && msg.attachments.length) {
await gmailClient.getMessage(msg.id, { textType: '*' });

Expand Down

0 comments on commit 1eb80a6

Please sign in to comment.