Skip to content

Commit

Permalink
Merge branch 'develop' into floky-trigger-test
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Oct 3, 2023
2 parents 11c8fca + 3fd0bc4 commit f0cf93c
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-cows-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

download translation files through CDN
7 changes: 7 additions & 0 deletions .changeset/tough-carrots-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rocket.chat/rest-typings': minor
'@rocket.chat/license': patch
'@rocket.chat/meteor': patch
---

feat: added `licenses.info` endpoint
2 changes: 2 additions & 0 deletions .github/actions/build-docker/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ runs:

steps:
- name: Login to GitHub Container Registry
if: github.event.pull_request.head.repo.full_name == github.repository
uses: docker/login-action@v2
with:
registry: ghcr.io
Expand Down Expand Up @@ -62,6 +63,7 @@ runs:
docker compose -f docker-compose-ci.yml build "${args[@]}"
- name: Publish Docker images to GitHub Container Registry
if: github.event.pull_request.head.repo.full_name == github.repository
shell: bash
run: |
args=(rocketchat)
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/ci-test-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ jobs:
cache-modules: true
install: true

# if we are testing a PR from a fork, we need to build the docker image at this point
- uses: ./.github/actions/build-docker
if: github.event.pull_request.head.repo.full_name != github.repository
with:
CR_USER: ${{ secrets.CR_USER }}
CR_PAT: ${{ secrets.CR_PAT }}
node-version: ${{ inputs.node-version }}

- uses: dtinth/setup-github-actions-caching-for-turbo@v1

- name: Start httpbin container and wait for it to be ready
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ jobs:
build-gh-docker-coverage:
name: 🚢 Build Docker Images for Testing
needs: [build, release-versions]
if: (github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'release' || github.ref == 'refs/heads/develop')
runs-on: ubuntu-20.04

env:
Expand All @@ -189,7 +188,10 @@ jobs:

steps:
- uses: actions/checkout@v3

# we only build and publish the actual docker images if not a PR from a fork
- uses: ./.github/actions/build-docker
if: (github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'release' || github.ref == 'refs/heads/develop')
with:
CR_USER: ${{ secrets.CR_USER }}
CR_PAT: ${{ secrets.CR_PAT }}
Expand Down
10 changes: 5 additions & 5 deletions apps/meteor/client/providers/TranslationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useLocalStorage, useMutableCallback } from '@rocket.chat/fuselage-hooks
import languages from '@rocket.chat/i18n/dist/languages';
import en from '@rocket.chat/i18n/src/locales/en.i18n.json';
import type { TranslationKey, TranslationContextValue } from '@rocket.chat/ui-contexts';
import { useMethod, useSetting, TranslationContext, useAbsoluteUrl } from '@rocket.chat/ui-contexts';
import { useMethod, useSetting, TranslationContext } from '@rocket.chat/ui-contexts';
import type i18next from 'i18next';
import I18NextHttpBackend from 'i18next-http-backend';
import sprintf from 'i18next-sprintf-postprocessor';
Expand All @@ -12,6 +12,7 @@ import React, { useEffect, useMemo } from 'react';
import { I18nextProvider, initReactI18next, useTranslation } from 'react-i18next';

import { CachedCollectionManager } from '../../app/ui-cached-collection/client';
import { getURL } from '../../app/utils/client';
import { i18n, addSprinfToI18n } from '../../app/utils/lib/i18n';
import { AppClientOrchestratorInstance } from '../../ee/client/apps/orchestrator';
import { applyCustomTranslations } from '../lib/utils/applyCustomTranslations';
Expand Down Expand Up @@ -39,8 +40,6 @@ const parseToJSON = (customTranslations: string): Record<string, Record<string,
const localeCache = new Map<string, Promise<string>>();

