import Inject from "@entity-access/entity-access/dist/di/di.js"; import { Prepare } from "@entity-access/server-pages/dist/decorators/Prepare.js"; import SocialMailContext from "../../../../../../../server/model/SocialMailContext.js"; import CachedEmailService from "../../../../../../../server/smtp/services/CachedEmailService.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import { Email } from "../../../../../../../server/model/entities/Email.js"; import WebSiteChannelService from "../../../../../../../server/smtp/services/WebSiteChannelService.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; @Prepare.authorize export default class extends Page { @Inject private db: SocialMailContext; @Inject private cacheEmailService: CachedEmailService; @Inject private wcs: WebSiteChannelService; async run() { const [channelID, emailID, messageID] = this.childPath as any[]; const host = this.request.host; const userID = this.sessionUser.userID; let email: Email; const { mailboxes, domains } = await this.wcs.getChannelAssociations(host); // verify host ... const q = this.db.emails .where({ emailID, messageID }, (x, p) => x.emailID === p.emailID && x.headerMessageID === p.messageID ); if (this.sessionUser.isAdmin) { email = mailboxes ? await q .where({ mailboxes }, (x, p) => x.channelEmails.some((c) => Sql.in(c.mailboxID, p.mailboxes) ) ) .first() : await q .where({ domains }, (x, p) => x.channelEmails.some((c) => Sql.in(c.mailbox.domainID, p.domains) ) ) .first(); if (!email) { throw new Error("Post not found"); } } else { if (this.sessionUser.userID) { email = mailboxes ? await q .where({ channelID, mailboxes, userID }, (x, p) => x.channelEmails.some((c) => c.mailboxID === p.channelID && Sql.in(c.mailboxID, p.mailboxes) && c.mailbox.userMailboxes.some((mu) => mu.userID === p.userID) ) ) .first() : await q .where({ channelID, domains, userID }, (x, p) => x.channelEmails.some((c) => c.mailboxID === p.channelID && Sql.in(c.mailbox.domainID, p.domains) ) ) .first(); if (!email) { throw new Error("Post not found"); } } else { email = mailboxes ? await q .where({ mailboxes }, (x, p) => x.channelEmails.some((c) => Sql.in(c.mailboxID, p.mailboxes)) ) .first() : await q .where({ domains }, (x, p) => x.channelEmails.some((c) => Sql.in(c.mailbox.domainID, p.domains)) ) .first(); if (!email) { throw new Error("Post not found"); } } } const body = await this.cacheEmailService.getHtmlText(email); this.cacheControl = "public, max-age=2592000, immutable"; return this.content({ body, contentType: "text/plain", compress: true }); } }