should implement continuous syncing

This commit is contained in:
grimhilt 2023-04-07 17:15:28 +02:00
parent 20fe48974f
commit d641b01758
2 changed files with 43 additions and 13 deletions

View File

@ -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));
});
});
}

View File

@ -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);
// 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<unknown>[] = [];
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);
}
});
}
}