import { IEntityQuery } from "@entity-access/entity-access/dist/model/IFilterWithParameter.js"; import MailboxContact from "../entities/MailboxContact.js"; import AuthenticatedEvents from "./AuthenticatedEvents.js"; import ChangeEntry from "@entity-access/entity-access/dist/model/changes/ChangeEntry.js"; import Inject, { ServiceProvider } from "@entity-access/entity-access/dist/di/di.js"; import EmailAddressService from "../../services/EmailAddressService.js"; import { ForeignKeyFilter } from "@entity-access/entity-access/dist/model/events/EntityEvents.js"; import SocialMailContext from "../SocialMailContext.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import TagService from "../../services/tags/TagService.js"; import MailboxFolderService from "../../services/mailboxes/MailboxFolderService.js"; export default class MailboxContactEvents extends AuthenticatedEvents { @Inject private eas: EmailAddressService; @Inject private ts: TagService; @Inject private mfs: MailboxFolderService; filter(query: IEntityQuery): IEntityQuery { if (this.verify) { this.sessionUser.ensureLoggedIn(); } if (this.sessionUser.isAdmin) { return query; } const { userID } = this.sessionUser; return query.where({ userID }, (x, p) => x.mailbox.userMailboxes.some((u) => u.userID === p.userID) || x.mailbox.domain.hostedDomain.domainUsers.some((u) => u.userID === p.userID || u.isAdmin === true)); } modify(query: IEntityQuery): IEntityQuery { return this.filter(query); } async beforeInsert(entity: MailboxContact, entry: ChangeEntry) { if (this.verify) { this.sessionUser.ensureLoggedIn(); } if (entity.emailAddressText) { entity.emailAddressID = await this.eas.getId(entity.emailAddressText, entity.name); // when this is added by the user // we need to make sure the folder is contact entity.folderTagID ??= await this.ts.getSystemTagID("Contact"); } if (entity.folderTagText) { entity.folderTagID ??= await this.ts.getTagID(entity.folderTagText); } } onForeignKeyFilter(filter: ForeignKeyFilter) { if(filter.is((x) => x.emailAddressID)) { return null; } if(filter.is((x) => x.folderTagID)) { return null; } } async beforeUpdate(entity: MailboxContact, entry: ChangeEntry) { // if blocked was changed const { mailboxID, blocked, autoClose, emailAddressID, openThreads } = entity; const db = ServiceProvider.resolve(this, SocialMailContext); if (entity.closeAllConversations) { // we will simply put a dummy email // with userID set to current userid // and set it as lastReplyID // check if there are any open threads.. if (openThreads) { const dateCreated = DateTime.now; const dateSent = dateCreated; const dateUpdated = dateCreated; const dateReceived = dateCreated; const { userID } = this.sessionUser; const user = await db.users.statements.select({}, { userID }); const senderName = user.displayName; const email = await db.emails.statements.insert({ subject: "Thread Closed", textBody: "", htmlBody: "", senderID: mailboxID, userID: this.sessionUser.userID, senderName, dateCreated, dateSent, dateUpdated, dateReceived, received: false, status: "closed" }); const { emailID } = email; await db.mailboxContactEmails.statements.insert({ emailID, emailAddressID, mailboxID, dateReceived, timeRead: dateSent }); // await db.mailboxEmails.statements.insert({ // emailID, // mailboxID // }); await db.emails .where(entity , (x, p) => x.threadContacts.some((t) => t.mailboxID === p.mailboxID && t.emailAddressID === p.emailAddressID) && x.threadID === null && x.lastReplyID === null ) .update(email, (x, p) => ({ lastReplyID: Sql.cast.asBigInt(p.emailID) })); await db.mailboxes.where(entity, (x, p) => x.mailboxID === p.mailboxID) .update(entity, (x, p) => ({ openThreads: Sql.math.max(0, x.openThreads - p.openThreads) })); entity.openThreads = 0; } } let tagUpdated = false; if (entry.isModified("folderTagID")) { tagUpdated = true; const oldID = entry.getChange("folderTagID").oldValue; if (oldID) { await this.mfs.incrementFolderCount(mailboxID, oldID, -1); } await this.mfs.incrementFolderCount(mailboxID, entity.folderTagID, 1); const ts = ServiceProvider.resolve(this, TagService); const spam = await ts.getSystemTagID("Spam"); const newRequest = await ts.getSystemTagID("New"); const isBlocked = await ts.getSystemTagID("Blocked"); entity.spam = spam === entity.folderTagID; entity.newRequest = newRequest === entity.folderTagID; entity.blocked = isBlocked === entity.folderTagID; entry.detect(); } const modifiedBlocked = entry.isModified("blocked"); if ( modifiedBlocked || ( !tagUpdated && (entry.isModified("spam") || entry.isModified("newRequest")))) { // update tag folder... // only if older folder is one of the system folder... if (entity.blocked) { entity.newRequest = false; entity.spam = false; } if (entity.spam) { entity.newRequest = false; } const ts = ServiceProvider.resolve(this, TagService); const systemIDs = await ts.getSystemTagIDs(["Contacts", "New", "Spam", "Blocked"]); if (modifiedBlocked || systemIDs.includes(entity.folderTagID) || entity.newRequest) { const oldID = entity.folderTagID; const tagID = await ts.getSystemTagID(entity.spam ? "Spam" : entity.blocked ? "Blocked" : entity.newRequest ? "New" : "Contacts"); entity.folderTagID = tagID; if (tagID !== oldID) { await this.mfs.incrementFolderCount(mailboxID, oldID, -1); await this.mfs.incrementFolderCount(mailboxID, tagID, 1); } } } if (entry.isModified("archived") || entry.isModified("alwaysArchived")) { if(entity.alwaysArchived) { entity.archived = true; } const tagID = entity.folderTagID; const diff = entity.archived ? 1 : -1; await this.mfs.incrementFolderCount(mailboxID, tagID, diff, diff); } if (entry.isModified("autoClose") || entry.isModified("blocked")) { if (autoClose || blocked) { await db.mailboxes.where({ mailboxID }, (x, p) => x.mailboxID === p.mailboxID) .update({ openThreads }, (x, p) => ({ openThreads: Sql.math.max(0, x.openThreads - Sql.cast.asInteger(p.openThreads)), })); entity.openThreads = 0; } else { // we need to recalculate const all = await db.emails .where(entity , (x, p) => x.threadContacts.some((t) => t.mailboxID === p.mailboxID && t.emailAddressID === p.emailAddressID) && x.threadID === null ) .sum((x) => ({ totalThreads: 1, openThreads: x.lastReplyID === null ? 1 : 0 })); entity.openThreads = all.openThreads; entity.totalThreads = all.totalThreads; } } } }