import Inject, { RegisterScoped, ServiceProvider } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import { SmtpStatusCodes } from "../client/SmtpStatusCodes.js"; import SmtpError from "./SmtpError.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import type Mailbox from "../../model/entities/Mailbox.js"; import type EmailRecipient from "../../model/entities/EmailRecipient.js"; import SendChannelInvitation from "../../workflows/channel/invite/SendChannelInvitation.js"; import SubscriberRequested from "../../templates/channel/SubscriberRequested.js"; import EmailAddressService from "../../services/EmailAddressService.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import type MailboxContact from "../../model/entities/MailboxContact.js"; import { AppWorkflowContext } from "../../workflows/AppWorkflowContext.js"; @RegisterScoped export default class ChannelService { @Inject private db: SocialMailContext; async getAccess(recipientChannel: Mailbox, mailboxContact: MailboxContact) { if (!recipientChannel.isPrivateChannel && !recipientChannel.requiresMembershipApproval) { // we should accept everything... return mailboxContact; } if (recipientChannel.isPrivateChannel) { if (!mailboxContact) { throw new SmtpError("Only active members of channel can send message to channel", SmtpStatusCodes.DeliveryNotPermitted); } } if (mailboxContact?.blocked) { throw new SmtpError("Only active members of channel can send message to channel", SmtpStatusCodes.DeliveryNotPermitted); } return mailboxContact; } async subscribe(mailboxID: number, emailAddressID: number) { const dateUpdated = DateTime.now; await this.db.mailboxContacts.statements.upsert({ mailboxID, emailAddressID, status: "active", dateUpdated }); } async unsubscribe(mailboxID: number, emailAddressID: number) { const dateUpdated = DateTime.now; await this.db.mailboxContacts.statements.upsert({ mailboxID, emailAddressID, status: "unsubscribed", dateUpdated }); } async acceptEmailRecipient(entity: EmailRecipient) { const { emailAddressID: recipientID } = entity; const workflowContext = ServiceProvider.resolve(this, AppWorkflowContext); const emailAddressService = ServiceProvider.resolve(this, EmailAddressService); const { senderID } = entity.email; const channel = await this.db.mailboxes.where({ recipientID },(x, p) => x.mailboxID === p.recipientID) .first(); let mailboxContact = await this.db.mailboxContacts .where({ recipientID, senderID }, (x, p) => x.mailboxID === p.recipientID && x.emailAddressID === p.senderID) .first(); mailboxContact = await this.getAccess(channel, mailboxContact); const isActiveSubscriber = mailboxContact && (mailboxContact.isOwner || mailboxContact.canContribute || mailboxContact.status === "active"); if(isActiveSubscriber) { return; } if (channel.isPrivateChannel) { throw new SmtpError("You must join the channel first.", SmtpStatusCodes.DeliveryNotPermitted); } if (channel.requiresMembershipApproval) { const { email } = entity; if (/^(subscribe|join|follow|➕|🔔)/i.test(email.subject)) { // lets add the recipient... const ea = await emailAddressService.getEmailAddress(senderID); email.engagement = "➕"; email.htmlBody = SubscriberRequested({ channelEmail: await emailAddressService.getEmailAddress(recipientID), name: ea.name, emailAddress: ea.emailAddress, html: email.htmlBody }).render(""); // this.db.queuePostSaveTask(() => // this.db.emails // .where(entity.email, (x, p) => x.emailID === p.emailID) // .update({ engagement, htmlBody }, (x, p) => ({ // engagement: p.engagement, // htmlBody: p.htmlBody // })) // ); return; } // throw an smtp error and send an invitation email... // send an invitation... await workflowContext.queue(SendChannelInvitation, { channelID: channel.mailboxID, emailAddressID: senderID }, { id: `send-channel-invitation-${channel.mailboxID}-${senderID}` }); throw new SmtpError("You must join the channel first.", SmtpStatusCodes.DeliveryNotPermitted); } } async approveSubscriber(emailID: number) { const parent = await this.db.mailboxContactEmails.where({ emailID}, (x, p) => x.mailbox.isChannel === true && x.email.children.some((c) => c.emailID === p.emailID)) .first(); const { mailboxID, emailAddressID } = parent; await this.db.mailboxContacts .where({ mailboxID, emailAddressID }, (x, p) => x.mailboxID === p.mailboxID && x.emailAddressID === p.emailAddressID) .update((x) => ({ status: "active" })); // send welcome email... const channelConfig = await this.db.channelConfigs.where({ mailboxID }, (x, p) => x.mailboxID === p.mailboxID && !Sql.text.isNullOrEmpty(x.welcomeContent)) .first(); if(!channelConfig) { return; } // send welcome email.. // to do.. } }