try using search with syncManager

This commit is contained in:
grimhilt 2023-05-06 12:28:53 +02:00
parent 5aef5ab7b0
commit 3dab9c8db1

View File

@ -35,17 +35,19 @@ 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, (err, box) => { this.imap.openBox(this.boxName, isReadOnly, async (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 - 1, this.box.uidnext + numNewMsgs - 1); this.syncManager(this.box.uidnext, this.box.uidnext + numNewMsgs);
} else { } else {
// else save number of message to sync latter // else save number of message to sync latter
this.msgToSync += numNewMsgs; this.msgToSync += numNewMsgs;
@ -72,6 +74,7 @@ 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);
@ -104,57 +107,70 @@ 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`);
const nbMessageToSync = currentUid - savedUid; // todo why cannot take currentUid ?
let STEP = nbMessageToSync > 300 ? Math.floor(nbMessageToSync / 7) : nbMessageToSync; this.imap.search([`${savedUid}:*`], async (err, uids) => {
let mails: AttrsWithEnvelope[] = []; if (err) {
logger.err(err);
for (let i = 0; i < nbMessageToSync; i += STEP) { throw err;
mails = [];
try {
// fetch mails
let secondUid = savedUid + STEP < currentUid ? savedUid + STEP : currentUid;
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++) {
try {
const messageId = await saveMessage(mails[k], this.id, this.imap);
const register = new RegisterMessageInApp(messageId, mails[k], this.id);
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(`Saved messages ${i + STEP > nbMessageToSync ? nbMessageToSync : i + STEP}/${nbMessageToSync}`); console.log(`Found ${uids.length} messages`);
} const nbMessageToSync = uids.length;
// if has receive new msg during last sync then start a new sync let STEP = nbMessageToSync > 100 ? Math.floor(nbMessageToSync / 10) : nbMessageToSync;
if (this.msgToSync > 0) { let mails: AttrsWithEnvelope[] = [];
const currentUid = this.box.uidnext;
this.box.uidnext += this.msgToSync; for (let i = 0; i < nbMessageToSync; i += STEP) {
// reset value to allow to detect new incoming message while syncing mails = [];
this.msgToSync = 0; try {
await this.syncManager(currentUid, this.box.uidnext); // fetch mails
} let secondUid = savedUid + STEP < currentUid ? savedUid + STEP : currentUid;
this.syncing = false; await this.mailFetcher(savedUid, secondUid, mails);
logger.log(`Finished syncing messages`); logger.log(`Fetched ${STEP} uids (${mails.length} messages)`);
// save same in the database
for (let k = 0; k < mails.length; k++) {
try {
const messageId = await saveMessage(mails[k], this.id, this.imap);
const register = new RegisterMessageInApp(messageId, mails[k], this.id);
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(
`Saved messages ${i + STEP > nbMessageToSync ? nbMessageToSync : i + STEP}/${nbMessageToSync}`,
);
}
// 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> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const f = this.imap.seq.fetch(`${startUid}:${endUid}`, { const f = this.imap.fetch(`${startUid}:${endUid}`, {
size: true, size: true,
envelope: true, envelope: true,
}); });
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);
}); });