Skip to content

Commit 2c3d63a

Browse files
committed
fix(gmail-api): Fixed webhook notifications for authenticationSuccess and authenticationError
1 parent 7dc6e96 commit 2c3d63a

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

lib/api-client/gmail-client.js

+39-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { BaseClient } = require('./base-client');
1111
const settings = require('../settings');
1212
const { arfDetect } = require('../arf-detect');
1313
const { bounceDetect } = require('../bounce-detect');
14-
const { filterEmptyObjectValues } = require('../tools');
14+
const { filterEmptyObjectValues, emitChangeEvent } = require('../tools');
1515
const simpleParser = require('mailparser').simpleParser;
1616
const ical = require('ical.js');
1717
const { llmPreProcess } = require('../llm-pre-process');
@@ -27,7 +27,8 @@ const {
2727
MESSAGE_NEW_NOTIFY,
2828
EMAIL_BOUNCE_NOTIFY,
2929
EMAIL_COMPLAINT_NOTIFY,
30-
AUTH_ERROR_NOTIFY
30+
AUTH_ERROR_NOTIFY,
31+
AUTH_SUCCESS_NOTIFY
3132
} = require('../consts');
3233

3334
const GMAIL_API_BASE = 'https://gmail.googleapis.com';
@@ -247,8 +248,9 @@ class GmailClient extends BaseClient {
247248
await this.setStateVal();
248249

249250
err.authenticationFailed = true;
251+
250252
await this.notify(false, AUTH_ERROR_NOTIFY, {
251-
response: err.oauthRequest?.response?.message || err.response,
253+
response: err.oauthRequest?.response?.error?.message || err.response,
252254
serverResponseCode: 'ApiRequestError'
253255
});
254256

@@ -265,6 +267,40 @@ class GmailClient extends BaseClient {
265267

266268
this.state = 'connected';
267269
await this.setStateVal();
270+
271+
let prevConnectedCount = await this.redis.hget(this.getAccountKey(), `state:count:connected`);
272+
let isFirstSuccessfulConnection = prevConnectedCount === '0'; // string zero means the account has been initialized but not yet connected
273+
274+
let isiInitial = !!isFirstSuccessfulConnection;
275+
276+
if (!isFirstSuccessfulConnection) {
277+
// check if the connection was previously in an errored state
278+
let prevLastErrorState = await this.redis.hget(this.getAccountKey(), 'lastErrorState');
279+
if (prevLastErrorState) {
280+
try {
281+
prevLastErrorState = JSON.parse(prevLastErrorState);
282+
} catch (err) {
283+
// ignore
284+
}
285+
}
286+
287+
if (prevLastErrorState && typeof prevLastErrorState === 'object' && Object.keys(prevLastErrorState).length) {
288+
// was previously errored
289+
isFirstSuccessfulConnection = true;
290+
}
291+
}
292+
293+
if (isFirstSuccessfulConnection) {
294+
this.logger.info({ msg: 'Successful login without a previous active session', account: this.account, isiInitial, prevActive: false });
295+
await this.notify(false, AUTH_SUCCESS_NOTIFY, {
296+
user: accountData.oauth2?.auth?.user
297+
});
298+
} else {
299+
this.logger.info({ msg: 'Successful login with a previous active session', account: this.account, isiInitial, prevActive: true });
300+
}
301+
302+
await this.redis.hSetExists(this.getAccountKey(), 'lastErrorState', '{}');
303+
await emitChangeEvent(this.logger, this.account, 'state', this.state);
268304
}
269305

270306
async close() {

0 commit comments

Comments
 (0)