const useI18next = (lng: string): typeof i18next => {
const basePath = useAbsoluteUrl()('/i18n');

const customTranslations = useSetting('Custom_Translations');

const parsedCustomTranslations = useMemo(() => {
Expand Down Expand Up @@ -100,17 +99,18 @@ const useI18next = (lng: string): typeof i18next => {
partialBundledLanguages: true,
defaultNS: 'core',
backend: {
loadPath: `${basePath}/{{lng}}.json`,
loadPath: 'i18n/{{lng}}.json',
parse: (data: string, lngs?: string | string[], namespaces: string | string[] = []) =>
extractKeys(JSON.parse(data), lngs, namespaces),
request: (_options, url, _payload, callback) => {
const params = url.split('/');

const lng = params[params.length - 1];

let promise = localeCache.get(lng);

if (!promise) {
promise = fetch(url).then((res) => res.text());
promise = fetch(getURL(url)).then((res) => res.text());
localeCache.set(lng, promise);
}

Expand Down
13 changes: 13 additions & 0 deletions apps/meteor/ee/server/api/licenses.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { License } from '@rocket.chat/license';
import { Settings, Users } from '@rocket.chat/models';
import { isLicensesInfoProps } from '@rocket.chat/rest-typings';
import { check } from 'meteor/check';

import { API } from '../../../app/api/server/api';
Expand All @@ -22,6 +23,18 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
'licenses.info',
{ authRequired: true, validateParams: isLicensesInfoProps, permissionsRequired: ['view-privileged-setting'] },
{
async get() {
const data = await License.getInfo(Boolean(this.queryParams.loadValues));

return API.v1.success({ data });
},
},
);

API.v1.addRoute(
'licenses.add',
{ authRequired: true },
Expand Down
10 changes: 9 additions & 1 deletion ee/packages/license/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { LicenseLimitKind } from './definition/ILicenseV3';
import type { ILicenseV3, LicenseLimitKind } from './definition/ILicenseV3';
import type { LicenseModule } from './definition/LicenseModule';
import type { LimitContext } from './definition/LimitContext';
import { getAppsConfig, getMaxActiveUsers, getUnmodifiedLicenseAndModules } from './deprecated';
import { onLicense } from './events/deprecated';
Expand Down Expand Up @@ -45,6 +46,13 @@ interface License {
onInvalidateLicense: typeof onInvalidateLicense;
onLimitReached: typeof onLimitReached;

getInfo: (loadCurrentValues: boolean) => Promise<{
license: ILicenseV3 | undefined;
activeModules: LicenseModule[];
limits: Record<LicenseLimitKind, { value?: number; max: number }>;
inFairPolicy: boolean;
}>;

// Deprecated:
onLicense: typeof onLicense;
// Deprecated:
Expand Down
41 changes: 40 additions & 1 deletion ee/packages/license/src/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DuplicatedLicenseError } from './errors/DuplicatedLicenseError';
import { InvalidLicenseError } from './errors/InvalidLicenseError';
import { NotReadyForValidation } from './errors/NotReadyForValidation';
import { logger } from './logger';
import { invalidateAll, replaceModules } from './modules';
import { getModules, invalidateAll, replaceModules } from './modules';
import { applyPendingLicense, clearPendingLicense, hasPendingLicense, isPendingLicense, setPendingLicense } from './pendingLicense';
import { showLicense } from './showLicense';
import { replaceTags } from './tags';
Expand Down Expand Up @@ -227,4 +227,43 @@ export class LicenseManager extends Emitter<
.some(({ max }) => max < currentValue),
);
}

public async getInfo(loadCurrentValues = false): Promise<{
license: ILicenseV3 | undefined;
activeModules: LicenseModule[];
limits: Record<LicenseLimitKind, { value?: number; max: number }>;
inFairPolicy: boolean;
}> {
const activeModules = getModules.call(this);
const license = this.getLicense();

// Get all limits present in the license and their current value
const limits = (
(license &&
(await Promise.all(
(['activeUsers', 'guestUsers', 'privateApps', 'marketplaceApps', 'monthlyActiveContacts'] as LicenseLimitKind[])
.map((limitKey) => ({
limitKey,
max: Math.max(-1, Math.min(...Array.from(license.limits[limitKey as LicenseLimitKind] || [])?.map(({ max }) => max))),
}))
.filter(({ max }) => max >= 0 && max < Infinity)
.map(async ({ max, limitKey }) => {
return {
[limitKey as LicenseLimitKind]: {
...(loadCurrentValues ? { value: await getCurrentValueForLicenseLimit.call(this, limitKey as LicenseLimitKind) } : {}),
max,
},
};
}),
))) ||
[]
).reduce((prev, curr) => ({ ...prev, ...curr }), {});

return {
license,
activeModules,
limits: limits as Record<LicenseLimitKind, { max: number; value: number }>,
inFairPolicy: this.inFairPolicy,
};
}
}
4 changes: 2 additions & 2 deletions ee/packages/license/src/pendingLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export function setPendingLicense(this: LicenseManager, encryptedLicense: string
}
}

export function applyPendingLicense(this: LicenseManager) {
export async function applyPendingLicense(this: LicenseManager) {
if (this.pendingLicense) {
logger.info('Applying pending license.');
this.setLicense(this.pendingLicense);
return this.setLicense(this.pendingLicense);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/rest-typings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export * from './v1/invites';
export * from './v1/dm';
export * from './v1/dm/DmHistoryProps';
export * from './v1/integrations';
export * from './v1/licenses';
export * from './v1/omnichannel';
export * from './v1/oauthapps';
export * from './v1/oauthapps/UpdateOAuthAppParamsPOST';
Expand Down
29 changes: 28 additions & 1 deletion packages/rest-typings/src/v1/licenses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ILicenseV2, ILicenseV3 } from '@rocket.chat/license';
import type { ILicenseV2, ILicenseV3, LicenseLimitKind } from '@rocket.chat/license';
import Ajv from 'ajv';

const ajv = new Ajv({
Expand All @@ -22,10 +22,37 @@ const licensesAddPropsSchema = {

export const isLicensesAddProps = ajv.compile<licensesAddProps>(licensesAddPropsSchema);

type licensesInfoProps = {
loadValues?: boolean;
};

const licensesInfoPropsSchema = {
type: 'object',
properties: {
loadValues: {
type: 'boolean',
},
},
required: [],
additionalProperties: false,
};

export const isLicensesInfoProps = ajv.compile<licensesInfoProps>(licensesInfoPropsSchema);

export type LicensesEndpoints = {
'/v1/licenses.get': {
GET: () => { licenses: Array<ILicenseV2 | (ILicenseV3 & { modules: string[] })> };
};
'/v1/licenses.info': {
GET: (params: licensesInfoProps) => {
data: {
license: ILicenseV3 | undefined;
activeModules: string[];
limits: Record<LicenseLimitKind, { max: number; value?: number }>;
inFairPolicy: boolean;
};
};
};
'/v1/licenses.add': {
POST: (params: licensesAddProps) => void;
};
Expand Down

0 comments on commit f0cf93c

Please sign in to comment.