Skip to content

Commit

Permalink
fix(sync): send webhooks for old messages during first sync
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Apr 19, 2024
1 parent 7646db2 commit 5d05986
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/imap-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ class IMAPConnection extends BaseClient {
}

async setStateVal() {
let [[e1], [e2, prevVal], [e3, incrVal], [e4, stateVal]] = await this.redis
let [[e1], [e2], [e3, prevVal], [e4, incrVal], [e5, stateVal]] = await this.redis
.multi()
.hSetExists(this.getAccountKey(), 'state', this.state)
.hSetBigger(this.getAccountKey(), 'runIndex', this.runIndex.toString())
Expand All @@ -748,8 +748,8 @@ class IMAPConnection extends BaseClient {
.hget(this.getAccountKey(), 'state')
.exec();

if (e1 || e2 || e3 || e4) {
throw e1 || e2 || e3 || e4;
if (e1 || e2 || e3 || e4 || e5) {
throw e1 || e2 || e3 || e4 || e5;
}

if (stateVal === 'connected' && incrVal === 1 && prevVal === '0') {
Expand Down
15 changes: 13 additions & 2 deletions lib/mailbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class Mailbox {
this.isAllMail = this.isGmail && this.listingEntry.specialUse === '\\All';

this.selected = false;
// does the mailbox open happen before or after initial syncing
this.previouslyConnected = false;

this.redisKey = BigInt('0x' + crypto.createHash('sha1').update(normalizePath(this.path)).digest('hex')).toString(36);

Expand Down Expand Up @@ -793,8 +795,15 @@ class Mailbox {
return;
}

if (this.path !== 'INBOX' && !this.isAllMail && storedStatus.initialUidNext > messageData.uid) {
// skip too old messages
if (this.previouslyConnected && this.path !== 'INBOX' && !this.isAllMail && storedStatus.initialUidNext > messageData.uid) {
this.logger.debug({
msg: 'Skip old message',
action: 'webhook_ignore',
initialUidNext: storedStatus.initialUidNext,
id: messageInfo.id,
uid: messageInfo.uid,
connectCount: this.previouslyConnected
});
return;
}

Expand Down Expand Up @@ -1952,6 +1961,8 @@ class Mailbox {
clearTimeout(this.runPartialSyncTimer);
this.selected = true;

this.previouslyConnected = Number(await this.connection.redis.hget(this.connection.getAccountKey(), `state:count:connected`)) || 0;

let mailboxStatus = this.getMailboxStatus();

try {
Expand Down

0 comments on commit 5d05986

Please sign in to comment.