chore: add new route to get favorinte and archive mailboxx messsage

This commit is contained in:
mahyargdz
2025-07-10 12:16:55 +03:30
parent 7ebd83cce3
commit 935cc50cb1
4 changed files with 100 additions and 3 deletions
+24
View File
@@ -86,6 +86,30 @@ export class EmailController {
return this.emailService.getTrashMessages(userId, query); return this.emailService.getTrashMessages(userId, query);
} }
@Get("messages/junk")
@ApiOperation({ summary: "List messages in junk mailbox" })
@ApiResponse({ status: 200, description: "List of junk messages" })
@ApiResponse({ status: 404, description: "junk mailbox not found" })
getJunkMessages(@UserDec("wildduckUserId") userId: string, @Query() query: MessageListQueryDto) {
return this.emailService.getJunkMessages(userId, query);
}
@Get("messages/archive")
@ApiOperation({ summary: "List messages in archive mailbox" })
@ApiResponse({ status: 200, description: "List of archive messages" })
@ApiResponse({ status: 404, description: "archive mailbox not found" })
getArchiveMessages(@UserDec("wildduckUserId") userId: string, @Query() query: MessageListQueryDto) {
return this.emailService.getArchivedMessages(userId, query);
}
@Get("messages/favorite")
@ApiOperation({ summary: "List messages in favorite mailbox" })
@ApiResponse({ status: 200, description: "List of favorite messages" })
@ApiResponse({ status: 404, description: "favorite mailbox not found" })
getFavoriteMessages(@UserDec("wildduckUserId") userId: string, @Query() query: MessageListQueryDto) {
return this.emailService.getFavoriteMessages(userId, query);
}
@Get("messages/drafts") @Get("messages/drafts")
@ApiOperation({ summary: "List messages in drafts mailbox" }) @ApiOperation({ summary: "List messages in drafts mailbox" })
@ApiResponse({ status: 200, description: "List of drafts messages" }) @ApiResponse({ status: 200, description: "List of drafts messages" })
@@ -193,6 +193,63 @@ export class EmailService {
throw error; throw error;
} }
} }
//==============================================
async getJunkMessages(userId: string, query: MessageListQueryDto) {
try {
const junkMailboxId = await this.mailboxResolverService.getJunkMailboxId(userId);
const { results: junkMails, total: count } = await firstValueFrom(this.mailServerService.messages.listMessages(userId, junkMailboxId, query));
return {
junkMails,
count,
paginate: true,
};
} catch (error) {
this.logger.error(`Failed to get junk messages for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
//==============================================
async getArchivedMessages(userId: string, query: MessageListQueryDto) {
try {
const archiveMailboxId = await this.mailboxResolverService.getArchiveMailboxId(userId);
const { results: archiveMails, total: count } = await firstValueFrom(
this.mailServerService.messages.listMessages(userId, archiveMailboxId, query),
);
return {
archiveMails,
count,
paginate: true,
};
} catch (error) {
this.logger.error(`Failed to get archive messages for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
//==============================================
async getFavoriteMessages(userId: string, query: MessageListQueryDto) {
try {
const favoriteMailboxId = await this.mailboxResolverService.getFavoriteMailboxId(userId);
const { results: favoriteMails, total: count } = await firstValueFrom(
this.mailServerService.messages.listMessages(userId, favoriteMailboxId, query),
);
return {
favoriteMails,
count,
paginate: true,
};
} catch (error) {
this.logger.error(`Failed to get junk messages for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
//==============================================
async getDraftsMessages(userId: string, query: MessageListQueryDto) { async getDraftsMessages(userId: string, query: MessageListQueryDto) {
try { try {
@@ -1,6 +1,7 @@
import { Injectable, Logger, NotFoundException } from "@nestjs/common"; import { Injectable, Logger, NotFoundException } from "@nestjs/common";
import { firstValueFrom } from "rxjs"; import { firstValueFrom } from "rxjs";
import { MailboxInfo } from "../../mail-server/interfaces/mailboxes-response.interface";
import { MailServerService } from "../../mail-server/services/mail-server.service"; import { MailServerService } from "../../mail-server/services/mail-server.service";
import { MailboxEnum } from "../../mailbox/enums/mailbox.enum"; import { MailboxEnum } from "../../mailbox/enums/mailbox.enum";
@@ -9,8 +10,9 @@ export interface UserMailboxIds {
sent: string; sent: string;
drafts: string; drafts: string;
trash: string; trash: string;
junk?: string; junk: string;
archive?: string; archive: string;
favorite: string;
} }
@Injectable() @Injectable()
@@ -43,6 +45,7 @@ export class MailboxResolverService {
trash: this.findMailboxId(mailboxes.results, MailboxEnum.TRASH), trash: this.findMailboxId(mailboxes.results, MailboxEnum.TRASH),
junk: this.findMailboxId(mailboxes.results, MailboxEnum.Junk, false), junk: this.findMailboxId(mailboxes.results, MailboxEnum.Junk, false),
archive: this.findMailboxId(mailboxes.results, MailboxEnum.ARCHIVE, false), archive: this.findMailboxId(mailboxes.results, MailboxEnum.ARCHIVE, false),
favorite: this.findMailboxId(mailboxes.results, MailboxEnum.FAVORITE, false),
}; };
this.cacheMailboxIds(userId, mailboxIds); this.cacheMailboxIds(userId, mailboxIds);
@@ -97,6 +100,18 @@ export class MailboxResolverService {
return this.getMailboxId(userId, "trash"); return this.getMailboxId(userId, "trash");
} }
async getJunkMailboxId(userId: string): Promise<string> {
return this.getMailboxId(userId, "junk");
}
async getArchiveMailboxId(userId: string): Promise<string> {
return this.getMailboxId(userId, "archive");
}
async getFavoriteMailboxId(userId: string): Promise<string> {
return this.getMailboxId(userId, "favorite");
}
/** /**
* Clear cache for a specific user * Clear cache for a specific user
*/ */
@@ -146,7 +161,7 @@ export class MailboxResolverService {
/** /**
* Find mailbox ID by name * Find mailbox ID by name
*/ */
private findMailboxId(mailboxes: any[], mailboxName: string, required: boolean = true): string { private findMailboxId(mailboxes: MailboxInfo[], mailboxName: string, required: boolean = true): string {
const mailbox = mailboxes.find((mb) => mb.name === mailboxName || mb.path === mailboxName || mb.specialUse === mailboxName); const mailbox = mailboxes.find((mb) => mb.name === mailboxName || mb.path === mailboxName || mb.specialUse === mailboxName);
if (!mailbox && required) { if (!mailbox && required) {
@@ -10,6 +10,7 @@ export interface MailboxInfo {
size?: number; size?: number;
retention?: number; retention?: number;
hidden?: boolean; hidden?: boolean;
encryptMessages?: boolean;
} }
export interface MailboxListResponse { export interface MailboxListResponse {