fix errors and beautify with use of class
This commit is contained in:
@@ -7,52 +7,40 @@ import Message from "../mails/message/Message";
|
||||
import Room from "../mails/room/Room";
|
||||
|
||||
export default class MessageAbl {
|
||||
static async addFlag(body, res: Response) {
|
||||
static async changeFlag(body, res: Response, isDelete: boolean) {
|
||||
const { mailboxId, messageId, flag } = body;
|
||||
const uid = (await getMessageUid(messageId))[0]?.uid;
|
||||
if (!uid) {
|
||||
const message = new Message().setMessageId(messageId);
|
||||
|
||||
try {
|
||||
await message.useUid();
|
||||
} catch (err) {
|
||||
res.status(statusCode.NOT_FOUND).send({ error: "Message uid not found." });
|
||||
}
|
||||
|
||||
const user = (await getUserOfMailbox(mailboxId))[0]?.user;
|
||||
if (!user) {
|
||||
try {
|
||||
await message.useMailbox(mailboxId);
|
||||
} catch (err) {
|
||||
res.status(statusCode.NOT_FOUND).send({ error: "Not account for this mailbox." });
|
||||
}
|
||||
emailManager
|
||||
.getImap(user)
|
||||
.getMailbox(mailboxId)
|
||||
.addFlag(uid.toString(), [flag])
|
||||
.then(() => {
|
||||
res.status(statusCode.OK).send();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
res.status(statusCode.METHOD_FAILURE).send({ error: err });
|
||||
});
|
||||
|
||||
try {
|
||||
if (isDelete) {
|
||||
await message.mailbox.removeFlag(message.uid.toString(), flag);
|
||||
} else {
|
||||
await message.mailbox.addFlag(message.uid.toString(), flag);
|
||||
}
|
||||
} catch (err) {
|
||||
res.status(statusCode.METHOD_FAILURE).send({ error: err });
|
||||
}
|
||||
res.status(statusCode.OK).send();
|
||||
}
|
||||
|
||||
static async addFlag(body, res: Response) {
|
||||
await MessageAbl.changeFlag(body, res, false);
|
||||
}
|
||||
|
||||
static async removeFlag(body, res: Response) {
|
||||
const { mailboxId, messageId, flag } = 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." });
|
||||
}
|
||||
emailManager
|
||||
.getImap(user)
|
||||
.getMailbox(mailboxId)
|
||||
.removeFlag(uid.toString(), [flag])
|
||||
.then(() => {
|
||||
res.status(statusCode.OK).send();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
res.status(statusCode.METHOD_FAILURE).send({ error: err });
|
||||
});
|
||||
await MessageAbl.changeFlag(body, res, true);
|
||||
}
|
||||
|
||||
static deleteRemoteOnly = async (body, res: Response) => {
|
||||
|
||||
Reference in New Issue
Block a user