From 82b3ff0b589ce039568b41b378cb256576380c56 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Tue, 3 Oct 2023 19:09:23 -0300 Subject: [PATCH 1/4] . --- .../supportedVersionsToken.ts | 20 +++- .../functions/buildVersionUpdateMessage.ts | 58 ++++++++++++ .../server/functions/checkVersionUpdate.ts | 93 +++++++------------ .../server/functions/getNewUpdates.ts | 2 + .../src/definitions/index.ts | 2 + 5 files changed, 110 insertions(+), 65 deletions(-) create mode 100644 apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts diff --git a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts index 577abd4383d0..18b01de60387 100644 --- a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts +++ b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts @@ -7,6 +7,7 @@ import { serverFetch as fetch } from '@rocket.chat/server-fetch'; import { SystemLogger } from '../../../../../server/lib/logger/system'; import { settings } from '../../../../settings/server'; +import { buildVersionUpdateMessage } from '../../../../version-check/server/functions/buildVersionUpdateMessage'; import { generateWorkspaceBearerHttpHeader } from '../getWorkspaceAccessToken'; import { supportedVersionsChooseLatest } from './supportedVersionsChooseLatest'; @@ -83,7 +84,10 @@ const cacheValueInSettings = ( ); }; -/** CODE */ +const releaseEndpoint = + process.env.OVERWRITE_INTERNAL_RELEASE_URL && process.env.OVERWRITE_INTERNAL_RELEASE_URL.trim().length > 0 + ? process.env.OVERWRITE_INTERNAL_RELEASE_URL + : 'https://releases.rocket.chat'; const getSupportedVersionsFromCloud = async () => { if (process.env.CLOUD_SUPPORTED_VERSIONS_TOKEN) { @@ -97,7 +101,7 @@ const getSupportedVersionsFromCloud = async () => { const headers = await generateWorkspaceBearerHttpHeader(); const response = await handleResponse( - fetch('https://releases.rocket.chat/v2/server/supportedVersions', { + fetch(releaseEndpoint, { headers, }), ); @@ -105,7 +109,7 @@ const getSupportedVersionsFromCloud = async () => { if (!response.success) { SystemLogger.error({ msg: 'Failed to communicate with Rocket.Chat Cloud', - url: 'https://releases.rocket.chat/v2/server/supportedVersions', + url: releaseEndpoint, err: response.error, }); } @@ -123,8 +127,14 @@ const getSupportedVersionsToken = async () => { const [versionsFromLicense, response] = await Promise.all([License.getLicense(), getSupportedVersionsFromCloud()]); - return (await supportedVersionsChooseLatest(versionsFromLicense?.supportedVersions, (response.success && response.result) || undefined)) - ?.signed; + const supportedVersions = await supportedVersionsChooseLatest( + versionsFromLicense?.supportedVersions, + (response.success && response.result) || undefined, + ); + + await buildVersionUpdateMessage(supportedVersions?.versions); + + return supportedVersions?.signed; }; export const getCachedSupportedVersionsToken = cacheValueInSettings('Cloud_Workspace_Supported_Versions_Token', getSupportedVersionsToken); diff --git a/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts b/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts new file mode 100644 index 000000000000..621feb2a2b94 --- /dev/null +++ b/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts @@ -0,0 +1,58 @@ +import { Settings } from '@rocket.chat/models'; +import semver from 'semver'; + +import { i18n } from '../../../../server/lib/i18n'; +import { sendMessagesToAdmins } from '../../../../server/lib/sendMessagesToAdmins'; +import { settings } from '../../../settings/server'; +import { Info } from '../../../utils/rocketchat.info'; + +export const buildVersionUpdateMessage = async ( + versions: { + version: string; + security: boolean; + infoUrl: string; + }[], +) => { + const lastCheckedVersion = settings.get('Update_LatestAvailableVersion'); + + for await (const version of versions) { + if (!lastCheckedVersion) { + break; + } + if (semver.lte(version.version, lastCheckedVersion)) { + continue; + } + + if (semver.lte(version.version, Info.version)) { + continue; + } + + await Settings.updateValueById('Update_LatestAvailableVersion', version.version); + + await sendMessagesToAdmins({ + msgs: async ({ adminUser }) => [ + { + msg: `*${i18n.t('Update_your_RocketChat', { ...(adminUser.language && { lng: adminUser.language }) })}*\n${i18n.t( + 'New_version_available_(s)', + { + postProcess: 'sprintf', + sprintf: [version.version], + }, + )}\n${version.infoUrl}`, + }, + ], + banners: [ + { + id: `versionUpdate-${version.version}`.replace(/\./g, '_'), + priority: 10, + title: 'Update_your_RocketChat', + text: 'New_version_available_(s)', + textArguments: [version.version], + link: version.infoUrl, + modifiers: [], + }, + ], + }); + break; + } +}; diff --git a/apps/meteor/app/version-check/server/functions/checkVersionUpdate.ts b/apps/meteor/app/version-check/server/functions/checkVersionUpdate.ts index 16a3034c0bd3..ca616950a55b 100644 --- a/apps/meteor/app/version-check/server/functions/checkVersionUpdate.ts +++ b/apps/meteor/app/version-check/server/functions/checkVersionUpdate.ts @@ -1,14 +1,11 @@ import type { IUser } from '@rocket.chat/core-typings'; -import { Settings, Users } from '@rocket.chat/models'; -import semver from 'semver'; +import { Users } from '@rocket.chat/models'; import { i18n } from '../../../../server/lib/i18n'; import { sendMessagesToAdmins } from '../../../../server/lib/sendMessagesToAdmins'; -import { settings } from '../../../settings/server'; -import { Info } from '../../../utils/rocketchat.info'; import logger from '../logger'; +import { buildVersionUpdateMessage } from './buildVersionUpdateMessage'; import { getNewUpdates } from './getNewUpdates'; -// import getNewUpdates from '../sampleUpdateData'; const getMessagesToSendToAdmins = async ( alerts: { @@ -42,67 +39,43 @@ const getMessagesToSendToAdmins = async ( } return msgs; }; - +/** + * @deprecated + */ export const checkVersionUpdate = async () => { logger.info('Checking for version updates'); const { versions, alerts } = await getNewUpdates(); - const lastCheckedVersion = settings.get('Update_LatestAvailableVersion'); - - for await (const version of versions) { - if (!lastCheckedVersion) { - break; - } - if (semver.lte(version.version, lastCheckedVersion)) { - continue; - } + await buildVersionUpdateMessage(versions); - if (semver.lte(version.version, Info.version)) { - continue; - } - - await Settings.updateValueById('Update_LatestAvailableVersion', version.version); - - await sendMessagesToAdmins({ - msgs: async ({ adminUser }) => [ - { - msg: `*${i18n.t('Update_your_RocketChat', { ...(adminUser.language && { lng: adminUser.language }) })}*\n${i18n.t( - 'New_version_available_(s)', - { - postProcess: 'sprintf', - sprintf: [version.version], - }, - )}\n${version.infoUrl}`, - }, - ], - banners: [ - { - id: `versionUpdate-${version.version}`.replace(/\./g, '_'), - priority: 10, - title: 'Update_your_RocketChat', - text: 'New_version_available_(s)', - textArguments: [version.version], - link: version.infoUrl, - modifiers: [], - }, - ], - }); - break; - } + await showAlertsFromCloud(alerts); +}; - if (alerts?.length) { - await sendMessagesToAdmins({ - msgs: async ({ adminUser }) => getMessagesToSendToAdmins(alerts, adminUser), - banners: alerts.map((alert) => ({ - id: `alert-${alert.id}`.replace(/\./g, '_'), - priority: 10, - title: alert.title, - text: alert.text, - textArguments: alert.textArguments, - modifiers: alert.modifiers, - link: alert.infoUrl, - })), - }); +const showAlertsFromCloud = async ( + alerts?: { + id: string; + priority: number; + title: string; + text: string; + textArguments?: string[]; + modifiers: string[]; + infoUrl: string; + }[], +) => { + if (!alerts?.length) { + return; } + return sendMessagesToAdmins({ + msgs: async ({ adminUser }) => getMessagesToSendToAdmins(alerts, adminUser), + banners: alerts.map((alert) => ({ + id: `alert-${alert.id}`.replace(/\./g, '_'), + priority: 10, + title: alert.title, + text: alert.text, + textArguments: alert.textArguments, + modifiers: alert.modifiers, + link: alert.infoUrl, + })), + }); }; diff --git a/apps/meteor/app/version-check/server/functions/getNewUpdates.ts b/apps/meteor/app/version-check/server/functions/getNewUpdates.ts index d17191a09be7..926926253a6c 100644 --- a/apps/meteor/app/version-check/server/functions/getNewUpdates.ts +++ b/apps/meteor/app/version-check/server/functions/getNewUpdates.ts @@ -7,6 +7,8 @@ import { check, Match } from 'meteor/check'; import { getWorkspaceAccessToken } from '../../../cloud/server'; import { Info } from '../../../utils/rocketchat.info'; +/** @deprecated */ + export const getNewUpdates = async () => { try { const uniqueID = await Settings.findOne('uniqueID'); diff --git a/packages/server-cloud-communication/src/definitions/index.ts b/packages/server-cloud-communication/src/definitions/index.ts index d554aa538059..4ac455ccefd1 100644 --- a/packages/server-cloud-communication/src/definitions/index.ts +++ b/packages/server-cloud-communication/src/definitions/index.ts @@ -17,6 +17,8 @@ type Message = { type Version = { version: string; expiration: Date; + security: boolean; + infoUrl: string; messages?: Message[]; }; From 664371ac778ea9f923c87ae2d6ac9bc5fcfc61b4 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Tue, 3 Oct 2023 19:28:39 -0300 Subject: [PATCH 2/4] Apply suggestions from code review --- .../functions/supportedVersionsToken/supportedVersionsToken.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts index 18b01de60387..861b46e56acc 100644 --- a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts +++ b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts @@ -87,7 +87,7 @@ const cacheValueInSettings = ( const releaseEndpoint = process.env.OVERWRITE_INTERNAL_RELEASE_URL && process.env.OVERWRITE_INTERNAL_RELEASE_URL.trim().length > 0 ? process.env.OVERWRITE_INTERNAL_RELEASE_URL - : 'https://releases.rocket.chat'; + : 'https://releases.rocket.chat/v2/server/supportedVersions'; const getSupportedVersionsFromCloud = async () => { if (process.env.CLOUD_SUPPORTED_VERSIONS_TOKEN) { From ed5734c178be8dc1972cf4259cb958053e4c9604 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 4 Oct 2023 00:26:15 -0300 Subject: [PATCH 3/4] .. --- .../version-check/server/functions/buildVersionUpdateMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts b/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts index 621feb2a2b94..a4b4121e415f 100644 --- a/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts +++ b/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts @@ -11,7 +11,7 @@ export const buildVersionUpdateMessage = async ( version: string; security: boolean; infoUrl: string; - }[], + }[] = [], ) => { const lastCheckedVersion = settings.get('Update_LatestAvailableVersion'); From 1ec329b1a9e755c990ba3ef4bc38fab5c1737a43 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 23 Oct 2023 16:14:39 -0300 Subject: [PATCH 4/4] review --- .../supportedVersionsToken/supportedVersionsToken.ts | 7 +++---- .../server/functions/buildVersionUpdateMessage.ts | 7 ++++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts index 861b46e56acc..f4c662362f5c 100644 --- a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts +++ b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts @@ -84,10 +84,9 @@ const cacheValueInSettings = ( ); }; -const releaseEndpoint = - process.env.OVERWRITE_INTERNAL_RELEASE_URL && process.env.OVERWRITE_INTERNAL_RELEASE_URL.trim().length > 0 - ? process.env.OVERWRITE_INTERNAL_RELEASE_URL - : 'https://releases.rocket.chat/v2/server/supportedVersions'; +const releaseEndpoint = process.env.OVERWRITE_INTERNAL_RELEASE_URL?.trim() + ? process.env.OVERWRITE_INTERNAL_RELEASE_URL.trim() + : 'https://releases.rocket.chat/v2/server/supportedVersions'; const getSupportedVersionsFromCloud = async () => { if (process.env.CLOUD_SUPPORTED_VERSIONS_TOKEN) { diff --git a/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts b/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts index a4b4121e415f..8d159213d4e3 100644 --- a/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts +++ b/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts @@ -15,10 +15,11 @@ export const buildVersionUpdateMessage = async ( ) => { const lastCheckedVersion = settings.get('Update_LatestAvailableVersion'); + if (!lastCheckedVersion) { + return; + } + for await (const version of versions) { - if (!lastCheckedVersion) { - break; - } if (semver.lte(version.version, lastCheckedVersion)) { continue; }