diff --git a/packages/rocketchat-lib/package.js b/packages/rocketchat-lib/package.js index 8d856d350958..80a3bf8ee166 100644 --- a/packages/rocketchat-lib/package.js +++ b/packages/rocketchat-lib/package.js @@ -123,7 +123,6 @@ Package.onUse(function(api) { api.addFiles('server/functions/saveCustomFields.js', 'server'); api.addFiles('server/functions/saveCustomFieldsWithoutValidation.js', 'server'); api.addFiles('server/functions/sendMessage.js', 'server'); - api.addFiles('server/functions/settings.js', 'server'); api.addFiles('server/functions/setUserAvatar.js', 'server'); api.addFiles('server/functions/setUsername.js', 'server'); api.addFiles('server/functions/setRealName.js', 'server'); diff --git a/packages/rocketchat-lib/server/functions/settings.js b/packages/rocketchat-lib/server/functions/settings.js deleted file mode 100644 index 4ffe64fc9250..000000000000 --- a/packages/rocketchat-lib/server/functions/settings.js +++ /dev/null @@ -1,297 +0,0 @@ -import { Meteor } from 'meteor/meteor'; -import _ from 'underscore'; - -const blockedSettings = {}; - -if (process.env.SETTINGS_BLOCKED) { - process.env.SETTINGS_BLOCKED.split(',').forEach((settingId) => blockedSettings[settingId] = 1); -} - -const hiddenSettings = {}; -if (process.env.SETTINGS_HIDDEN) { - process.env.SETTINGS_HIDDEN.split(',').forEach((settingId) => hiddenSettings[settingId] = 1); -} - -RocketChat.settings._sorter = {}; - - -/* -* Add a setting -* @param {String} _id -* @param {Mixed} value -* @param {Object} setting -*/ - -RocketChat.settings.add = function(_id, value, options = {}) { - if (options == null) { - options = {}; - } - if (!_id || value == null) { - return false; - } - if (RocketChat.settings._sorter[options.group] == null) { - RocketChat.settings._sorter[options.group] = 0; - } - options.packageValue = value; - options.valueSource = 'packageValue'; - options.hidden = options.hidden || false; - options.blocked = options.blocked || false; - if (options.sorter == null) { - options.sorter = RocketChat.settings._sorter[options.group]++; - } - if (options.enableQuery != null) { - options.enableQuery = JSON.stringify(options.enableQuery); - } - if (options.i18nDefaultQuery != null) { - options.i18nDefaultQuery = JSON.stringify(options.i18nDefaultQuery); - } - if (typeof process !== 'undefined' && process.env && process.env[_id]) { - value = process.env[_id]; - if (value.toLowerCase() === 'true') { - value = true; - } else if (value.toLowerCase() === 'false') { - value = false; - } else if (options.type === 'int') { - value = parseInt(value); - } - options.processEnvValue = value; - options.valueSource = 'processEnvValue'; - } else if (Meteor.settings && typeof Meteor.settings[_id] !== 'undefined') { - if (Meteor.settings[_id] == null) { - return false; - } - - value = Meteor.settings[_id]; - options.meteorSettingsValue = value; - options.valueSource = 'meteorSettingsValue'; - } - if (options.i18nLabel == null) { - options.i18nLabel = _id; - } - if (options.i18nDescription == null) { - options.i18nDescription = `${ _id }_Description`; - } - if (blockedSettings[_id] != null) { - options.blocked = true; - } - if (hiddenSettings[_id] != null) { - options.hidden = true; - } - if (options.autocomplete == null) { - options.autocomplete = true; - } - if (typeof process !== 'undefined' && process.env && process.env[`OVERWRITE_SETTING_${ _id }`]) { - let value = process.env[`OVERWRITE_SETTING_${ _id }`]; - if (value.toLowerCase() === 'true') { - value = true; - } else if (value.toLowerCase() === 'false') { - value = false; - } else if (options.type === 'int') { - value = parseInt(value); - } - options.value = value; - options.processEnvValue = value; - options.valueSource = 'processEnvValue'; - } - const updateOperations = { - $set: options, - $setOnInsert: { - createdAt: new Date, - }, - }; - if (options.editor != null) { - updateOperations.$setOnInsert.editor = options.editor; - delete options.editor; - } - if (options.value == null) { - if (options.force === true) { - updateOperations.$set.value = options.packageValue; - } else { - updateOperations.$setOnInsert.value = value; - } - } - const query = _.extend({ - _id, - }, updateOperations.$set); - if (options.section == null) { - updateOperations.$unset = { - section: 1, - }; - query.section = { - $exists: false, - }; - } - const existantSetting = RocketChat.models.Settings.db.findOne(query); - if (existantSetting != null) { - if (existantSetting.editor == null && updateOperations.$setOnInsert.editor != null) { - updateOperations.$set.editor = updateOperations.$setOnInsert.editor; - delete updateOperations.$setOnInsert.editor; - } - } else { - updateOperations.$set.ts = new Date; - } - return RocketChat.models.Settings.upsert({ - _id, - }, updateOperations); -}; - - -/* -* Add a setting group -* @param {String} _id -*/ - -RocketChat.settings.addGroup = function(_id, options = {}, cb) { - if (!_id) { - return false; - } - if (_.isFunction(options)) { - cb = options; - options = {}; - } - if (options.i18nLabel == null) { - options.i18nLabel = _id; - } - if (options.i18nDescription == null) { - options.i18nDescription = `${ _id }_Description`; - } - options.ts = new Date; - options.blocked = false; - options.hidden = false; - if (blockedSettings[_id] != null) { - options.blocked = true; - } - if (hiddenSettings[_id] != null) { - options.hidden = true; - } - RocketChat.models.Settings.upsert({ - _id, - }, { - $set: options, - $setOnInsert: { - type: 'group', - createdAt: new Date, - }, - }); - if (cb != null) { - cb.call({ - add(id, value, options) { - if (options == null) { - options = {}; - } - options.group = _id; - return RocketChat.settings.add(id, value, options); - }, - section(section, cb) { - return cb.call({ - add(id, value, options) { - if (options == null) { - options = {}; - } - options.group = _id; - options.section = section; - return RocketChat.settings.add(id, value, options); - }, - }); - }, - }); - } -}; - - -/* -* Remove a setting by id -* @param {String} _id -*/ - -RocketChat.settings.removeById = function(_id) { - if (!_id) { - return false; - } - return RocketChat.models.Settings.removeById(_id); -}; - - -/* -* Update a setting by id -* @param {String} _id -*/ - -RocketChat.settings.updateById = function(_id, value, editor) { - if (!_id || value == null) { - return false; - } - if (editor != null) { - return RocketChat.models.Settings.updateValueAndEditorById(_id, value, editor); - } - return RocketChat.models.Settings.updateValueById(_id, value); -}; - - -/* -* Update options of a setting by id -* @param {String} _id -*/ - -RocketChat.settings.updateOptionsById = function(_id, options) { - if (!_id || options == null) { - return false; - } - return RocketChat.models.Settings.updateOptionsById(_id, options); -}; - - -/* -* Update a setting by id -* @param {String} _id -*/ - -RocketChat.settings.clearById = function(_id) { - if (_id == null) { - return false; - } - return RocketChat.models.Settings.updateValueById(_id, undefined); -}; - - -/* -* Update a setting by id -*/ - -RocketChat.settings.init = function() { - RocketChat.settings.initialLoad = true; - RocketChat.models.Settings.find().observe({ - added(record) { - Meteor.settings[record._id] = record.value; - if (record.env === true) { - process.env[record._id] = record.value; - } - return RocketChat.settings.load(record._id, record.value, RocketChat.settings.initialLoad); - }, - changed(record) { - Meteor.settings[record._id] = record.value; - if (record.env === true) { - process.env[record._id] = record.value; - } - return RocketChat.settings.load(record._id, record.value, RocketChat.settings.initialLoad); - }, - removed(record) { - delete Meteor.settings[record._id]; - if (record.env === true) { - delete process.env[record._id]; - } - return RocketChat.settings.load(record._id, undefined, RocketChat.settings.initialLoad); - }, - }); - RocketChat.settings.initialLoad = false; - RocketChat.settings.afterInitialLoad.forEach((fn) => fn(Meteor.settings)); -}; - -RocketChat.settings.afterInitialLoad = []; - -RocketChat.settings.onAfterInitialLoad = function(fn) { - RocketChat.settings.afterInitialLoad.push(fn); - if (RocketChat.settings.initialLoad === false) { - return fn(Meteor.settings); - } -}; diff --git a/packages/rocketchat-lib/startup/index.js b/packages/rocketchat-lib/startup/index.js index ebfc1a2c6047..90c94606f7c8 100644 --- a/packages/rocketchat-lib/startup/index.js +++ b/packages/rocketchat-lib/startup/index.js @@ -1,3 +1,4 @@ import * as Mailer from 'meteor/rocketchat:mailer'; +import { settings } from 'meteor/rocketchat:settings'; -Mailer.setSettings(RocketChat.settings); +Mailer.setSettings(settings); diff --git a/packages/rocketchat-models/package.js b/packages/rocketchat-models/package.js index f09b9482f4aa..63cd19c90037 100755 --- a/packages/rocketchat-models/package.js +++ b/packages/rocketchat-models/package.js @@ -8,7 +8,6 @@ Package.describe({ Package.onUse(function(api) { api.use([ 'ecmascript', - 'rocketchat:settings', 'rocketchat:callbacks', 'rocketchat:ui-cached-collection', 'konecty:multiple-instances-status', diff --git a/packages/rocketchat-models/server/models/Messages.js b/packages/rocketchat-models/server/models/Messages.js index d56c4a255ba3..de009f321fef 100644 --- a/packages/rocketchat-models/server/models/Messages.js +++ b/packages/rocketchat-models/server/models/Messages.js @@ -1,6 +1,5 @@ import { Meteor } from 'meteor/meteor'; import { Match } from 'meteor/check'; -import { settings } from 'meteor/rocketchat:settings'; import { Base } from './_Base'; import Rooms from './Rooms'; import Users from './Users'; @@ -24,6 +23,14 @@ export class Messages extends Base { this.tryEnsureIndex({ snippeted: 1 }, { sparse: 1 }); this.tryEnsureIndex({ location: '2dsphere' }); this.tryEnsureIndex({ slackBotId: 1, slackTs: 1 }, { sparse: 1 }); + this.loadSettings(); + } + + loadSettings() { + Meteor.startup(async() => { + const { settings } = await import('meteor/rocketchat:settings'); + this.settings = settings; + }); } countVisibleByRoomIdBetweenTimestampsInclusive(roomId, afterTimestamp, beforeTimestamp, options) { @@ -600,7 +607,7 @@ export class Messages extends Base { groupable: false, }; - if (settings.get('Message_Read_Receipt_Enabled')) { + if (this.settings.get('Message_Read_Receipt_Enabled')) { record.unread = true; } @@ -629,7 +636,7 @@ export class Messages extends Base { groupable: false, }; - if (settings.get('Message_Read_Receipt_Enabled')) { + if (this.settings.get('Message_Read_Receipt_Enabled')) { record.unread = true; } diff --git a/packages/rocketchat-models/server/models/Users.js b/packages/rocketchat-models/server/models/Users.js index 7206dd9d9f6e..f90e64fa4a93 100644 --- a/packages/rocketchat-models/server/models/Users.js +++ b/packages/rocketchat-models/server/models/Users.js @@ -1,6 +1,5 @@ import { Meteor } from 'meteor/meteor'; import { Accounts } from 'meteor/accounts-base'; -import { settings } from 'meteor/rocketchat:settings'; import { Base } from './_Base'; import Subscriptions from './Subscriptions'; import _ from 'underscore'; @@ -17,6 +16,14 @@ export class Users extends Base { this.tryEnsureIndex({ active: 1 }, { sparse: 1 }); this.tryEnsureIndex({ statusConnection: 1 }, { sparse: 1 }); this.tryEnsureIndex({ type: 1 }); + this.loadSettings(); + } + + loadSettings() { + Meteor.startup(async() => { + const { settings } = await import('meteor/rocketchat:settings'); + this.settings = settings; + }); } roleBaseQuery(userId) { @@ -200,7 +207,7 @@ export class Users extends Base { const termRegex = new RegExp(s.escapeRegExp(searchTerm), 'i'); - const orStmt = _.reduce(settings.get('Accounts_SearchFields').trim().split(','), function(acc, el) { + const orStmt = _.reduce(this.settings.get('Accounts_SearchFields').trim().split(','), function(acc, el) { acc.push({ [el.trim()]: termRegex }); return acc; }, []); diff --git a/packages/rocketchat-models/server/models/_BaseDb.js b/packages/rocketchat-models/server/models/_BaseDb.js index fea2c84cab0a..d32642bcc145 100644 --- a/packages/rocketchat-models/server/models/_BaseDb.js +++ b/packages/rocketchat-models/server/models/_BaseDb.js @@ -1,6 +1,6 @@ +import { Meteor } from 'meteor/meteor'; import { Match } from 'meteor/check'; import { Mongo, MongoInternals } from 'meteor/mongo'; -import { settings } from 'meteor/rocketchat:settings'; import _ from 'underscore'; import { EventEmitter } from 'events'; @@ -16,9 +16,6 @@ try { const isOplogAvailable = MongoInternals.defaultRemoteCollectionDriver().mongo._oplogHandle && !!MongoInternals.defaultRemoteCollectionDriver().mongo._oplogHandle.onOplogEntry; let isOplogEnabled = isOplogAvailable; -settings.get('Force_Disable_OpLog_For_Cache', (key, value) => { - isOplogEnabled = isOplogAvailable && value === false; -}); export class BaseDb extends EventEmitter { constructor(model, baseModel) { @@ -39,6 +36,7 @@ export class BaseDb extends EventEmitter { this.wrapModel(); let alreadyListeningToOplog = false; + this.listenSettings(); // When someone start listening for changes we start oplog if available this.on('newListener', (event/* , listener*/) => { if (event === 'change' && alreadyListeningToOplog === false) { @@ -57,6 +55,15 @@ export class BaseDb extends EventEmitter { this.tryEnsureIndex({ _updatedAt: 1 }); } + listenSettings() { + Meteor.startup(async() => { + const { settings } = await import('meteor/rocketchat:settings'); + settings.get('Force_Disable_OpLog_For_Cache', (key, value) => { + isOplogEnabled = isOplogAvailable && value === false; + }); + }); + } + get baseName() { return baseName; } diff --git a/packages/rocketchat-settings/package.js b/packages/rocketchat-settings/package.js index 227969519e91..577c3640ce3e 100644 --- a/packages/rocketchat-settings/package.js +++ b/packages/rocketchat-settings/package.js @@ -9,6 +9,7 @@ Package.onUse(function(api) { api.use([ 'ecmascript', 'rocketchat:ui-cached-collection', + 'rocketchat:models', ]); api.mainModule('client/index.js', 'client'); api.mainModule('server/index.js', 'server'); diff --git a/packages/rocketchat-settings/server/functions/settings.js b/packages/rocketchat-settings/server/functions/settings.js index 408e31c4450f..ba37d75c9d63 100644 --- a/packages/rocketchat-settings/server/functions/settings.js +++ b/packages/rocketchat-settings/server/functions/settings.js @@ -1,9 +1,8 @@ import { Meteor } from 'meteor/meteor'; import { settings } from '../../lib/settings'; +import { Settings } from 'meteor/rocketchat:models'; import _ from 'underscore'; -let _Settings; - const blockedSettings = {}; if (process.env.SETTINGS_BLOCKED) { @@ -25,11 +24,7 @@ settings._sorter = {}; * @param {Object} setting */ -settings.add = async function(_id, value, options = {}) { - if (!_Settings) { - const { Settings } = await import('meteor/rocketchat:models'); - _Settings = Settings; - } +settings.add = function(_id, value, options = {}) { if (options == null) { options = {}; } @@ -128,7 +123,7 @@ settings.add = async function(_id, value, options = {}) { $exists: false, }; } - const existantSetting = _Settings.db.findOne(query); + const existantSetting = Settings.db.findOne(query); if (existantSetting != null) { if (existantSetting.editor == null && updateOperations.$setOnInsert.editor != null) { updateOperations.$set.editor = updateOperations.$setOnInsert.editor; @@ -137,7 +132,7 @@ settings.add = async function(_id, value, options = {}) { } else { updateOperations.$set.ts = new Date; } - return _Settings.upsert({ + return Settings.upsert({ _id, }, updateOperations); }; @@ -148,11 +143,7 @@ settings.add = async function(_id, value, options = {}) { * @param {String} _id */ -settings.addGroup = async function(_id, options = {}, cb) { - if (!_Settings) { - const { Settings } = await import('meteor/rocketchat:models'); - _Settings = Settings; - } +settings.addGroup = function(_id, options = {}, cb) { if (!_id) { return false; } @@ -175,7 +166,7 @@ settings.addGroup = async function(_id, options = {}, cb) { if (hiddenSettings[_id] != null) { options.hidden = true; } - _Settings.upsert({ + Settings.upsert({ _id, }, { $set: options, @@ -215,15 +206,11 @@ settings.addGroup = async function(_id, options = {}, cb) { * @param {String} _id */ -settings.removeById = async function(_id) { - if (!_Settings) { - const { Settings } = await import('meteor/rocketchat:models'); - _Settings = Settings; - } +settings.removeById = function(_id) { if (!_id) { return false; } - return _Settings.removeById(_id); + return Settings.removeById(_id); }; @@ -232,18 +219,14 @@ settings.removeById = async function(_id) { * @param {String} _id */ -settings.updateById = async function(_id, value, editor) { - if (!_Settings) { - const { Settings } = await import('meteor/rocketchat:models'); - _Settings = Settings; - } +settings.updateById = function(_id, value, editor) { if (!_id || value == null) { return false; } if (editor != null) { - return _Settings.updateValueAndEditorById(_id, value, editor); + return Settings.updateValueAndEditorById(_id, value, editor); } - return _Settings.updateValueById(_id, value); + return Settings.updateValueById(_id, value); }; @@ -252,15 +235,11 @@ settings.updateById = async function(_id, value, editor) { * @param {String} _id */ -settings.updateOptionsById = async function(_id, options) { - if (!_Settings) { - const { Settings } = await import('meteor/rocketchat:models'); - _Settings = Settings; - } +settings.updateOptionsById = function(_id, options) { if (!_id || options == null) { return false; } - return _Settings.updateOptionsById(_id, options); + return Settings.updateOptionsById(_id, options); }; @@ -269,15 +248,11 @@ settings.updateOptionsById = async function(_id, options) { * @param {String} _id */ -settings.clearById = async function(_id) { - if (!_Settings) { - const { Settings } = await import('meteor/rocketchat:models'); - _Settings = Settings; - } +settings.clearById = function(_id) { if (_id == null) { return false; } - return _Settings.updateValueById(_id, undefined); + return Settings.updateValueById(_id, undefined); }; @@ -285,13 +260,9 @@ settings.clearById = async function(_id) { * Update a setting by id */ -settings.init = async function() { - if (!_Settings) { - const { Settings } = await import('meteor/rocketchat:models'); - _Settings = Settings; - } +settings.init = function() { settings.initialLoad = true; - _Settings.find().observe({ + Settings.find().observe({ added(record) { Meteor.settings[record._id] = record.value; if (record.env === true) {