fix live sync mail

This commit is contained in:
grimhilt 2023-05-06 12:31:13 +02:00
parent 3dab9c8db1
commit 2c7b4f1c78

View File

@ -35,19 +35,17 @@ export default class Mailbox {
// get mailbox from the database // get mailbox from the database
this.box = (await getMailbox(this.id))[0]; this.box = (await getMailbox(this.id))[0];
const isReadOnly = false; const isReadOnly = false;
this.imap.openBox(this.boxName, isReadOnly, async (err, box) => { this.imap.openBox(this.boxName, isReadOnly, (err, box) => {
if (err) logger.err(err); if (err) logger.err(err);
// sync messages and flags // sync messages and flags
// this.initSync(box); this.initSync(box);
const mails = [];
await this.mailFetcher(6000, 7000, mails);
console.log(mails);
// wait for new mails // wait for new mails
this.imap.on("mail", (numNewMsgs: number) => { this.imap.on("mail", (numNewMsgs: number) => {
if (!this.syncing) { if (!this.syncing) {
// if not syncing restart a sync // if not syncing restart a sync
this.syncManager(this.box.uidnext, this.box.uidnext + numNewMsgs); this.syncManager(this.box.uidnext - 1, this.box.uidnext + numNewMsgs - 1);
} else { } else {
// else save number of message to sync latter // else save number of message to sync latter
this.msgToSync += numNewMsgs; this.msgToSync += numNewMsgs;
@ -74,7 +72,6 @@ export default class Mailbox {
} }
async initSync(box: Box) { async initSync(box: Box) {
console.log(box.uidnext);
// sync mail only if has new messages // sync mail only if has new messages
if (this.box.uidnext < box.uidnext) { if (this.box.uidnext < box.uidnext) {
this.syncManager(this.box.uidnext, box.uidnext); this.syncManager(this.box.uidnext, box.uidnext);
@ -107,16 +104,8 @@ export default class Mailbox {
syncManager = async (savedUid: number, currentUid: number) => { syncManager = async (savedUid: number, currentUid: number) => {
this.syncing = true; this.syncing = true;
logger.log(`Fetching from ${savedUid} to ${currentUid} uid`); logger.log(`Fetching from ${savedUid} to ${currentUid} uid`);
// todo why cannot take currentUid ? const nbMessageToSync = currentUid - savedUid;
this.imap.search([`${savedUid}:*`], async (err, uids) => { let STEP = nbMessageToSync > 300 ? Math.floor(nbMessageToSync / 7) : nbMessageToSync;
if (err) {
logger.err(err);
throw err;
}
console.log(`Found ${uids.length} messages`);
const nbMessageToSync = uids.length;
let STEP = nbMessageToSync > 100 ? Math.floor(nbMessageToSync / 10) : nbMessageToSync;
let mails: AttrsWithEnvelope[] = []; let mails: AttrsWithEnvelope[] = [];
for (let i = 0; i < nbMessageToSync; i += STEP) { for (let i = 0; i < nbMessageToSync; i += STEP) {
@ -124,7 +113,7 @@ export default class Mailbox {
try { try {
// fetch mails // fetch mails
let secondUid = savedUid + STEP < currentUid ? savedUid + STEP : currentUid; let secondUid = savedUid + STEP < currentUid ? savedUid + STEP : currentUid;
await this.mailFetcher(savedUid, secondUid, mails); await this.mailFetcher(savedUid, secondUid, mails)
logger.log(`Fetched ${STEP} uids (${mails.length} messages)`); logger.log(`Fetched ${STEP} uids (${mails.length} messages)`);
// save same in the database // save same in the database
for (let k = 0; k < mails.length; k++) { for (let k = 0; k < mails.length; k++) {
@ -137,15 +126,13 @@ export default class Mailbox {
} }
} }
savedUid = secondUid; savedUid = secondUid;
this.box.uidnext = savedUid; this.box.uidnext += savedUid;
updateMailbox(this.id, savedUid); updateMailbox(this.id, savedUid);
} catch (error) { } catch (error) {
logger.err("Failed to sync message " + error); logger.err("Failed to sync message " + error);
} }
logger.log( logger.log(`Saved messages ${i + STEP > nbMessageToSync ? nbMessageToSync : i + STEP}/${nbMessageToSync}`);
`Saved messages ${i + STEP > nbMessageToSync ? nbMessageToSync : i + STEP}/${nbMessageToSync}`,
);
} }
// if has receive new msg during last sync then start a new sync // if has receive new msg during last sync then start a new sync
@ -158,7 +145,6 @@ export default class Mailbox {
} }
this.syncing = false; this.syncing = false;
logger.log(`Finished syncing messages`); logger.log(`Finished syncing messages`);
});
}; };
async mailFetcher(startUid: number, endUid: number, mails: Attrs[]): Promise<any> { async mailFetcher(startUid: number, endUid: number, mails: Attrs[]): Promise<any> {
@ -169,8 +155,6 @@ export default class Mailbox {
}); });
f.on("message", (msg, seqno) => { f.on("message", (msg, seqno) => {
console.log("seqno" + seqno);
msg.once("attributes", (attrs: AttrsWithEnvelope) => { msg.once("attributes", (attrs: AttrsWithEnvelope) => {
mails.push(attrs); mails.push(attrs);
}); });