import Inject, { RegisterScoped } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import { Email } from "../../model/entities/Email.js"; import TempFileService from "../../storage/TempFileService.js"; import FileConversionService from "../convert/FileConversionService.js"; import { AppStringHelper } from "../../../common/AppStringHelper.js"; @RegisterScoped export default class IndexEmailService { @Inject db: SocialMailContext; @Inject fcs: FileConversionService; @Inject tempFileService: TempFileService; async indexEmail({ emailID, subject, textBody }: Partial) { // delete all search email tokens if exists const { db } = this; let text = subject || ""; if(textBody) { text += " " + textBody; } // for all attachments... const attachments = await db.appFiles.where({ emailID}, (x, p) => x.emailID === p.emailID) .toArray(); for(const att of attachments) { text += " " + att.name + " "; const txt = await db.textDocuments .statements.select({}, { textDocumentID: att.fileContentID}); if (txt) { text += " " + txt.searchable; continue; } const tf = await this.tempFileService.downloadAppFile(att); const textFile = await this.fcs.extractText(tf); text += " " + await textFile.readAsText(); } text = AppStringHelper.remove00(text.replace(/\s+/g, " ").trim().toLowerCase()); // deduplication // text = Array.from(new Set(text.split(" "))).join(" "); // await db.emailTexts.statements.selectOrInsert({ text, emailID }, { emailID }); await db.emailTexts.withKeys({ emailID }) .updateOrInsert({ text }); } }