import Inject, { RegisterScoped } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import EmailAddressService from "../../services/EmailAddressService.js"; import ChannelService from "./ChannelService.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import BroadcastEmail from "../../workflows/channel/BroadcastEmail.js"; import Calendar from "@social-mail/shared/dist/ical-parser/Calender.js"; import { AppStringHelper } from "../../../common/AppStringHelper.js"; import { EmojiDateParser } from "../../workflows/post-receive/EmojiDateParser.js"; import SendWebPushNotification from "../../workflows/web-push/SendWebPushNotification.js"; import TempFileService from "../../storage/TempFileService.js"; import CachedEmailService from "./CachedEmailService.js"; import QueueInvokeWebHookWorkflow from "../../workflows/web-hooks/QueueInvokeWebHookWorkflow.js"; import EmailThreadService from "../../services/threads/EmailThreadService.js"; import Mailbox from "../../model/entities/Mailbox.js"; import UpdatePendingModerationCountWorkflow from "../../workflows/channel/moderation/UpdatePendingModerationCountWorkflow.js"; import { AppWorkflowContext } from "../../workflows/AppWorkflowContext.js"; @RegisterScoped export default class EmailPostReceiveService { @Inject db?: SocialMailContext; @Inject eas?: EmailAddressService; @Inject channelService?: ChannelService; @Inject tfs?: TempFileService; @Inject ces?: CachedEmailService; @Inject context?: AppWorkflowContext; @Inject ets?: EmailThreadService; async executeAll({ senderID, emailID }) { const { db } = this; const mcs = await db.mailboxContactEmails.where({ emailID, senderID}, (x, p) => x.emailID === p.emailID && x.mailboxID !== p.senderID ) .select(({ mailboxID }) => ({ mailboxID })) .toArray(); for (const { mailboxID } of mcs) { await this.execute({ senderID, emailID, recipientID: mailboxID }); } await db.emails.statements.update({ statePostReceiveInvoked: true}, { emailID }); } async execute(input: { senderID, emailID, recipientID }) { const { eas } = this; let channel = await eas.getHostedMailboxByID(input.recipientID); channel = channel?.isChannel ? channel : void 0; if (channel) { await this.dispatchChannel(channel, input); } else { await this.dispatchMailbox(input); } await this.queueWebHooks(input); } async queueWebHooks(input: { senderID, emailID, recipientID }) { const { db } = this; const { senderID: mailboxID, recipientID: emailAddressID, emailID } = input; let hasWebHook = await db.webHooks.where({ mailboxID }, (x, p) => x.mailboxID === p.mailboxID && x.emailAddressID === null) .some(); if(hasWebHook) { await QueueInvokeWebHookWorkflow.queue(this.context, { type: "email-received", mailboxID, input: { mailboxID, emailAddressID, emailID } }); } hasWebHook = await db.webHooks.where({ mailboxID, emailAddressID }, (x, p) => x.mailboxID === p.mailboxID && x.emailAddressID === p.emailAddressID) .some(); if(hasWebHook) { await QueueInvokeWebHookWorkflow.queue(this.context, { type: "email-received", emailAddressID, mailboxID, input: { mailboxID, emailAddressID, emailID } }); } } async dispatchChannel(channel: Mailbox, input: { senderID, emailID, recipientID }) { const { db, channelService, tfs, ces } = this; const { senderID, emailID, recipientID } = input; // check if it is not an engagement and can send an email const ne = await db.mailboxContactEmails.where(input,(x,p) => x.emailID === p.emailID && x.mailboxID === p.recipientID && x.emailAddressID === p.senderID && x.email.status !== "published") .first(); if (!ne) { return; } if (ne.engagement) { await db.mailboxes.where(input, (x, p) => x.mailboxID === p.recipientID) .update(() => ({ dateEngaged: Sql.date.now() })); switch(ne.engagement) { case "✅": return await channelService.approveSubscriber(emailID); case "🔔": case "📈": return; case "🔕": case "❌": // unsubscribe particular thread... await this.ets.unsubscribe(senderID, emailID); return; case "➕": await BroadcastEmail.broadcast(this.context, { mailboxID: recipientID, emailID, senderID, sendToModeratorsOnly: true }); return; case "💬": // following will make comment appear publicly if (!channel.isPrivateChannel && channel.requiresCommentModeration) { await db.emails.where(input, (x, p) => x.emailID === p.emailID && x.parent.status === "published" ) .update(() => ({ status: "pending-approval" })); return; } await db.emails.where(input, (x, p) => x.emailID === p.emailID && x.parent.status === "published" ) .update(() => ({ status: "published" })); await this.ets.saveParticipant(senderID, emailID); return; default: await this.ets.saveParticipant(senderID, emailID); return; } } const mc = await db.mailboxContacts.where(input, (x, p) => x.mailboxID === p.recipientID && x.emailAddressID === p.senderID) .first(); // can send... if (await db.emails.where(input, (x, p) => x.emailID === p.emailID && x.status !== "approved") .some()) { if (channel.isPrivateChannel) { if (!mc || !(mc.canContribute || mc.isOwner)) { return; } } if (!channel.isPrivateChannel && channel.requiresPostModeration && !mc.isOwner && !mc.isModerator) { await this.db.emails.statements.update({ status: "pending-approval" }, { emailID }); await UpdatePendingModerationCountWorkflow.queue(this, { mailboxID: channel.mailboxID, diff: 1 }); return; } } // Has event attachment? const calendarAttachments = await db.appFiles.where(input, (x, p) => x.emailID === p.emailID && x.contentType === "text/calendar") .include((x) => x.fileContent) .toArray(); for (const file of calendarAttachments) { const localFile = await tfs.downloadAppFile(file); const events = await Calendar.parse(await localFile.readAsText()); for (const e of events.events) { await db.mailboxContactEvents.statements.insert({ mailboxID: recipientID, emailAddressID: senderID, emailID, startDate: e.startDate, endDate: e.endDate, title: AppStringHelper.left(e.summary, 200), location: AppStringHelper.left(e.location, 400) }); } } const { textBody, subject } = await ces.get(emailID); const calendarDate = EmojiDateParser.parse(textBody); if (calendarDate) { const { date, location } = calendarDate; if (date) { await db.mailboxContactEvents.statements.insert({ mailboxID: recipientID, emailAddressID: senderID, emailID, startDate: date, endDate: date, title: AppStringHelper.left(subject, 200), location: location ? AppStringHelper.left(location, 400) : void 0 }); } } await this.ets.saveParticipant(senderID, emailID); await BroadcastEmail.broadcast(this.context, { mailboxID: recipientID, emailID, senderID }); } async dispatchMailbox(input: { senderID, emailID, recipientID }) { const { db } = this; const { senderID, emailID } = input; // check if it is not an engagement and can send an email const ne = await db.mailboxContactEmails.where(input, (x, p) => x.emailID === p.emailID && x.mailboxID === p.recipientID && x.emailAddressID === p.senderID && x.email.status !== "published") .first(); if (!ne) { return; } await this.ets.saveParticipant(senderID, emailID); if (ne.engagement) { await db.mailboxes.where(input, (x, p) => x.mailboxID === p.recipientID) .update(() => ({ dateEngaged: Sql.date.now() })); } const mc = await db.mailboxContacts.where(input, (x, p) => x.mailboxID === p.recipientID && x.emailAddressID === p.senderID) .first(); // send push notification here... if (mc?.blocked) { return; } if (ne.timeRead) { return; } // verify if anyone is listening... const hasSubscriptions = await db.userPushSubscriptions.where(input, (x, p) => x.mailboxID === p.recipientID).some(); if (hasSubscriptions) { const id = `send-push-${input.emailID}-${input.recipientID}`; await this.context.queue(SendWebPushNotification, { mailboxID: ne.mailboxID, emailID: ne.emailID, emailAddressID: ne.emailAddressID }, { id, throwIfExists: false }); } } }