import Inject, { RegisterScoped, ServiceProvider } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import TempFileService from "../../storage/TempFileService.js"; import TimeSpan from "@entity-access/entity-access/dist/types/TimeSpan.js"; import HtmlSanitizer from "../../services/dom/HtmlSanitizer.js"; import EmailParserService from "./EmailParserService.js"; import CacheService from "../../services/cache/CacheService.js"; import { Email, EmailLog } from "../../model/entities/Email.js"; import EmailLogService from "../../services/email-logs/EmailLogService.js"; import EncryptionService from "../../services/encryption/EncryptionService.js"; import { LocalFile } from "@entity-access/server-pages/dist/core/LocalFile.js"; import BlogContentTransformer from "../../services/dom/BlogContentTransformer.js"; import { globalEnv } from "../../../common/globalEnv.js"; @RegisterScoped export default class CachedEmailService { @Inject db: SocialMailContext; @Inject tempFileService: TempFileService; @Inject cacheService: CacheService; @Inject encryptionService: EncryptionService; async getHtmlForRecipient({ emailID, status = "published" }: { emailID: number, status?: string }, blog: string = "", senderDomain = void 0) { const factory = async () => { let logFile: EmailLog; if (!logFile) { logFile = await this.db.emailLogs .where({ emailID }, (x, p) => x.mimeFileEmails.some((mf) => mf.emailID === p.emailID)) .first(); } let html = ""; if (!logFile) { const email = await this.db.emails .where({ emailID }, (x, p) => x.emailID === p.emailID) .include((x) => x.htmlBodyFile) .first(); html = email.htmlBody; if (!html && email.htmlBodyFile) { const els = ServiceProvider.resolve(this, EmailLogService); const hbf = await els.load(email.htmlBodyFile); html = await hbf.readAsText(); } } this.db.verifyFilters = false; let emailHtmlFile: LocalFile; if (logFile) { const els = ServiceProvider.resolve(this, EmailLogService); emailHtmlFile = await els.load(logFile); const emailSerializerService = ServiceProvider.resolve(this, EmailParserService); const mail = await emailSerializerService.parse(emailHtmlFile); html = mail.html || mail.textAsHtml || ""; } const htmlService = ServiceProvider.resolve(this, BlogContentTransformer); if (status !== "draft") { let ei; let prefix; const { host, encryptionKey } = globalEnv.emailContentProxy; if (host) { ei = this.encryptionService.global.encrypt(emailID.toString(), encryptionKey, host); prefix = `https://${host}/api/emails/d/${ei}/${emailID}/`; } else { ei = this.encryptionService.general.encrypt(emailID.toString()); prefix = `/api/emails/d/${ei}/${emailID}/`; } html = await htmlService.sanitizeExternalResources({ html, emailID, messageID: void 0, senderDomain, prefix, blog }); } const htmlFile = await this.tempFileService.createTempFile(".html", "text/html"); await htmlFile.writeAllText(html || ""); return htmlFile; }; const ef = await this.cacheService.get({ key: `cached-email-html-b19-${emailID}-${senderDomain}-${status}`, factory, ttl: TimeSpan.fromDays(30) }); return await ef.readAsText(); // return (await factory()).readAsText(); } getHtml(emailID: number, email?: Email) { const factory = async () => { this.db.verifyFilters = false; const htmlLog = await this.db.emailLogs.where({ emailID }, (x, p) => x.htmlBodyFileEmails.some((b) => b.emailID === p.emailID)) .first(); if (htmlLog) { const els = ServiceProvider.resolve(this, EmailLogService); return await els.load(htmlLog); } const file = await this.getEmailFile(emailID); const htmlFile = await this.tempFileService.createTempFile(".html", "text/html"); if (!file) { if (!email) { email = await this.db.emails.where({ emailID }, (x, p) => x.emailID === p.emailID).first(); } await htmlFile.writeAllText(email.htmlBody ?? ""); return htmlFile; } const emailSerializerService = ServiceProvider.resolve(this, EmailParserService); const mail = await emailSerializerService.parse(file); const htmlService = ServiceProvider.resolve(this, HtmlSanitizer); const { html } = htmlService.sanitize(mail.html || mail.textAsHtml || "", mail.text); await htmlFile.writeAllText(html || ""); return htmlFile; }; return this.cacheService.get({ key: `cached-email-html-4-${emailID}`, factory, ttl: TimeSpan.fromDays(30) }); } async getHtmlText(email: Email) { const file = await this.getHtml(email.emailID, email); return await file.readAsText(); } async getEmailFile(emailID: number) { const mimeFile = await this.db.emailLogs.where({ emailID }, (x, p) => x.mimeFileEmails.some((m) => m.emailID === p.emailID)) .first(); if (mimeFile) { const els = ServiceProvider.resolve(this, EmailLogService); return await els.load(mimeFile); } // const appFile = await this.db.appFiles.where({ emailID }, (x, p) => x.originalEmails.some((e) => e.emailID === p.emailID)) // .first(); // if (appFile) { // return this.tempFileService.downloadFileWithoutCache(appFile); // } } async get(emailID) { return await this.db.emails.statements.select({ }, { emailID }); } }