From f6c79406e4eb6febeac63e2434d23c5cc2be237c Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Mon, 9 May 2022 18:33:35 -0300 Subject: [PATCH 1/8] Fix overwritten settings not getting cached --- apps/meteor/app/settings/server/SettingsRegistry.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/meteor/app/settings/server/SettingsRegistry.ts b/apps/meteor/app/settings/server/SettingsRegistry.ts index 86bde0d764f7..f03159511c00 100644 --- a/apps/meteor/app/settings/server/SettingsRegistry.ts +++ b/apps/meteor/app/settings/server/SettingsRegistry.ts @@ -163,12 +163,15 @@ export class SettingsRegistry { }), }, ); + + this.store.set(settingOverwritten); return; } if (settingStored && isOverwritten) { if (settingStored.value !== settingOverwritten.value) { this.model.upsert({ _id }, settingProps); + this.store.set(settingOverwritten); } return; } From 2a543511ca99779853bb38c165e5a427d4110587 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 10 May 2022 19:50:44 -0300 Subject: [PATCH 2/8] Reorganize settings startup --- apps/meteor/app/lib/server/index.js | 8 --- apps/meteor/app/lib/server/startup/index.ts | 8 +++ .../app/models/server/models/Messages.js | 2 +- apps/meteor/app/models/server/models/Users.js | 2 +- .../app/settings/server/SettingsRegistry.ts | 2 - .../app/settings/server/applyMiddlewares.ts | 63 +++++++++++++++++ .../app/settings/server/functions/settings.ts | 14 ---- apps/meteor/app/settings/server/index.ts | 13 +++- apps/meteor/app/settings/server/startup.ts | 70 +++---------------- .../meteor/ee/app/settings/server/settings.ts | 3 +- apps/meteor/server/importPackages.ts | 2 +- apps/meteor/server/main.ts | 4 +- apps/meteor/server/services/meteor/service.ts | 2 +- 13 files changed, 99 insertions(+), 94 deletions(-) create mode 100644 apps/meteor/app/lib/server/startup/index.ts create mode 100644 apps/meteor/app/settings/server/applyMiddlewares.ts delete mode 100644 apps/meteor/app/settings/server/functions/settings.ts diff --git a/apps/meteor/app/lib/server/index.js b/apps/meteor/app/lib/server/index.js index bd6b00d2751c..a67ce3fb2f63 100644 --- a/apps/meteor/app/lib/server/index.js +++ b/apps/meteor/app/lib/server/index.js @@ -1,11 +1,3 @@ -import './startup/email'; -import './startup/oAuthServicesUpdate'; -import './startup/rateLimiter'; -import './startup/robots'; -import './startup/settings'; -import './startup/settingsOnLoadCdnPrefix'; -import './startup/settingsOnLoadDirectReply'; -import './startup/settingsOnLoadSMTP'; import '../lib/MessageTypes'; import './lib/bugsnag'; import './lib/debug'; diff --git a/apps/meteor/app/lib/server/startup/index.ts b/apps/meteor/app/lib/server/startup/index.ts new file mode 100644 index 000000000000..06a310bb733f --- /dev/null +++ b/apps/meteor/app/lib/server/startup/index.ts @@ -0,0 +1,8 @@ +import './email'; +import './oAuthServicesUpdate'; +import './rateLimiter'; +import './robots'; +import './settings'; +import './settingsOnLoadCdnPrefix'; +import './settingsOnLoadDirectReply'; +import './settingsOnLoadSMTP'; diff --git a/apps/meteor/app/models/server/models/Messages.js b/apps/meteor/app/models/server/models/Messages.js index dae73072ba18..ebb91426337e 100644 --- a/apps/meteor/app/models/server/models/Messages.js +++ b/apps/meteor/app/models/server/models/Messages.js @@ -3,7 +3,7 @@ import _ from 'underscore'; import { Base } from './_Base'; import Rooms from './Rooms'; -import { settings } from '../../../settings/server/functions/settings'; +import { settings } from '../../../settings/server'; import { otrSystemMessages } from '../../../otr/lib/constants'; export class Messages extends Base { diff --git a/apps/meteor/app/models/server/models/Users.js b/apps/meteor/app/models/server/models/Users.js index 633fe3188450..0a794af7e32f 100644 --- a/apps/meteor/app/models/server/models/Users.js +++ b/apps/meteor/app/models/server/models/Users.js @@ -6,7 +6,7 @@ import { escapeRegExp } from '@rocket.chat/string-helpers'; import { Base } from './_Base'; import Subscriptions from './Subscriptions'; -import { settings } from '../../../settings/server/functions/settings'; +import { settings } from '../../../settings/server'; const queryStatusAgentOnline = (extraFilters = {}) => ({ statusLivechat: 'available', diff --git a/apps/meteor/app/settings/server/SettingsRegistry.ts b/apps/meteor/app/settings/server/SettingsRegistry.ts index f03159511c00..d6799596ed9a 100644 --- a/apps/meteor/app/settings/server/SettingsRegistry.ts +++ b/apps/meteor/app/settings/server/SettingsRegistry.ts @@ -164,14 +164,12 @@ export class SettingsRegistry { }, ); - this.store.set(settingOverwritten); return; } if (settingStored && isOverwritten) { if (settingStored.value !== settingOverwritten.value) { this.model.upsert({ _id }, settingProps); - this.store.set(settingOverwritten); } return; } diff --git a/apps/meteor/app/settings/server/applyMiddlewares.ts b/apps/meteor/app/settings/server/applyMiddlewares.ts new file mode 100644 index 000000000000..95f4ad1c9239 --- /dev/null +++ b/apps/meteor/app/settings/server/applyMiddlewares.ts @@ -0,0 +1,63 @@ +import { Meteor } from 'meteor/meteor'; + +import { use } from './Middleware'; +import { settings } from './index'; + +const getProcessingTimeInMS = (time: [number, number]): number => time[0] * 1000 + time[1] / 1e6; + +settings.watch = use(settings.watch, (context, next) => { + const [_id, callback, ...args] = context; + return next(_id, Meteor.bindEnvironment(callback), ...args); +}); + +if (process.env.DEBUG_SETTINGS === 'true') { + settings.watch = use(settings.watch, function watch(context, next) { + const [_id, callback, options] = context; + return next( + _id, + (...args) => { + const start = process.hrtime(); + callback(...args); + const elapsed = process.hrtime(start); + console.log(`settings.watch: ${_id} ${getProcessingTimeInMS(elapsed)}ms`); + }, + options, + ); + }); +} +settings.watchMultiple = use(settings.watchMultiple, (context, next) => { + const [_id, callback, ...args] = context; + return next(_id, Meteor.bindEnvironment(callback), ...args); +}); +settings.watchOnce = use(settings.watchOnce, (context, next) => { + const [_id, callback, ...args] = context; + return next(_id, Meteor.bindEnvironment(callback), ...args); +}); + +settings.watchByRegex = use(settings.watchByRegex, (context, next) => { + const [_id, callback, ...args] = context; + return next(_id, Meteor.bindEnvironment(callback), ...args); +}); + +settings.change = use(settings.change, (context, next) => { + const [_id, callback, ...args] = context; + return next(_id, Meteor.bindEnvironment(callback), ...args); +}); +settings.changeMultiple = use(settings.changeMultiple, (context, next) => { + const [_id, callback, ...args] = context; + return next(_id, Meteor.bindEnvironment(callback), ...args); +}); +settings.changeOnce = use(settings.changeOnce, (context, next) => { + const [_id, callback, ...args] = context; + return next(_id, Meteor.bindEnvironment(callback), ...args); +}); + +settings.changeByRegex = use(settings.changeByRegex, (context, next) => { + const [_id, callback, ...args] = context; + return next(_id, Meteor.bindEnvironment(callback), ...args); +}); + +settings.onReady = use(settings.onReady, (context, next) => { + const [callback, ...args] = context; + return next(Meteor.bindEnvironment(callback), ...args); +}); diff --git a/apps/meteor/app/settings/server/functions/settings.ts b/apps/meteor/app/settings/server/functions/settings.ts deleted file mode 100644 index d5deac757c0a..000000000000 --- a/apps/meteor/app/settings/server/functions/settings.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ISetting } from '@rocket.chat/core-typings'; - -import SettingsModel from '../../../models/server/models/Settings'; -import { CachedSettings } from '../CachedSettings'; -import { SettingsRegistry } from '../SettingsRegistry'; - -export const settings = new CachedSettings(); -SettingsModel.find().forEach((record: ISetting) => { - settings.set(record); -}); - -settings.initilized(); - -export const settingsRegistry = new SettingsRegistry({ store: settings, model: SettingsModel }); diff --git a/apps/meteor/app/settings/server/index.ts b/apps/meteor/app/settings/server/index.ts index f581b33a1af6..a69253a69a37 100644 --- a/apps/meteor/app/settings/server/index.ts +++ b/apps/meteor/app/settings/server/index.ts @@ -1,4 +1,11 @@ -import { settings, settingsRegistry } from './functions/settings'; -import { SettingsEvents } from './SettingsRegistry'; +import SettingsModel from '../../models/server/models/Settings'; +import { CachedSettings } from './CachedSettings'; +import { SettingsRegistry } from './SettingsRegistry'; +import { initializeSettings } from './startup'; -export { settings, settingsRegistry, SettingsEvents }; +export { SettingsEvents } from './SettingsRegistry'; + +export const settings = new CachedSettings(); +export const settingsRegistry = new SettingsRegistry({ store: settings, model: SettingsModel }); + +initializeSettings({ SettingsModel, settings }); diff --git a/apps/meteor/app/settings/server/startup.ts b/apps/meteor/app/settings/server/startup.ts index 74a71bb9e11e..191f831b4513 100644 --- a/apps/meteor/app/settings/server/startup.ts +++ b/apps/meteor/app/settings/server/startup.ts @@ -1,63 +1,15 @@ -import { Meteor } from 'meteor/meteor'; +import type { ISetting } from '@rocket.chat/core-typings'; -import { use } from './Middleware'; -import { settings } from './functions/settings'; +import { Settings } from '../../models/server/models/Settings'; +import { ICachedSettings } from './CachedSettings'; -const getProcessingTimeInMS = (time: [number, number]): number => time[0] * 1000 + time[1] / 1e6; - -settings.watch = use(settings.watch, (context, next) => { - const [_id, callback, ...args] = context; - return next(_id, Meteor.bindEnvironment(callback), ...args); -}); - -if (process.env.DEBUG_SETTINGS === 'true') { - settings.watch = use(settings.watch, function watch(context, next) { - const [_id, callback, options] = context; - return next( - _id, - (...args) => { - const start = process.hrtime(); - callback(...args); - const elapsed = process.hrtime(start); - console.log(`settings.watch: ${_id} ${getProcessingTimeInMS(elapsed)}ms`); - }, - options, - ); +export function initializeSettings({ SettingsModel, settings }: { SettingsModel: Settings; settings: ICachedSettings }): void { + SettingsModel.find().forEach((record: ISetting) => { + if (record._id.startsWith('Prometheus')) { + console.log('store cache', record); + } + settings.set(record); }); -} -settings.watchMultiple = use(settings.watchMultiple, (context, next) => { - const [_id, callback, ...args] = context; - return next(_id, Meteor.bindEnvironment(callback), ...args); -}); -settings.watchOnce = use(settings.watchOnce, (context, next) => { - const [_id, callback, ...args] = context; - return next(_id, Meteor.bindEnvironment(callback), ...args); -}); - -settings.watchByRegex = use(settings.watchByRegex, (context, next) => { - const [_id, callback, ...args] = context; - return next(_id, Meteor.bindEnvironment(callback), ...args); -}); -settings.change = use(settings.change, (context, next) => { - const [_id, callback, ...args] = context; - return next(_id, Meteor.bindEnvironment(callback), ...args); -}); -settings.changeMultiple = use(settings.changeMultiple, (context, next) => { - const [_id, callback, ...args] = context; - return next(_id, Meteor.bindEnvironment(callback), ...args); -}); -settings.changeOnce = use(settings.changeOnce, (context, next) => { - const [_id, callback, ...args] = context; - return next(_id, Meteor.bindEnvironment(callback), ...args); -}); - -settings.changeByRegex = use(settings.changeByRegex, (context, next) => { - const [_id, callback, ...args] = context; - return next(_id, Meteor.bindEnvironment(callback), ...args); -}); - -settings.onReady = use(settings.onReady, (context, next) => { - const [callback, ...args] = context; - return next(Meteor.bindEnvironment(callback), ...args); -}); + settings.initilized(); +} diff --git a/apps/meteor/ee/app/settings/server/settings.ts b/apps/meteor/ee/app/settings/server/settings.ts index 9f01e759bfbd..a4da8d0ed4e9 100644 --- a/apps/meteor/ee/app/settings/server/settings.ts +++ b/apps/meteor/ee/app/settings/server/settings.ts @@ -1,11 +1,10 @@ import { Meteor } from 'meteor/meteor'; import { ISetting, SettingValue } from '@rocket.chat/core-typings'; -import { settings } from '../../../../app/settings/server/functions/settings'; import { isEnterprise, hasLicense, onValidateLicenses } from '../../license/server/license'; import SettingsModel from '../../../../app/models/server/models/Settings'; import { use } from '../../../../app/settings/server/Middleware'; -import { SettingsEvents } from '../../../../app/settings/server'; +import { settings, SettingsEvents } from '../../../../app/settings/server'; export function changeSettingValue(record: ISetting): SettingValue { if (!record.enterprise) { diff --git a/apps/meteor/server/importPackages.ts b/apps/meteor/server/importPackages.ts index 44ff3394678b..2aaf4578dc9c 100644 --- a/apps/meteor/server/importPackages.ts +++ b/apps/meteor/server/importPackages.ts @@ -41,7 +41,7 @@ import '../app/integrations/server'; import '../app/irc'; import '../app/issuelinks/server'; import '../app/katex/server'; -import '../app/lib'; +import '../app/lib/server'; import '../app/livestream/server'; import '../app/logger'; import '../app/token-login/server'; diff --git a/apps/meteor/server/main.ts b/apps/meteor/server/main.ts index 9c867d86240c..a7356479ad9f 100644 --- a/apps/meteor/server/main.ts +++ b/apps/meteor/server/main.ts @@ -1,11 +1,11 @@ +import './services/startup'; import '../app/settings/server/startup'; import '../lib/oauthRedirectUri'; import './overrides/http'; import './lib/logger/startup'; import './importPackages'; import '../imports/startup/server'; - -import './services/startup'; +import '../app/lib/server/startup'; import '../ee/server'; import './lib/pushConfig'; diff --git a/apps/meteor/server/services/meteor/service.ts b/apps/meteor/server/services/meteor/service.ts index 8c22d21fda47..a402c0307443 100644 --- a/apps/meteor/server/services/meteor/service.ts +++ b/apps/meteor/server/services/meteor/service.ts @@ -10,7 +10,7 @@ import { AutoUpdateRecord, IMeteor } from '../../sdk/types/IMeteor'; import { api } from '../../sdk/api'; import { Users } from '../../../app/models/server/raw/index'; import { Livechat } from '../../../app/livechat/server'; -import { settings } from '../../../app/settings/server/functions/settings'; +import { settings } from '../../../app/settings/server'; import { setValue, updateValue } from '../../../app/settings/server/raw'; import { RoutingManager } from '../../../app/livechat/server/lib/RoutingManager'; import { onlineAgents, monitorAgents } from '../../../app/livechat/server/lib/stream/agentStatus'; From 9f88304d12c1e18028ecb1edc5fbdd1d6d6fd34c Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 10 May 2022 21:11:27 -0300 Subject: [PATCH 3/8] Fix federations settings --- apps/meteor/app/federation/server/startup/settings.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/apps/meteor/app/federation/server/startup/settings.ts b/apps/meteor/app/federation/server/startup/settings.ts index 746a2b5e694b..04a9c6a49add 100644 --- a/apps/meteor/app/federation/server/startup/settings.ts +++ b/apps/meteor/app/federation/server/startup/settings.ts @@ -69,7 +69,7 @@ settingsRegistry.addGroup('Federation', function () { const updateSettings = async function (): Promise { // Get the key pair - if (getFederationDiscoveryMethod() === 'hub' && !Promise.await(isRegisteringOrEnabled())) { + if (getFederationDiscoveryMethod() === 'hub' && !(await isRegisteringOrEnabled())) { // Register with hub try { await updateStatus(STATUS_REGISTERING); @@ -89,20 +89,18 @@ const updateSettings = async function (): Promise { }; // Add settings listeners -settings.watch('FEDERATION_Enabled', function enableOrDisable(value) { +settings.watch('FEDERATION_Enabled', async function enableOrDisable(value) { setupLogger.info(`Federation is ${value ? 'enabled' : 'disabled'}`); if (value) { - Promise.await(updateSettings()); + await updateSettings(); enableCallbacks(); } else { - Promise.await(updateStatus(STATUS_DISABLED)); + await updateStatus(STATUS_DISABLED); disableCallbacks(); } - - value && updateSettings(); }); settings.watchMultiple(['FEDERATION_Discovery_Method', 'FEDERATION_Domain'], updateSettings); From b5c636258de097662fa04d6d51b8fb1a49489841 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 11 May 2022 01:36:08 -0300 Subject: [PATCH 4/8] generateBoilerplate --- .../app/lib/server/startup/settingsOnLoadSiteUrl.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/meteor/app/lib/server/startup/settingsOnLoadSiteUrl.ts b/apps/meteor/app/lib/server/startup/settingsOnLoadSiteUrl.ts index 11983536c40d..c722390b7b62 100644 --- a/apps/meteor/app/lib/server/startup/settingsOnLoadSiteUrl.ts +++ b/apps/meteor/app/lib/server/startup/settingsOnLoadSiteUrl.ts @@ -5,6 +5,12 @@ import { settings } from '../../../settings/server'; export let hostname: string; +const generateBoilerplate = Meteor.bindEnvironment(function _generateBoilerplate(): void { + if (typeof WebAppInternals !== 'undefined' && WebAppInternals.generateBoilerplate) { + return WebAppInternals.generateBoilerplate(); + } +}); + settings.watch('Site_Url', function (value) { if (value == null || value.trim() === '') { return; @@ -25,7 +31,5 @@ settings.watch('Site_Url', function (value) { hostname = host.replace(/^https?:\/\//, ''); process.env.MOBILE_ROOT_URL = host; process.env.MOBILE_DDP_URL = host; - if (typeof WebAppInternals !== 'undefined' && WebAppInternals.generateBoilerplate) { - return WebAppInternals.generateBoilerplate(); - } + generateBoilerplate(); }); From 719237b9afbe26b0eb5d69b7f17c4facd382676a Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 11 May 2022 11:51:52 -0300 Subject: [PATCH 5/8] setInlineScriptsAllowed --- apps/meteor/app/cors/server/cors.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/meteor/app/cors/server/cors.js b/apps/meteor/app/cors/server/cors.js index 5f4e9db2a34b..b34479a88f1c 100644 --- a/apps/meteor/app/cors/server/cors.js +++ b/apps/meteor/app/cors/server/cors.js @@ -5,14 +5,18 @@ import { WebApp, WebAppInternals } from 'meteor/webapp'; import _ from 'underscore'; import { settings } from '../../settings/server'; -import { Logger } from '../../logger'; +import { Logger } from '../../logger/server'; const logger = new Logger('CORS'); -settings.watch('Enable_CSP', (enabled) => { +const setInlineScriptsAllowed = Meteor.bindEnvironment(function _setInlineScriptsAllowed(enabled) { WebAppInternals.setInlineScriptsAllowed(!enabled); }); +settings.watch('Enable_CSP', (enabled) => { + setInlineScriptsAllowed(!enabled); +}); + WebApp.rawConnectHandlers.use(function (req, res, next) { // XSS Protection for old browsers (IE) res.setHeader('X-XSS-Protection', '1'); From b99ba2289adddec88b8d0f293a990e86c538b788 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 11 May 2022 15:06:08 -0300 Subject: [PATCH 6/8] revert bindEnvironment changes --- apps/meteor/app/cors/server/cors.js | 6 +----- .../app/lib/server/startup/settingsOnLoadSiteUrl.ts | 10 +++------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/apps/meteor/app/cors/server/cors.js b/apps/meteor/app/cors/server/cors.js index b34479a88f1c..c4f3c9e7693c 100644 --- a/apps/meteor/app/cors/server/cors.js +++ b/apps/meteor/app/cors/server/cors.js @@ -9,12 +9,8 @@ import { Logger } from '../../logger/server'; const logger = new Logger('CORS'); -const setInlineScriptsAllowed = Meteor.bindEnvironment(function _setInlineScriptsAllowed(enabled) { - WebAppInternals.setInlineScriptsAllowed(!enabled); -}); - settings.watch('Enable_CSP', (enabled) => { - setInlineScriptsAllowed(!enabled); + WebAppInternals.setInlineScriptsAllowed(!enabled); }); WebApp.rawConnectHandlers.use(function (req, res, next) { diff --git a/apps/meteor/app/lib/server/startup/settingsOnLoadSiteUrl.ts b/apps/meteor/app/lib/server/startup/settingsOnLoadSiteUrl.ts index c722390b7b62..11983536c40d 100644 --- a/apps/meteor/app/lib/server/startup/settingsOnLoadSiteUrl.ts +++ b/apps/meteor/app/lib/server/startup/settingsOnLoadSiteUrl.ts @@ -5,12 +5,6 @@ import { settings } from '../../../settings/server'; export let hostname: string; -const generateBoilerplate = Meteor.bindEnvironment(function _generateBoilerplate(): void { - if (typeof WebAppInternals !== 'undefined' && WebAppInternals.generateBoilerplate) { - return WebAppInternals.generateBoilerplate(); - } -}); - settings.watch('Site_Url', function (value) { if (value == null || value.trim() === '') { return; @@ -31,5 +25,7 @@ settings.watch('Site_Url', function (value) { hostname = host.replace(/^https?:\/\//, ''); process.env.MOBILE_ROOT_URL = host; process.env.MOBILE_DDP_URL = host; - generateBoilerplate(); + if (typeof WebAppInternals !== 'undefined' && WebAppInternals.generateBoilerplate) { + return WebAppInternals.generateBoilerplate(); + } }); From 2e09707ac0c095b733a37a57c9dedc9d33145847 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 11 May 2022 15:06:37 -0300 Subject: [PATCH 7/8] fix imports --- apps/meteor/app/settings/server/applyMiddlewares.ts | 2 +- apps/meteor/app/settings/server/cached.ts | 3 +++ apps/meteor/app/settings/server/index.ts | 6 ++++-- apps/meteor/server/importPackages.ts | 1 - apps/meteor/server/main.ts | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 apps/meteor/app/settings/server/cached.ts diff --git a/apps/meteor/app/settings/server/applyMiddlewares.ts b/apps/meteor/app/settings/server/applyMiddlewares.ts index 95f4ad1c9239..21ee517dc0cc 100644 --- a/apps/meteor/app/settings/server/applyMiddlewares.ts +++ b/apps/meteor/app/settings/server/applyMiddlewares.ts @@ -1,7 +1,7 @@ import { Meteor } from 'meteor/meteor'; import { use } from './Middleware'; -import { settings } from './index'; +import { settings } from './cached'; const getProcessingTimeInMS = (time: [number, number]): number => time[0] * 1000 + time[1] / 1e6; diff --git a/apps/meteor/app/settings/server/cached.ts b/apps/meteor/app/settings/server/cached.ts new file mode 100644 index 000000000000..078bb1c12088 --- /dev/null +++ b/apps/meteor/app/settings/server/cached.ts @@ -0,0 +1,3 @@ +import { CachedSettings } from './CachedSettings'; + +export const settings = new CachedSettings(); diff --git a/apps/meteor/app/settings/server/index.ts b/apps/meteor/app/settings/server/index.ts index a69253a69a37..765516219eaa 100644 --- a/apps/meteor/app/settings/server/index.ts +++ b/apps/meteor/app/settings/server/index.ts @@ -1,11 +1,13 @@ import SettingsModel from '../../models/server/models/Settings'; -import { CachedSettings } from './CachedSettings'; import { SettingsRegistry } from './SettingsRegistry'; import { initializeSettings } from './startup'; +import { settings } from './cached'; +import './applyMiddlewares'; export { SettingsEvents } from './SettingsRegistry'; -export const settings = new CachedSettings(); +export { settings }; + export const settingsRegistry = new SettingsRegistry({ store: settings, model: SettingsModel }); initializeSettings({ SettingsModel, settings }); diff --git a/apps/meteor/server/importPackages.ts b/apps/meteor/server/importPackages.ts index 2aaf4578dc9c..ee0796b90ba2 100644 --- a/apps/meteor/server/importPackages.ts +++ b/apps/meteor/server/importPackages.ts @@ -101,7 +101,6 @@ import '../app/bigbluebutton/server'; import '../app/mail-messages/server'; import '../app/user-status'; import '../app/utils'; -import '../app/settings'; import '../app/models'; import '../app/metrics'; import '../app/notifications'; diff --git a/apps/meteor/server/main.ts b/apps/meteor/server/main.ts index a7356479ad9f..a68d452ea530 100644 --- a/apps/meteor/server/main.ts +++ b/apps/meteor/server/main.ts @@ -1,5 +1,5 @@ import './services/startup'; -import '../app/settings/server/startup'; +import '../app/settings/server'; import '../lib/oauthRedirectUri'; import './overrides/http'; import './lib/logger/startup'; From 5ae43e05fdbe23926bf24d9b6ae192b2e5be243e Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 11 May 2022 16:13:43 -0300 Subject: [PATCH 8/8] Fix async callbacks --- .../livechat/server/hooks/processRoomAbandonment.js | 6 ++++-- .../livechat/server/hooks/saveLastMessageToInquiry.ts | 4 ++-- .../app/videobridge/server/methods/jitsiSetTimeout.js | 6 +++--- .../server/hooks/scheduleAutoTransfer.ts | 10 ++++++---- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/apps/meteor/app/livechat/server/hooks/processRoomAbandonment.js b/apps/meteor/app/livechat/server/hooks/processRoomAbandonment.js index 7990980bf65c..ebc4a018144c 100644 --- a/apps/meteor/app/livechat/server/hooks/processRoomAbandonment.js +++ b/apps/meteor/app/livechat/server/hooks/processRoomAbandonment.js @@ -58,7 +58,7 @@ const getSecondsSinceLastAgentResponse = async (room, agentLastMessage) => { callbacks.add( 'livechat.closeRoom', - async (room) => { + (room) => { const closedByAgent = room.closer !== 'visitor'; const wasTheLastMessageSentByAgent = room.lastMessage && !room.lastMessage.token; if (!closedByAgent || !wasTheLastMessageSentByAgent) { @@ -68,8 +68,10 @@ callbacks.add( if (!agentLastMessage) { return; } - const secondsSinceLastAgentResponse = await getSecondsSinceLastAgentResponse(room, agentLastMessage); + const secondsSinceLastAgentResponse = Promise.await(getSecondsSinceLastAgentResponse(room, agentLastMessage)); LivechatRooms.setVisitorInactivityInSecondsById(room._id, secondsSinceLastAgentResponse); + + return room; }, callbacks.priority.HIGH, 'process-room-abandonment', diff --git a/apps/meteor/app/livechat/server/hooks/saveLastMessageToInquiry.ts b/apps/meteor/app/livechat/server/hooks/saveLastMessageToInquiry.ts index 6372de157738..a5c43e7397cb 100644 --- a/apps/meteor/app/livechat/server/hooks/saveLastMessageToInquiry.ts +++ b/apps/meteor/app/livechat/server/hooks/saveLastMessageToInquiry.ts @@ -7,7 +7,7 @@ import { RoutingManager } from '../lib/RoutingManager'; callbacks.add( 'afterSaveMessage', - async (message, room) => { + (message, room) => { if (!isOmnichannelRoom(room)) { return message; } @@ -26,7 +26,7 @@ callbacks.add( return message; } - await LivechatInquiry.setLastMessageByRoomId(room._id, message); + Promise.await(LivechatInquiry.setLastMessageByRoomId(room._id, message)); return message; }, diff --git a/apps/meteor/app/videobridge/server/methods/jitsiSetTimeout.js b/apps/meteor/app/videobridge/server/methods/jitsiSetTimeout.js index 61bc09f33d8d..0789b9d82c69 100644 --- a/apps/meteor/app/videobridge/server/methods/jitsiSetTimeout.js +++ b/apps/meteor/app/videobridge/server/methods/jitsiSetTimeout.js @@ -75,10 +75,10 @@ Meteor.methods({ } return jitsiTimeout || nextTimeOut; - } catch (error) { - SystemLogger.error('Error starting video call:', error.message); + } catch (err) { + SystemLogger.error({ msg: 'Error starting video call:', err }); - throw new Meteor.Error('error-starting-video-call', error.message, { + throw new Meteor.Error('error-starting-video-call', err.message, { method: 'jitsi:updateTimeout', }); } diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/scheduleAutoTransfer.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/scheduleAutoTransfer.ts index 97bce8e54747..1182c46bba43 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/scheduleAutoTransfer.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/scheduleAutoTransfer.ts @@ -1,3 +1,5 @@ +import { IMessage, IRoom } from '@rocket.chat/core-typings'; + import { AutoTransferChatScheduler } from '../lib/AutoTransferChatScheduler'; import { callbacks } from '../../../../../lib/callbacks'; import { settings } from '../../../../../app/settings/server'; @@ -30,7 +32,7 @@ const handleAfterTakeInquiryCallback = async (inquiry: any = {}): Promise = return inquiry; }; -const handleAfterSaveMessage = async (message: any = {}, room: any = {}): Promise => { +const handleAfterSaveMessage = (message: any = {}, room: any = {}): IMessage => { const { _id: rid, t, autoTransferredAt, autoTransferOngoing } = room; const { token } = message; @@ -50,11 +52,11 @@ const handleAfterSaveMessage = async (message: any = {}, room: any = {}): Promis return message; } - await AutoTransferChatScheduler.unscheduleRoom(rid); + Promise.await(AutoTransferChatScheduler.unscheduleRoom(rid)); return message; }; -const handleAfterCloseRoom = async (room: any = {}): Promise => { +const handleAfterCloseRoom = (room: any = {}): IRoom => { const { _id: rid, autoTransferredAt, autoTransferOngoing } = room; if (!autoTransferTimeout || autoTransferTimeout <= 0) { @@ -69,7 +71,7 @@ const handleAfterCloseRoom = async (room: any = {}): Promise => { return room; } - await AutoTransferChatScheduler.unscheduleRoom(rid); + Promise.await(AutoTransferChatScheduler.unscheduleRoom(rid)); return room; };