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,58 +104,47 @@ 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) { let mails: AttrsWithEnvelope[] = [];
logger.err(err);
throw err;
}
console.log(`Found ${uids.length} messages`);
const nbMessageToSync = uids.length;
let STEP = nbMessageToSync > 100 ? Math.floor(nbMessageToSync / 10) : nbMessageToSync; for (let i = 0; i < nbMessageToSync; i += STEP) {
let mails: AttrsWithEnvelope[] = []; mails = [];
try {
for (let i = 0; i < nbMessageToSync; i += STEP) { // fetch mails
mails = []; let secondUid = savedUid + STEP < currentUid ? savedUid + STEP : currentUid;
try { await this.mailFetcher(savedUid, secondUid, mails)
// fetch mails logger.log(`Fetched ${STEP} uids (${mails.length} messages)`);
let secondUid = savedUid + STEP < currentUid ? savedUid + STEP : currentUid; // save same in the database
await this.mailFetcher(savedUid, secondUid, mails); for (let k = 0; k < mails.length; k++) {
logger.log(`Fetched ${STEP} uids (${mails.length} messages)`); try {
// save same in the database const messageId = await saveMessage(mails[k], this.id, this.imap);
for (let k = 0; k < mails.length; k++) { const register = new RegisterMessageInApp(messageId, mails[k], this.id);
try { await register.save();
const messageId = await saveMessage(mails[k], this.id, this.imap); } catch (error) {
const register = new RegisterMessageInApp(messageId, mails[k], this.id); logger.err("Failed to save a message: " + error);
await register.save();
} catch (error) {
logger.err("Failed to save a message: " + error);
}
} }
savedUid = secondUid;
this.box.uidnext = savedUid;
updateMailbox(this.id, savedUid);
} catch (error) {
logger.err("Failed to sync message " + error);
} }
logger.log( savedUid = secondUid;
`Saved messages ${i + STEP > nbMessageToSync ? nbMessageToSync : i + STEP}/${nbMessageToSync}`, this.box.uidnext += savedUid;
);
}
// if has receive new msg during last sync then start a new sync updateMailbox(this.id, savedUid);
if (this.msgToSync > 0) { } catch (error) {
const currentUid = this.box.uidnext; logger.err("Failed to sync message " + error);
this.box.uidnext += this.msgToSync;
// reset value to allow to detect new incoming message while syncing
this.msgToSync = 0;
await this.syncManager(currentUid, this.box.uidnext);
} }
this.syncing = false; logger.log(`Saved messages ${i + STEP > nbMessageToSync ? nbMessageToSync : i + STEP}/${nbMessageToSync}`);
logger.log(`Finished syncing messages`); }
});
// if has receive new msg during last sync then start a new sync
if (this.msgToSync > 0) {
const currentUid = this.box.uidnext;
this.box.uidnext += this.msgToSync;
// reset value to allow to detect new incoming message while syncing
this.msgToSync = 0;
await this.syncManager(currentUid, this.box.uidnext);
}
this.syncing = false;
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);
}); });