delete from server working
This commit is contained in:
parent
c3374a612e
commit
b821c89e20
@ -3,6 +3,7 @@ import { Response } from "express";
|
||||
import { getMessageUid, getUserOfMailbox } from "../db/utils/mail";
|
||||
import emailManager from "../mails/EmailManager";
|
||||
import { deleteMessage } from "../db/message/updateMessage-db";
|
||||
import logger from "../system/Logger";
|
||||
|
||||
export default class Message {
|
||||
static async addFlag(body, res: Response) {
|
||||
@ -54,8 +55,36 @@ export default class Message {
|
||||
}
|
||||
|
||||
static deleteRemoteOnly = async (body, res: Response) => {
|
||||
body.flag = "\\Deleted";
|
||||
await this.addFlag(body, res);
|
||||
const { mailboxId, messageId } = body;
|
||||
const uid = (await getMessageUid(messageId))[0]?.uid;
|
||||
if (!uid) {
|
||||
res.status(statusCode.NOT_FOUND).send({ error: "Message uid not found." });
|
||||
}
|
||||
|
||||
const user = (await getUserOfMailbox(mailboxId))[0]?.user;
|
||||
if (!user) {
|
||||
res.status(statusCode.NOT_FOUND).send({ error: "Not account for this mailbox." });
|
||||
}
|
||||
const mailbox = emailManager.getImap(user).getMailbox(mailboxId);
|
||||
|
||||
// add flag for deletion
|
||||
mailbox.addFlag(uid.toString(), ["\\Deleted"])
|
||||
.then(() => {
|
||||
// move message to trash
|
||||
mailbox.moveToTrash(uid.toString(), (err) => {
|
||||
if (err) {
|
||||
logger.err(err);
|
||||
res.status(statusCode.METHOD_FAILURE).send({ error: err });
|
||||
} else {
|
||||
res.status(statusCode.OK).send();
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.log(err);
|
||||
res.status(statusCode.METHOD_FAILURE).send({ error: err });
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
static async deleteEverywhere(body, res: Response) {
|
||||
|
@ -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 = "";
|
||||
getMailboxName(boxToFound: string): Promise<string> {
|
||||
return new Promise((resolve, rejects) => {
|
||||
let matchBox = "";
|
||||
this.imap.getBoxes("", (err, boxes) => {
|
||||
Object.keys(boxes).forEach((key) => {
|
||||
if (key === "INBOX") return;
|
||||
if (allBox.includes("/")) return; // already found
|
||||
if (matchBox.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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user