Skip to content

Commit

Permalink
rename types
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Sep 29, 2023
1 parent c555df1 commit 0cac727
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import type { CloudVersionsResponse } from '@rocket.chat/server-cloud-communication';
import type { SupportedVersions } from '@rocket.chat/server-cloud-communication';

import { supportedVersionsChooseLatest } from './supportedVersionsChooseLatest';

describe('supportedVersionsChooseLatest', () => {
test('should return the latest version', async () => {
const versionFromLicense: CloudVersionsResponse = {
const versionFromLicense: SupportedVersions = {
timestamp: '2021-08-31T18:00:00.000Z',
signed: 'license',
versions: [],
};

const versionFromCloud: CloudVersionsResponse = {
const versionFromCloud: SupportedVersions = {
timestamp: '2021-08-31T19:00:00.000Z',
signed: 'cloud',
versions: [],
};

const result = await supportedVersionsChooseLatest(versionFromLicense, versionFromCloud);

expect(result).toBe(versionFromCloud.signed);
expect(result.timestamp).toBe(versionFromCloud.timestamp);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CloudVersionsResponse } from '@rocket.chat/server-cloud-communication';
import type { SupportedVersions } from '@rocket.chat/server-cloud-communication';

export const supportedVersionsChooseLatest = async (...tokens: (CloudVersionsResponse | undefined)[]) => {
const [token] = (tokens.filter(Boolean) as CloudVersionsResponse[]).sort((a, b) => {
export const supportedVersionsChooseLatest = async (...tokens: (SupportedVersions | undefined)[]) => {
const [token] = (tokens.filter(Boolean) as SupportedVersions[]).sort((a, b) => {
return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { SettingValue } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';
import { Settings } from '@rocket.chat/models';
import type { CloudVersionsResponse } from '@rocket.chat/server-cloud-communication';
import type { SupportedVersions } from '@rocket.chat/server-cloud-communication';
import type { Response } from '@rocket.chat/server-fetch';
import { serverFetch as fetch } from '@rocket.chat/server-fetch';

import { supportedVersions } from '../../../../../ee/app/license/server/license';
import { SystemLogger } from '../../../../../server/lib/logger/system';
import { settings } from '../../../../settings/server';
import { generateWorkspaceBearerHttpHeader } from '../getWorkspaceAccessToken';
Expand Down Expand Up @@ -80,9 +80,17 @@ const cacheValueInSettings = <T extends SettingValue>(
/** CODE */

const getSupportedVersionsFromCloud = async () => {
if (process.env.CLOUD_SUPPORTED_VERSIONS_TOKEN) {
return {
success: true,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
result: JSON.parse(process.env.CLOUD_SUPPORTED_VERSIONS!),
};
}

const headers = await generateWorkspaceBearerHttpHeader();

const response = await handleResponse<CloudVersionsResponse>(
const response = await handleResponse<SupportedVersions>(
fetch('https://releases.rocket.chat/supported/server', {
headers,
}),
Expand All @@ -107,9 +115,7 @@ const getSupportedVersionsToken = async () => {
* return the token
*/

const [versionsFromLicense, response] = await Promise.all([supportedVersions(), getSupportedVersionsFromCloud()]);

// TODO: get values from jtw token
const [versionsFromLicense, response] = await Promise.all([License.supportedVersions(), getSupportedVersionsFromCloud()]);

return supportedVersionsChooseLatest(versionsFromLicense, (response.success && response.result) || undefined);
};
Expand Down
3 changes: 2 additions & 1 deletion ee/packages/license/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"dependencies": {
"@rocket.chat/core-typings": "workspace:^",
"@rocket.chat/jwt": "workspace:^",
"@rocket.chat/logger": "workspace:^"
"@rocket.chat/logger": "workspace:^",
"@rocket.chat/server-cloud-communication": "workspace:^"
}
}
4 changes: 4 additions & 0 deletions ee/packages/license/src/definition/ILicenseV3.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { SignedSupportedVersions } from '@rocket.chat/server-cloud-communication';

import type { ILicenseTag } from './ILicenseTag';
import type { LicenseLimit } from './LicenseLimit';
import type { LicenseModule } from './LicenseModule';
Expand Down Expand Up @@ -59,6 +61,8 @@ export interface ILicenseV3 {
monthlyActiveContacts?: LicenseLimit[];
};
cloudMeta?: Record<string, any>;

supportedVersions?: SignedSupportedVersions;
}

export type LicenseLimitKind = keyof ILicenseV3['limits'];
8 changes: 7 additions & 1 deletion ee/packages/license/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LicenseLimitKind } from './definition/ILicenseV3';
import type { ILicenseV3, LicenseLimitKind } from './definition/ILicenseV3';
import type { LimitContext } from './definition/LimitContext';
import { getAppsConfig, getMaxActiveUsers, getUnmodifiedLicenseAndModules } from './deprecated';
import { onLicense } from './events/deprecated';
Expand Down Expand Up @@ -45,6 +45,8 @@ interface License {
onInvalidateLicense: typeof onInvalidateLicense;
onLimitReached: typeof onLimitReached;

supportedVersions(): ILicenseV3['supportedVersions'];

// Deprecated:
onLicense: typeof onLicense;
// Deprecated:
Expand All @@ -56,6 +58,10 @@ interface License {
}

export class LicenseImp extends LicenseManager implements License {
supportedVersions() {
return this.getLicense()?.supportedVersions;
}

validateFormat = validateFormat;

hasModule = hasModule;
Expand Down
11 changes: 1 addition & 10 deletions packages/server-cloud-communication/src/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ export interface SupportedVersions {
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export interface CloudVersionsResponse {
export interface SignedSupportedVersions extends SupportedVersions {
signed: string; // SerializedJWT<SupportedVersions>;
timestamp: string;
messages?: Message[];
versions: Version[];
exceptions?: {
domain: string;
uniqueId: string;
messages?: Message[];
versions: Version[];
};
}
4 changes: 2 additions & 2 deletions packages/server-cloud-communication/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { CloudVersionsResponse } from './definitions';
import type { SupportedVersions, SignedSupportedVersions } from './definitions';

export { CloudVersionsResponse };
export { SupportedVersions, SignedSupportedVersions };
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8472,6 +8472,7 @@ __metadata:
"@rocket.chat/core-typings": "workspace:^"
"@rocket.chat/jwt": "workspace:^"
"@rocket.chat/logger": "workspace:^"
"@rocket.chat/server-cloud-communication": "workspace:^"
"@swc/core": ^1.3.66
"@swc/jest": ^0.2.26
"@types/babel__core": ^7
Expand Down

0 comments on commit 0cac727

Please sign in to comment.