import Inject from "@entity-access/entity-access/dist/di/di.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import SocialMailContext from "../../../../../server/model/SocialMailContext.js"; import EmailLogService from "../../../../../server/services/email-logs/EmailLogService.js"; import { MimeNode, LineStream } from "@social-mail/shared/index.js"; import { Prepare } from "@entity-access/server-pages/dist/decorators/Prepare.js"; import { secureFetch } from "../../../../../server/services/root-certs/RootCertificates.js"; @Prepare.authorize export default class extends Page { @Inject db: SocialMailContext; @Inject private els: EmailLogService; async run() { const [emailID] = this.childPath as any[]; const email = await this.db.emails .filtered("read") .where({ emailID }, (x, p) => x.emailID === p.emailID) .include((x) => x.mimeFile) .first(); const file = await this.els.load(email.mimeFile); const root = new MimeNode(); await root.parse( new (class extends LineStream { lines() { return file.lines(); } })()); let u = root.header("List-Unsubscribe") as string; u = u.split(",").find((x) => /^")) { u = u.substring(0, u.length - 1); } if (u.startsWith("<")) { u = u.substring(1); } let method = "GET"; let headers = void 0; const body = root.header("List-Unsubscribe-Post") || void 0; if (root.header("List-Unsubscribe-Post")) { method = "POST"; headers = { "content-type": "application/x-www-form-urlencoded" }; } const rs = await secureFetch(u, { method, headers, body }); let failed = ""; let result = ""; const status = rs.status; if (status > 399) { failed = await rs.text(); } else { result = await rs.text(); } return this.json({ failed, result }, void 0, { status }); } }