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
this.box = (await getMailbox(this.id))[0];
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);
// 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
this.imap.on("mail", (numNewMsgs: number) => {
if (!this.syncing) {
// 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 save number of message to sync latter
this.msgToSync += numNewMsgs;
@ -72,6 +74,7 @@ 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);
@ -104,8 +107,16 @@ export default class Mailbox {
syncManager = async (savedUid: number, currentUid: number) => {
this.syncing = true;
logger.log(`Fetching from ${savedUid} to ${currentUid} uid`);
const nbMessageToSync = currentUid - savedUid;
let STEP = nbMessageToSync > 300 ? Math.floor(nbMessageToSync / 7) : nbMessageToSync;
// 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;
let mails: AttrsWithEnvelope[] = [];
for (let i = 0; i < nbMessageToSync; i += STEP) {
@ -113,7 +124,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++) {
@ -126,13 +137,15 @@ 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
@ -145,16 +158,19 @@ export default class Mailbox {
}
this.syncing = false;
logger.log(`Finished syncing messages`);
});
};
async mailFetcher(startUid: number, endUid: number, mails: Attrs[]): Promise<any> {
return new Promise((resolve, reject) => {
const f = this.imap.seq.fetch(`${startUid}:${endUid}`, {
const f = this.imap.fetch(`${startUid}:${endUid}`, {
size: true,
envelope: true,
});
f.on("message", (msg, seqno) => {
console.log("seqno" + seqno);
msg.once("attributes", (attrs: AttrsWithEnvelope) => {
mails.push(attrs);
});