Skip to content

Commit

Permalink
fix(gmail-api): Fixed threading for replied messages when using Gmail…
Browse files Browse the repository at this point in the history
… API
  • Loading branch information
andris9 committed Dec 7, 2024
1 parent 9182ea0 commit 2b4d5bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/email-client/base-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,10 @@ class BaseClient {
data.reference.update = true;
}

if (referencedMessage.threadId) {
data.reference.threadId = referencedMessage.threadId;
}

if (!data.subject && referencedMessage.subject) {
let subject = referencedMessage.subject;
let prefix;
Expand Down
26 changes: 23 additions & 3 deletions lib/email-client/gmail-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
AUTH_ERROR_NOTIFY,
AUTH_SUCCESS_NOTIFY
} = require('../consts');
const { threadId } = require('worker_threads');

Check failure on line 24 in lib/email-client/gmail-client.js

View workflow job for this annotation

GitHub Actions / Test Suite (18.x, ubuntu-20.04)

'threadId' is assigned a value but never used

const GMAIL_API_BASE = 'https://gmail.googleapis.com';
const LIST_BATCH_SIZE = 10; // how many listing requests to run at the same time
Expand Down Expand Up @@ -1086,10 +1087,16 @@ class GmailClient extends BaseClient {
raw = Buffer.from(raw);
}

const uploadInfo = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages?internalDateSource=dateHeader`, 'post', {
let payload = {
labelIds: [targetLabel.id],
raw: raw.toString('base64url')
});
};

if (referencedMessage?.threadId) {
payload.threadId = referencedMessage.threadId;
}

const uploadInfo = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages?internalDateSource=dateHeader`, 'post', payload);

let response = {
message: uploadInfo?.id,
Expand Down Expand Up @@ -1157,7 +1164,20 @@ class GmailClient extends BaseClient {
return false;
}

const submitInfo = await this.request(`${GMAIL_API_BASE}/upload/gmail/v1/users/me/messages/send`, 'post', raw, { contentType: 'message/rfc822' });
let contentType = 'message/rfc822';
let payload = raw;
let targetEndpoint = `/upload/gmail/v1/users/me/messages/send`;

if (data?.reference?.threadId) {
targetEndpoint = `/gmail/v1/users/me/messages/send`;
contentType = 'application/json';
payload = {
raw: raw.toString('base64'),
threadId: data?.reference?.threadId
};
}

const submitInfo = await this.request(`${GMAIL_API_BASE}${targetEndpoint}`, 'post', payload, { contentType });
/*
SEND RESPONSE {
id: '18f85d2eb6adb232',
Expand Down

0 comments on commit 2b4d5bb

Please sign in to comment.