import Inject, { RegisterScoped } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import { LocalFile } from "@entity-access/server-pages/dist/core/LocalFile.js"; import { Base36 } from "../../../common/Base36.js"; import XNode from "@entity-access/server-pages/dist/html/XNode.js"; import { JSDOM } from 'jsdom'; import CachedEmailService from "../../smtp/services/CachedEmailService.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import { globalEnv } from "../../../common/globalEnv.js"; import WebSiteChannelService from "../../smtp/services/WebSiteChannelService.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import EncryptionService from "../encryption/EncryptionService.js"; import EmailAddressService from "../EmailAddressService.js"; const render = (parent: HTMLElement, { name, attributes, children }: XNode) => { const child = parent.ownerDocument.createElement(name); if (attributes) { for (const key in attributes) { if (Object.hasOwn(attributes, key)) { const element = attributes[key]; switch(key) { case "innerHTML": child.innerHTML = element; continue; case "text": case "textContent": child.textContent = element; continue; } child.setAttribute(key, element); } } } parent.appendChild(child); if (children) { for (const iterator of children) { if (iterator === false as any as string || iterator === null || iterator === void 0) { continue; } if (typeof iterator === "string") { child.append(iterator); continue; } render(child, iterator); } } return child; }; @RegisterScoped export default class ChannelPageService { @Inject db: SocialMailContext; @Inject ce: CachedEmailService; @Inject wcs: WebSiteChannelService; @Inject encryptionService: EncryptionService; @Inject eas: EmailAddressService; getMailboxEmail(mailboxSlug: string, postSlug: string) { mailboxSlug = mailboxSlug.toLowerCase(); postSlug = postSlug.toLowerCase(); const match = /\-([^\-]+)\/?$/.exec(postSlug); if (!match) { return null; } const { 1: emailIDBase36 } = match; const emailID = Base36.decode(emailIDBase36) as any; if (mailboxSlug === "@") { return this.db.channelEmails.where({ emailID}, (x, p) => x.emailID === p.emailID).first(); } if(mailboxSlug.startsWith("@")) { mailboxSlug = mailboxSlug.substring(1) + "@%"; } return this.db.channelEmails.where({ emailID, mailboxSlug}, (x, p) => Sql.text.like(x.mailbox.emailAddress.emailAddress,p.mailboxSlug) && x.emailID === p.emailID ).first(); } getMeta(host, url, [blogID, type, p1]) { if (type !== "p") { return Promise.resolve({} as any); } blogID = Base36.decode(blogID) as any; const emailID = Base36.decode(p1) as any; return this.getEmailMeta(host, url, blogID, emailID); } async getEmailMeta(host, url, blogID, emailID) { const channelEmail = await this.db.channelEmails .where({ blogID, emailID }, (x, p) => x.emailID === p.emailID && x.datePublished !== null) .include((x) => [x.email]) .firstOrFail(); if (channelEmail.previewFileID) { await this.db.changeSet.getEntry(channelEmail).loadNavigationAsync((x) => x.previewFile); } const { email, previewFile } = channelEmail; const blog = await this.eas.getHostedMailboxByID(blogID); const title = channelEmail.email.subject; const meta = [] as XNode[]; meta.push(); meta.push(); meta.push(); meta.push(); meta.push(); meta.push(); meta.push(); if (email.summary) { meta.push(); } // find first image... if (previewFile) { const cdn = globalEnv.cdn ?? host; const postID = this.encryptionService.general.encrypt(emailID); meta.push(); } return { meta, email, title }; } async modifyPage({ host, url, file, blogID, emailID, type }:{ host: string, url: string, file: LocalFile, type, blogID, emailID }) { if (!blogID) { // find first from host... const { mailboxes, domains } = await this.wcs.getChannelAssociations(host); const q = mailboxes ? this.db.mailboxes .where({ mailboxes}, (x, p) => Sql.in(x.mailboxID, p.mailboxes) ) : this.db.mailboxes .where({ domains }, (x, p) => Sql.in(x.domainID, p.domains) ); const b = await q .orderByDescending((x) => x.datePublished) .first(); blogID = Base36.encode(b.mailboxID); const e = await this.db.emails .where({ blogID }, (x, p) => x.status === "published" && x.channelEmails.some((m) => m.mailboxID === p.blogID) ) .orderByDescending((x) => x.emailID) .first(); emailID = Base36.encode(e.emailID); } blogID = Base36.decode(blogID); emailID = Base36.decode(emailID); const html = await file.readAsText(); const { meta , email, title } = await this.getMeta(host, url, [blogID, "p", emailID]); const emailBody = await this.ce.getHtmlText(email); return await this.inject({ html, title, meta, content: [