mail/back/mails/imap/Mailbox.ts
2023-04-07 17:15:28 +02:00

108 lines
3.8 KiB
TypeScript

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 Mailbox {
imap: Imap;
boxName: string;
id: number;
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);
// 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;
}
});
});
}
async sync(savedUid: number, currentUid: number) {
this.syncing = true;
const promises: Promise<unknown>[] = [];
const mails: Attrs[] = [];
logger.log(`Syncing from ${savedUid} to ${currentUid} uid`);
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);
mails.push(attrs);
promises.push(saveMessage(attrs, this.id, this.imap));
});
});
f.once("error", (err) => {
logger.err("Fetch error: " + err);
});
f.once("end", async () => {
let step = 20;
for (let i = 0; i < promises.length; i += step) {
for (let j = i; j < (i + step && promises.length); j++) {
await new Promise((resolve, reject) => {
promises[j]
.then(async (res: number) => {
const register = new RegisterMessageInApp(res, mails[j], this.id);
await register.save();
resolve("");
})
.catch((err) => {
reject(err);
});
});
}
logger.log(`Saved messages ${i + step > promises.length ? promises.length : i + step}/${mails.length}`);
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);
}
});
}
}