delete from server working
This commit is contained in:
@@ -4,6 +4,7 @@ import Imap from "imap";
|
||||
import { getAllMailboxes, registerMailbox } from "../../db/imap/imap-db";
|
||||
import logger from "../../system/Logger";
|
||||
import Mailbox from "./Mailbox";
|
||||
import { rejects } from "assert";
|
||||
|
||||
export class ImapInstance {
|
||||
imap: Imap;
|
||||
@@ -41,38 +42,45 @@ export class ImapInstance {
|
||||
this.imap.connect();
|
||||
}
|
||||
|
||||
imapReady() {
|
||||
imapReady = () => {
|
||||
getAllMailboxes(this.account.id).then((mailboxes) => {
|
||||
if (mailboxes.length > 0) {
|
||||
this.boxes.push(new Mailbox(this.imap, mailboxes[0].mailbox_id, mailboxes[0].mailbox_name));
|
||||
this.boxes.push(new Mailbox(mailboxes[0].mailbox_id, mailboxes[0].mailbox_name, this));
|
||||
} else {
|
||||
this.imap.getBoxes("", (err, boxes) => {
|
||||
if (err) logger.err(err);
|
||||
const allBoxName = this.getAllBox(boxes);
|
||||
this.getMailboxName("All").then((allBoxName) => {
|
||||
registerMailbox(this.account.id, allBoxName).then((mailboxId) => {
|
||||
this.boxes.push(new Mailbox(this.imap, mailboxId, allBoxName));
|
||||
this.boxes.push(new Mailbox(mailboxId, allBoxName, this));
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
getAllBox(boxes) {
|
||||
// ideally we should get the all box to get all messages
|
||||
let allBox = "";
|
||||
Object.keys(boxes).forEach((key) => {
|
||||
if (key === "INBOX") return;
|
||||
if (allBox.includes("/")) return; // already found
|
||||
if (!boxes[key].children) return; // no children
|
||||
allBox = key;
|
||||
Object.keys(boxes[key].children).forEach((childBoxes) => {
|
||||
if (boxes[key].children[childBoxes].attribs.includes("\\All")) {
|
||||
allBox += "/" + childBoxes;
|
||||
getMailboxName(boxToFound: string): Promise<string> {
|
||||
return new Promise((resolve, rejects) => {
|
||||
let matchBox = "";
|
||||
this.imap.getBoxes("", (err, boxes) => {
|
||||
Object.keys(boxes).forEach((key) => {
|
||||
if (matchBox.includes("/")) return; // already found
|
||||
if (!boxes[key].children) return; // no children
|
||||
matchBox = key;
|
||||
Object.keys(boxes[key].children).forEach((childBox) => {
|
||||
let attribs = boxes[key].children[childBox].attribs;
|
||||
for (let i = 0; i < attribs.length; i++) {
|
||||
if (attribs[i].includes(boxToFound)) {
|
||||
matchBox += "/" + childBox;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
if (!matchBox.includes("/")) {
|
||||
logger.warn(`Did not find "${boxToFound}" mailbox`);
|
||||
rejects();
|
||||
} else {
|
||||
resolve(matchBox);
|
||||
}
|
||||
});
|
||||
});
|
||||
if (!allBox.includes("/")) logger.warn("Did not find 'All' mailbox");
|
||||
return allBox;
|
||||
}
|
||||
|
||||
getMailbox(mailboxId: number): Mailbox {
|
||||
|
||||
@@ -6,6 +6,7 @@ import logger from "../../system/Logger";
|
||||
import RegisterMessageInApp from "../message/saveMessage";
|
||||
import { saveMessage } from "../message/storeMessage";
|
||||
import updateMessage from "../message/updateMessage";
|
||||
import { ImapInstance } from "./ImapInstance";
|
||||
|
||||
export interface ImapInfo {
|
||||
uid: number;
|
||||
@@ -20,14 +21,16 @@ export default class Mailbox {
|
||||
box: Box;
|
||||
msgToSync: number;
|
||||
syncing: boolean;
|
||||
imapInstance: ImapInstance;
|
||||
|
||||
constructor(_imap: Imap, _boxId: number, _boxName: string) {
|
||||
this.imap = _imap;
|
||||
constructor(_boxId: number, _boxName: string, _imapInstance: ImapInstance) {
|
||||
this.imap = _imapInstance.imap;
|
||||
this.boxName = _boxName;
|
||||
this.id = _boxId;
|
||||
this.box;
|
||||
this.msgToSync = 0;
|
||||
this.syncing = false;
|
||||
this.imapInstance = _imapInstance;
|
||||
this.init();
|
||||
}
|
||||
|
||||
@@ -61,6 +64,7 @@ export default class Mailbox {
|
||||
|
||||
// wait for deletion
|
||||
this.imap.on("expunge", (seqno: number) => {
|
||||
// const updateMsg = new updateMessage(info.)
|
||||
console.log("Message with sequence number " + seqno + " has been deleted from the server.");
|
||||
});
|
||||
});
|
||||
@@ -106,7 +110,7 @@ export default class Mailbox {
|
||||
this.syncing = true;
|
||||
logger.log(`Fetching from ${savedUid} to ${currentUid} uid`);
|
||||
const nbMessageToSync = currentUid - savedUid;
|
||||
let STEP = nbMessageToSync > 300 ? Math.floor(nbMessageToSync / 7) : nbMessageToSync;
|
||||
let STEP = nbMessageToSync > 200 ? Math.floor(nbMessageToSync / 7) : nbMessageToSync;
|
||||
let mails: AttrsWithEnvelope[] = [];
|
||||
|
||||
for (let i = 0; i < nbMessageToSync; i += STEP) {
|
||||
@@ -199,4 +203,9 @@ export default class Mailbox {
|
||||
move(source: string, mailboxName: string, callback: (error: Error) => void) {
|
||||
this.imap.move(source, mailboxName, callback);
|
||||
}
|
||||
|
||||
async moveToTrash(source: string, callback: (error: Error) => void) {
|
||||
const trashName = await this.imapInstance.getMailboxName("Trash");
|
||||
this.move(source, trashName, callback);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user