update: the email notif service
This commit is contained in:
@@ -5,10 +5,10 @@ import { firstValueFrom } from "rxjs";
|
||||
|
||||
import { WorkerProcessor } from "../../../common/queues/worker.processor";
|
||||
import { EMAIL_QUEUE_CONSTANTS } from "../../email/constants/email-events.constant";
|
||||
import { UserMailboxIds } from "../../email/interfaces/user-mailbox.interface";
|
||||
import { EmailGateway } from "../../email-gateway/email.gateway";
|
||||
import { MailServerService } from "../../mail-server/services/mail-server.service";
|
||||
import { EmailPollingJobData } from "../interfaces/email-polling-job.interface";
|
||||
import { EmailNotificationService } from "../services/email-notification.service";
|
||||
import { MailboxResolverService } from "../services/mailbox-resolver.service";
|
||||
|
||||
@Processor(EMAIL_QUEUE_CONSTANTS.EMAIL_POLLING_QUEUE)
|
||||
@@ -16,6 +16,7 @@ export class EmailPollingProcessor extends WorkerProcessor {
|
||||
protected readonly logger = new Logger(EmailPollingProcessor.name);
|
||||
constructor(
|
||||
private readonly emailGateway: EmailGateway,
|
||||
private readonly emailNotificationService: EmailNotificationService,
|
||||
private readonly mailboxResolverService: MailboxResolverService,
|
||||
private readonly mailServerService: MailServerService,
|
||||
) {
|
||||
@@ -50,9 +51,9 @@ export class EmailPollingProcessor extends WorkerProcessor {
|
||||
this.logger.log(`Found ${newEmails.length} new emails for user ${wildduckUserId}`);
|
||||
|
||||
const lastEmail = newEmails[newEmails.length - 1];
|
||||
await this.emailGateway.notifyNewEmail(userId, {
|
||||
wildduckUserId,
|
||||
await this.emailNotificationService.notifyNewEmail({
|
||||
userId,
|
||||
wildduckUserId,
|
||||
messageId: lastEmail.id,
|
||||
date: lastEmail.date || new Date().toISOString(),
|
||||
from: lastEmail.from,
|
||||
@@ -65,16 +66,6 @@ export class EmailPollingProcessor extends WorkerProcessor {
|
||||
hasAttachments: !!(lastEmail.attachments && Array.isArray(lastEmail.attachments) && lastEmail.attachments.length > 0),
|
||||
});
|
||||
|
||||
// Update unread count
|
||||
const unreadStats = await this.getUnreadCounts(wildduckUserId, mailboxIds);
|
||||
await this.emailGateway.notifyUnreadCountUpdate(userId, {
|
||||
userId,
|
||||
totalUnread: unreadStats.total,
|
||||
mailboxCounts: unreadStats.mailboxCounts,
|
||||
count: unreadStats.total,
|
||||
timestamp: now.toISOString(),
|
||||
});
|
||||
|
||||
this.logger.log(`Notified user ${userId} of ${newEmails.length} new emails`);
|
||||
} else {
|
||||
this.logger.debug(`No new emails found for user ${userId}`);
|
||||
@@ -115,38 +106,4 @@ export class EmailPollingProcessor extends WorkerProcessor {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
//=======================================================
|
||||
private async getUnreadCounts(wildduckUserId: string, mailboxIds: UserMailboxIds) {
|
||||
try {
|
||||
const mailboxCounts: Record<string, number> = {};
|
||||
let total = 0;
|
||||
|
||||
for (const [mailboxName, mailboxId] of Object.entries(mailboxIds)) {
|
||||
if (mailboxId) {
|
||||
try {
|
||||
const response = await firstValueFrom(this.mailServerService.mailboxes.getMailbox(wildduckUserId, mailboxId));
|
||||
|
||||
if (response.success && response.unseen !== undefined) {
|
||||
mailboxCounts[mailboxName] = response.unseen;
|
||||
total += response.unseen;
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.warn(`Error getting unread count for ${mailboxName} (${mailboxId}):`, error);
|
||||
mailboxCounts[mailboxName] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
total,
|
||||
mailboxCounts,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(`Error calculating unread counts for user ${wildduckUserId}:`, error);
|
||||
return {
|
||||
total: 0,
|
||||
mailboxCounts: {},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user