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