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 { getMessageUid, getUserOfMailbox } from "../db/utils/mail";
|
||||||
import emailManager from "../mails/EmailManager";
|
import emailManager from "../mails/EmailManager";
|
||||||
import { deleteMessage } from "../db/message/updateMessage-db";
|
import { deleteMessage } from "../db/message/updateMessage-db";
|
||||||
|
import logger from "../system/Logger";
|
||||||
|
|
||||||
export default class Message {
|
export default class Message {
|
||||||
static async addFlag(body, res: Response) {
|
static async addFlag(body, res: Response) {
|
||||||
@ -54,8 +55,36 @@ export default class Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static deleteRemoteOnly = async (body, res: Response) => {
|
static deleteRemoteOnly = async (body, res: Response) => {
|
||||||
body.flag = "\\Deleted";
|
const { mailboxId, messageId } = body;
|
||||||
await this.addFlag(body, res);
|
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) {
|
static async deleteEverywhere(body, res: Response) {
|
||||||
|
@ -4,6 +4,7 @@ import Imap from "imap";
|
|||||||
import { getAllMailboxes, registerMailbox } from "../../db/imap/imap-db";
|
import { getAllMailboxes, registerMailbox } from "../../db/imap/imap-db";
|
||||||
import logger from "../../system/Logger";
|
import logger from "../../system/Logger";
|
||||||
import Mailbox from "./Mailbox";
|
import Mailbox from "./Mailbox";
|
||||||
|
import { rejects } from "assert";
|
||||||
|
|
||||||
export class ImapInstance {
|
export class ImapInstance {
|
||||||
imap: Imap;
|
imap: Imap;
|
||||||
@ -41,38 +42,45 @@ export class ImapInstance {
|
|||||||
this.imap.connect();
|
this.imap.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
imapReady() {
|
imapReady = () => {
|
||||||
getAllMailboxes(this.account.id).then((mailboxes) => {
|
getAllMailboxes(this.account.id).then((mailboxes) => {
|
||||||
if (mailboxes.length > 0) {
|
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 {
|
} else {
|
||||||
this.imap.getBoxes("", (err, boxes) => {
|
this.getMailboxName("All").then((allBoxName) => {
|
||||||
if (err) logger.err(err);
|
|
||||||
const allBoxName = this.getAllBox(boxes);
|
|
||||||
registerMailbox(this.account.id, allBoxName).then((mailboxId) => {
|
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) {
|
getMailboxName(boxToFound: string): Promise<string> {
|
||||||
// ideally we should get the all box to get all messages
|
return new Promise((resolve, rejects) => {
|
||||||
let allBox = "";
|
let matchBox = "";
|
||||||
Object.keys(boxes).forEach((key) => {
|
this.imap.getBoxes("", (err, boxes) => {
|
||||||
if (key === "INBOX") return;
|
Object.keys(boxes).forEach((key) => {
|
||||||
if (allBox.includes("/")) return; // already found
|
if (matchBox.includes("/")) return; // already found
|
||||||
if (!boxes[key].children) return; // no children
|
if (!boxes[key].children) return; // no children
|
||||||
allBox = key;
|
matchBox = key;
|
||||||
Object.keys(boxes[key].children).forEach((childBoxes) => {
|
Object.keys(boxes[key].children).forEach((childBox) => {
|
||||||
if (boxes[key].children[childBoxes].attribs.includes("\\All")) {
|
let attribs = boxes[key].children[childBox].attribs;
|
||||||
allBox += "/" + childBoxes;
|
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 {
|
getMailbox(mailboxId: number): Mailbox {
|
||||||
|
@ -6,6 +6,7 @@ import logger from "../../system/Logger";
|
|||||||
import RegisterMessageInApp from "../message/saveMessage";
|
import RegisterMessageInApp from "../message/saveMessage";
|
||||||
import { saveMessage } from "../message/storeMessage";
|
import { saveMessage } from "../message/storeMessage";
|
||||||
import updateMessage from "../message/updateMessage";
|
import updateMessage from "../message/updateMessage";
|
||||||
|
import { ImapInstance } from "./ImapInstance";
|
||||||
|
|
||||||
export interface ImapInfo {
|
export interface ImapInfo {
|
||||||
uid: number;
|
uid: number;
|
||||||
@ -20,14 +21,16 @@ export default class Mailbox {
|
|||||||
box: Box;
|
box: Box;
|
||||||
msgToSync: number;
|
msgToSync: number;
|
||||||
syncing: boolean;
|
syncing: boolean;
|
||||||
|
imapInstance: ImapInstance;
|
||||||
|
|
||||||
constructor(_imap: Imap, _boxId: number, _boxName: string) {
|
constructor(_boxId: number, _boxName: string, _imapInstance: ImapInstance) {
|
||||||
this.imap = _imap;
|
this.imap = _imapInstance.imap;
|
||||||
this.boxName = _boxName;
|
this.boxName = _boxName;
|
||||||
this.id = _boxId;
|
this.id = _boxId;
|
||||||
this.box;
|
this.box;
|
||||||
this.msgToSync = 0;
|
this.msgToSync = 0;
|
||||||
this.syncing = false;
|
this.syncing = false;
|
||||||
|
this.imapInstance = _imapInstance;
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,6 +64,7 @@ export default class Mailbox {
|
|||||||
|
|
||||||
// wait for deletion
|
// wait for deletion
|
||||||
this.imap.on("expunge", (seqno: number) => {
|
this.imap.on("expunge", (seqno: number) => {
|
||||||
|
// const updateMsg = new updateMessage(info.)
|
||||||
console.log("Message with sequence number " + seqno + " has been deleted from the server.");
|
console.log("Message with sequence number " + seqno + " has been deleted from the server.");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -106,7 +110,7 @@ export default class Mailbox {
|
|||||||
this.syncing = true;
|
this.syncing = true;
|
||||||
logger.log(`Fetching from ${savedUid} to ${currentUid} uid`);
|
logger.log(`Fetching from ${savedUid} to ${currentUid} uid`);
|
||||||
const nbMessageToSync = currentUid - savedUid;
|
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[] = [];
|
let mails: AttrsWithEnvelope[] = [];
|
||||||
|
|
||||||
for (let i = 0; i < nbMessageToSync; i += STEP) {
|
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) {
|
move(source: string, mailboxName: string, callback: (error: Error) => void) {
|
||||||
this.imap.move(source, mailboxName, callback);
|
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