import Inject, { RegisterSingleton, ServiceObject, ServiceProvider } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import { cacheFor, ICacheContainer, ObjectCache } from "../../../common/cacheFor.js"; import MessagingService from "../../services/message-events/MessagingService.js"; @RegisterSingleton export default class WebSiteChannelService extends ServiceObject implements ICacheContainer { cache: ObjectCache; @Inject private ms: MessagingService; @cacheFor() async getChannelAssociations(host: string) { using scope = ServiceProvider.createScope(this); const db = scope.resolve(SocialMailContext); host = host?.toLocaleLowerCase() ?? "localhost"; const mailboxList = await db.mailboxes.where({ host}, (x, p) => x.websiteChannels .some((w) => w.website.hosts .some((h)=> h.host === p.host))) .toArray(); const mailboxes = mailboxList.map((x) => x.mailboxID); const domainList = await db.hostedDomains.where({ host }, (x, p) => x.websiteChannelDomains .some((w) => w.website.hosts .some((h) => h.host === p.host)) ).toArray(); const domains = domainList.map((x) => x.domainID); if (domains.length === 0 && mailboxes.length === 0) { domains.push(0); } return { mailboxes: mailboxes.length ? mailboxes : void 0, domains }; } clear() { this.ms.channelRefresh.notify("clear"); } postInit() { this.ms.channelRefresh.on(() => this.cache?.clear("getChannelAssociations") ); } }