import { IConfigurationExtend, IConfigurationModify, IEnvironmentRead, IHttp, IHttpExtend, IModify, IPersistence, IRead, } from '../../definition/accessors'; import { ApiExtend, ConfigurationExtend, ConfigurationModify, EnvironmentalVariableRead, EnvironmentRead, ExternalComponentsExtend, Http, HttpExtend, LivechatRead, MessageRead, Modify, Notifier, Persistence, PersistenceRead, Reader, RoomRead, SchedulerExtend, SchedulerModify, ServerSettingRead, ServerSettingsModify, SettingRead, SettingsExtend, SlashCommandsExtend, SlashCommandsModify, UploadRead, UserRead, } from '../accessors'; import { CloudWorkspaceRead } from '../accessors/CloudWorkspaceRead'; import { UIExtend } from '../accessors/UIExtend'; import { AppManager } from '../AppManager'; import { AppBridges } from '../bridges/AppBridges'; export class AppAccessorManager { private readonly bridges: AppBridges; private readonly configExtenders: Map; private readonly envReaders: Map; private readonly configModifiers: Map; private readonly readers: Map; private readonly modifiers: Map; private readonly persists: Map; private readonly https: Map; constructor(private readonly manager: AppManager) { this.bridges = this.manager.getBridges(); this.configExtenders = new Map(); this.envReaders = new Map(); this.configModifiers = new Map(); this.readers = new Map(); this.modifiers = new Map(); this.persists = new Map(); this.https = new Map(); } /** * Purifies the accessors for the provided App. * * @param appId The id of the App to purge the accessors for. */ public purifyApp(appId: string): void { this.configExtenders.delete(appId); this.envReaders.delete(appId); this.configModifiers.delete(appId); this.readers.delete(appId); this.modifiers.delete(appId); this.persists.delete(appId); this.https.delete(appId); } public getConfigurationExtend(appId: string): IConfigurationExtend { if (!this.configExtenders.has(appId)) { const rl = this.manager.getOneById(appId); if (!rl) { throw new Error(`No App found by the provided id: ${ appId }`); } const htt = new HttpExtend(); const cmds = new SlashCommandsExtend(this.manager.getCommandManager(), appId); const apis = new ApiExtend(this.manager.getApiManager(), appId); const sets = new SettingsExtend(rl); const excs = new ExternalComponentsExtend(this.manager.getExternalComponentManager(), appId); const scheduler = new SchedulerExtend(this.manager.getSchedulerManager(), appId); const ui = new UIExtend(this.manager.getUIActionButtonManager(), appId); this.configExtenders.set(appId, new ConfigurationExtend(htt, sets, cmds, apis, excs, scheduler, ui)); } return this.configExtenders.get(appId); } public getEnvironmentRead(appId: string): IEnvironmentRead { if (!this.envReaders.has(appId)) { const rl = this.manager.getOneById(appId); if (!rl) { throw new Error(`No App found by the provided id: ${ appId }`); } const sets = new SettingRead(rl); const servsets = new ServerSettingRead(this.bridges.getServerSettingBridge(), appId); const env = new EnvironmentalVariableRead(this.bridges.getEnvironmentalVariableBridge(), appId); this.envReaders.set(appId, new EnvironmentRead(sets, servsets, env)); } return this.envReaders.get(appId); } public getConfigurationModify(appId: string): IConfigurationModify { if (!this.configModifiers.has(appId)) { this.configModifiers.set(appId, new ConfigurationModify( new ServerSettingsModify(this.bridges.getServerSettingBridge(), appId), new SlashCommandsModify(this.manager.getCommandManager(), appId), new SchedulerModify(this.bridges.getSchedulerBridge(), appId), )); } return this.configModifiers.get(appId); } public getReader(appId: string): IRead { if (!this.readers.has(appId)) { const env = this.getEnvironmentRead(appId); const msg = new MessageRead(this.bridges.getMessageBridge(), appId); const persist = new PersistenceRead(this.bridges.getPersistenceBridge(), appId); const room = new RoomRead(this.bridges.getRoomBridge(), appId); const user = new UserRead(this.bridges.getUserBridge(), appId); const noti = new Notifier(this.bridges.getUserBridge(), this.bridges.getMessageBridge(), appId); const livechat = new LivechatRead(this.bridges.getLivechatBridge(), appId); const upload = new UploadRead(this.bridges.getUploadBridge(), appId); const cloud = new CloudWorkspaceRead(this.bridges.getCloudWorkspaceBridge(), appId); this.readers.set(appId, new Reader(env, msg, persist, room, user, noti, livechat, upload, cloud)); } return this.readers.get(appId); } public getModifier(appId: string): IModify { if (!this.modifiers.has(appId)) { this.modifiers.set(appId, new Modify(this.bridges, appId)); } return this.modifiers.get(appId); } public getPersistence(appId: string): IPersistence { if (!this.persists.has(appId)) { this.persists.set(appId, new Persistence(this.bridges.getPersistenceBridge(), appId)); } return this.persists.get(appId); } public getHttp(appId: string): IHttp { if (!this.https.has(appId)) { let ext: IHttpExtend; if (this.configExtenders.has(appId)) { ext = this.configExtenders.get(appId).http; } else { const cf = this.getConfigurationExtend(appId); ext = cf.http; } this.https.set(appId, new Http(this, this.bridges, ext, appId)); } return this.https.get(appId); } }