sync flags on server start
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import Imap, { ImapMessageAttributes, Box } from "imap";
|
||||
import { getMailbox, updateMailbox } from "../../db/imap/imap-db";
|
||||
import { getMailbox, getMailboxModseq, updateMailbox, updateMailboxModseq } from "../../db/imap/imap-db";
|
||||
import { Attrs, AttrsWithEnvelope } from "../../interfaces/mail/attrs.interface";
|
||||
import logger from "../../system/Logger";
|
||||
import RegisterMessageInApp from "../message/saveMessage";
|
||||
@@ -33,29 +33,25 @@ export default class Mailbox {
|
||||
async init() {
|
||||
// get mailbox from the database
|
||||
this.box = (await getMailbox(this.id))[0];
|
||||
|
||||
const isReadOnly = false;
|
||||
this.imap.openBox(this.boxName, isReadOnly, (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");
|
||||
}
|
||||
// sync messages and flags
|
||||
this.initSync(box);
|
||||
|
||||
// wait for new mails
|
||||
this.imap.on("mail", (numNewMsgs: number) => {
|
||||
if (!this.syncing) {
|
||||
// if not syncing restart a sync
|
||||
this.sync(this.box.uidnext, this.box.uidnext + numNewMsgs);
|
||||
this.syncMail(this.box.uidnext, this.box.uidnext + numNewMsgs);
|
||||
} else {
|
||||
// else save number of message to sync latter
|
||||
this.msgToSync += numNewMsgs;
|
||||
}
|
||||
});
|
||||
|
||||
// wait for flags update
|
||||
this.imap.on("update", (seqno: number, info: ImapInfo) => {
|
||||
logger.log(`Update message ${info.uid} with ${info.flags}`);
|
||||
const updateMsg = new updateMessage(info.uid, info.flags);
|
||||
@@ -64,7 +60,43 @@ export default class Mailbox {
|
||||
});
|
||||
}
|
||||
|
||||
async sync(savedUid: number, currentUid: number) {
|
||||
async updateModseq(newModseq: number) {
|
||||
updateMailboxModseq(this.id, newModseq).then(() => {
|
||||
this.box.highestmodseq = newModseq;
|
||||
});
|
||||
}
|
||||
|
||||
async initSync(box: Box) {
|
||||
// sync mail only if has new messages
|
||||
if (this.box.uidnext < box.uidnext) {
|
||||
this.syncMail(this.box.uidnext, box.uidnext);
|
||||
} else {
|
||||
logger.log("Mail already up to date");
|
||||
}
|
||||
|
||||
// sync flags
|
||||
const lastModseq = (await getMailboxModseq(this.id))[0]?.modseq ?? 0;
|
||||
if (box.highestmodseq > lastModseq) {
|
||||
const fetchStream = this.imap.fetch("1:*", { bodies: "", modifiers: { changedsince: lastModseq } });
|
||||
fetchStream.on("message", (message) => {
|
||||
message.once("attributes", (attrs) => {
|
||||
const updateMsg = new updateMessage(attrs.uid, attrs.flags);
|
||||
updateMsg.updateFlags();
|
||||
});
|
||||
});
|
||||
fetchStream.once("error", function (err) {
|
||||
logger.err("Fetch error when syncing flags: " + err);
|
||||
});
|
||||
fetchStream.once("end", function () {
|
||||
logger.log("Done fetching new flags");
|
||||
});
|
||||
} else {
|
||||
logger.log("Flags already up to date")
|
||||
}
|
||||
this.updateModseq(box.highestmodseq);
|
||||
}
|
||||
|
||||
async syncMail(savedUid: number, currentUid: number) {
|
||||
this.syncing = true;
|
||||
const promises: Promise<unknown>[] = [];
|
||||
const mails: Attrs[] = [];
|
||||
@@ -113,7 +145,7 @@ export default class Mailbox {
|
||||
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);
|
||||
this.syncMail(currentUid, this.box.uidnext);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user