Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into impr…
Browse files Browse the repository at this point in the history
…ove-e2ee-threadlist

* 'develop' of github.com:RocketChat/Rocket.Chat:
  Chore: Remove settings Fibers usage (#26465)
  Chore: Refactor create-target-channel util (#26493)
  Chore: useEndpointData deprecation (#26494)
  [NEW] Surface featured apps endpoint  (#26416)
  [FIX] Request at least one field in the payload of `/v1/users.setStatus` (#26490)
  Chore: Exit process on `unhandledRejection` on CI (#26467)
  [FIX] Clear push token on save user password (#26466)
  • Loading branch information
gabriellsh committed Aug 8, 2022
2 parents eafa190 + f45b956 commit d10c3fc
Show file tree
Hide file tree
Showing 93 changed files with 999 additions and 1,235 deletions.
14 changes: 10 additions & 4 deletions apps/meteor/app/api/server/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,10 +978,16 @@ API.v1.addRoute(
post() {
check(
this.bodyParams,
Match.ObjectIncluding({
status: Match.Maybe(String),
message: Match.Maybe(String),
}),
Match.OneOf(
Match.ObjectIncluding({
status: Match.Maybe(String),
message: String,
}),
Match.ObjectIncluding({
status: String,
message: Match.Maybe(String),
}),
),
);

if (!settings.get('Accounts_AllowUserStatusMessageChange')) {
Expand Down
66 changes: 49 additions & 17 deletions apps/meteor/app/apps/server/communication/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ export class AppsRestApi {
'',
{ authRequired: true, permissionsRequired: ['manage-apps'] },
{
get() {
async get() {
const baseUrl = orchestrator.getMarketplaceUrl();

// Gets the Apps from the marketplace
if (this.queryParams.marketplace) {
const headers = getDefaultHeaders();
const token = getWorkspaceAccessToken();
const token = await getWorkspaceAccessToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}
Expand All @@ -100,7 +100,7 @@ export class AppsRestApi {

if (this.queryParams.categories) {
const headers = getDefaultHeaders();
const token = getWorkspaceAccessToken();
const token = await getWorkspaceAccessToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}
Expand Down Expand Up @@ -183,8 +183,8 @@ export class AppsRestApi {

const headers = getDefaultHeaders();
try {
const downloadToken = getWorkspaceAccessToken(true, 'marketplace:download', false);
const marketplaceToken = getWorkspaceAccessToken();
const downloadToken = await getWorkspaceAccessToken(true, 'marketplace:download', false);
const marketplaceToken = await getWorkspaceAccessToken();

const [downloadResponse, marketplaceResponse] = await Promise.all([
fetch(`${baseUrl}/v2/apps/${this.bodyParams.appId}/download/${this.bodyParams.version}?token=${downloadToken}`, {
Expand Down Expand Up @@ -318,11 +318,11 @@ export class AppsRestApi {
'bundles/:id/apps',
{ authRequired: true, permissionsRequired: ['manage-apps'] },
{
get() {
async get() {
const baseUrl = orchestrator.getMarketplaceUrl();

const headers = {};
const token = getWorkspaceAccessToken();
const token = await getWorkspaceAccessToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}
Expand All @@ -347,16 +347,48 @@ export class AppsRestApi {
},
);

this.api.addRoute(
'featured',
{ authRequired: true },
{
async get() {
const baseUrl = orchestrator.getMarketplaceUrl();

const headers = getDefaultHeaders();
const token = await getWorkspaceAccessToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}

let result;
try {
result = HTTP.get(`${baseUrl}/v1/apps/featured`, {
headers,
});
} catch (e) {
return handleError('Unable to access Marketplace. Does the server has access to the internet?', e);
}

if (!result || result.statusCode !== 200) {
orchestrator.getRocketChatLogger().error('Error getting the Featured Apps from the Marketplace:', result.data);
return API.v1.failure();
}

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

this.api.addRoute(
':id',
{ authRequired: true, permissionsRequired: ['manage-apps'] },
{
get() {
async get() {
if (this.queryParams.marketplace && this.queryParams.version) {
const baseUrl = orchestrator.getMarketplaceUrl();

const headers = {}; // DO NOT ATTACH THE FRAMEWORK/ENGINE VERSION HERE.
const token = getWorkspaceAccessToken();
const token = await getWorkspaceAccessToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}
Expand All @@ -382,7 +414,7 @@ export class AppsRestApi {
const baseUrl = orchestrator.getMarketplaceUrl();

const headers = getDefaultHeaders();
const token = getWorkspaceAccessToken();
const token = await getWorkspaceAccessToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}
Expand Down Expand Up @@ -434,7 +466,7 @@ export class AppsRestApi {
const baseUrl = orchestrator.getMarketplaceUrl();

const headers = getDefaultHeaders();
const token = getWorkspaceAccessToken(true, 'marketplace:download', false);
const token = await getWorkspaceAccessToken(true, 'marketplace:download', false);

try {
const response = await fetch(
Expand Down Expand Up @@ -532,11 +564,11 @@ export class AppsRestApi {
':id/versions',
{ authRequired: true, permissionsRequired: ['manage-apps'] },
{
get() {
async get() {
const baseUrl = orchestrator.getMarketplaceUrl();

const headers = {}; // DO NOT ATTACH THE FRAMEWORK/ENGINE VERSION HERE.
const token = getWorkspaceAccessToken();
const token = await getWorkspaceAccessToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}
Expand Down Expand Up @@ -564,16 +596,16 @@ export class AppsRestApi {
':id/sync',
{ authRequired: true, permissionsRequired: ['manage-apps'] },
{
post() {
async post() {
const baseUrl = orchestrator.getMarketplaceUrl();

const headers = getDefaultHeaders();
const token = getWorkspaceAccessToken();
const token = await getWorkspaceAccessToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}

const workspaceIdSetting = Promise.await(Settings.findOneById('Cloud_Workspace_Id'));
const workspaceIdSetting = await Settings.findOneById('Cloud_Workspace_Id');

let result;
try {
Expand All @@ -590,7 +622,7 @@ export class AppsRestApi {
return API.v1.failure();
}

Promise.await(Apps.updateAppsMarketplaceInfo([result.data]));
await Apps.updateAppsMarketplaceInfo([result.data]);

return API.v1.success({ app: result.data });
},
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/app/assets/server/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import sizeOf from 'image-size';
import sharp from 'sharp';
import { NextHandleFunction } from 'connect';
import { IRocketChatAssets, IRocketChatAsset } from '@rocket.chat/core-typings';
import { Settings } from '@rocket.chat/models';

import { settings, settingsRegistry } from '../../settings/server';
import { getURL } from '../../utils/lib/getURL';
import { getExtension } from '../../utils/lib/mimeTypes';
import { hasPermission } from '../../authorization/server';
import { RocketChatFile } from '../../file';
import { Settings } from '../../models/server';

const RocketChatAssetsInstance = new RocketChatFile.GridFS({
name: 'assets',
Expand Down Expand Up @@ -347,7 +347,7 @@ function addAssetToSetting(asset: string, value: IRocketChatAsset): void {

if (typeof currentValue === 'object' && currentValue.defaultUrl !== getAssetByKey(asset).defaultUrl) {
currentValue.defaultUrl = getAssetByKey(asset).defaultUrl;
Settings.updateValueById(key, currentValue);
Promise.await(Settings.updateValueById(key, currentValue));
}
}

Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/app/authentication/server/startup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Accounts } from 'meteor/accounts-base';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import _ from 'underscore';
import { escapeRegExp, escapeHTML } from '@rocket.chat/string-helpers';
import { Roles, Users as UsersRaw } from '@rocket.chat/models';
import { Roles, Settings, Users as UsersRaw } from '@rocket.chat/models';

import * as Mailer from '../../../mailer/server/api';
import { settings } from '../../../settings/server';
import { callbacks } from '../../../../lib/callbacks';
import { Settings, Users } from '../../../models/server';
import { Users } from '../../../models/server';
import { addUserRoles } from '../../../../server/lib/roles/addUserRoles';
import { getAvatarSuggestionForUser } from '../../../lib/server/functions/getAvatarSuggestionForUser';
import { parseCSV } from '../../../../lib/utils/parseCSV';
Expand Down Expand Up @@ -295,7 +295,7 @@ Accounts.insertUserDoc = _.wrap(Accounts.insertUserDoc, function (insertUserDoc,
if (!roles.includes('admin') && !hasAdmin) {
roles.push('admin');
if (settings.get('Show_Setup_Wizard') === 'pending') {
Settings.updateValueById('Show_Setup_Wizard', 'in_progress');
Promise.await(Settings.updateValueById('Show_Setup_Wizard', 'in_progress'));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { Users } from '../../../models/server';
import { statistics } from '../../../statistics/server';
import { LICENSE_VERSION } from '../license';

type WorkspaceRegistrationData = {
type WorkspaceRegistrationData<T> = {
uniqueId: string;
workspaceId: SettingValue;
address: SettingValue;
contactName: string;
contactEmail: string;
contactEmail: T;
seats: number;
allowMarketing: SettingValue;
accountName: SettingValue;
Expand All @@ -33,7 +33,7 @@ type WorkspaceRegistrationData = {
npsEnabled: SettingValue;
};

export async function buildWorkspaceRegistrationData(contactEmail: string): Promise<WorkspaceRegistrationData> {
export async function buildWorkspaceRegistrationData<T extends string | undefined>(contactEmail: T): Promise<WorkspaceRegistrationData<T>> {
const stats = (await Statistics.findLast()) || (await statistics.get());

const address = settings.get('Site_Url');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { HTTP } from 'meteor/http';
import { Settings } from '@rocket.chat/models';

import { getRedirectUri } from './getRedirectUri';
import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';
import { Settings } from '../../../models/server';
import { settings } from '../../../settings/server';
import { saveRegistrationData } from './saveRegistrationData';
import { SystemLogger } from '../../../../server/lib/logger/system';

export function connectWorkspace(token) {
export async function connectWorkspace(token: string) {
const { connectToCloud } = retrieveRegistrationStatus();
if (!connectToCloud) {
Settings.updateValueById('Register_Server', true);
await Settings.updateValueById('Register_Server', true);
}

// shouldn't get here due to checking this on the method
Expand All @@ -36,7 +36,7 @@ export function connectWorkspace(token) {
},
data: regInfo,
});
} catch (e) {
} catch (e: any) {
if (e.response && e.response.data && e.response.data.error) {
SystemLogger.error(`Failed to register with Rocket.Chat Cloud. Error: ${e.response.data.error}`);
} else {
Expand All @@ -52,7 +52,7 @@ export function connectWorkspace(token) {
return false;
}

Promise.await(saveRegistrationData(data));
await saveRegistrationData(data);

return true;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Settings } from '@rocket.chat/models';

import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';
import { Settings } from '../../../models/server';

export function disconnectWorkspace() {
export async function disconnectWorkspace() {
const { connectToCloud } = retrieveRegistrationStatus();
if (!connectToCloud) {
return true;
}

Settings.updateValueById('Register_Server', false);
await Settings.updateValueById('Register_Server', false);

return true;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Random } from 'meteor/random';
import { Settings } from '@rocket.chat/models';

import { getRedirectUri } from './getRedirectUri';
import { Settings } from '../../../models/server';
import { settings } from '../../../settings/server';
import { userScopes } from '../oauthScopes';

export function getOAuthAuthorizationUrl() {
export async function getOAuthAuthorizationUrl() {
const state = Random.id();

Settings.updateValueById('Cloud_Workspace_Registration_State', state);
await Settings.updateValueById('Cloud_Workspace_Registration_State', state);

const cloudUrl = settings.get('Cloud_Url');
const client_id = settings.get('Cloud_Workspace_Client_Id');
const clientId = settings.get('Cloud_Workspace_Client_Id');
const redirectUri = getRedirectUri();

const scope = userScopes.join(' ');

return `${cloudUrl}/authorize?response_type=code&client_id=${client_id}&redirect_uri=${redirectUri}&scope=${scope}&state=${state}`;
return `${cloudUrl}/authorize?response_type=code&client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&state=${state}`;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Settings } from '@rocket.chat/models';

import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';
import { getWorkspaceAccessTokenWithScope } from './getWorkspaceAccessTokenWithScope';
import { Settings } from '../../../models/server';
import { settings } from '../../../settings/server';

/**
Expand All @@ -9,25 +10,31 @@ import { settings } from '../../../settings/server';
* @param {boolean} save
* @returns string
*/
export function getWorkspaceAccessToken(forceNew = false, scope = '', save = true) {
export async function getWorkspaceAccessToken(forceNew = false, scope = '', save = true) {
const { connectToCloud, workspaceRegistered } = retrieveRegistrationStatus();

if (!connectToCloud || !workspaceRegistered) {
return '';
}

const expires = Settings.findOneById('Cloud_Workspace_Access_Token_Expires_At');
const expires = await Settings.findOneById('Cloud_Workspace_Access_Token_Expires_At');

if (expires === null) {
throw new Error('Cloud_Workspace_Access_Token_Expires_At is not set');
}
const now = new Date();

if (now < expires.value && !forceNew) {
if (expires.value && now < expires.value && !forceNew) {
return settings.get('Cloud_Workspace_Access_Token');
}

const accessToken = getWorkspaceAccessTokenWithScope(scope);

if (save) {
Settings.updateValueById('Cloud_Workspace_Access_Token', accessToken.token);
Settings.updateValueById('Cloud_Workspace_Access_Token_Expires_At', accessToken.expiresAt);
await Promise.all([
Settings.updateValueById('Cloud_Workspace_Access_Token', accessToken.token),
Settings.updateValueById('Cloud_Workspace_Access_Token_Expires_At', accessToken.expiresAt),
]);
}

return accessToken.token;
Expand Down
Loading

0 comments on commit d10c3fc

Please sign in to comment.