Skip to content

Commit

Permalink
fix(gmail-api): Fixed webhook notifications for authenticationSuccess…
Browse files Browse the repository at this point in the history
… and authenticationError
  • Loading branch information
andris9 committed Jul 21, 2024
1 parent 7dc6e96 commit 2c3d63a
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions lib/api-client/gmail-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { BaseClient } = require('./base-client');
const settings = require('../settings');
const { arfDetect } = require('../arf-detect');
const { bounceDetect } = require('../bounce-detect');
const { filterEmptyObjectValues } = require('../tools');
const { filterEmptyObjectValues, emitChangeEvent } = require('../tools');
const simpleParser = require('mailparser').simpleParser;
const ical = require('ical.js');
const { llmPreProcess } = require('../llm-pre-process');
Expand All @@ -27,7 +27,8 @@ const {
MESSAGE_NEW_NOTIFY,
EMAIL_BOUNCE_NOTIFY,
EMAIL_COMPLAINT_NOTIFY,
AUTH_ERROR_NOTIFY
AUTH_ERROR_NOTIFY,
AUTH_SUCCESS_NOTIFY
} = require('../consts');

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

err.authenticationFailed = true;

await this.notify(false, AUTH_ERROR_NOTIFY, {
response: err.oauthRequest?.response?.message || err.response,
response: err.oauthRequest?.response?.error?.message || err.response,
serverResponseCode: 'ApiRequestError'
});

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

this.state = 'connected';
await this.setStateVal();

let prevConnectedCount = await this.redis.hget(this.getAccountKey(), `state:count:connected`);
let isFirstSuccessfulConnection = prevConnectedCount === '0'; // string zero means the account has been initialized but not yet connected

let isiInitial = !!isFirstSuccessfulConnection;

if (!isFirstSuccessfulConnection) {
// check if the connection was previously in an errored state
let prevLastErrorState = await this.redis.hget(this.getAccountKey(), 'lastErrorState');
if (prevLastErrorState) {
try {
prevLastErrorState = JSON.parse(prevLastErrorState);
} catch (err) {
// ignore
}
}

if (prevLastErrorState && typeof prevLastErrorState === 'object' && Object.keys(prevLastErrorState).length) {
// was previously errored
isFirstSuccessfulConnection = true;
}
}

if (isFirstSuccessfulConnection) {
this.logger.info({ msg: 'Successful login without a previous active session', account: this.account, isiInitial, prevActive: false });
await this.notify(false, AUTH_SUCCESS_NOTIFY, {
user: accountData.oauth2?.auth?.user
});
} else {
this.logger.info({ msg: 'Successful login with a previous active session', account: this.account, isiInitial, prevActive: true });
}

await this.redis.hSetExists(this.getAccountKey(), 'lastErrorState', '{}');
await emitChangeEvent(this.logger, this.account, 'state', this.state);
}

async close() {
Expand Down

0 comments on commit 2c3d63a

Please sign in to comment.