Skip to content

Commit

Permalink
chore: deprecation api (#29347)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Jun 6, 2023
1 parent 4513378 commit 33ab16f
Show file tree
Hide file tree
Showing 53 changed files with 232 additions and 164 deletions.
10 changes: 6 additions & 4 deletions apps/meteor/app/api/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { parseJsonQuery } from './helpers/parseJsonQuery';
import { Logger } from '../../logger/server';
import { getUserInfo } from './helpers/getUserInfo';
import { hasPermissionAsync } from '../../authorization/server/functions/hasPermission';
import { apiDeprecationLogger } from '../../lib/server/lib/deprecationWarningLogger';

const logger = new Logger('API');

Expand Down Expand Up @@ -177,10 +178,7 @@ export class APIClass<TBasePath extends string = ''> extends Restivus {
}

async parseJsonQuery(this: PartialThis) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;

return parseJsonQuery(this.request.route, self.userId, self.queryParams, self.logger, self.queryFields, self.queryOperations);
return parseJsonQuery(this);
}

public addAuthMethod(func: (this: PartialThis, ...args: any[]) => any): void {
Expand Down Expand Up @@ -581,6 +579,10 @@ export class APIClass<TBasePath extends string = ''> extends Restivus {
};

try {
if (options.deprecationVersion) {
apiDeprecationLogger.endpoint(this.request.route, options.deprecationVersion, this.response);
}

await api.enforceRateLimit(objectForRateLimitMatch, this.request, this.response, this.userId);

if (_options.validateParams) {
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/app/api/server/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export type Options = (
) & {
validateParams?: ValidateFunction | { [key in Method]?: ValidateFunction };
authOrAnonRequired?: true;
deprecationVersion?: string;
};

export type PartialThis = {
Expand Down
26 changes: 0 additions & 26 deletions apps/meteor/app/api/server/helpers/deprecationWarning.ts

This file was deleted.

37 changes: 15 additions & 22 deletions apps/meteor/app/api/server/helpers/parseJsonQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,30 @@ import ejson from 'ejson';
import { isValidQuery } from '../lib/isValidQuery';
import { clean } from '../lib/cleanQuery';
import { API } from '../api';
import type { Logger } from '../../../logger/server';
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { apiDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
import type { PartialThis } from '../definition';

const pathAllowConf = {
'/api/v1/users.list': ['$or', '$regex', '$and'],
'def': ['$or', '$and', '$regex'],
};

const warnFields =
process.env.NODE_ENV !== 'production' || process.env.SHOW_WARNINGS === 'true'
? (...rest: any): void => {
console.warn(...rest, new Error().stack);
}
: new Function();

export async function parseJsonQuery(
route: string,
userId: string,
params: {
query?: string;
sort?: string;
fields?: string;
},
logger: Logger,
queryFields?: string[],
queryOperations?: string[],
): Promise<{
export async function parseJsonQuery(api: PartialThis): Promise<{
sort: Record<string, 1 | -1>;
fields: Record<string, 0 | 1>;
query: Record<string, unknown>;
}> {
const {
request: { route },
userId,
queryParams: params,
logger,
queryFields,
queryOperations,
response,
} = api;

let sort;
if (params.sort) {
try {
Expand All @@ -56,7 +49,7 @@ export async function parseJsonQuery(

let fields: Record<string, 0 | 1> | undefined;
if (params.fields) {
warnFields('attribute fields is deprecated');
apiDeprecationLogger.parameter(route, 'fields', '7.0.0', response);
try {
fields = JSON.parse(params.fields) as Record<string, 0 | 1>;

Expand Down Expand Up @@ -107,7 +100,7 @@ export async function parseJsonQuery(

let query: Record<string, any> = {};
if (params.query) {
warnFields('attribute query is deprecated');
apiDeprecationLogger.parameter(route, 'query', '7.0.0', response);

try {
query = ejson.parse(params.query);
Expand Down
1 change: 0 additions & 1 deletion apps/meteor/app/api/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import './helpers/composeRoomWithLastMessage';
import './helpers/deprecationWarning';
import './helpers/getLoggedInUser';
import './helpers/getPaginationItems';
import './helpers/getUserFromParams';
Expand Down
13 changes: 4 additions & 9 deletions apps/meteor/app/api/server/v1/oauthapps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { OAuthApps } from '@rocket.chat/models';
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { API } from '../api';
import { addOAuthApp } from '../../../oauth2-server-config/server/admin/functions/addOAuthApp';
import { deprecationWarning } from '../helpers/deprecationWarning';
import { apiDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';

API.v1.addRoute(
'oauth-apps.list',
Expand Down Expand Up @@ -32,16 +32,11 @@ API.v1.addRoute(
if (!oauthApp) {
return API.v1.failure('OAuth app not found.');
}

if ('appId' in this.queryParams) {
return API.v1.success(
deprecationWarning({
endpoint: 'oauth-apps.get',
warningMessage: ({ versionWillBeRemoved, endpoint }) =>
`appId get parameter from "${endpoint}" is deprecated and will be removed after version ${versionWillBeRemoved}. Use _id instead.`,
response: { oauthApp },
}),
);
apiDeprecationLogger.parameter(this.request.route, 'appId', '7.0.0', this.response);
}

return API.v1.success({
oauthApp,
});
Expand Down
13 changes: 10 additions & 3 deletions apps/meteor/app/api/server/v1/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ API.v1.addRoute(
return API.v1.failure('error-invalid-role-properties');
}

apiDeprecationLogger.warn(`Assigning roles by name is deprecated and will be removed on the next major release of Rocket.Chat`);
apiDeprecationLogger.parameter(this.request.route, 'roleName', '7.0.0', this.response);
}

const role = roleId ? await Roles.findOneById(roleId) : await Roles.findOneByIdOrName(roleName as string);
Expand Down Expand Up @@ -123,7 +123,14 @@ API.v1.addRoute(
throw new Meteor.Error('error-invalid-roleId');
}

apiDeprecationLogger.warn(`Querying roles by name is deprecated and will be removed on the next major release of Rocket.Chat`);
apiDeprecationLogger.deprecatedParameterUsage(
this.request.route,
'role',
'7.0.0',
this.response,
({ parameter, endpoint, version }) =>
`Querying \`${parameter}\` by name is deprecated in ${endpoint} and will be removed on the removed on version ${version}`,
);
}

const { cursor, totalCount } = await getUsersInRolePaginated(roleData._id, roomId, {
Expand Down Expand Up @@ -198,7 +205,7 @@ API.v1.addRoute(
return API.v1.failure('error-invalid-role-properties');
}

apiDeprecationLogger.warn(`Unassigning roles by name is deprecated and will be removed on the next major release of Rocket.Chat`);
apiDeprecationLogger.parameter(this.request.route, 'roleName', '7.0.0', this.response);
}

const user = await Users.findOneByUsername(username);
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/assets/server/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ declare module '@rocket.chat/ui-contexts' {
Meteor.methods<ServerMethods>({
async refreshClients() {
const uid = Meteor.userId();
methodDeprecationLogger.warn('refreshClients will be deprecated in future versions of Rocket.Chat');
methodDeprecationLogger.method('refreshClients', '7.0.0');

if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
Expand Down
10 changes: 7 additions & 3 deletions apps/meteor/app/authorization/server/methods/addUserToRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ServerMethods } from '@rocket.chat/ui-contexts';

import { settings } from '../../../settings/server';
import { hasPermissionAsync } from '../functions/hasPermission';
import { apiDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -41,8 +41,12 @@ Meteor.methods<ServerMethods>({
method: 'authorization:addUserToRole',
});
}

apiDeprecationLogger.warn(`Calling authorization:addUserToRole with role names will be deprecated in future versions of Rocket.Chat`);
methodDeprecationLogger.deprecatedParameterUsage(
'authorization:addUserToRole',
'role',
'7.0.0',
({ parameter, method, version }) => `Calling ${method} with \`${parameter}\` names is deprecated and will be removed ${version}`,
);
}

if (role._id === 'admin' && !(await hasPermissionAsync(userId, 'assign-admin-role'))) {
Expand Down
9 changes: 7 additions & 2 deletions apps/meteor/app/authorization/server/methods/deleteRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ServerMethods } from '@rocket.chat/ui-contexts';
import type { DeleteResult } from 'mongodb';

import { hasPermissionAsync } from '../functions/hasPermission';
import { apiDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -42,7 +42,12 @@ Meteor.methods<ServerMethods>({
});
}

apiDeprecationLogger.warn(`Calling authorization:deleteRole with role names will be deprecated in future versions of Rocket.Chat`);
methodDeprecationLogger.deprecatedParameterUsage(
'authorization:deleteRole',
'role',
'7.0.0',
({ parameter, method, version }) => `Calling ${method} with ${parameter} names is deprecated and will be removed ${version}`,
);
}

if (role.protected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ServerMethods } from '@rocket.chat/ui-contexts';

import { settings } from '../../../settings/server';
import { hasPermissionAsync } from '../functions/hasPermission';
import { apiDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -41,8 +41,11 @@ Meteor.methods<ServerMethods>({
});
}

apiDeprecationLogger.warn(
`Calling authorization:removeUserFromRole with role names will be deprecated in future versions of Rocket.Chat`,
methodDeprecationLogger.deprecatedParameterUsage(
'authorization:removeUserFromRole',
'role',
'7.0.0',
({ parameter, method, version }) => `Calling ${method} with ${parameter} names is deprecated and will be removed ${version}`,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare module '@rocket.chat/ui-contexts' {

Meteor.methods<ServerMethods>({
async listEmojiCustom(options = {}) {
methodDeprecationLogger.warn('listEmojiCustom will be removed in future versions of Rocket.Chat');
methodDeprecationLogger.method('listEmojiCustom', '7.0.0');

const user = await Meteor.userAsync();

Expand Down
Loading

0 comments on commit 33ab16f

Please sign in to comment.