/* eslint-disable react-hooks/rules-of-hooks */ import type { ISetting, SettingValue, LicenseModule } from '@rocket.chat/core-typings'; import { License } from '@rocket.chat/license'; import { Settings } from '@rocket.chat/models'; import { Meteor } from 'meteor/meteor'; import { settings, SettingsEvents } from '../../../server/settings'; import { use } from '../../../server/settings/Middleware'; export function changeSettingValue(record: Pick): SettingValue { if (!record.enterprise) { return record.value; } if (!License.hasValidLicense()) { return record.invalidValue; } if (!record.modules?.length) { return record.value; } for (const moduleName of record.modules) { if (!License.hasModule(moduleName as LicenseModule)) { return record.invalidValue; } } return record.value; } settings.set = use(settings.set, (context, next) => { const [record] = context; if (!record.enterprise) { return next(...context); } const value = changeSettingValue(record); return next({ ...record, value }); }); SettingsEvents.on('fetch-settings', (settings): void => { for (const setting of settings) { const changedValue = changeSettingValue(setting); if (changedValue === undefined) { continue; } setting.value = changedValue; } }); async function updateSettings(): Promise { const enterpriseSettings = await Settings.findEnterpriseSettings(); void enterpriseSettings.forEach((record: ISetting) => settings.set(record)); } Meteor.startup(async () => { await updateSettings(); License.onValidateLicense(updateSettings); License.onInvalidateLicense(updateSettings); License.onRemoveLicense(updateSettings); });