diff --git a/back/mails/imap/ImapInstance.ts b/back/mails/imap/ImapInstance.ts index 3248ef6..064307e 100644 --- a/back/mails/imap/ImapInstance.ts +++ b/back/mails/imap/ImapInstance.ts @@ -3,12 +3,12 @@ import { Account } from "./ImapSync"; import Imap from "imap"; import { getAllMailboxes, registerMailbox } from "../../db/imap/imap-db"; import logger from "../../system/Logger"; -import Box from "./Box"; +import Mailbox from "./Mailbox"; export class ImapInstance { imap: Imap; account: Account; - boxes: Box[]; + boxes: Mailbox[]; constructor(account) { this.imap = new Imap({ @@ -44,13 +44,13 @@ export class ImapInstance { imapReady() { getAllMailboxes(this.account.id).then((mailboxes) => { if (mailboxes.length > 0) { - this.boxes.push(new Box(this.imap, mailboxes[0].mailbox_id, mailboxes[0].mailbox_name)); + this.boxes.push(new Mailbox(this.imap, mailboxes[0].mailbox_id, mailboxes[0].mailbox_name)); } else { this.imap.getBoxes("", (err, boxes) => { if (err) logger.err(err); const allBoxName = this.getAllBox(boxes); registerMailbox(this.account.id, allBoxName).then((mailboxId) => { - this.boxes.push(new Box(this.imap, mailboxId, allBoxName)); + this.boxes.push(new Mailbox(this.imap, mailboxId, allBoxName)); }); }); } diff --git a/back/mails/imap/Box.ts b/back/mails/imap/Mailbox.ts similarity index 61% rename from back/mails/imap/Box.ts rename to back/mails/imap/Mailbox.ts index cb04aa1..8106650 100644 --- a/back/mails/imap/Box.ts +++ b/back/mails/imap/Mailbox.ts @@ -1,47 +1,68 @@ -import Imap, { ImapMessageAttributes, MailBoxes } from "imap"; +import Imap, { ImapMessageAttributes, Box } from "imap"; import { getMailbox, updateMailbox } from "../../db/imap/imap-db"; import { Attrs, AttrsWithEnvelope } from "../../interfaces/mail/attrs.interface"; import logger from "../../system/Logger"; import RegisterMessageInApp from "../saveMessage"; import { saveMessage } from "../storeMessage"; -export default class Box { +export default class Mailbox { imap: Imap; boxName: string; id: number; - box: MailBoxes; + box: Box; + msgToSync: number; + syncing: boolean; constructor(_imap, _boxId, _boxName) { this.imap = _imap; this.boxName = _boxName; this.id = _boxId; this.box; + this.msgToSync = 0; + this.syncing = false; this.init(); } async init() { + // get mailbox from the database this.box = (await getMailbox(this.id))[0]; const readOnly = true; this.imap.openBox(this.boxName, readOnly, (err, box) => { if (err) logger.err(err); - this.sync(this.box.uidnext, box.uidnext); + + // sync only if has new messages + if (this.box.uidnext < box.uidnext) { + this.sync(this.box.uidnext, box.uidnext); + } else { + logger.log("Already up to date") + } + + this.imap.on("mail", (numNewMsgs: number) => { + if (!this.syncing) { + // if not syncing restart a sync + this.sync(this.box.uidnext, this.box.uidnext + numNewMsgs); + } else { + // else save number of message to sync latter + this.msgToSync += numNewMsgs; + } + }); }); } - sync(savedUid, currentUid) { + async sync(savedUid: number, currentUid: number) { + this.syncing = true; const promises: Promise[] = []; const mails: Attrs[] = []; logger.log(`Syncing from ${savedUid} to ${currentUid} uid`); const f = this.imap.seq.fetch(`${savedUid}:${currentUid}`, { - // const f = this.imap.seq.fetch(`${savedUid}:${currentUid}`, { size: true, envelope: true, }); f.on("message", (msg, seqno) => { msg.once("attributes", (attrs: AttrsWithEnvelope) => { - console.log(attrs.envelope) + console.log(attrs.envelope); mails.push(attrs); promises.push(saveMessage(attrs, this.id, this.imap)); }); @@ -53,7 +74,6 @@ export default class Box { f.once("end", async () => { let step = 20; - logger.log(promises.length) for (let i = 0; i < promises.length; i += step) { for (let j = i; j < (i + step && promises.length); j++) { await new Promise((resolve, reject) => { @@ -72,6 +92,16 @@ export default class Box { updateMailbox(this.id, mails[i].uid); } updateMailbox(this.id, currentUid); + this.syncing = false; + + // 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; + this.sync(currentUid, this.box.uidnext); + } }); } -} \ No newline at end of file +}