update delete behavior when have thread and beautify code
This commit is contained in:
88
back/mails/message/Message.ts
Normal file
88
back/mails/message/Message.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { getFlagsOnId, getFlagsOnUid } from "../../db/message/message-db";
|
||||
import { deleteMessage, getFlags } from "../../db/message/updateMessage-db";
|
||||
import { getMessageUid, getUserOfMailbox } from "../../db/utils/mail";
|
||||
import emailManager from "../EmailManager";
|
||||
import Mailbox from "../imap/Mailbox";
|
||||
import Room from "../room/Room";
|
||||
|
||||
export default class Message extends Room {
|
||||
messageId: number;
|
||||
uid: number;
|
||||
private _flags: string[] | undefined;
|
||||
private _mailbox: Mailbox;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.messageId;
|
||||
this.flags;
|
||||
this._mailbox;
|
||||
}
|
||||
|
||||
setMessageId(messageId: number): Message {
|
||||
this.messageId = messageId;
|
||||
return this;
|
||||
}
|
||||
|
||||
async useUid(): Promise<Message> {
|
||||
if (!this.messageId) {
|
||||
throw "Define message id before trying to find uid";
|
||||
}
|
||||
this.uid = (await getMessageUid(this.messageId))[0]?.uid;
|
||||
if (!this.uid) {
|
||||
throw "Uid not found";
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
async useFlags(): Promise<Message> {
|
||||
if (!this._flags) {
|
||||
if (this.messageId) {
|
||||
await getFlagsOnId(this.messageId);
|
||||
return this;
|
||||
} else if (this.uid) {
|
||||
await getFlagsOnUid(this.uid);
|
||||
return this;
|
||||
} else {
|
||||
throw "Neither message id or uid are set, please do so before attempting to load flags";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async useMailbox(mailboxId: number, user?: string): Promise<Message> {
|
||||
if (!user) {
|
||||
user = (await getUserOfMailbox(mailboxId))[0]?.user;
|
||||
if (!user) {
|
||||
throw "Cannot find user of this mailbox";
|
||||
}
|
||||
}
|
||||
this._mailbox = emailManager.getImap(user).getMailbox(mailboxId);
|
||||
return this;
|
||||
}
|
||||
|
||||
get mailbox() {
|
||||
if (!this._mailbox) {
|
||||
throw "Call useMailbox before calling functions related to mailbox";
|
||||
}
|
||||
return this._mailbox;
|
||||
}
|
||||
|
||||
get flags(): string[] {
|
||||
if (!this._flags) {
|
||||
throw "Flags not loaded, call useFlags before calling functions related to flags";
|
||||
} else {
|
||||
return this._flags;
|
||||
}
|
||||
}
|
||||
|
||||
get isDeleted(): boolean {
|
||||
return this.flags.includes("\\Deleted");
|
||||
}
|
||||
|
||||
delete = async (messageId?: number): Promise<Message> => {
|
||||
if (!(this.messageId ?? messageId)) {
|
||||
throw "Delete need to have the message id";
|
||||
}
|
||||
await deleteMessage(this.messageId ?? messageId);
|
||||
return this;
|
||||
};
|
||||
}
|
||||
46
back/mails/room/Room.ts
Normal file
46
back/mails/room/Room.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { deleteRoom, getRoomNbMessageAndThread, getRoomOnMessageId } from "../../db/Room-db";
|
||||
|
||||
export default class Room {
|
||||
private _roomId: number;
|
||||
constructor() {
|
||||
this._roomId;
|
||||
}
|
||||
|
||||
setRoomId(roomId: number): Room {
|
||||
this._roomId = roomId;
|
||||
return this;
|
||||
}
|
||||
|
||||
get roomId(): number {
|
||||
return this._roomId;
|
||||
}
|
||||
|
||||
async setRoomIdOnMessageId(messageId: number): Promise<Room> {
|
||||
const res = await getRoomOnMessageId(messageId);
|
||||
if (res.length == 0) {
|
||||
throw "Message has no room";
|
||||
}
|
||||
this._roomId = res[0].room_id;
|
||||
return this;
|
||||
}
|
||||
|
||||
async shouldDelete(): Promise<boolean> {
|
||||
if (!this._roomId) {
|
||||
throw "shouldDelete needs to have a roomId set.";
|
||||
}
|
||||
const res = await getRoomNbMessageAndThread(this._roomId);
|
||||
if (res.length === 0) return true;
|
||||
if (res[0].nbMessage === 0 && res[0].nbThread === 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async delete(): Promise<Room> {
|
||||
if (!this._roomId) {
|
||||
throw "shouldDelete needs to have a roomId set.";
|
||||
}
|
||||
await deleteRoom(this._roomId);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user