diff --git a/apps/meteor/server/settings/retention-policy.ts b/apps/meteor/server/settings/retention-policy.ts index da986b683720..5e620e9a8e49 100644 --- a/apps/meteor/server/settings/retention-policy.ts +++ b/apps/meteor/server/settings/retention-policy.ts @@ -73,6 +73,19 @@ export const createRetentionSettings = () => i18nDescription: 'RetentionPolicy_AppliesToChannels_Description', enableQuery: globalQuery, }); + await this.add('RetentionPolicy_MaxAge_Channels', 30, { + type: 'int', + public: true, + hidden: true, + i18nLabel: 'RetentionPolicy_MaxAge_Channels', + enableQuery: [ + { + _id: 'RetentionPolicy_AppliesToChannels', + value: true, + }, + globalQuery, + ], + }); await this.add('RetentionPolicy_TTL_Channels', THIRTY_DAYS, { type: 'timespan', @@ -94,6 +107,19 @@ export const createRetentionSettings = () => i18nDescription: 'RetentionPolicy_AppliesToGroups_Description', enableQuery: globalQuery, }); + await this.add('RetentionPolicy_MaxAge_Groups', 30, { + type: 'int', + public: true, + hidden: true, + i18nLabel: 'RetentionPolicy_MaxAge_Groups', + enableQuery: [ + { + _id: 'RetentionPolicy_AppliesToGroups', + value: true, + }, + globalQuery, + ], + }); await this.add('RetentionPolicy_TTL_Groups', THIRTY_DAYS, { type: 'timespan', @@ -115,6 +141,20 @@ export const createRetentionSettings = () => enableQuery: globalQuery, }); + await this.add('RetentionPolicy_MaxAge_DMs', 30, { + type: 'int', + public: true, + hidden: true, + i18nLabel: 'RetentionPolicy_MaxAge_DMs', + enableQuery: [ + { + _id: 'RetentionPolicy_AppliesToDMs', + value: true, + }, + globalQuery, + ], + }); + await this.add('RetentionPolicy_TTL_DMs', THIRTY_DAYS, { type: 'timespan', public: true, diff --git a/apps/meteor/server/startup/migrations/v319.ts b/apps/meteor/server/startup/migrations/v319.ts deleted file mode 100644 index 996190ce112d..000000000000 --- a/apps/meteor/server/startup/migrations/v319.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { Settings } from '@rocket.chat/models'; -import type { UpdateResult } from 'mongodb'; - -import { settings } from '../../../app/settings/server'; -import { addMigration } from '../../lib/migrations'; - -const maxAgeSettingMap = new Map([ - ['RetentionPolicy_MaxAge_Channels', 'RetentionPolicy_TTL_Channels'], - ['RetentionPolicy_MaxAge_Groups', 'RetentionPolicy_TTL_Groups'], - ['RetentionPolicy_MaxAge_DMs', 'RetentionPolicy_TTL_DMs'], -]); - -addMigration({ - version: 318, - name: 'Move retention policy settings', - async up() { - const convertDaysToMs = (days: number) => days * 24 * 60 * 60 * 1000; - - const promises: Array> = []; - await Settings.find( - // we have to test value to avoid updating records that were changed before this version - { _id: { $in: Array.from(maxAgeSettingMap.keys()) }, value: { $ne: -1 } }, - { projection: { _id: 1, value: 1 } }, - ).forEach(({ _id, value }) => { - const newSettingId = maxAgeSettingMap.get(_id); - if (!newSettingId) { - throw new Error(`moveRetentionSetting - Setting ${_id} equivalent does not exist`); - } - - const newValue = convertDaysToMs(Number(value)); - - // TODO: audit - - promises.push( - Settings.updateOne( - { - _id: maxAgeSettingMap.get(_id), - }, - { - $set: { - value: newValue, - }, - }, - ), - ); - - // This is necessary because the cachedCollection is started before watchDb is initialized - const currentCache = settings.getSetting(newSettingId); - if (!currentCache) { - return; - } - settings.set({ ...currentCache, value: newValue }); - }); - - await Promise.all(promises); - await Settings.deleteMany({ _id: { $in: Array.from(maxAgeSettingMap.keys()) } }); - }, -}); diff --git a/apps/meteor/server/startup/migrations/xrun.ts b/apps/meteor/server/startup/migrations/xrun.ts index 61cfaff50231..0344649f9993 100644 --- a/apps/meteor/server/startup/migrations/xrun.ts +++ b/apps/meteor/server/startup/migrations/xrun.ts @@ -1,4 +1,8 @@ +import { Settings } from '@rocket.chat/models'; +import type { UpdateResult } from 'mongodb'; + import { upsertPermissions } from '../../../app/authorization/server/functions/upsertPermissions'; +import { settings } from '../../../app/settings/server'; import { migrateDatabase, onServerVersionChange } from '../../lib/migrations'; import { ensureCloudWorkspaceRegistered } from '../cloudRegistration'; @@ -6,11 +10,57 @@ const { MIGRATION_VERSION = 'latest' } = process.env; const [version, ...subcommands] = MIGRATION_VERSION.split(','); +const maxAgeSettingMap = new Map([ + ['RetentionPolicy_MaxAge_Channels', 'RetentionPolicy_TTL_Channels'], + ['RetentionPolicy_MaxAge_Groups', 'RetentionPolicy_TTL_Groups'], + ['RetentionPolicy_MaxAge_DMs', 'RetentionPolicy_TTL_DMs'], +]); + +const moveRetentionSetting = async () => { + const convertDaysToMs = (days: number) => days * 24 * 60 * 60 * 1000; + + const promises: Array> = []; + await Settings.find( + { _id: { $in: Array.from(maxAgeSettingMap.keys()) }, value: { $ne: -1 } }, + { projection: { _id: 1, value: 1 } }, + ).forEach(({ _id, value }) => { + const newSettingId = maxAgeSettingMap.get(_id); + if (!newSettingId) { + throw new Error(`moveRetentionSetting - Setting ${_id} equivalent does not exist`); + } + + const newValue = convertDaysToMs(Number(value)); + + promises.push( + Settings.updateOne( + { + _id: maxAgeSettingMap.get(_id), + }, + { + $set: { + value: newValue, + }, + }, + ), + ); + + const currentCache = settings.getSetting(newSettingId); + if (!currentCache) { + return; + } + settings.set({ ...currentCache, value: newValue }); + }); + + await Promise.all(promises); + await Settings.updateMany({ _id: { $in: Array.from(maxAgeSettingMap.keys()) } }, { $set: { value: -1 } }); +}; + export const performMigrationProcedure = async (): Promise => { await migrateDatabase(version === 'latest' ? version : parseInt(version), subcommands); // perform operations when the server is starting with a different version await onServerVersionChange(async () => { await upsertPermissions(); await ensureCloudWorkspaceRegistered(); + await moveRetentionSetting(); }); };