emailengine-app 2.85.0 → 2.87.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ncurc.js +24 -6
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +97 -0
- package/DOCKER_DEPLOYMENT.md +2 -1
- package/Dockerfile +16 -3
- package/ENVIRONMENT.md +139 -0
- package/README.md +5 -1
- package/config/default.toml +2 -0
- package/copy-static-files.sh +4 -2
- package/data/google-crawlers.json +1 -1
- package/data/public_suffix_list.dat +4 -3
- package/lib/account-assignment.js +19 -0
- package/lib/account.js +285 -195
- package/lib/api-error-codes.js +49 -0
- package/lib/api-reference/model.js +6 -1
- package/lib/api-routes/account-routes.js +91 -32
- package/lib/api-routes/blocklist-routes.js +68 -2
- package/lib/api-routes/delivery-test-routes.js +1 -1
- package/lib/api-routes/gateway-routes.js +82 -10
- package/lib/api-routes/index.js +4 -0
- package/lib/api-routes/license-routes.js +2 -2
- package/lib/api-routes/mailbox-routes.js +3 -3
- package/lib/api-routes/message-routes.js +15 -7
- package/lib/api-routes/oauth2-app-routes.js +15 -8
- package/lib/api-routes/outbox-routes.js +77 -66
- package/lib/api-routes/permission-map.js +55 -6
- package/lib/api-routes/scoped-routes.js +74 -0
- package/lib/api-routes/settings-routes.js +11 -4
- package/lib/api-routes/stats-routes.js +5 -1
- package/lib/api-routes/submit-routes.js +25 -5
- package/lib/api-routes/template-routes.js +7 -6
- package/lib/api-routes/token-routes.js +11 -5
- package/lib/api-routes/webhook-delivery-routes.js +262 -0
- package/lib/api-routes/webhook-route-routes.js +119 -4
- package/lib/calendar-event.js +81 -1
- package/lib/consts.js +38 -46
- package/lib/db.js +14 -6
- package/lib/delivery-error.js +6 -0
- package/lib/deprecations.js +270 -0
- package/lib/email-client/base-client.js +134 -158
- package/lib/email-client/fallback-poll.js +62 -0
- package/lib/email-client/gmail-client.js +59 -11
- package/lib/email-client/imap/adaptive-idle-store.js +73 -0
- package/lib/email-client/imap/adaptive-idle.js +278 -0
- package/lib/email-client/imap/connection-errors.js +14 -1
- package/lib/email-client/imap/idle-settings.js +31 -5
- package/lib/email-client/imap/listing-diff.js +37 -0
- package/lib/email-client/imap/mailbox.js +380 -227
- package/lib/email-client/imap/subconnection.js +7 -29
- package/lib/email-client/imap/sync-operations.js +220 -127
- package/lib/email-client/imap-client.js +227 -163
- package/lib/email-client/message-builder.js +14 -79
- package/lib/email-client/outlook-client.js +378 -338
- package/lib/email-client/smtp-pool-manager.js +16 -13
- package/lib/email-client/sync-state-fields.js +19 -0
- package/lib/encrypt.js +157 -41
- package/lib/enum-descriptions.js +3 -1
- package/lib/env-setting-overrides.js +288 -0
- package/lib/export-worker.js +751 -0
- package/lib/export.js +64 -15
- package/lib/field-usage.js +1 -1
- package/lib/gateway.js +17 -1
- package/lib/get-raw-email.js +35 -108
- package/lib/health.js +75 -0
- package/lib/lock-run.js +119 -0
- package/lib/logger-consts.js +48 -0
- package/lib/logger.js +1 -1
- package/lib/lua/z-head-scores.lua +28 -0
- package/lib/network-errors.js +1 -1
- package/lib/node-version.js +24 -0
- package/lib/notify-attachments.js +20 -1
- package/lib/oauth/gmail.js +54 -29
- package/lib/oauth2-apps.js +630 -655
- package/lib/otp.js +66 -0
- package/lib/outbox.js +184 -54
- package/lib/parse-message.js +3 -3
- package/lib/privileged-settings.js +2 -2
- package/lib/queue-browser/definitions.js +11 -0
- package/lib/queue-browser/index.js +68 -19
- package/lib/queue-job-actions.js +18 -1
- package/lib/queue-retention.js +48 -12
- package/lib/queue-stats.js +11 -2
- package/lib/read-env-value.js +3 -0
- package/lib/redis-memory-warnings.js +99 -0
- package/lib/redis-reconnect.js +29 -0
- package/lib/request-timing.js +23 -0
- package/lib/respawn-backoff.js +35 -0
- package/lib/rewrite-text-nodes.js +19 -59
- package/lib/schemas.js +225 -41
- package/lib/settings.js +6 -2
- package/lib/smtp-auth.js +19 -1
- package/lib/swagger-options.js +2 -2
- package/lib/thread-diagnostics.js +18 -1
- package/lib/tls/provision.js +6 -3
- package/lib/token-audit-log.js +19 -20
- package/lib/token-permission-view.js +4 -3
- package/lib/tools.js +8 -41
- package/lib/ui-routes/admin-config-routes.js +64 -90
- package/lib/ui-routes/admin-entities-routes.js +2 -14
- package/lib/ui-routes/imap-idle-field.js +1 -1
- package/lib/ui-routes/outlook-subscription-view.js +1 -1
- package/lib/ui-routes/overridable-fields.js +84 -0
- package/lib/ui-routes/queue-routes.js +8 -5
- package/lib/ui-routes/settings-page.js +6 -0
- package/lib/utils/decode-headers.js +3 -12
- package/lib/utils/filter-empty.js +20 -0
- package/lib/utils/int-setting.js +48 -0
- package/lib/utils/tracking-settings.js +35 -0
- package/lib/web-safe-html.js +18 -105
- package/lib/webhook-delivery.js +461 -0
- package/lib/webhook-failures.js +132 -0
- package/lib/webhook-request.js +29 -1
- package/lib/webhook-routing.js +30 -1
- package/lib/webhook-test.js +88 -0
- package/lib/webhooks.js +59 -41
- package/lib/worker-rpc-error.js +2 -1
- package/package.json +28 -26
- package/sbom.json +600 -678
- package/server.js +149 -76
- package/static/css/flyonui.css +1 -1
- package/static/js/ace/ace.js +1 -1
- package/static/js/ee-client.js +650 -196
- package/static/licenses.html +49 -59
- package/translations/messages.pot +26 -26
- package/views/config/security.hbs +22 -0
- package/views/config/service.hbs +44 -0
- package/views/config/webhooks.hbs +36 -0
- package/views/layout/app.hbs +9 -6
- package/views/legal.hbs +8 -0
- package/views/partials/queue_retention_note.hbs +12 -8
- package/views/partials/ui/env-managed-alert.hbs +16 -7
- package/workers/api.js +112 -37
- package/workers/export.js +16 -631
- package/workers/imap.js +13 -0
- package/workers/smtp.js +9 -11
- package/workers/webhooks.js +18 -432
- package/lib/email-client/outlook-fallback-poll.js +0 -30
|
@@ -4,6 +4,7 @@ const { BaseClient, metricsMeta } = require('./base-client');
|
|
|
4
4
|
const { isSafePathSegment } = require('../utils/provider-id');
|
|
5
5
|
const { isTransientCredentialError } = require('./credential-errors');
|
|
6
6
|
const { Account } = require('../account');
|
|
7
|
+
const { runLocked, isContention } = require('../lock-run');
|
|
7
8
|
const { checkAccountScopes } = require('../oauth/scope-checker');
|
|
8
9
|
const settings = require('../settings');
|
|
9
10
|
const { oauth2Apps } = require('../oauth2-apps');
|
|
@@ -11,7 +12,7 @@ const getSecret = require('../get-secret');
|
|
|
11
12
|
const msgpack = require('../msgpack');
|
|
12
13
|
const he = require('he');
|
|
13
14
|
const { emitChangeEvent, prepareUrl, collectCidReferences, embedCidImages, truncateTextContents } = require('../tools');
|
|
14
|
-
const { parseFallbackPollInterval, FALLBACK_POLL_INTERVAL } = require('./
|
|
15
|
+
const { parseFallbackPollInterval, OUTLOOK_FALLBACK_POLL_INTERVAL: FALLBACK_POLL_INTERVAL } = require('./fallback-poll');
|
|
15
16
|
const { messageWebSafeHtml, applyWebSafeHtmlOptions } = require('../web-safe-html');
|
|
16
17
|
const crypto = require('crypto');
|
|
17
18
|
const { Gateway } = require('../gateway');
|
|
@@ -21,6 +22,7 @@ const { detectMimeType, detectExtension } = require('nodemailer/lib/mime-funcs/m
|
|
|
21
22
|
const graphApi = require('./outlook/graph-api');
|
|
22
23
|
const { buildRawSendMailRequest, buildStructuredSendMailRequest, buildDraftSendRequest } = require('./outlook/send-request');
|
|
23
24
|
const { pushHeaderValue } = require('../utils/header-map');
|
|
25
|
+
const { getMessageOtp } = require('../otp');
|
|
24
26
|
|
|
25
27
|
const {
|
|
26
28
|
REDIS_PREFIX,
|
|
@@ -36,6 +38,7 @@ const {
|
|
|
36
38
|
OUTLOOK_CLOCK_SKEW_BUFFER,
|
|
37
39
|
OUTLOOK_MISSED_LOOKBACK,
|
|
38
40
|
OUTLOOK_ANNOUNCED_RETENTION,
|
|
41
|
+
OUTLOOK_SYNC_MAX_LOOKBACK,
|
|
39
42
|
MESSAGE_UPDATED_NOTIFY,
|
|
40
43
|
MESSAGE_DELETED_NOTIFY,
|
|
41
44
|
MESSAGE_MISSING_NOTIFY,
|
|
@@ -54,6 +57,22 @@ const RENEW_WATCH_TTL = 60 * 60 * 1000; // 1h
|
|
|
54
57
|
// unannounced ones and announce hours of mail again
|
|
55
58
|
const ANNOUNCED_SINCE_FIELD = 'outlookAnnouncedInSince';
|
|
56
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Where the recovery pass after a flush starts: the flush's `notifyFrom` when it lies in the past,
|
|
62
|
+
* no further back than "Run sync" may look. Null when there is nothing before now to recover
|
|
63
|
+
* @param {string|Date} [notifyFrom] - Stored notifyFrom value
|
|
64
|
+
* @param {number} now - Epoch ms
|
|
65
|
+
* @returns {number|null} Epoch ms
|
|
66
|
+
*/
|
|
67
|
+
function flushRecoveryStart(notifyFrom, now) {
|
|
68
|
+
const time = notifyFrom ? new Date(notifyFrom).getTime() : NaN;
|
|
69
|
+
// a minute of slack, so the default flush ("now", stored a few seconds ago) needs no pass
|
|
70
|
+
if (!Number.isFinite(time) || time >= now - 60 * 1000) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return Math.max(time, now - OUTLOOK_SYNC_MAX_LOOKBACK);
|
|
74
|
+
}
|
|
75
|
+
|
|
57
76
|
// The `serverResponseCode` a failed change subscription is reported with, and the marker that
|
|
58
77
|
// tells the report apart from anything else the account may have stored. Only this client reads
|
|
59
78
|
// it: reportSubscriptionFailure() writes it, clearSubscriptionFailure() lifts it, and
|
|
@@ -569,10 +588,25 @@ class OutlookClient extends BaseClient {
|
|
|
569
588
|
}
|
|
570
589
|
|
|
571
590
|
/**
|
|
572
|
-
*
|
|
591
|
+
* Deletes the account's Graph subscription. Best effort: Graph lets an orphaned subscription
|
|
592
|
+
* expire within three days on its own, it only keeps posting to the notification endpoint and
|
|
593
|
+
* counting against the tenant's subscription quota until then
|
|
594
|
+
* @returns {Promise<boolean>} True when a subscription was deleted
|
|
573
595
|
*/
|
|
574
|
-
async
|
|
575
|
-
|
|
596
|
+
async releaseRemoteResources() {
|
|
597
|
+
try {
|
|
598
|
+
const outlookSubscription = await this.getStoredSubscription();
|
|
599
|
+
if (!outlookSubscription.id) {
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
await this.request(`/subscriptions/${outlookSubscription.id}`, 'DELETE');
|
|
603
|
+
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
604
|
+
this.logger.info({ msg: 'Deleted MS Graph subscription', account: this.account, subscription: outlookSubscription.id });
|
|
605
|
+
return true;
|
|
606
|
+
} catch (err) {
|
|
607
|
+
this.logger.warn({ msg: 'Failed to delete MS Graph subscription', account: this.account, err });
|
|
608
|
+
return false;
|
|
609
|
+
}
|
|
576
610
|
}
|
|
577
611
|
|
|
578
612
|
/**
|
|
@@ -582,15 +616,9 @@ class OutlookClient extends BaseClient {
|
|
|
582
616
|
this.clearTimers();
|
|
583
617
|
this.closed = true;
|
|
584
618
|
|
|
585
|
-
//
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
if (outlookSubscription.id) {
|
|
589
|
-
await this.request(`/subscriptions/${outlookSubscription.id}`, 'DELETE');
|
|
590
|
-
}
|
|
591
|
-
} catch (err) {
|
|
592
|
-
this.logger.warn({ msg: 'Failed to delete MS Graph subscription', account: this.account, err });
|
|
593
|
-
}
|
|
619
|
+
// Normally done already by the teardown that runs before the account record is removed,
|
|
620
|
+
// and then a no-op here
|
|
621
|
+
await this.releaseRemoteResources();
|
|
594
622
|
|
|
595
623
|
if (['init', 'connecting', 'syncing', 'connected'].includes(this.state)) {
|
|
596
624
|
this.state = 'disconnected';
|
|
@@ -601,6 +629,23 @@ class OutlookClient extends BaseClient {
|
|
|
601
629
|
return null;
|
|
602
630
|
}
|
|
603
631
|
|
|
632
|
+
/**
|
|
633
|
+
* Starts syncing again after a flush. The subscription only reports changes made from now on,
|
|
634
|
+
* so a flush that asked for messages from an earlier `notifyFrom` also gets the pass "Run sync"
|
|
635
|
+
* makes, from that point and bounded the same way. A flush clears the announced set, so the
|
|
636
|
+
* messages it finds are announced again, as on an IMAP account
|
|
637
|
+
*/
|
|
638
|
+
async resumeAfterFlush() {
|
|
639
|
+
await this.reconnect();
|
|
640
|
+
|
|
641
|
+
// the one field this needs, read on its own: reconnect() has just loaded the whole account
|
|
642
|
+
const notifyFrom = await this.redis.hget(this.getAccountKey(), 'notifyFrom');
|
|
643
|
+
const since = flushRecoveryStart(notifyFrom, Date.now());
|
|
644
|
+
if (since && !this.closed && this.state === 'connected') {
|
|
645
|
+
await this.syncMailboxes({ since });
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
604
649
|
/**
|
|
605
650
|
* Reconnect to the account (re-initialize)
|
|
606
651
|
*/
|
|
@@ -3535,183 +3580,163 @@ class OutlookClient extends BaseClient {
|
|
|
3535
3580
|
return { success: false, reason: 'renewal_in_progress' };
|
|
3536
3581
|
}
|
|
3537
3582
|
|
|
3538
|
-
const
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
if (!skipLock) {
|
|
3542
|
-
try {
|
|
3543
|
-
renewLock = await lock.acquireLock(lockId, OUTLOOK_SUBSCRIPTION_LOCK_TTL);
|
|
3544
|
-
} catch (err) {
|
|
3545
|
-
this.logger.error({ msg: 'Failed to acquire subscription renew lock', account: this.account, err });
|
|
3546
|
-
this.scheduleSubscriptionRetry({
|
|
3547
|
-
delayMs: OUTLOOK_SUBSCRIPTION_RETRY_BASE_DELAY * 1000,
|
|
3548
|
-
reason: 'lock_error',
|
|
3549
|
-
errorMsg: 'Subscription renew retry after lock error failed',
|
|
3550
|
-
fn: () => this.renewSubscription({ force: false })
|
|
3551
|
-
});
|
|
3552
|
-
return { success: false, reason: 'lock_error' };
|
|
3553
|
-
}
|
|
3554
|
-
|
|
3555
|
-
if (!renewLock.success) {
|
|
3556
|
-
this.logger.info({
|
|
3557
|
-
msg: 'Subscription renewal skipped',
|
|
3558
|
-
reason: 'lock_exists',
|
|
3559
|
-
account: this.account
|
|
3560
|
-
});
|
|
3561
|
-
return { success: false, reason: 'lock_exists' };
|
|
3562
|
-
}
|
|
3563
|
-
}
|
|
3583
|
+
const renew = async () => {
|
|
3584
|
+
// Set in-memory flag to prevent concurrent calls
|
|
3585
|
+
this.renewalInProgress = true;
|
|
3564
3586
|
|
|
3565
|
-
|
|
3566
|
-
|
|
3587
|
+
try {
|
|
3588
|
+
// Get current subscription
|
|
3589
|
+
let outlookSubscription = await this.getStoredSubscription();
|
|
3567
3590
|
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3591
|
+
// Check if subscription exists and is valid
|
|
3592
|
+
if (!outlookSubscription.id) {
|
|
3593
|
+
this.subscriptionState = 'unset';
|
|
3594
|
+
return { success: false, reason: 'no_subscription' };
|
|
3595
|
+
}
|
|
3571
3596
|
|
|
3572
|
-
|
|
3573
|
-
if (!outlookSubscription.id) {
|
|
3574
|
-
this.subscriptionState = 'unset';
|
|
3575
|
-
return { success: false, reason: 'no_subscription' };
|
|
3576
|
-
}
|
|
3597
|
+
let existingExpirationDateTime = this.parseExpirationDate(outlookSubscription.expirationDateTime);
|
|
3577
3598
|
|
|
3578
|
-
|
|
3599
|
+
const now = Date.now();
|
|
3579
3600
|
|
|
3580
|
-
|
|
3601
|
+
// Check if already expired (with buffer for clock skew)
|
|
3602
|
+
if (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_CLOCK_SKEW_BUFFER) {
|
|
3603
|
+
this.logger.warn({
|
|
3604
|
+
msg: 'Subscription already expired or expiring imminently',
|
|
3605
|
+
subscriptionId: outlookSubscription.id,
|
|
3606
|
+
expirationDateTime: outlookSubscription.expirationDateTime,
|
|
3607
|
+
account: this.account,
|
|
3608
|
+
msUntilExpiry: existingExpirationDateTime.getTime() - now
|
|
3609
|
+
});
|
|
3610
|
+
// Clear the expired subscription
|
|
3611
|
+
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
3612
|
+
this.subscriptionState = 'expired';
|
|
3613
|
+
return { success: false, reason: 'expired' };
|
|
3614
|
+
}
|
|
3581
3615
|
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
this.logger.warn({
|
|
3585
|
-
msg: 'Subscription already expired or expiring imminently',
|
|
3586
|
-
subscriptionId: outlookSubscription.id,
|
|
3587
|
-
expirationDateTime: outlookSubscription.expirationDateTime,
|
|
3588
|
-
account: this.account,
|
|
3589
|
-
msUntilExpiry: existingExpirationDateTime.getTime() - now
|
|
3590
|
-
});
|
|
3591
|
-
// Clear the expired subscription
|
|
3592
|
-
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
3593
|
-
this.subscriptionState = 'expired';
|
|
3594
|
-
return { success: false, reason: 'expired' };
|
|
3595
|
-
}
|
|
3616
|
+
// Check if renewal is needed
|
|
3617
|
+
const shouldRenew = force || (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_EXPIRATION_RENEW_TIME);
|
|
3596
3618
|
|
|
3597
|
-
|
|
3598
|
-
|
|
3619
|
+
if (!shouldRenew) {
|
|
3620
|
+
this.logger.debug({
|
|
3621
|
+
msg: 'Subscription renewal not needed',
|
|
3622
|
+
subscriptionId: outlookSubscription.id,
|
|
3623
|
+
expirationDateTime: outlookSubscription.expirationDateTime,
|
|
3624
|
+
account: this.account
|
|
3625
|
+
});
|
|
3626
|
+
this.subscriptionState = 'valid';
|
|
3627
|
+
// A subscription that is still good is the same recovery as one just renewed
|
|
3628
|
+
await this.clearSubscriptionFailure();
|
|
3629
|
+
return { success: true, reason: 'not_needed' };
|
|
3630
|
+
}
|
|
3599
3631
|
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
account: this.account
|
|
3606
|
-
});
|
|
3607
|
-
this.subscriptionState = 'valid';
|
|
3608
|
-
// A subscription that is still good is the same recovery as one just renewed
|
|
3609
|
-
await this.clearSubscriptionFailure();
|
|
3610
|
-
return { success: true, reason: 'not_needed' };
|
|
3611
|
-
}
|
|
3632
|
+
// Perform renewal
|
|
3633
|
+
const expirationDateTime = new Date(now + OUTLOOK_EXPIRATION_TIME);
|
|
3634
|
+
const subscriptionPayload = {
|
|
3635
|
+
expirationDateTime: expirationDateTime.toISOString()
|
|
3636
|
+
};
|
|
3612
3637
|
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
|
|
3616
|
-
expirationDateTime: expirationDateTime.toISOString()
|
|
3617
|
-
};
|
|
3638
|
+
// Capture previous retry counts before overwriting state
|
|
3639
|
+
const previousRetryCount = outlookSubscription.state?.retryCount || 0;
|
|
3640
|
+
const previousCreateRetryCount = outlookSubscription.state?.createRetryCount || 0;
|
|
3618
3641
|
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3642
|
+
// Update state to renewing
|
|
3643
|
+
outlookSubscription.state = {
|
|
3644
|
+
state: 'renewing',
|
|
3645
|
+
time: Date.now()
|
|
3646
|
+
};
|
|
3647
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3648
|
+
this.subscriptionState = 'pending';
|
|
3622
3649
|
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
state: 'renewing',
|
|
3626
|
-
time: Date.now()
|
|
3627
|
-
};
|
|
3628
|
-
await this.saveStoredSubscription(outlookSubscription);
|
|
3629
|
-
this.subscriptionState = 'pending';
|
|
3650
|
+
try {
|
|
3651
|
+
const subscriptionRes = await this.request(`/subscriptions/${outlookSubscription.id}`, 'PATCH', subscriptionPayload);
|
|
3630
3652
|
|
|
3631
|
-
|
|
3632
|
-
|
|
3653
|
+
if (subscriptionRes?.expirationDateTime) {
|
|
3654
|
+
outlookSubscription.expirationDateTime = subscriptionRes.expirationDateTime;
|
|
3655
|
+
outlookSubscription.state = {
|
|
3656
|
+
state: 'created',
|
|
3657
|
+
time: Date.now(),
|
|
3658
|
+
retryCount: 0,
|
|
3659
|
+
createRetryCount: 0,
|
|
3660
|
+
error: null
|
|
3661
|
+
};
|
|
3633
3662
|
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
state: 'created',
|
|
3638
|
-
time: Date.now(),
|
|
3639
|
-
retryCount: 0,
|
|
3640
|
-
createRetryCount: 0,
|
|
3641
|
-
error: null
|
|
3642
|
-
};
|
|
3663
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3664
|
+
this.subscriptionState = 'valid';
|
|
3665
|
+
await this.clearSubscriptionFailure();
|
|
3643
3666
|
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3667
|
+
this.logger.info({
|
|
3668
|
+
msg: previousRetryCount > 0 ? 'Subscription renewed successfully after retry' : 'Subscription renewed successfully',
|
|
3669
|
+
subscriptionId: outlookSubscription.id,
|
|
3670
|
+
newExpirationDateTime: subscriptionRes.expirationDateTime,
|
|
3671
|
+
account: this.account,
|
|
3672
|
+
previousRetryCount
|
|
3673
|
+
});
|
|
3647
3674
|
|
|
3648
|
-
|
|
3649
|
-
|
|
3675
|
+
return { success: true, expirationDateTime: subscriptionRes.expirationDateTime };
|
|
3676
|
+
} else {
|
|
3677
|
+
throw new Error('Empty server response');
|
|
3678
|
+
}
|
|
3679
|
+
} catch (err) {
|
|
3680
|
+
this.logger.warn({
|
|
3681
|
+
msg: 'Subscription renewal failed',
|
|
3650
3682
|
subscriptionId: outlookSubscription.id,
|
|
3651
|
-
newExpirationDateTime: subscriptionRes.expirationDateTime,
|
|
3652
3683
|
account: this.account,
|
|
3653
|
-
|
|
3684
|
+
err
|
|
3654
3685
|
});
|
|
3655
3686
|
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
err
|
|
3666
|
-
});
|
|
3667
|
-
|
|
3668
|
-
outlookSubscription.state = {
|
|
3669
|
-
state: 'error',
|
|
3670
|
-
error: `Subscription renewal failed: ${err.oauthRequest?.response?.error?.message || err.message}`,
|
|
3671
|
-
time: Date.now(),
|
|
3672
|
-
retryCount: previousRetryCount + 1,
|
|
3673
|
-
createRetryCount: previousCreateRetryCount
|
|
3674
|
-
};
|
|
3675
|
-
await this.saveStoredSubscription(outlookSubscription);
|
|
3676
|
-
this.subscriptionState = 'failed';
|
|
3687
|
+
outlookSubscription.state = {
|
|
3688
|
+
state: 'error',
|
|
3689
|
+
error: `Subscription renewal failed: ${err.oauthRequest?.response?.error?.message || err.message}`,
|
|
3690
|
+
time: Date.now(),
|
|
3691
|
+
retryCount: previousRetryCount + 1,
|
|
3692
|
+
createRetryCount: previousCreateRetryCount
|
|
3693
|
+
};
|
|
3694
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3695
|
+
this.subscriptionState = 'failed';
|
|
3677
3696
|
|
|
3678
|
-
|
|
3679
|
-
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
3697
|
+
// Schedule retry with exponential backoff
|
|
3698
|
+
const retryCount = outlookSubscription.state.retryCount;
|
|
3699
|
+
if (retryCount < OUTLOOK_MAX_RETRY_ATTEMPTS) {
|
|
3700
|
+
const retryDelay =
|
|
3701
|
+
Math.min(OUTLOOK_SUBSCRIPTION_RETRY_BASE_DELAY * Math.pow(2, retryCount - 1), OUTLOOK_SUBSCRIPTION_RETRY_MAX_DELAY) * 1000;
|
|
3683
3702
|
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
|
|
3690
|
-
|
|
3703
|
+
this.scheduleSubscriptionRetry({
|
|
3704
|
+
delayMs: retryDelay,
|
|
3705
|
+
reason: 'renewal_failure',
|
|
3706
|
+
errorMsg: 'Subscription renewal retry failed',
|
|
3707
|
+
fn: () => this.renewSubscription({ force }),
|
|
3708
|
+
retryAttempt: retryCount
|
|
3709
|
+
});
|
|
3691
3710
|
|
|
3692
|
-
|
|
3693
|
-
|
|
3694
|
-
|
|
3695
|
-
|
|
3696
|
-
|
|
3697
|
-
|
|
3698
|
-
|
|
3699
|
-
|
|
3700
|
-
|
|
3711
|
+
this.logger.info({
|
|
3712
|
+
msg: 'Scheduling subscription renewal retry',
|
|
3713
|
+
account: this.account,
|
|
3714
|
+
retryAttempt: retryCount,
|
|
3715
|
+
retryDelayMs: retryDelay
|
|
3716
|
+
});
|
|
3717
|
+
} else {
|
|
3718
|
+
await this.reportSubscriptionFailure('renewal', outlookSubscription.state, retryCount);
|
|
3719
|
+
}
|
|
3701
3720
|
|
|
3702
|
-
|
|
3703
|
-
}
|
|
3704
|
-
} finally {
|
|
3705
|
-
// Always release the lock and clear in-memory flag
|
|
3706
|
-
this.renewalInProgress = false;
|
|
3707
|
-
if (!skipLock && renewLock?.success) {
|
|
3708
|
-
try {
|
|
3709
|
-
await lock.releaseLock(renewLock);
|
|
3710
|
-
} catch (err) {
|
|
3711
|
-
this.logger.error({ msg: 'Failed to release subscription renew lock', account: this.account, err });
|
|
3721
|
+
return { success: false, reason: 'renewal_failed', error: err.message };
|
|
3712
3722
|
}
|
|
3723
|
+
} finally {
|
|
3724
|
+
this.renewalInProgress = false;
|
|
3713
3725
|
}
|
|
3726
|
+
};
|
|
3727
|
+
|
|
3728
|
+
if (skipLock) {
|
|
3729
|
+
return await renew();
|
|
3714
3730
|
}
|
|
3731
|
+
|
|
3732
|
+
// One try at the lock: a renewal already running elsewhere is left to finish
|
|
3733
|
+
return await this.withSubscriptionLock(renew, {
|
|
3734
|
+
action: 'renew',
|
|
3735
|
+
skippedMsg: 'Subscription renewal skipped',
|
|
3736
|
+
retry: () => this.renewSubscription({ force: false }),
|
|
3737
|
+
skipped: { success: false, reason: 'lock_exists' },
|
|
3738
|
+
failed: { success: false, reason: 'lock_error' }
|
|
3739
|
+
});
|
|
3715
3740
|
}
|
|
3716
3741
|
|
|
3717
3742
|
/**
|
|
@@ -3741,6 +3766,35 @@ class OutlookClient extends BaseClient {
|
|
|
3741
3766
|
}
|
|
3742
3767
|
}
|
|
3743
3768
|
|
|
3769
|
+
/**
|
|
3770
|
+
* Runs fn under the account's subscription lock, with one try at it: a renewal or creation
|
|
3771
|
+
* already running elsewhere is left to finish (answered with `skipped`), and a lock that could
|
|
3772
|
+
* not be taken at all schedules `retry` (answered with `failed`)
|
|
3773
|
+
* @param {Function} fn
|
|
3774
|
+
* @param {Object} opts
|
|
3775
|
+
* @param {string} opts.action - 'renew' or 'ensure', for the log
|
|
3776
|
+
* @param {string} opts.skippedMsg - Log message when another holder has the lock
|
|
3777
|
+
* @param {Function} opts.retry - What to retry after a lock error
|
|
3778
|
+
* @param {*} [opts.skipped] - Result when another holder has the lock
|
|
3779
|
+
* @param {*} [opts.failed] - Result when the lock could not be taken
|
|
3780
|
+
*/
|
|
3781
|
+
async withSubscriptionLock(fn, { action, skippedMsg, retry, skipped, failed }) {
|
|
3782
|
+
return await runLocked(this.accountObject.getLock(), this.getSubscriptionLockId(), { ttl: OUTLOOK_SUBSCRIPTION_LOCK_TTL, wait: false }, fn, err => {
|
|
3783
|
+
if (isContention(err)) {
|
|
3784
|
+
this.logger.info({ msg: skippedMsg, reason: 'lock_exists', account: this.account });
|
|
3785
|
+
return skipped;
|
|
3786
|
+
}
|
|
3787
|
+
this.logger.error({ msg: `Failed to acquire subscription ${action} lock`, account: this.account, err });
|
|
3788
|
+
this.scheduleSubscriptionRetry({
|
|
3789
|
+
delayMs: OUTLOOK_SUBSCRIPTION_RETRY_BASE_DELAY * 1000,
|
|
3790
|
+
reason: 'lock_error',
|
|
3791
|
+
errorMsg: `Subscription ${action} retry after lock error failed`,
|
|
3792
|
+
fn: retry
|
|
3793
|
+
});
|
|
3794
|
+
return failed;
|
|
3795
|
+
});
|
|
3796
|
+
}
|
|
3797
|
+
|
|
3744
3798
|
/**
|
|
3745
3799
|
* Ensure webhook subscription exists and is valid
|
|
3746
3800
|
* Creates new subscription or renews existing one
|
|
@@ -3750,8 +3804,9 @@ class OutlookClient extends BaseClient {
|
|
|
3750
3804
|
return;
|
|
3751
3805
|
}
|
|
3752
3806
|
|
|
3753
|
-
|
|
3754
|
-
let
|
|
3807
|
+
const urls = await settings.getMulti('serviceUrl', 'notificationBaseUrl');
|
|
3808
|
+
let serviceUrl = urls.serviceUrl || null;
|
|
3809
|
+
let notificationBaseUrl = urls.notificationBaseUrl || serviceUrl || '';
|
|
3755
3810
|
|
|
3756
3811
|
if (!serviceUrl) {
|
|
3757
3812
|
this.logger.error({ msg: 'Cannot create MS Graph subscription, service URL is not set', account: this.account });
|
|
@@ -3759,201 +3814,181 @@ class OutlookClient extends BaseClient {
|
|
|
3759
3814
|
return false;
|
|
3760
3815
|
}
|
|
3761
3816
|
|
|
3762
|
-
//
|
|
3763
|
-
|
|
3764
|
-
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
|
|
3817
|
+
// A creation already running elsewhere is left to finish. Do not forward clearExisting on a
|
|
3818
|
+
// retry after a lock error: by the time it fires, another path may have created a valid
|
|
3819
|
+
// subscription that should not be cleared
|
|
3820
|
+
return await this.withSubscriptionLock(() => this.ensureSubscriptionLocked({ clearExisting, serviceUrl, notificationBaseUrl }), {
|
|
3821
|
+
action: 'ensure',
|
|
3822
|
+
skippedMsg: 'Subscription creation skipped',
|
|
3823
|
+
retry: () => this.ensureSubscription()
|
|
3824
|
+
});
|
|
3825
|
+
}
|
|
3826
|
+
|
|
3827
|
+
/**
|
|
3828
|
+
* The part of ensureSubscription() that runs under the subscription lock
|
|
3829
|
+
* @param {Object} opts
|
|
3830
|
+
* @param {boolean} opts.clearExisting
|
|
3831
|
+
* @param {string|null} opts.serviceUrl
|
|
3832
|
+
* @param {string} opts.notificationBaseUrl
|
|
3833
|
+
*/
|
|
3834
|
+
async ensureSubscriptionLocked({ clearExisting, serviceUrl, notificationBaseUrl }) {
|
|
3835
|
+
if (clearExisting) {
|
|
3836
|
+
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
3782
3837
|
this.logger.info({
|
|
3783
|
-
msg: '
|
|
3784
|
-
reason: 'lock_exists',
|
|
3838
|
+
msg: 'Cleared existing subscription data for re-creation',
|
|
3785
3839
|
account: this.account
|
|
3786
3840
|
});
|
|
3787
|
-
return;
|
|
3788
3841
|
}
|
|
3789
3842
|
|
|
3790
|
-
|
|
3791
|
-
if (clearExisting) {
|
|
3792
|
-
await this.redis.hdel(this.getAccountKey(), 'outlookSubscription');
|
|
3793
|
-
this.logger.info({
|
|
3794
|
-
msg: 'Cleared existing subscription data for re-creation',
|
|
3795
|
-
account: this.account
|
|
3796
|
-
});
|
|
3797
|
-
}
|
|
3798
|
-
|
|
3799
|
-
let outlookSubscription = await this.getStoredSubscription();
|
|
3843
|
+
let outlookSubscription = await this.getStoredSubscription();
|
|
3800
3844
|
|
|
3801
|
-
|
|
3802
|
-
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3845
|
+
// If state is creating/renewing, the previous lock holder crashed before completing.
|
|
3846
|
+
// Since we hold the lock, no other operation is in progress — reset to a clean state.
|
|
3847
|
+
if (['creating', 'renewing'].includes(outlookSubscription.state?.state)) {
|
|
3848
|
+
this.logger.warn({
|
|
3849
|
+
msg: 'Found stale subscription state from crashed operation, resetting state',
|
|
3850
|
+
subscriptionId: outlookSubscription?.id,
|
|
3851
|
+
state: outlookSubscription.state?.state,
|
|
3852
|
+
stateTime: outlookSubscription.state?.time,
|
|
3853
|
+
account: this.account
|
|
3854
|
+
});
|
|
3811
3855
|
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3856
|
+
// Reset transient state so downstream logic sees a clean state
|
|
3857
|
+
outlookSubscription.state.state = outlookSubscription.id ? 'created' : 'error';
|
|
3858
|
+
outlookSubscription.state.time = Date.now();
|
|
3859
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3860
|
+
}
|
|
3817
3861
|
|
|
3818
|
-
|
|
3819
|
-
|
|
3862
|
+
let now = Date.now();
|
|
3863
|
+
let expirationDateTime = new Date(now + OUTLOOK_EXPIRATION_TIME);
|
|
3820
3864
|
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3865
|
+
// Renew existing subscription if needed
|
|
3866
|
+
if (outlookSubscription.id) {
|
|
3867
|
+
let existingExpirationDateTime = this.parseExpirationDate(outlookSubscription.expirationDateTime);
|
|
3824
3868
|
|
|
3825
|
-
|
|
3826
|
-
|
|
3869
|
+
if (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_CLOCK_SKEW_BUFFER) {
|
|
3870
|
+
// already expired (with buffer for clock skew)
|
|
3871
|
+
outlookSubscription = {};
|
|
3872
|
+
} else if (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_EXPIRATION_RENEW_TIME) {
|
|
3873
|
+
// Use the unified renewal method (skip lock since we already hold it)
|
|
3874
|
+
const renewalResult = await this.renewSubscription({ force: false, skipLock: true });
|
|
3875
|
+
if (!renewalResult.success && renewalResult.reason === 'expired') {
|
|
3876
|
+
// Subscription expired, clear it to create a new one
|
|
3827
3877
|
outlookSubscription = {};
|
|
3828
|
-
} else if (existingExpirationDateTime && existingExpirationDateTime.getTime() < now + OUTLOOK_EXPIRATION_RENEW_TIME) {
|
|
3829
|
-
// Use the unified renewal method (skip lock since we already hold it)
|
|
3830
|
-
const renewalResult = await this.renewSubscription({ force: false, skipLock: true });
|
|
3831
|
-
if (!renewalResult.success && renewalResult.reason === 'expired') {
|
|
3832
|
-
// Subscription expired, clear it to create a new one
|
|
3833
|
-
outlookSubscription = {};
|
|
3834
|
-
} else {
|
|
3835
|
-
return renewalResult.success;
|
|
3836
|
-
}
|
|
3837
3878
|
} else {
|
|
3838
|
-
|
|
3839
|
-
this.logger.debug({
|
|
3840
|
-
msg: 'Subscription renewal skipped',
|
|
3841
|
-
reason: 'valid',
|
|
3842
|
-
subscriptionId: outlookSubscription?.id,
|
|
3843
|
-
state: outlookSubscription.state?.state,
|
|
3844
|
-
created: outlookSubscription.state?.time,
|
|
3845
|
-
expirationDateTime: outlookSubscription.expirationDateTime
|
|
3846
|
-
});
|
|
3847
|
-
this.subscriptionState = 'valid';
|
|
3848
|
-
// A subscription that is still good is the same recovery as one just created
|
|
3849
|
-
await this.clearSubscriptionFailure();
|
|
3850
|
-
return;
|
|
3879
|
+
return renewalResult.success;
|
|
3851
3880
|
}
|
|
3881
|
+
} else {
|
|
3882
|
+
// Subscription is valid, do nothing
|
|
3883
|
+
this.logger.debug({
|
|
3884
|
+
msg: 'Subscription renewal skipped',
|
|
3885
|
+
reason: 'valid',
|
|
3886
|
+
subscriptionId: outlookSubscription?.id,
|
|
3887
|
+
state: outlookSubscription.state?.state,
|
|
3888
|
+
created: outlookSubscription.state?.time,
|
|
3889
|
+
expirationDateTime: outlookSubscription.expirationDateTime
|
|
3890
|
+
});
|
|
3891
|
+
this.subscriptionState = 'valid';
|
|
3892
|
+
// A subscription that is still good is the same recovery as one just created
|
|
3893
|
+
await this.clearSubscriptionFailure();
|
|
3894
|
+
return;
|
|
3852
3895
|
}
|
|
3896
|
+
}
|
|
3853
3897
|
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3898
|
+
// Create new subscription if needed
|
|
3899
|
+
if (!outlookSubscription.id) {
|
|
3900
|
+
const notificationUrl = prepareUrl('/oauth/msg/notification', notificationBaseUrl, {
|
|
3901
|
+
account: this.account
|
|
3902
|
+
});
|
|
3859
3903
|
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3904
|
+
const lifecycleNotificationUrl = prepareUrl('/oauth/msg/lifecycle', notificationBaseUrl, {
|
|
3905
|
+
account: this.account
|
|
3906
|
+
});
|
|
3863
3907
|
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3908
|
+
const subscriptionPayload = {
|
|
3909
|
+
changeType: 'created,updated,deleted',
|
|
3910
|
+
notificationUrl,
|
|
3911
|
+
lifecycleNotificationUrl,
|
|
3912
|
+
resource: `/${this.oauth2UserPath}/messages`,
|
|
3913
|
+
expirationDateTime: expirationDateTime.toISOString(),
|
|
3914
|
+
clientState: crypto.randomUUID()
|
|
3915
|
+
};
|
|
3872
3916
|
|
|
3873
|
-
|
|
3874
|
-
|
|
3917
|
+
// Capture previous create retry count before overwriting state
|
|
3918
|
+
const previousCreateRetryCount = outlookSubscription.state?.createRetryCount || 0;
|
|
3875
3919
|
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3920
|
+
outlookSubscription.state = {
|
|
3921
|
+
state: 'creating',
|
|
3922
|
+
time: Date.now()
|
|
3923
|
+
};
|
|
3924
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3925
|
+
this.subscriptionState = 'pending';
|
|
3882
3926
|
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3895
|
-
|
|
3896
|
-
|
|
3897
|
-
};
|
|
3898
|
-
this.subscriptionState = 'valid';
|
|
3899
|
-
await this.clearSubscriptionFailure();
|
|
3900
|
-
} else {
|
|
3901
|
-
throw new Error('Empty server response');
|
|
3902
|
-
}
|
|
3903
|
-
} catch (err) {
|
|
3904
|
-
const errorMessage = err.oauthRequest?.response?.error?.message || err.message;
|
|
3905
|
-
this.logger.warn({
|
|
3906
|
-
msg: 'Failed to create MS Graph subscription',
|
|
3907
|
-
account: this.account,
|
|
3908
|
-
error: errorMessage,
|
|
3909
|
-
code: err.oauthRequest?.response?.error?.code,
|
|
3910
|
-
statusCode: err.oauthRequest?.status
|
|
3911
|
-
});
|
|
3912
|
-
outlookSubscription.state = {
|
|
3913
|
-
state: 'error',
|
|
3914
|
-
error: `Subscription failed: ${errorMessage}`,
|
|
3915
|
-
time: Date.now(),
|
|
3916
|
-
createRetryCount: previousCreateRetryCount + 1
|
|
3927
|
+
let subscriptionRes;
|
|
3928
|
+
try {
|
|
3929
|
+
subscriptionRes = await this.request(`/subscriptions`, 'post', subscriptionPayload);
|
|
3930
|
+
if (subscriptionRes?.expirationDateTime) {
|
|
3931
|
+
outlookSubscription = {
|
|
3932
|
+
id: subscriptionRes.id,
|
|
3933
|
+
expirationDateTime: subscriptionRes.expirationDateTime,
|
|
3934
|
+
clientState: subscriptionRes.clientState,
|
|
3935
|
+
state: {
|
|
3936
|
+
state: 'created',
|
|
3937
|
+
time: Date.now(),
|
|
3938
|
+
createRetryCount: 0,
|
|
3939
|
+
retryCount: 0
|
|
3940
|
+
}
|
|
3917
3941
|
};
|
|
3918
|
-
this.subscriptionState = '
|
|
3919
|
-
|
|
3920
|
-
|
|
3942
|
+
this.subscriptionState = 'valid';
|
|
3943
|
+
await this.clearSubscriptionFailure();
|
|
3944
|
+
} else {
|
|
3945
|
+
throw new Error('Empty server response');
|
|
3921
3946
|
}
|
|
3947
|
+
} catch (err) {
|
|
3948
|
+
const errorMessage = err.oauthRequest?.response?.error?.message || err.message;
|
|
3949
|
+
this.logger.warn({
|
|
3950
|
+
msg: 'Failed to create MS Graph subscription',
|
|
3951
|
+
account: this.account,
|
|
3952
|
+
error: errorMessage,
|
|
3953
|
+
code: err.oauthRequest?.response?.error?.code,
|
|
3954
|
+
statusCode: err.oauthRequest?.status
|
|
3955
|
+
});
|
|
3956
|
+
outlookSubscription.state = {
|
|
3957
|
+
state: 'error',
|
|
3958
|
+
error: `Subscription failed: ${errorMessage}`,
|
|
3959
|
+
time: Date.now(),
|
|
3960
|
+
createRetryCount: previousCreateRetryCount + 1
|
|
3961
|
+
};
|
|
3962
|
+
this.subscriptionState = 'failed';
|
|
3963
|
+
} finally {
|
|
3964
|
+
await this.saveStoredSubscription(outlookSubscription);
|
|
3965
|
+
}
|
|
3922
3966
|
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3967
|
+
// Schedule retry with exponential backoff if creation failed
|
|
3968
|
+
if (outlookSubscription.state?.state === 'error') {
|
|
3969
|
+
const createRetryCount = outlookSubscription.state.createRetryCount || 0;
|
|
3970
|
+
if (createRetryCount < OUTLOOK_MAX_RETRY_ATTEMPTS) {
|
|
3971
|
+
const retryDelay =
|
|
3972
|
+
Math.min(OUTLOOK_SUBSCRIPTION_RETRY_BASE_DELAY * Math.pow(2, createRetryCount - 1), OUTLOOK_SUBSCRIPTION_RETRY_MAX_DELAY) * 1000;
|
|
3929
3973
|
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
|
|
3974
|
+
this.scheduleSubscriptionRetry({
|
|
3975
|
+
delayMs: retryDelay,
|
|
3976
|
+
reason: 'creation_failure',
|
|
3977
|
+
errorMsg: 'Subscription creation retry failed',
|
|
3978
|
+
fn: () => this.ensureSubscription(),
|
|
3979
|
+
retryAttempt: createRetryCount
|
|
3980
|
+
});
|
|
3937
3981
|
|
|
3938
|
-
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
}
|
|
3949
|
-
}
|
|
3950
|
-
}
|
|
3951
|
-
} finally {
|
|
3952
|
-
if (ensureLock?.success) {
|
|
3953
|
-
try {
|
|
3954
|
-
await lock.releaseLock(ensureLock);
|
|
3955
|
-
} catch (err) {
|
|
3956
|
-
this.logger.error({ msg: 'Failed to release subscription ensure lock', account: this.account, err });
|
|
3982
|
+
this.logger.info({
|
|
3983
|
+
msg: 'Scheduling subscription creation retry',
|
|
3984
|
+
account: this.account,
|
|
3985
|
+
retryAttempt: createRetryCount,
|
|
3986
|
+
retryDelayMs: retryDelay
|
|
3987
|
+
});
|
|
3988
|
+
} else {
|
|
3989
|
+
// End of the fast ladder, not the end of trying: renewOrCreateSubscription()
|
|
3990
|
+
// clears the counters and starts a new one on the next hourly pass
|
|
3991
|
+
await this.reportSubscriptionFailure('creation', outlookSubscription.state, createRetryCount);
|
|
3957
3992
|
}
|
|
3958
3993
|
}
|
|
3959
3994
|
}
|
|
@@ -4085,6 +4120,11 @@ class OutlookClient extends BaseClient {
|
|
|
4085
4120
|
message.isAutoReply = true;
|
|
4086
4121
|
}
|
|
4087
4122
|
|
|
4123
|
+
const otp = getMessageOtp(message.headers, this.summaryProvider());
|
|
4124
|
+
if (otp) {
|
|
4125
|
+
message.otp = otp;
|
|
4126
|
+
}
|
|
4127
|
+
|
|
4088
4128
|
// Process attachments
|
|
4089
4129
|
if (messageData.attachments?.length) {
|
|
4090
4130
|
let cidReferences = collectCidReferences(message.text.html);
|
|
@@ -4322,7 +4362,7 @@ class OutlookClient extends BaseClient {
|
|
|
4322
4362
|
|
|
4323
4363
|
async _updateLastNotificationTime() {
|
|
4324
4364
|
try {
|
|
4325
|
-
await this.redis.
|
|
4365
|
+
await this.redis.hSetExists(this.getAccountKey(), 'outlookLastNotification', Date.now().toString());
|
|
4326
4366
|
} catch (err) {
|
|
4327
4367
|
this.logger.error({ msg: 'Failed to update last notification time', err });
|
|
4328
4368
|
}
|
|
@@ -5011,4 +5051,4 @@ class OutlookClient extends BaseClient {
|
|
|
5011
5051
|
}
|
|
5012
5052
|
}
|
|
5013
5053
|
|
|
5014
|
-
module.exports = { OutlookClient, encodeFolderSegment, decodeFolderSegment, parseFallbackPollInterval };
|
|
5054
|
+
module.exports = { OutlookClient, encodeFolderSegment, decodeFolderSegment, parseFallbackPollInterval, flushRecoveryStart };
|