Skip to content

Commit

Permalink
Merge branch 'develop' into fix/accounts-email-verification
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Oct 23, 2023
2 parents 74c8c07 + 6190c92 commit c114095
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -83,7 +84,9 @@ const cacheValueInSettings = <T extends SettingValue>(
);
};

/** CODE */
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) {
Expand All @@ -97,15 +100,15 @@ const getSupportedVersionsFromCloud = async () => {
const headers = await generateWorkspaceBearerHttpHeader();

const response = await handleResponse<SupportedVersions>(
fetch('https://releases.rocket.chat/v2/server/supportedVersions', {
fetch(releaseEndpoint, {
headers,
}),
);

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,
});
}
Expand All @@ -123,8 +126,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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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<string>('Update_LatestAvailableVersion');

if (!lastCheckedVersion) {
return;
}

for await (const version of versions) {
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;
}
};
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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<string>('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,
})),
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions packages/server-cloud-communication/src/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Message = {
type Version = {
version: string;
expiration: Date;
security: boolean;
infoUrl: string;
messages?: Message[];
};

Expand Down

0 comments on commit c114095

Please sign in to comment.