routes of update flags with imap
This commit is contained in:
@@ -42,7 +42,11 @@ class EmailManager {
|
||||
}
|
||||
|
||||
getSmtp(email: string): SmtpInstance | undefined {
|
||||
return this.smtpInstances.find((instance) => instance.user == email);
|
||||
return this.smtpInstances.find((instance) => instance.user === email);
|
||||
}
|
||||
|
||||
getImap(email: string): ImapInstance | undefined {
|
||||
return this.imapInstances.find((instance) => instance.account.user === email);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ export class ImapInstance {
|
||||
this.boxes = [];
|
||||
|
||||
/**
|
||||
* IMAP
|
||||
* IMAP init
|
||||
*/
|
||||
this.imap.once("ready", () => {
|
||||
logger.log("Imap connected for " + this.account.user);
|
||||
@@ -59,7 +59,7 @@ export class ImapInstance {
|
||||
|
||||
getAllBox(boxes) {
|
||||
// ideally we should get the all box to get all messages
|
||||
let allBox = '';
|
||||
let allBox = "";
|
||||
Object.keys(boxes).forEach((key) => {
|
||||
if (key === "INBOX") return;
|
||||
if (allBox.includes("/")) return; // already found
|
||||
@@ -74,4 +74,8 @@ export class ImapInstance {
|
||||
if (!allBox.includes("/")) logger.warn("Did not find 'All' mailbox");
|
||||
return allBox;
|
||||
}
|
||||
}
|
||||
|
||||
getMailbox(mailboxId: number): Mailbox {
|
||||
return this.boxes.find((box) => box.id === mailboxId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ export default class Mailbox {
|
||||
msgToSync: number;
|
||||
syncing: boolean;
|
||||
|
||||
constructor(_imap, _boxId, _boxName) {
|
||||
constructor(_imap: Imap, _boxId: number, _boxName: string) {
|
||||
this.imap = _imap;
|
||||
this.boxName = _boxName;
|
||||
this.id = _boxId;
|
||||
@@ -34,15 +34,15 @@ export default class Mailbox {
|
||||
// get mailbox from the database
|
||||
this.box = (await getMailbox(this.id))[0];
|
||||
|
||||
const readOnly = true;
|
||||
this.imap.openBox(this.boxName, readOnly, (err, box) => {
|
||||
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")
|
||||
logger.log("Already up to date");
|
||||
}
|
||||
|
||||
// wait for new mails
|
||||
@@ -57,6 +57,7 @@ export default class Mailbox {
|
||||
});
|
||||
|
||||
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);
|
||||
updateMsg.updateFlags();
|
||||
});
|
||||
@@ -75,7 +76,6 @@ export default class Mailbox {
|
||||
|
||||
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));
|
||||
});
|
||||
@@ -117,4 +117,28 @@ export default class Mailbox {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
addFlag(source: string, flags: string[]): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.imap.addFlags(source, flags, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
removeFlag(source: string, flags: string[]): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.imap.delFlags(source, flags, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user