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 { 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 { 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 { 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 => { if (!(this.messageId ?? messageId)) { throw "Delete need to have the message id"; } await deleteMessage(this.messageId ?? messageId); return this; }; }