From 1cd9df2af1cf80dc5d1e51ea01b4294c408daab8 Mon Sep 17 00:00:00 2001 From: yash-rajpal Date: Mon, 23 Sep 2024 18:47:36 +0530 Subject: [PATCH] update migration --- apps/meteor/server/startup/migrations/v311.ts | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 apps/meteor/server/startup/migrations/v311.ts diff --git a/apps/meteor/server/startup/migrations/v311.ts b/apps/meteor/server/startup/migrations/v311.ts new file mode 100644 index 0000000000000..49bbf7ace36be --- /dev/null +++ b/apps/meteor/server/startup/migrations/v311.ts @@ -0,0 +1,84 @@ +import { Settings } from '@rocket.chat/models'; + +import { settings } from '../../../app/settings/server'; +import { isTruthy } from '../../../lib/isTruthy'; +import { SystemLogger } from '../../lib/logger/system'; +import { addMigration } from '../../lib/migrations'; +import { refreshLoginServices } from '../../lib/refreshLoginServices'; + +const newDefaultButtonColor = '#e4e7ea'; +const newDefaultButtonLabelColor = '#1f2329'; + +const settingsToUpdate = [ + // button background colors + { key: 'SAML_Custom_Default_button_color', defaultValue: '#1d74f5', newValue: newDefaultButtonColor }, + { key: 'CAS_button_color', defaultValue: '#1d74f5', newValue: newDefaultButtonColor }, + { key: 'Accounts_OAuth_Nextcloud_button_color', defaultValue: '#0082c9', newValue: newDefaultButtonColor }, + { key: 'Accounts_OAuth_Dolphin_button_color', defaultValue: '#1d74f5', newValue: newDefaultButtonColor }, + // button label colors + { key: 'SAML_Custom_Default_button_label_color', defaultValue: '#1d74f5', newValue: newDefaultButtonLabelColor }, + { key: 'CAS_button_label_color', defaultValue: '#1d74f5', newValue: newDefaultButtonLabelColor }, + { key: 'Accounts_OAuth_Nextcloud_button_label_color', defaultValue: '#1d74f5', newValue: newDefaultButtonLabelColor }, + { key: 'Accounts_OAuth_Dolphin_button_label_color', defaultValue: '#1d74f5', newValue: newDefaultButtonLabelColor }, +]; + +addMigration({ + version: 311, + name: 'Change default color of OAuth login services buttons', + async up() { + const promises = settingsToUpdate + .map(({ key, defaultValue, newValue }) => { + const oldSettingValue = settings.get(key); + + if (!oldSettingValue || oldSettingValue !== defaultValue) { + return; + } + + SystemLogger.warn(`The default value of the setting ${key} has changed to ${newValue}. Please review your settings.`); + + return Settings.updateOne({ _id: key }, { $set: { value: newValue } }); + }) + .filter(isTruthy); + + await Promise.all(promises); + + const customOAuthButtonColors = await Settings.find({ + _id: { $regex: /^Accounts_OAuth_Custom-[a-zA-Z0-9_-]+-button_color$/ }, + }).toArray(); + const customOAuthButtonLabelColors = await Settings.find({ + _id: { $regex: /^Accounts_OAuth_Custom-[a-zA-Z0-9_-]+-button_label_color$/ }, + }).toArray(); + + const buttonColorPromises = customOAuthButtonColors + .map(({ _id, value, packageValue }) => { + if (packageValue !== value) { + return; + } + + SystemLogger.warn( + `The default value of the custom setting ${_id} has changed to ${newDefaultButtonColor}. Please review your settings.`, + ); + + return Settings.updateOne({ _id }, { $set: { value: newDefaultButtonColor } }); + }) + .filter(isTruthy); + + const buttonLabelColorPromises = customOAuthButtonLabelColors + .map(({ _id, value, packageValue }) => { + if (packageValue !== value) { + return; + } + + SystemLogger.warn( + `The default value of the custom setting ${_id} has changed to ${newDefaultButtonLabelColor}. Please review your settings.`, + ); + + return Settings.updateOne({ _id }, { $set: { value: newDefaultButtonLabelColor } }); + }) + .filter(isTruthy); + + await Promise.all([...buttonColorPromises, ...buttonLabelColorPromises]); + // update login service configurations + await refreshLoginServices(false); + }, +});