From b821c89e201e8fa06af413a4374c628bed1fed40 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sat, 6 May 2023 14:57:55 +0200 Subject: [PATCH] delete from server working --- back/abl/Message-abl.ts | 33 +++++++++++++++++++++-- back/mails/imap/ImapInstance.ts | 48 +++++++++++++++++++-------------- back/mails/imap/Mailbox.ts | 15 ++++++++--- 3 files changed, 71 insertions(+), 25 deletions(-) diff --git a/back/abl/Message-abl.ts b/back/abl/Message-abl.ts index 4bf0743..12ddab3 100644 --- a/back/abl/Message-abl.ts +++ b/back/abl/Message-abl.ts @@ -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) { diff --git a/back/mails/imap/ImapInstance.ts b/back/mails/imap/ImapInstance.ts index 323a53a..3f25925 100644 --- a/back/mails/imap/ImapInstance.ts +++ b/back/mails/imap/ImapInstance.ts @@ -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 { + 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 { diff --git a/back/mails/imap/Mailbox.ts b/back/mails/imap/Mailbox.ts index f7d1914..90eed06 100644 --- a/back/mails/imap/Mailbox.ts +++ b/back/mails/imap/Mailbox.ts @@ -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); + } }